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 // SPDX-License-Identifier: MIT
import { packageId } from '../../constants'; import { packageId } from '../../constants';
import { getGame } from '../../helpers';
import type { TickwerkCombatant } from './combatant'; import type { TickwerkCombatant } from './combatant';
@ -46,28 +45,7 @@ const CombatMixin = (BaseCombat: typeof Combat) => {
} }
override async nextTurn() { override async nextTurn() {
const game = getGame(); await this.combatant?.advanceTicksDialog();
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);
}
return this; 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 type { CombatantDataConstructorData } from '@league-of-foundry-developers/foundry-vtt-types/src/foundry/common/data/data.mjs/combatantData';
import { packageId } from '../../constants'; import { packageId } from '../../constants';
import { getGame } from '../../helpers';
export const registerCombatantFunctionality = () => { export const registerCombatantFunctionality = () => {
CONFIG.Combatant.documentClass = CombatantMixin(CONFIG.Combatant.documentClass); 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. * Update tiebreaker data for a given creation or update.
* @param data The data of the creation / update * @param data The data of the creation / update
@ -91,6 +111,15 @@ const CombatantMixin = (BaseCombatant: typeof Combatant) => {
return getTiebreaker(this, combatants); 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 { protected override _getInitiativeFormula(): string {
const getInitiativeFormula = CONFIG.tickwerk?.getInitiativeFormula; const getInitiativeFormula = CONFIG.tickwerk?.getInitiativeFormula;
if (getInitiativeFormula) return getInitiativeFormula(this); if (getInitiativeFormula) return getInitiativeFormula(this);

View file

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