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 authored and xdy committed Jan 7, 2021
1 parent 064b9a6 commit cd249f3
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 40 deletions.
24 changes: 24 additions & 0 deletions src/module/entities/TwodsixActor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,16 @@ import {CharacteristicType, UpdateData} from "../../types/twodsix";
import {TWODSIX} from "../config";
import { TwodsixRollSettings } from "../utils/TwodsixRollSettings";
import { TwodsixDiceRoll } from "../utils/TwodsixDiceRoll";
import TwodsixItem from "./TwodsixItem";

export default class TwodsixActor extends Actor {
public static async create(data: Record<string, unknown>, options?: Record<string, unknown>): Promise<Entity> {
const actor = await super.create(data, options) as TwodsixActor;
if (actor.data.type == "traveller") {
actor.createUntrainedSkill();
}
return actor;
}

/**
* Augment the basic actor data with additional dynamic data.
Expand Down Expand Up @@ -133,4 +141,20 @@ export default class TwodsixActor extends Actor {
await this.update(updateData);
}

public getUntrainedSkill():TwodsixItem {
return this.getOwnedItem(this.data.data.untrainedSkill) as TwodsixItem;
}

private async createUntrainedSkill() {
const data = {
"name": game.i18n.localize("TWODSIX.Actor.Skills.Untrained") + "WAZZAA",
"data": {
"value": game.settings.get('twodsix', 'untrainedSkillValue')
},
"type": "skills",
"flags": {'twodsix.hide': true}
};
const untrainedSkill = await this.createOwnedItem(data) as TwodsixItem;
await this.update({"data.untrainedSkill": untrainedSkill._id});
}
}
9 changes: 9 additions & 0 deletions src/module/handlebars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@ export default function registerHandlebarsHelpers():void {
return TwodsixItem.burstBonusDamage(number);
});

Handlebars.registerHelper('twodsix_filterSkills', (skill) => {
return !skill.data.flags.twodsix?.hide && skill.type === "skills";
});

// Handy for debugging
Handlebars.registerHelper('json', function(context) {
return JSON.stringify(context);
});

//From https://discord.com/channels/732325252788387980/732328233630171188/790507540818690068
//Not used yet
Handlebars.registerHelper("iff", function (v1, operator, v2, options) {
Expand Down
1 change: 1 addition & 0 deletions src/module/sheets/AbstractTwodsixActorSheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ export abstract class AbstractTwodsixActorSheet extends ActorSheet {
itemData.data.value = String(0);
}
}
itemData.data.skill = this.actor.getUntrainedSkill()._id;
// Finally, create the item!
return this.actor.createOwnedItem(itemData);
}
Expand Down
22 changes: 1 addition & 21 deletions src/module/sheets/TwodsixActorSheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,29 +142,9 @@ export class TwodsixActorSheet extends AbstractTwodsixActorSheet {
* @private
*/
private async _onRollUntrained(event:Event, showTrowDiag:boolean):Promise<void> {
const data = {
"name": game.i18n.localize("TWODSIX.Actor.Skills.Untrained"),
"data": {
"value": game.settings.get('twodsix', 'untrainedSkillValue')
},
"type": "skills"
};

// this feels very hacky, but creating temporary TwodsixItems only returns a plain js object..
// however the rest of the handling of this roll then behaves exactly like a normal skill, so it feels
// somewhat worth it.
let skill:TwodsixItem;
try {
skill = TwodsixItem.createOwned(data, this.actor) as TwodsixItem;
await skill.skillRoll(showTrowDiag);
} finally {
if (skill) {
skill.delete();
}
}
this.actor.getUntrainedSkill().skillRoll(showTrowDiag);
}


/**
* Handle clickable damage rolls.
* @param {Event} event The originating click event
Expand Down
32 changes: 15 additions & 17 deletions src/module/utils/TwodsixDiceRoll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ export class TwodsixDiceRoll {
effect: number;
roll: Roll;

constructor (settings: TwodsixRollSettings, actor: TwodsixActor, skill: TwodsixItem=null, item: TwodsixItem=null) {
this.settings = settings;
this.actor = actor;
this.skill = skill;
this.item = item;

this.createRoll();

this.naturalTotal = this.roll.dice[0].results.reduce((total:number, dice) => {
return dice["active"] ? total + dice["result"] : total;
}, 0);

this.calculateEffect();
}

private createRoll(): void {
const difficultiesAsTargetNumber = game.settings.get('twodsix', 'difficultiesAsTargetNumber');
Expand Down Expand Up @@ -50,23 +64,7 @@ export class TwodsixDiceRoll {
data["difficultyMod"] = this.settings.difficulty.mod;
}

this.roll = new Roll(formula, data);
this.roll.roll();
}

constructor (settings: TwodsixRollSettings, actor: TwodsixActor, skill: TwodsixItem=null, item: TwodsixItem=null) {
this.settings = settings;
this.actor = actor;
this.skill = skill;
this.item = item;

this.createRoll();

this.naturalTotal = this.roll.dice[0].results.reduce((total:number, dice) => {
return dice["active"] ? total + dice["result"] : total;
}, 0);

this.calculateEffect();
this.roll = new Roll(formula, data).roll();
}

public getCrit(): Crit {
Expand Down
1 change: 1 addition & 0 deletions static/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"contacts": "",
"allies": "",
"enemies": "",
"untrainedSkill": null,
"description": "",
"bio": "",
"notes": "",
Expand Down
4 changes: 2 additions & 2 deletions static/templates/items/parts/common-parts.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
<label for="data.skill" class="resource-label">{{localize "TWODSIX.Items.Equipment.AssignSkill"}}:
<select id="data.skill" name="data.skill" value="{{data.skill}}">
{{#select data.skill}}
<option>{{localize "TWODSIX.Actor.Skills.Untrained"}}</option>
<option value="{{data.owner.data.data.untrainedSkill}}" >{{localize "TWODSIX.Actor.Skills.Untrained"}}</option>
{{#each data.owner.items as |skill|}}
{{#if (eq skill.type 'skills')}}
{{#if (twodsix_filterSkills skill)}}
{{#if (twodsix_hideUntrainedSkills item.data.value)}}
<!-- Hiding {{item.name}} -->
{{else}}
Expand Down

0 comments on commit cd249f3

Please sign in to comment.