kopparhavet/module/helpers/dice-helper.js

476 lines
14 KiB
JavaScript

export default class KHDiceRoller {
async rollSpellCostInChat(cost, speaker) {
let formula = cost + "d6"
const roll = new Roll(formula);
await roll.evaluate();
let rollData = {
name: "SPELL.COST",
res: roll,
showFormula: true
};
const html = await renderTemplate("systems/kopparhavet/templates/dice/roll.html", rollData);
if(game.settings.get("kopparhavet", "applySpellCost")) {
let totalCostForActor = speaker.data.data.mana.value + roll.total
speaker.update({ ["data.mana.value"]: totalCostForActor });
}
await roll.toMessage({
create: true,
content: html,
user: game.user.id,
speaker: {
actor: speaker.id,
token: speaker.token,
alias: speaker.name,
},
});
}
async rollDamageInChat(facesNr, speaker) {
let formula = facesNr + "d6x6"
const roll = new Roll(formula);
await roll.evaluate();
let rollData = {
name: "ITEM.DAMAGE",
res: roll,
showFormula: true
};
const html = await renderTemplate("systems/kopparhavet/templates/dice/roll.html", rollData);
await roll.toMessage({
create: true,
content: html,
user: game.user.id,
speaker: {
actor: speaker.id,
token: speaker.token,
alias: speaker.name,
},
});
}
async rollArmorInChat(armor, hasHelmet, speaker) {
let formula = armor + "d6"
if(hasHelmet) {
formula += "x6"
}
const roll = new Roll(formula);
await roll.evaluate();
let rollData = {
name: "ITEM.DEFENCE",
res: roll,
showFormula: true
};
const html = await renderTemplate("systems/kopparhavet/templates/dice/roll.html", rollData);
await roll.toMessage({
create: true,
content: html,
user: game.user.id,
speaker: {
actor: speaker.id,
token: speaker.token,
alias: speaker.name,
},
});
}
async rollSkillInChat(skillName, skillValue, showValue, speaker, openclosed, opposite) {
const roll = new Roll(`1d100`);
await roll.evaluate();
let computedName = skillName
if(showValue) {
computedName += " (" + skillValue + ")"
}
if(openclosed === undefined) {
openclosed = 0
}
if(opposite === undefined) {
opposite = []
}
let oppositeValue = ""
opposite.forEach(function(skill) {
if(oppositeValue !== "") {
oppositeValue = oppositeValue + " " + game.i18n.localize("ROLL.OR") + " "
}
oppositeValue = oppositeValue + skill
})
let rollData = {
name: computedName,
res: roll,
opposite: oppositeValue
};
if(skillValue > 0) {
let oneRes = Math.floor((roll.total / 1) % 10);
let tenRes = Math.floor((roll.total / 10) % 10);
if(openclosed < 0) {
rollData.closed = Math.abs(openclosed)
}
if(openclosed > 0) {
rollData.opened = Math.abs(openclosed)
}
if(openclosed < 0 && oneRes !== 0 && Math.abs(openclosed) >= oneRes) {
// roll is closed
rollData.failure = true
} else if(openclosed > 0 && oneRes !== 0 && Math.abs(openclosed) >= oneRes){
// roll is opened
rollData.success = true
} else {
if (roll.total <= skillValue) {
rollData.success = true
} else {
rollData.failure = true
}
}
if(oneRes === tenRes) {
rollData.excetional = true
}
}
const html = await renderTemplate("systems/kopparhavet/templates/dice/roll.html", rollData);
await roll.toMessage({
create: true,
content: html,
user: game.user.id,
speaker: {
actor: speaker.id,
token: speaker.token,
alias: speaker.name,
},
});
}
async rollSkillDialogInChat(skillName, skillValue, showValue, speaker, startopen, opposite) {
const id = randomID();
if(startopen === undefined) {
startopen = 0
}
const content = await renderTemplate("systems/kopparhavet/templates/roll-dialog.html", {
id,
startopen,
skillName,
skillValue,
});
await new Dialog({
title: game.i18n.localize("ROLL.TITLE"),
content,
buttons: {
one: {
icon: '<i class="fas fa-check"></i>',
label: game.i18n.localize("BUTTON.ROLL"),
callback: async () => {
const container = document.getElementById(id);
let openclosed = container.querySelector('[name="openclosed"]').value
await this.rollSkillInChat(skillName, skillValue, showValue, speaker, openclosed, opposite)
/*
const roll = new Roll(`1d100`);
let res = roll.roll();
let computedName = skillName
if(showValue) {
computedName += " (" + skillValue + ")"
}
let rollData = {
name: computedName,
res: res
};
// Evaluate result only if we have a positive skillvalue
if(skillValue > 0) {
let oneRes = Math.floor((res.total / 1) % 10);
let tenRes = Math.floor((res.total / 10) % 10);
if(openclosed < 0) {
rollData.closed = Math.abs(openclosed)
}
if(openclosed > 0) {
rollData.opened = Math.abs(openclosed)
}
if(openclosed < 0 && oneRes !== 0 && Math.abs(openclosed) >= oneRes) {
// roll is closed
rollData.failure = true
} else if(openclosed > 0 && oneRes !== 0 && Math.abs(openclosed) >= oneRes){
// roll is opened
rollData.success = true
} else {
if (res.total <= skillValue) {
rollData.success = true
} else {
rollData.failure = true
}
}
if(oneRes === tenRes) {
rollData.excetional = true
}
}
const html = await renderTemplate("systems/kopparhavet/templates/dice/roll.html", rollData);
await roll.toMessage({
create: true,
content: html,
user: game.user._id,
speaker: {
actor: speaker._id,
token: speaker.token,
alias: speaker.name,
},
});
*/
},
},
two: {
icon: '<i class="fas fa-times"></i>',
label: game.i18n.localize("BUTTON.CANCEL"),
},
},
},
{
classes: ["dialog", "kopparhavet"],
}).render(true);
}
async rollSkillRitualInChat(ritualName, skills, showValue, speaker, openclosed) {
let rollData = {
name: ritualName,
res: []
};
if(openclosed === undefined) {
openclosed = 0
}
if(openclosed < 0) {
rollData.closed = Math.abs(openclosed)
}
if(openclosed > 0) {
rollData.opened = Math.abs(openclosed)
}
let successes = 0
let totalrolls = 0
for (const skill of skills) {
totalrolls++
const roll = new Roll(`1d100`);
await roll.evaluate();
let skillName = skill.name
let success = false
let failure = false
if(showValue) {
skillName = skillName + " (" + skill.value + ")"
}
let rollData2 = {
name: skillName,
res: roll,
};
if(openclosed < 0) {
rollData2.closed = Math.abs(openclosed)
}
if(openclosed > 0) {
rollData2.opened = Math.abs(openclosed)
}
if(skill.value > 0) {
let oneRes = Math.floor((roll.total / 1) % 10);
//let tenRes = Math.floor((res.total / 10) % 10);
if(openclosed < 0 && oneRes !== 0 && Math.abs(openclosed) >= oneRes) {
// roll is closed
failure = true
} else if(openclosed > 0 && oneRes !== 0 && Math.abs(openclosed) >= oneRes){
// roll is opened
success = true
successes++
} else {
if (roll.total <= skill.value) {
success = true
successes++
} else {
failure = true
}
}
}
rollData.res.push({
name: skillName,
failure: failure,
success: success,
result: roll,
})
rollData2.success = success
rollData2.failure = failure
const html = await renderTemplate("systems/kopparhavet/templates/dice/roll.html", rollData2);
await roll.toMessage({
create: true,
content: html,
user: game.user.id,
speaker: {
actor: speaker.id,
token: speaker.token,
alias: speaker.name,
},
});
}
switch (totalrolls) {
case 0:
case 1:
case 2:
break;
case 3:
if(successes === 0) {
rollData.result = "0"
} else if(successes === 1) {
rollData.result = "2"
} else if(successes === 2) {
rollData.result = "3"
} else if(successes === 3) {
rollData.result = "4"
}
break;
case 4:
if(successes === 0) {
rollData.result = "0"
} else if(successes === 1) {
rollData.result = "1"
} else if(successes === 2) {
rollData.result = "2"
} else if(successes === 3) {
rollData.result = "3"
} else if(successes === 4) {
rollData.result = "4"
}
break;
case 5:
if(successes === 0) {
rollData.result = "0"
} else if(successes === 1) {
rollData.result = "1"
} else if(successes === 2) {
rollData.result = "1"
} else if(successes === 3) {
rollData.result = "2"
} else if(successes === 4) {
rollData.result = "3"
} else if(successes === 5) {
rollData.result = "4"
}
break;
}
const html = await renderTemplate("systems/kopparhavet/templates/dice/ritual-roll.html", rollData);
/*
await roll.toMessage({
create: true,
content: html,
user: game.user._id,
speaker: {
actor: speaker._id,
token: speaker.token,
alias: speaker.name,
},
});
*/
await ChatMessage.create({
create: true,
content: html,
user: game.user.id,
speaker: {
actor: speaker.id,
token: speaker.token,
alias: speaker.name,
},
});
}
async rollSkillChallangeDialogInChat(name, skills, showValue, speaker, startopen) {
const id = randomID();
if(startopen === undefined) {
startopen = 0
}
const content = await renderTemplate("systems/kopparhavet/templates/roll-dialog.html", {
id,
startopen,
name,
});
await new Dialog({
title: game.i18n.localize("ROLL.TITLE"),
content,
buttons: {
one: {
icon: '<i class="fas fa-check"></i>',
label: game.i18n.localize("BUTTON.ROLL"),
callback: async () => {
const container = document.getElementById(id);
let openclosed = container.querySelector('[name="openclosed"]').value
await this.rollSkillRitualInChat(name, skills, showValue, speaker, openclosed)
},
},
two: {
icon: '<i class="fas fa-times"></i>',
label: game.i18n.localize("BUTTON.CANCEL"),
},
},
},
{
classes: ["dialog", "kopparhavet"],
}).render(true);
}
}