Skip to content

Commit

Permalink
fix: change rolling
Browse files Browse the repository at this point in the history
Big change to how rolling is performed. Backward compatible, and using
both methods for now. Not meant to be merged.

Fix #212
  • Loading branch information
jonepatr committed Jan 6, 2021
1 parent e69f1b2 commit 9f76527
Show file tree
Hide file tree
Showing 16 changed files with 607 additions and 74 deletions.
5 changes: 1 addition & 4 deletions src/module/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,10 @@ const DIFFICULTIES = Object.freeze({
}
});

const CRIT = Object.freeze({"SUCCESS": 1, "FAIL": 2});

export const TWODSIX = {
CHARACTERISTICS: CHARACTERISTICS,
VARIANTS: VARIANTS,
ROLLTYPES: ROLLTYPES,
DIFFICULTIES: DIFFICULTIES,
RULESETS: RULESETS,
CRIT: CRIT
RULESETS: RULESETS
};
32 changes: 31 additions & 1 deletion src/module/entities/TwodsixActor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
* Extend the base Actor entity by defining a custom roll data structure which is ideal for the Simple system.
* @extends {Actor}
*/
import {calcModFor} from "../utils/sheetUtils";
import {calcModFor, getKeyByValue} from "../utils/sheetUtils";
import {CharacteristicType, UpdateData} from "../../types/twodsix";
import {TWODSIX} from "../config";
import { TwodsixRollSettings } from "../utils/TwodsixRollSettings";
import { TwodsixDiceRoll } from "../utils/TwodsixDiceRoll";

