refactor: move advance ticks dialog into combatant

This commit is contained in:
Johannes Loher 2022-05-19 00:28:55 +02:00
parent 2e9c56cc2b
commit 9a9bff08c0
3 changed files with 36 additions and 31 deletions

View file

@ -3,7 +3,6 @@
// SPDX-License-Identifier: MIT
import { packageId } from '../../constants';
import { getGame } from '../../helpers';
import type { TickwerkCombatant } from './combatant';
@ -46,28 +45,7 @@ const CombatMixin = (BaseCombat: typeof Combat) => {
}
override async nextTurn() {
const game = getGame();
const combatant = this.combatant;
if (combatant === undefined || combatant.id === null) {
return this;
}
const ticks = await Dialog.prompt({
title: game.i18n.localize('TICKWERK.AdvanceTicks'),
content: '<input name="ticks" type="number" value="5" min="0" />',
label: game.i18n.localize('TICKWERK.AdvanceTicks'),
callback: (html) => {
const ticks = html[0]?.querySelector<HTMLInputElement>('input[name="ticks"]')?.value;
return ticks !== undefined ? parseInt(ticks) : undefined;
},
rejectClose: false,
});
if (ticks !== undefined && ticks !== null) {
await combatant.advanceTicks(ticks);
}
await this.combatant?.advanceTicksDialog();
return this;
}

View file

@ -4,6 +4,7 @@
import type { CombatantDataConstructorData } from '@league-of-foundry-developers/foundry-vtt-types/src/foundry/common/data/data.mjs/combatantData';
import { packageId } from '../../constants';
import { getGame } from '../../helpers';
export const registerCombatantFunctionality = () => {
CONFIG.Combatant.documentClass = CombatantMixin(CONFIG.Combatant.documentClass);
@ -62,6 +63,25 @@ const CombatantMixin = (BaseCombatant: typeof Combatant) => {
}
}
async advanceTicksDialog(): Promise<void> {
const game = getGame();
const ticks = await Dialog.prompt({
title: game.i18n.localize('TICKWERK.AdvanceTicks'),
content: '<input name="ticks" type="number" value="5" min="0" />',
label: game.i18n.localize('TICKWERK.AdvanceTicks'),
callback: (html) => {
const ticks = html[0]?.querySelector<HTMLInputElement>('input[name="ticks"]')?.value;
return ticks !== undefined ? parseInt(ticks) : undefined;
},
rejectClose: false,
});
if (ticks !== undefined && ticks !== null) {
await this.advanceTicks(ticks);
}
}
/**
* Update tiebreaker data for a given creation or update.
* @param data The data of the creation / update
@ -91,6 +111,15 @@ const CombatantMixin = (BaseCombatant: typeof Combatant) => {
return getTiebreaker(this, combatants);
}
override testUserPermission(
user: foundry.documents.BaseUser,
permission: keyof typeof foundry.CONST.DOCUMENT_PERMISSION_LEVELS | foundry.CONST.DOCUMENT_PERMISSION_LEVELS,
{ exact }: { exact?: boolean | undefined } = {},
): boolean {
if (user.isGM) return true;
return super.testUserPermission(user, permission, { exact });
}
protected override _getInitiativeFormula(): string {
const getInitiativeFormula = CONFIG.tickwerk?.getInitiativeFormula;
if (getInitiativeFormula) return getInitiativeFormula(this);

View file

@ -22,15 +22,9 @@ const getTiebreaker = async (combatant: TickwerkCombatant, combatants: TickwerkC
for (const combatant of ds4combatants) {
const tiebreaker = combatant._newTiebreaker ?? combatant.getFlag(packageId, 'tiebreaker') ?? 0;
if (
(combatant.actor?.data.data.combatValues.initiative.total ?? -Infinity) >
(ds4combatant.actor?.data.data.combatValues.initiative.total ?? -Infinity)
) {
if (getInitiative(combatant) > getInitiative(ds4combatant)) {
lowerBounds.push(tiebreaker);
} else if (
(combatant.actor?.data.data.combatValues.initiative.total ?? -Infinity) <
(ds4combatant.actor?.data.data.combatValues.initiative.total ?? -Infinity)
) {
} else if (getInitiative(combatant) < getInitiative(ds4combatant)) {
upperBounds.push(tiebreaker);
} else {
equals.push(tiebreaker);
@ -74,6 +68,10 @@ const getInitiativeFormula = (combatant: TickwerkCombatant) => {
type DS4TickwerkCombatant = TickwerkCombatant & { actor: (Actor & { data: { data: ActorData } }) | null };
const getInitiative = (combatant: DS4TickwerkCombatant): number => {
return combatant.actor?.data.data.combatValues.initiative.total ?? -Infinity;
};
interface ActorData {
combatValues: {
initiative: {