Skip to content

Commit

Permalink
Working on issue #102. Basic support for linking items to skills, and…
Browse files Browse the repository at this point in the history
… making skill rolls using the item.

Can now select a skill on owned items that is saved on the item. Deliberately no filtering of the skill you can select.
If you click an item with a linked skill, it is rolled, emitting a text like: 'Rolling Skillname using Itemname'. If it isn't linked, no roll occurs.

TODO Support advanced skill rolls (shift-click)
TODO Add a 'permanent modifier' field
TODO Clean up the code
TODO Clean up the layout
  • Loading branch information
xdy committed Sep 12, 2020
1 parent 08bd2c3 commit 951fcae
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 67 deletions.
14 changes: 13 additions & 1 deletion src/module/sheets/AbstractTwodsixItemSheet.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
export class AbstractTwodsixItemSheet extends ItemSheet {

protected handleContentEditable(html:JQuery<HTMLElement>) {
protected handleContentEditable(html:JQuery<HTMLElement>):void {
html.find('div[contenteditable="true"][data-edit]').on(
'focusout',
this._onSubmit.bind(this)
);
}


protected activateListeners(html:JQuery):void {
super.activateListeners(html);
}

getData():ItemSheetData<any> {
const data = super.getData();
data.data.owner = this.actor;

return data;
}
}
59 changes: 4 additions & 55 deletions src/module/sheets/TwodsixActorSheet.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import TwodsixItem from "../entities/TwodsixItem";
import {skillRollResultDisplay} from "../utils/sheetUtils";
import {TwodsixRolls} from "../utils/TwodsixRolls";
import {AbstractTwodsixActorSheet} from "./AbstractTwodsixActorSheet";
import TwodsixActor from "../entities/TwodsixActor";

export class TwodsixActorSheet extends AbstractTwodsixActorSheet {

Expand Down Expand Up @@ -51,61 +50,11 @@ export class TwodsixActorSheet extends AbstractTwodsixActorSheet {
/**
* Handle clickable rolls.
* @param {Event} event The originating click event
* @param actor
* @private
*/
_onRoll(event:{ preventDefault:any; currentTarget:any; shiftKey?:any; }):void {
event.preventDefault();
const element = event.currentTarget;
const dataset = element.dataset;

const itemId = $(event.currentTarget).parents('.item').attr('data-item-id');
const item = this.actor.getOwnedItem(itemId) as TwodsixItem;

if (dataset.roll) {
if (item != null && 'skills' === item.type && event.shiftKey) {
this.advancedSkillRoll(itemId, event, dataset);
} else {
this.simpleSkillRoll(dataset);
}
}
}

private simpleSkillRoll(dataset:DOMStringMap) {
const rollParts = dataset.roll.split("+");
const flavorParts:string[] = [];
const label = dataset.label ? game.i18n.localize("TWODSIX.Actor.Rolling") + ` ${dataset.label}` : '';
flavorParts.push(label);
skillRollResultDisplay(rollParts, flavorParts);
const flavor = flavorParts.join(' ');
const roll = new Roll(rollParts.join('+'), this.actor.data.data);

roll.roll().toMessage({
speaker: ChatMessage.getSpeaker({actor: this.actor}),
flavor: flavor
});
}

advancedSkillRoll(skillId:string, event:{ preventDefault:() => void; currentTarget:any; }, dataset:{ roll:string; }):Promise<any> {

const skillData = {};
const skills = this.getData().actor.skills;
if (!skills.length) {
return;
}

const rollParts = dataset.roll.split("+");

const flavorParts:string[] = [];
const skill = skills.filter(x => x._id === skillId)[0];
flavorParts.push(`${skill.name}`);

return TwodsixRolls.Roll({
parts: rollParts,
data: skillData,
flavorParts: flavorParts,
title: `${skill.name}`,
speaker: ChatMessage.getSpeaker({actor: this.getData().actor}),
});
public _onRoll(event:{ preventDefault:any; currentTarget:any; shiftKey?:any; }, actor:TwodsixActor):void {
TwodsixRolls.handleSkillRoll(event, this.actor);
}

//Unused, but something like it is needed to support cascade/subskills, so letting it stay for now.
Expand Down
1 change: 0 additions & 1 deletion src/module/sheets/TwodsixItemSheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export class TwodsixItemSheet extends AbstractTwodsixItemSheet {
/** @override */
getData():ItemSheetData {
const data = super.getData();
data.data.owner = this.actor;

return data;
}
Expand Down
68 changes: 66 additions & 2 deletions src/module/utils/TwodsixRolls.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { TWODSIX } from "../config";
import {TWODSIX} from "../config";
import {advantageDisadvantageTerm} from "../settings";
import { skillRollResultDisplay } from "./sheetUtils";
import {skillRollResultDisplay} from "./sheetUtils";
import TwodsixItem from "../entities/TwodsixItem";
import TwodsixActor from "../entities/TwodsixActor";

export class TwodsixRolls {
static async Roll({
Expand Down Expand Up @@ -106,4 +108,66 @@ export class TwodsixRolls {
);
return roll;
}

private static simpleSkillRoll(dataset:DOMStringMap, actor:TwodsixActor):void {
const rollParts = dataset.roll.split("+");
const flavorParts:string[] = [];
const label = dataset.label ? game.i18n.localize("TWODSIX.Actor.Rolling") + ` ${dataset.label}` : '';
flavorParts.push(label);
skillRollResultDisplay(rollParts, flavorParts);
const flavor = flavorParts.join(' ');
const roll = new Roll(rollParts.join('+'), actor.data.data);

roll.roll().toMessage({
speaker: ChatMessage.getSpeaker({actor: actor}),
flavor: flavor
});
}

private static advancedSkillRoll(skillId:string, event:{ preventDefault:() => void; currentTarget:any; }, dataset:{ roll:string; }, actor:any):Promise<any> {
const skillData = {};
const skills = actor.skills;
if (!skills.length) {
return;
}

const rollParts = dataset.roll.split("+");

const flavorParts:string[] = [];
const skill = skills.filter(x => x._id === skillId)[0];
flavorParts.push(`${skill.name}`);

return TwodsixRolls.Roll({
parts: rollParts,
data: skillData,
flavorParts: flavorParts,
title: `${skill.name}`,
speaker: ChatMessage.getSpeaker({actor: actor}),
});
}


static handleSkillRoll(event:{ preventDefault:any; currentTarget:any; shiftKey?:any }, actor:TwodsixActor):void {
event.preventDefault();
const element = event.currentTarget;
const dataset = element.dataset;

const itemId = $(event.currentTarget).parents('.item').attr('data-item-id');
const item = actor.getOwnedItem(itemId) as TwodsixItem;

if (!dataset.roll && 'skills' != item.type) {
const skill = actor.getOwnedItem(item.data.data.skill) as TwodsixItem;
if (skill!=null) {
dataset.roll = "2d6+" + skill.data.data.mod + "+" + skill.data.data.value;
dataset.label = skill.name + " using " + item.name; //TODO Localize
//TODO Or should I do some replacement of values in the data-roll formula?
}
}

if (item != null && 'skills' === item.type && event.shiftKey) {
TwodsixRolls.advancedSkillRoll(itemId, event, dataset, actor.data);
} else {
TwodsixRolls.simpleSkillRoll(dataset, actor);
}
}
}
17 changes: 9 additions & 8 deletions static/templates/actors/parts/actor/actor-items.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<span class="item-title">{{localize "TWODSIX.Actor.Items.WEAPONS"}}</span>
<div class="items-weapons gear-header">
<span class="item-name">{{localize "TWODSIX.Actor.Items.Name"}}</span>
<span class="item-name rollable">{{localize "TWODSIX.Actor.Items.Name"}}</span>
<span class="item-name centre">{{localize "TWODSIX.Actor.Items.TL"}}</span>
<span class="item-name centre">{{localize "TWODSIX.Actor.Items.Qty"}}</span>
<span class="item-name centre">{{localize "TWODSIX.Actor.Items.Weight"}}</span>
Expand All @@ -22,7 +22,8 @@
<ol class="ol-no-indent">
<li class="item flexrow" data-item-id="{{item._id}}">
<span class="items-weapons">
<span class="item-name">{{item.name}}</span>
<span class="mini-dice centre"><img class="rollable" data-label="{{item.name}}"src="./systems/twodsix/assets/d6-icon.svg"/></span>
<span class="item-name rollable">{{item.name}}</span>
<span class="item-name centre">{{data.techLevel}}</span>
<span class="item-name centre">{{data.quantity}}</span>
<span class="item-name centre">{{data.weight}}</span>
Expand All @@ -44,7 +45,7 @@
<div><span class="pusher"></span>
<span class="item-title">{{localize "TWODSIX.Actor.Items.ARMOR"}}</span>
<div class="items-armor gear-header">
<span class="item-name">{{localize "TWODSIX.Actor.Items.Name"}}</span>
|<span class="item-name rollable">{{localize "TWODSIX.Actor.Items.Name"}}</span>
<span class="item-name centre">{{localize "TWODSIX.Actor.Items.TL"}}</span>
<span class="item-name centre">{{localize "TWODSIX.Actor.Items.Qty"}}</span>
<span class="item-name centre">{{localize "TWODSIX.Actor.Items.Weight"}}</span>
Expand All @@ -59,7 +60,7 @@
<ol class="ol-no-indent">
<li class="item flexrow" data-item-id="{{item._id}}">
<span class="items-armor">
<span class="item-name">{{item.name}}</span>
|<span class="item-name rollable">{{item.name}}</span>
<span class="item-name centre">{{data.techLevel}}</span>
<span class="item-name centre">{{data.quantity}}</span>
<span class="item-name centre">{{data.weight}}</span>
Expand All @@ -79,7 +80,7 @@
<div><span class="pusher"></span>
<span class="item-title">{{localize "TWODSIX.Actor.Items.AUGMENTS"}}</span>
<div class="items-augments gear-header">
<span class="item-name">{{localize "TWODSIX.Actor.Items.Name"}}</span>
|<span class="item-name rollable">{{localize "TWODSIX.Actor.Items.Name"}}</span>
<span class="item-name centre">{{localize "TWODSIX.Actor.Items.TL"}}</span>
<span class="item-name centre">{{localize "TWODSIX.Actor.Items.Qty"}}</span>
<span class="item-name centre">{{localize "TWODSIX.Actor.Items.Effect"}}</span>
Expand All @@ -93,7 +94,7 @@
<ol class="ol-no-indent">
<li class="item flexrow" data-item-id="{{item._id}}">
<span class="items-augments">
<span class="item-name">{{item.name}}</span>
|<span class="item-name rollable">{{item.name}}</span>
<span class="item-name centre">{{data.techLevel}}</span>
<span class="item-name centre">{{data.quantity}}</span>
<span class="item-name centre">{{data.bonus}}</span>
Expand All @@ -113,7 +114,7 @@
<div><span class="pusher"></span>
<span class="item-title">{{localize "TWODSIX.Actor.Items.EQUIPMENT"}}</span>
<div class="items-equipment gear-header">
<span class="item-name">{{localize "TWODSIX.Actor.Items.Name"}}</span>
|<span class="item-name rollable">{{localize "TWODSIX.Actor.Items.Name"}}</span>
<span class="item-name centre">{{localize "TWODSIX.Actor.Items.TL"}}</span>
<span class="item-name centre">{{localize "TWODSIX.Actor.Items.Qty"}}</span>
<span class="item-name centre">{{localize "TWODSIX.Actor.Items.ShortDescr"}}</span>
Expand All @@ -127,7 +128,7 @@
<ol class="ol-no-indent">
<li class="item flexrow" data-item-id="{{item._id}}">
<span class="items-equipment items-tools items-junk">
<span class="item-name">{{item.name}}</span>
|<span class="item-name rollable">{{item.name}}</span>
<span class="item-name centre">{{data.techLevel}}</span>
<span class="item-name centre">{{data.quantity}}</span>
<span class="item-name centre">{{data.shortdescr}}</span>
Expand Down

0 comments on commit 951fcae

Please sign in to comment.