Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: individual initiative dice spam #44

Merged
merged 7 commits into from
Jan 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dist/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"OSE.roll.reaction": "Reaction roll",
"OSE.roll.initiative": "Group {group} rolls for Initiative!",
"OSE.roll.individualInit": "{name} rolls for Initiative!",
"OSE.roll.individualInitGroup": "Initiative Rolls",

"OSE.table.treasure.roll": "Roll Treasure",

Expand Down
85 changes: 42 additions & 43 deletions dist/module/combat.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,58 +65,57 @@ export class OseCombat {

static async individualInitiative(combat, data) {
let updates = [];
let messages = [];

let rolls = []
for (let i = 0; i < combat.data.combatants.size; i++) {
let c = combat.data.combatants.contents[i];

// This comes from foundry.js, had to remove the update turns thing
// Roll initiative
const cf = await c._getInitiativeFormula(c);
const roll = await c.getInitiativeRoll(cf).evaluate({ async: true });

let value = roll.total;
if (combat.settings.skipDefeated && c.defeated) {
const roll = await c.getInitiativeRoll(cf)
rolls.push(roll)
const data = { _id: c.id };
updates.push(data);
}
//combine init rolls
const pool = PoolTerm.fromRolls(rolls);
const combinedRoll = await Roll.fromTerms([pool]);
//get evaluated chat message
const evalRoll = await combinedRoll.toMessage({},{create: false})
let rollArr = combinedRoll.terms[0].rolls
let msgContent = ``
for(let i = 0; i < rollArr.length; i++){
let roll = rollArr[i]
//get combatant
let cbt = game.combats.viewed.combatants.find(c=>c.id == updates[i]._id)
//add initiative value to update
//check if actor is slow
let value = cbt.actor.data.data.isSlow ? OseCombat.STATUS_SLOW : roll.total;
//check if actor is defeated
if (combat.settings.skipDefeated &&cbt.isDefeated) {
value = OseCombat.STATUS_DIZZY;
}
const data = { _id: c.id, initiative: value };

updates.push(data);
updates[i].initiative = value

// Determine the roll mode
let rollMode = game.settings.get("core", "rollMode");
if ((c.token.hidden || c.hidden) && rollMode === "roll")
rollMode = "gmroll";

// Construct chat message data
let messageData = foundry.utils.mergeObject(
{
speaker: {
scene: combat.scene.id,
actor: c.actor?.id,
token: c.token?.id,
alias: c.name,
},
flavor: game.i18n.format("OSE.roll.individualInit", {
name: c.token.name,
}),
flags: { "ose.initiativeRoll": true },
},
{}
);
const chatData = await roll.toMessage(messageData, {
rollMode: c.hidden && rollMode === "roll" ? "gmroll" : rollMode,
create: false,
});
if (i > 0) chatData.sound = null; // Only play 1 sound for the whole set
messages.push(chatData);
//render template
let template = "systems/ose/dist/templates/chat/roll-individual-initiative.html"
let tData = {
name: cbt.name,
formula: roll.formula,
result: roll.result,
total: roll.total,
}
let rendered = await renderTemplate(template, tData)
msgContent += rendered
}
if (game.user.isGM) {
await combat.updateEmbeddedDocuments("Combatant", updates);
}

await ChatMessage.implementation.create(messages);
data.turn = 0;
evalRoll.content = `
<details>
<summary>${game.i18n.localize('OSE.roll.individualInitGroup')}</summary>
${msgContent}
</details>`
ChatMessage.create(evalRoll);
//update tracker
if (game.user.isGM) await combat.updateEmbeddedDocuments('Combatant', updates);
data.turn = 0;
}

static format(object, html, user) {
Expand Down
6 changes: 3 additions & 3 deletions dist/module/item/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,13 +272,13 @@ export class OseItem extends Item {
return this.update({ data: newData });
}

roll() {
roll(options = {}) {
switch (this.type) {
case "weapon":
this.rollWeapon();
this.rollWeapon(options);
break;
case "spell":
this.spendSpell();
this.spendSpell(options);
break;
case "ability":
if (this.data.data.roll) {
Expand Down
23 changes: 23 additions & 0 deletions dist/templates/chat/roll-individual-initiative.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<div><b>{{name}}</b></div>
<div class="dice-roll">
<div class="dice-result">
<div class="dice-formula">{{formula}}</div>
<div class="dice-tooltip expanded" style="display: none">
<section class="tooltip-part">
<div class="dice">
<header class="part-header flexrow">
<span class="part-formula">{{result}}</span>

<span class="part-total">{{total}}</span>
</header>
<ol class="dice-rolls">
<li class="roll die d6">{{total}}</li>
</ol>
</div>
</section>
</div>

<h4 class="dice-total">{{total}}</h4>
</div>
</div>
</br>