diff --git a/module/sos-combat.js b/module/sos-combat.js index 7de2083..3afd1c8 100644 --- a/module/sos-combat.js +++ b/module/sos-combat.js @@ -7,11 +7,13 @@ import { SoSDialogCombatActions } from "./sos-dialog-combat-actions.js"; export class SoSCombat extends Combat { /* -------------------------------------------- */ - async nextRound() { - console.log("NEXT ROUND !!!!"); - - if ( game.user.isGM ) { + requestActions() { + if ( game.user.isGM && !this.actionsRequested) { + console.log("REQUEST ACTIONS !!!"); + this.actionsRequested = true; + this.phaseSetup = {}; // Reset each new round/update for( let combatant of this.combatants) { + this.setInitiative(combatant._id, -1 ); // Reset init let uniq = randomID(16); if ( combatant.players[0]) { // A player controls this combatant -> message ! @@ -25,15 +27,89 @@ export class SoSCombat extends Combat { } } } + } + + /* -------------------------------------------- */ + async nextRound() { + this.actionsRequested = false; super.nextRound(); } + /* -------------------------------------------- */ + gotoNextTurn() { + this.phaseNumber -= 1; + if ( this.phaseNumber <= 0) { + this.nextRound(); // Auto-switch to next round + } else { + this.nextTurn(); + } + } + + /* -------------------------------------------- */ + async nextTurn() { + console.log("Goingo to phase !", this.phaseNumber ); + // Get all actions for this phase + let phaseIndex = this.phaseNumber - 1; + let actionList = []; + let actionMsg = `

Actions for phase ${this.phaseNumber}

`; + for (let combatantId in this.phaseSetup ) { + let actionData = this.phaseSetup[combatantId]; + if ( actionData.phaseArray[phaseIndex].name != 'No Action' ) { + let combatant = this.combatants.find( comb => comb._id == actionData.combatantId); + actionList.push( { combatant: combatant, + action: actionData.phaseArray[phaseIndex], + isDone: false + }); + actionMsg += `
${combatant.actor.name} is going to : ${actionData.phaseArray[phaseIndex].name}`; + } + } + if ( actionList.length == 0) { + actionMsg += "
No actions for the phase !"; + this.gotoNextTurn(); + } + // Display a nice message + ChatMessage.create( { content: actionMsg }); + + // Now push specific messages + for ( let action of actionList) { + let uniq = randomID(16); + action.uniqId = uniq; // Easy tracking with chat messages + if ( action.combatant.players[0]) { + // A player controls this combatant -> message ! + ChatMessage.create( { content: `Phase ${this.phaseNumber} ! ${action.combatant.actor.data.name} must perform a ${action.action.name} action. + When done, click on the button below to close the action. + Action is done !`, + whisper: [ action.combatant.players[0].data._id] } ); + } else { + ChatMessage.create( { content: `Phase ${this.phaseNumber} ! ${action.combatant.actor.data.name} must perform a ${action.action.name} action.
+ When done, click on the button below to close the action. + Action is done !`, + whisper: [ ChatMessage.getWhisperRecipients("GM") ] } ); + } + } + // Save for easy access + this.currentActions = actionList; + } + + /* -------------------------------------------- */ + closeAction( uniqId) { + let action = this.currentActions.find( _action => _action.uniqId == uniqId ); + if (action) { + action.isDone = true; + + let filtered = this.currentActions.filter( _action => action.isDone ); + if ( filtered.length == this.currentActions.length) { // All actions closed ! + console.log("Going next turn !!!"); + this.gotoNextTurn(); + } + } + } + /* -------------------------------------------- */ getPhaseRank( actionConf) { for (let i=2; i>=0; i--) { let action = actionConf.phaseArray[i]; if (action.name != "No Action") { - console.log("Init is : ", i+1); return i+1; } } @@ -41,25 +117,27 @@ export class SoSCombat extends Combat { } /* -------------------------------------------- */ - setupActorActions(actionConf) { + async setupActorActions(actionConf) { console.log("Setting combat for phase : ", actionConf); - if ( !this.phaseSetup) this.phaseSetup = []; // Opportunistic init - if ( !this.phaseSetup[this.round] ) this.phaseSetup[this.round] = {}; // Bis + if ( !this.phaseSetup) this.phaseSetup = {}; // Opportunistic init // Keep track - this.phaseSetup[this.round][actionConf.combatantId] = actionConf; + this.phaseSetup[actionConf.combatantId] = actionConf; console.log( this.combatants); - let combatant = this.combatants.find( comb => comb._id == actionConf.combatantId); - this.setInitiative( actionConf.combatantId, this.getPhaseRank( actionConf ) ); + //let combatant = this.combatants.find( comb => comb._id == actionConf.combatantId); + await this.setInitiative( actionConf.combatantId, this.getPhaseRank( actionConf ) ); let actionsDone = true for( let combatant of this.combatants) { - if ( !combatant.initiative ) actionsDone = false; + if ( combatant.initiative == -1 ) actionsDone = false; } if ( actionsDone ) { + this.actionsRequested = false; ChatMessage.create( { content: `Action phase has been completed ! Now proceeding with actions.`, whisper: [ ChatMessage.getWhisperRecipients("GM") ] } ); + this.phaseNumber = 3; + this.nextTurn(); } } diff --git a/module/sos-dialog-combat-actions.js b/module/sos-dialog-combat-actions.js index 872b9ab..064344d 100644 --- a/module/sos-dialog-combat-actions.js +++ b/module/sos-dialog-combat-actions.js @@ -53,9 +53,9 @@ export class SoSDialogCombatActions extends Dialog { let action3Index = $('#action3').val(); let action3 = duplicate(this.combatActions.actionsList[action3Index]); let action2Index = $('#action2').val(); - let action2 = duplicate(this.combatActions.actionsList[action2Index]._id); + let action2 = duplicate(this.combatActions.actionsList[action2Index]); let action1Index = $('#action1').val(); - let action1 = duplicate(this.combatActions.actionsList[action1Index]._id); + let action1 = duplicate(this.combatActions.actionsList[action1Index]); let msgdata = { combatId: this.combatActions.combatId, diff --git a/module/sos-main.js b/module/sos-main.js index 375ad32..3521fa9 100644 --- a/module/sos-main.js +++ b/module/sos-main.js @@ -57,6 +57,10 @@ Hooks.once("init", async function () { Hooks.on('renderChatLog', (log, html, data) => { SoSUtility.registerChatCallbacks(html); }); + // Init/registers + Hooks.on('updateCombat', (combat, round, diff, id) => { + SoSUtility.updateCombat(combat, round, diff, id); + }); }); diff --git a/module/sos-utility.js b/module/sos-utility.js index c2dc6dd..f85cbb9 100644 --- a/module/sos-utility.js +++ b/module/sos-utility.js @@ -30,8 +30,14 @@ import { SoSDialogCombatActions } from "./sos-dialog-combat-actions.js"; /* -------------------------------------------- */ onSocketMesssage( msg ) { if (msg.name == 'msg_declare_actions' ) { - let combat = game.combats.get( msg.data.combatId); // Get the associated combat - combat.setupActorActions( msg.data ); + if (game.user.isGM) { + let combat = game.combats.get( msg.data.combatId); // Get the associated combat + combat.setupActorActions( msg.data ); + } + } else if (msg.name == 'msg_close_action') { + if (game.user.isGM) { + game.combat.closeAction( msg.data.uniqId ); + } } } @@ -60,6 +66,11 @@ import { SoSDialogCombatActions } from "./sos-dialog-combat-actions.js"; return list; } + /* -------------------------------------------- */ + static updateCombat(combat, round, diff, id) { + combat.requestActions(); + } + /* -------------------------------------------- */ static async openDeclareActions( event) { event.preventDefault(); @@ -71,12 +82,29 @@ import { SoSDialogCombatActions } from "./sos-dialog-combat-actions.js"; d.render(true); } + /* -------------------------------------------- */ + static closeAction(event) { + let uniqId = event.currentTarget.attributes['data-uniq-id'].value; + // Delete message ! + const toDelete = game.messages.filter(it => it.data.content.includes( uniqId )); + toDelete.forEach(it => it.delete()); + + if ( game.user.isGM ) { + game.combat.closeAction( uniqId ); + } else { + game.socket.emit("system.foundryvtt-reve-de-dragon", { + name: "msg_close_action", data: { uniqId: uniqId} } ); + } + } + /* -------------------------------------------- */ static async registerChatCallbacks(html) { html.on("click", '#button-declare-actions', event => { SoSUtility.openDeclareActions( event ); }); - + html.on("click", '#button-end-action', event => { + SoSUtility.closeAction( event ); + }); } } \ No newline at end of file