Skip to content

Commit

Permalink
closes #87
Browse files Browse the repository at this point in the history
  • Loading branch information
Muttley committed May 31, 2024
1 parent 5b7c8ee commit a50fc2b
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 87 deletions.
3 changes: 2 additions & 1 deletion scss/components/_momentum-app.scss
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@

input[type="number"] {
margin: 0 5px;
font-size: 16px;
font-size: 20px;
text-align: center;
color: #494e31;
}

.ap-btn {
Expand Down
91 changes: 47 additions & 44 deletions system/src/apps/MomentumTracker.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,45 @@ export default class MomentumTracker extends Application {
if (MomentumTracker._instance) {
throw new Error("MomentumTracker already has an instance!!!");
}

super(options);

MomentumTracker._instance = this;
MomentumTracker.closed = true;
this.data = {};
}

// override
static get defaultOptions() {
return foundry.utils.mergeObject(super.defaultOptions, {
title: "AP Tracker",
template: "systems/ac2d20/templates/ap/momentum-tracker.hbs",
classes: ["ac2d20", "momentum-tracker"],
height: "200",
id: "momentum-tracker-app",
popOut: false,
resizable: false,
template: "systems/ac2d20/templates/app/momentum-tracker.hbs",
title: "AP Tracker",
width: "auto",
height: "200",
});
}

// override
getData() {
super.getData();
this.data.isGM = game.user.isGM;
this.data.partyMomentum = game.settings.get("ac2d20", "partyMomentum");
this.data.gmMomentum = game.settings.get("ac2d20", "gmMomentum");
this.data.maxMomentum = game.settings.get("ac2d20", "maxMomentum");
if (game.user.isGM) this.data.showGMMomentumToPlayers = true;
else this.data.showGMMomentumToPlayers = game.settings.get("ac2d20", "gmMomentumShowToPlayers");
if (game.user.isGM) this.data.maxAppShowToPlayers = true;
else this.data.maxAppShowToPlayers = game.settings.get("ac2d20", "maxAppShowToPlayers");
return this.data;
const data = {
gmMomentum: game.settings.get(SYSTEM_ID, "gmMomentum"),
isGM: game.user.isGM,
maxMomentum: game.settings.get(SYSTEM_ID, "maxMomentum"),
partyMomentum: game.settings.get(SYSTEM_ID, "partyMomentum"),
};

data.showGMMomentum = game.user.isGM
? true
: game.settings.get(SYSTEM_ID, "gmMomentumShowToPlayers");

data.showMaxApp = game.user.isGM
? true
: game.settings.get(SYSTEM_ID, "maxAppShowToPlayers");

return data;
}

static renderApTracker() {
Expand All @@ -51,19 +58,18 @@ export default class MomentumTracker extends Application {
html.find(".ap-input").change(ev => {
const type = $(ev.currentTarget).parents(".ap-resource").attr("data-type");
const value = ev.target.value;

MomentumTracker.setAP(type, value);
});

html.find(".ap-add, .ap-sub").click(ev => {
const type = $(ev.currentTarget).parents(".ap-resource").attr("data-type");
const change = $(ev.currentTarget).hasClass("ap-add") ? 1 : -1;
let currentValue = game.settings.get("ac2d20", type);
let maxMomentum = game.settings.get("ac2d20", "maxMomentum");
if (parseInt(currentValue) < maxMomentum || parseInt(currentValue) > 0) {
let newValue = parseInt(currentValue) + change;
MomentumTracker.setAP(type, newValue);
}

const currentValue = game.settings.get(SYSTEM_ID, type);
const newValue = parseInt(currentValue) + change;

MomentumTracker.setAP(type, newValue);
});

html.find(".toggle-maxAp").click(ev => {
Expand All @@ -74,8 +80,6 @@ export default class MomentumTracker extends Application {
}

static async adjustAP(type, diff) {
diff = Math.round(diff);

if (!game.user.isGM) {
game.socket.emit("system.ac2d20", {
operation: "adjustAP",
Expand All @@ -84,14 +88,16 @@ export default class MomentumTracker extends Application {
return;
}

let momentum = game.settings.get("ac2d20", type);
diff = Math.round(diff);

let momentum = game.settings.get(SYSTEM_ID, type);
momentum += diff;

this.setAP(type, momentum);
}


static async setAP(type, value) {
value = Math.round(value);
if (!game.user.isGM) {
game.socket.emit("system.ac2d20", {
operation: "setAP",
Expand All @@ -100,29 +106,26 @@ export default class MomentumTracker extends Application {
return;
}

let maxMomentum = game.settings.get("ac2d20", "maxMomentum");
let partyMomentum = game.settings.get("ac2d20", "partyMomentum");
if (partyMomentum > value && type === "maxMomentum") {
await game.settings.set("ac2d20", "maxMomentum", value);
await game.settings.set("ac2d20", "partyMomentum", value);
MomentumTracker.renderApTracker();
game.socket.emit("system.ac2d20", { operation: "updateAP" });
return;
}
value = Math.round(value);
value = Math.max(0, value);

if (value > maxMomentum && type === "partyMomentum") {
await game.settings.set("ac2d20", type, maxMomentum);
MomentumTracker.renderApTracker();
}
else if (value < 0) {
await game.settings.set("ac2d20", type, 0);
MomentumTracker.renderApTracker();
}
else {
await game.settings.set("ac2d20", type, value);
MomentumTracker.renderApTracker();
const maxMomentum = await game.settings.get(SYSTEM_ID, "maxMomentum");

if (type === "partyMomentum") value = Math.min(value, maxMomentum);

if (type === "maxMomentum") {
const currentPartyMomentum =
await game.settings.get(SYSTEM_ID, "partyMomentum");

const newPartyMomentum = Math.min(value, currentPartyMomentum);

await game.settings.set(SYSTEM_ID, "partyMomentum", newPartyMomentum);
}

await game.settings.set(SYSTEM_ID, type, value);

MomentumTracker.renderApTracker();

// emit socket event for the players to update
game.socket.emit("system.ac2d20", { operation: "updateAP" });
}
Expand Down
42 changes: 0 additions & 42 deletions system/templates/ap/momentum-tracker.hbs

This file was deleted.

97 changes: 97 additions & 0 deletions system/templates/app/momentum-tracker.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<div class="ac2d20 momentum-tracker">
{{#if showGMMomentum}}
<div class="ap-resource gm" data-type="gmMomentum">
<div class="flexcol">
<h4>
{{localize "AC2D20.TEMPLATES.GM_MOMENTUM"}}
</h4>
</div>
<div class="flexrow align-mid">
{{#if game.user.isGM}}
<a class="ap-btn ap-sub">
<i class="fa-solid fa-minus"></i>
</a>
{{else}}
<a class="ap-btn">
<i class="fa-solid fa-minus"></i>
</a>
{{/if}}

<input
class="ap-input num-short-3"
type="number"
value={{gmMomentum}}
{{#unless isGM}}
disabled
{{/unless}}
>

{{#if isGM}}
<a class="ap-btn ap-add">
<i class="fa-solid fa-plus"></i>
</a>
{{else}}
<a class="ap-btn">
<i class="fa-solid fa-plus"></i>
</a>
{{/if}}
</div>
</div>
{{/if}}

{{#if showMaxApp}}
<div class="ap-resource maxMomentum-box" data-type="maxMomentum">
<div class="flexcol">
<h4>
{{localize "AC2D20.TEMPLATES.MAX_PARTY_MOMENTUM"}}
</h4>
</div>
<div class="flexrow align-mid">
<a class="ap-btn ap-sub">
<i class="fa-solid fa-minus"></i>
</a>

<input
class="ap-input num-short-3"
type="number"
value="{{maxMomentum}}"
>

<a class="ap-btn ap-add">
<i class="fa-solid fa-plus"></i>
</a>
</div>
</div>
{{/if}}

<div class="ap-resource party" data-type="partyMomentum">
{{#if showMaxApp}}
<a class='toggle-maxAp'>
<i class="fas fa-cog"></i>
</a>
{{/if}}

<div class="flexcol">
<h4>
{{localize "AC2D20.TEMPLATES.PARTY_MOMENTUM"}}
</h4>
</div>

<div class="flexrow align-mid">
<a class="ap-btn ap-sub">
<i class="fa-solid fa-minus"></i>
</a>

<input
class="ap-input num-short-3"
type="number"
value="{{partyMomentum}}"
>

<a class="ap-btn ap-add">
<i class="fa-solid fa-plus"></i>
</a>
</div>
</div>

</div>

0 comments on commit a50fc2b

Please sign in to comment.