export default class TwodsixActor extends Actor {

Expand Down Expand Up @@ -70,6 +73,33 @@ export default class TwodsixActor extends Actor {
return remaining;
}

public getCharacteristicModifier(characteristic:string):number {
if (characteristic === 'NONE') {
return 0;
} else {
const keyByValue = getKeyByValue(TWODSIX.CHARACTERISTICS, characteristic);
return calcModFor(this.data.data.characteristics[keyByValue].current);
}
}

public async characteristicRoll(tmpSettings: any, showThrowDialog:boolean, showInChat=true):Promise<TwodsixDiceRoll> {
if (!tmpSettings["characteristic"]) {
ui.notifications.error(game.i18n.localize("Errors.NoCharacteristicForRoll"));
return;
}
const settings = await TwodsixRollSettings.create(showThrowDialog, tmpSettings);
if (!settings.shouldRoll) {
return;
}

const diceRoll = new TwodsixDiceRoll(settings, this);
if (showInChat) {
diceRoll.sendToChat();
}
console.log("DEBUG CHARACTERISTICS ROLL:", diceRoll);
return diceRoll;
}

private static addDamage(damage:number, characteristic:CharacteristicType):number {
let handledDamage = 0;
if (damage + characteristic.damage > characteristic.value) {
Expand Down
154 changes: 153 additions & 1 deletion src/module/entities/TwodsixItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
* @extends {Item}
*/

import { TwodsixDiceRoll } from "../utils/TwodsixDiceRoll";
import { TwodsixRollSettings } from "../utils/TwodsixRollSettings";
import TwodsixActor from "./TwodsixActor";

export default class TwodsixItem extends Item {

/**
Expand All @@ -10,6 +14,154 @@ export default class TwodsixItem extends Item {
prepareData():void {
super.prepareData();
}
}

public async performAttack(attackType:string, showThrowDialog:boolean, rateOfFireCE:number=null, showInChat=true):Promise<void> {
if (this.type !== "weapon") {
return;
}

let numberOfAttacks = 1;
let bonusDamage = "0";
const rateOfFire = this.data.data.rateOfFire;

if (attackType && !rateOfFire) {
ui.notifications.error(game.i18n.localize("Errors.NoROFForAttack"));
}
const skill:TwodsixItem = this.actor.getOwnedItem(this.data.data.skill) as TwodsixItem;
const tmpSettings = {"characteristic": skill.data.data.characteristic};

switch (attackType) {
case "auto-full":
numberOfAttacks = parseInt(rateOfFire);
break;
case "auto-burst":
bonusDamage = rateOfFire;
break;
case "burst-attack-dm":
tmpSettings["diceModifier"] = TwodsixItem.burstAttackDM(rateOfFireCE);
break;
case "burst-bonus-damage":
bonusDamage = TwodsixItem.burstBonusDamage(rateOfFireCE);
break;
}

const settings = await TwodsixRollSettings.create(showThrowDialog, tmpSettings, skill, this);

if (!settings.shouldRoll) {
return;
}

for (let i = 0; i < numberOfAttacks; i++) {
const roll = await this.skillRoll(false, settings, showInChat);
if (game.settings.get("twodsix", "automateDamageRollOnHit") && roll.isSuccess()) {
await this.rollDamage(settings.rollMode, `${roll.effect} + ${bonusDamage}`, showInChat);
}
}
}


public async skillRoll(showThrowDialog:boolean, tmpSettings=null, showInChat=true):Promise<TwodsixDiceRoll> {
let skill: TwodsixItem;
let item: TwodsixItem;

// Determine if this is a skill or an item
if (this.type == "skills") {
skill = this;
item = null;
} else if (this.data.data.skill) {
skill = this.actor.getOwnedItem(this.data.data.skill) as TwodsixItem;
item = this;
}

if (!skill) {
ui.notifications.error(game.i18n.localize("Errors.NoSkillForSkillRoll"));
return;
}

const settings = await TwodsixRollSettings.create(showThrowDialog, tmpSettings, skill, item);
if (!settings.shouldRoll) {
return;
}

const diceRoll = new TwodsixDiceRoll(settings, <TwodsixActor>this.actor, skill, item);

if (showInChat) {
diceRoll.sendToChat();
}
console.log("DEBUG ROLL:", diceRoll);
return diceRoll;
}

public async rollDamage(rollMode:string, bonusDamage="", showInChat=true):Promise<Roll> {
const doesDamage = this.data.data.damage != null;
if (!doesDamage) {
ui.notifications.error(game.i18n.localize("Errors.NoDamageForWeapon"));
}

const damageFormula = this.data.data.damage + (bonusDamage ? "+" + bonusDamage : "");
const damageRoll = new Roll(damageFormula, {});
const damage:Roll = damageRoll.roll();
if (showInChat) {
const results = damage.terms[0]["results"];
const contentData = {
flavor: `${game.i18n.localize("TWODSIX.Rolls.DamageUsing")} ${this.name}`,
roll: damage,
damage: damage.total,
dice: results
};

const html = await renderTemplate('systems/twodsix/templates/chat/damage-message.html', contentData);

const messageData = {
user: game.user._id,
speaker: ChatMessage.getSpeaker({actor: this.actor}),
content: html,
type: CONST.CHAT_MESSAGE_TYPES.ROLL,
roll: damage,
rollMode: rollMode,
flags: {"core.canPopout": true}
};

messageData["flags.transfer"] = JSON.stringify(
{
type: 'damageItem',
payload: contentData
}
);

ChatMessage.create(messageData, {rollMode: rollMode});
}
console.log("DEBUG DAMAGE ROLL:", damageRoll);
return damageRoll;
}

public static burstAttackDM(number: number):number {
if (number >= 100) {
return 4;
} else if (number >= 20) {
return 3;
} else if (number >= 10) {
return 2;
} else if (number >= 3) {
return 1;
} else {
return 0;
}
}

public static burstBonusDamage(number: number):string {
if (number >= 100) {
return '4d6';
} else if (number >= 20) {
return '3d6';
} else if (number >= 10) {
return '2d6';
} else if (number >= 4) {
return '1d6';
} else if (number >= 3) {
return '1';
} else {
return '0';
}
}
}
27 changes: 3 additions & 24 deletions src/module/handlebars.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {advantageDisadvantageTerm} from "./i18n";
import {calcModFor, getKeyByValue} from "./utils/sheetUtils";
import {TWODSIX} from "./config";
import TwodsixItem from "./entities/TwodsixItem";

export default function registerHandlebarsHelpers():void {

Expand Down Expand Up @@ -79,34 +80,12 @@ export default function registerHandlebarsHelpers():void {

Handlebars.registerHelper('twodsix_burstAttackDM', (burstSize:string) => {
const number = Number(burstSize);
if (number <= 2) {
return 0;
} else if (number >= 100) {
return 4;
} else if (number >= 20) {
return 3;
} else if (number >= 10) {
return 2;
} else if (number >= 4) {
return 1;
}
return TwodsixItem.burstAttackDM(number);
});

Handlebars.registerHelper('twodsix_burstBonusDamage', (burstSize) => {
const number = Number(burstSize);
if (number <= 2) {
return '0';
} else if (number >= 100) {
return '4d6';
} else if (number >= 20) {
return '3d6';
} else if (number >= 10) {
return '2d6';
} else if (number >= 4) {
return '1d6';
} else if (number === 3) {
return '1';
}
return TwodsixItem.burstBonusDamage(number);
});

//From https://discord.com/channels/732325252788387980/732328233630171188/790507540818690068
Expand Down
22 changes: 14 additions & 8 deletions src/module/hooks/renderChatMessage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TWODSIX } from "../config";
import {Crit} from "../../types/twodsix";

Hooks.on('renderChatMessage', (app, html) => {
const damageMessage = html.find(".damage-message")[0];
Expand All @@ -10,14 +10,20 @@ Hooks.on('renderChatMessage', (app, html) => {
});
}

const diceTotal = html.find(".dice-total");
if (!damageMessage) {
const diceTotal = html.find(".dice-total");

if (diceTotal.length > 0) {
const crit = app.getFlag("twodsix", "crit");
if (crit && crit == TWODSIX.CRIT.SUCCESS) {
diceTotal.addClass("crit-success-roll");
} else if (crit && crit == TWODSIX.CRIT.FAIL) {
diceTotal.addClass("crit-fail-roll");
// Add effect
diceTotal.text(`${diceTotal.text()} (${app.getFlag("twodsix", "effect")})`);

// Color crits
if (diceTotal.length > 0) {
const crit = app.getFlag("twodsix", "crit");
if (crit && crit == Crit.success) {
diceTotal.addClass("crit-success-roll");
} else if (crit && crit == Crit.fail) {
diceTotal.addClass("crit-fail-roll");
}
}
}
});
Loading

0 comments on commit 9f76527

Please sign in to comment.