Skip to content

Commit

Permalink
Merge pull request FreezingMoon#2398 from allmtz/master
Browse files Browse the repository at this point in the history
adding types to ability.ts FreezingMoon#1969
  • Loading branch information
DreadKnight authored Jul 6, 2023
2 parents 4f2bc71 + 3b1b6a0 commit 3c5cb88
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 29 deletions.
57 changes: 32 additions & 25 deletions src/ability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ import { Creature, CreatureMasteries } from './creature';
import { isTeam, Team } from './utility/team';
import * as arrayUtils from './utility/arrayUtils';
import Game from './game';
import { ScoreEvent } from './player';

/*
* NOTE
*
* convert game.js -> game.ts to get rid of @ts-expect-error 2339
*
* for @ts-expect-error 2554: might have to adjust faceHex type definition in creature.ts
*
*/

/**
Expand All @@ -21,18 +20,30 @@ import Game from './game';
* Class parsing function from creature abilities
*/

export type AbilitySlot = 0 | 1 | 2 | 3;

export type AbilityTrigger = 'onQuery' | 'onStartPhase' | 'onDamage' | 'onEndPhase';

// Could get rid of the union and optionals by creating a separate (or conditional) type for Dark Priest's Cost
// This might narrow down the types in the constructor by checking `creature.name`
type Cost = {
plasma?: string | number;
special: string;
plasma?: string | number;
energy?: number;
};

type AbilitySlot = 0 | 1 | 2 | 3;
type Requirement = { plasma: number; energy?: number } | Cost;

type Target = { hexesHit: number; target: Creature };

type AbilityEffect = {
special?: string;
offense?: number;
defense?: number;
regrowth?: number;
frost?: number;
};

export class Ability {
creature: Creature;
game: Game;
Expand All @@ -46,17 +57,17 @@ export class Ability {
title: string;

// TODO properly type all these unknowns
requirements?: unknown;
costs?: Cost;
trigger?: string;
triggerFunc?: () => string;
requirements?: Requirement | undefined;
costs?: Cost | undefined;
trigger?: AbilityTrigger;
triggerFunc?: () => AbilityTrigger;
require?: () => boolean;
query?: (...args: unknown[]) => unknown;
query?: () => unknown;
affectedByMatSickness?: boolean;
activate?: (...args: unknown[]) => unknown;
getAnimationData?: (...args: unknown[]) => unknown;
damages?: CreatureMasteries & { pure?: number | string };
effects?: any[];
effects?: AbilityEffect[];
message?: string;

_disableCooldowns: boolean;
Expand Down Expand Up @@ -86,7 +97,7 @@ export class Ability {
this.game.signals.metaPowers.add(this.handleMetaPowerEvent, this);
}

handleMetaPowerEvent(message, payload) {
handleMetaPowerEvent(message: string, payload: boolean) {
if (message === 'toggleResetCooldowns') {
// Prevent ability from going on cooldown.
this._disableCooldowns = payload;
Expand Down Expand Up @@ -192,7 +203,7 @@ export class Ability {
* End the ability. Must be called at the end of each ability function;
*
*/
end(disableLogMsg, deferredEnding) {
end(disableLogMsg: boolean, deferredEnding: boolean) {
const game = this.game;

if (!disableLogMsg) {
Expand Down Expand Up @@ -253,21 +264,20 @@ export class Ability {
game.log(this.title + ' has been upgraded');
// Upgrade bonus uniqueness managed by preventing multiple bonuses
// with the same ability ID (which is an index 0,1,2,3 into the creature's abilities) and the creature ID
const bonus = {
const bonus: ScoreEvent = {
type: 'upgrade',
ability: this.id,
creature: this.creature.id,
creature: this.creature,
};

const find = (scorePart) =>
scorePart.type === bonus.type &&
scorePart.ability === bonus.ability &&
scorePart.creature === bonus.creature;
const matchingBonus = (score: ScoreEvent) =>
score.type === bonus.type &&
score.ability === bonus.ability &&
score.creature.id === bonus.creature.id;

// Only add the bonus when it has not already been awarded
if (!this.creature.player.score.find(find)) {
// TODO: adjust ScoreEvent to include this bonus type
this.creature.player.score.push(bonus as any);
if (!this.creature.player.score.find(matchingBonus)) {
this.creature.player.score.push(bonus);
}
}
}
Expand Down Expand Up @@ -406,13 +416,10 @@ export class Ability {
// Force creatures to face towards their target
if (args[0]) {
if (args[0] instanceof Creature) {
// TODO: adjust type definition of `faceHex`
// @ts-expect-error 2554
this.creature.faceHex(args[0]);
} else if (args[0] instanceof Array) {
for (const argument of args[0]) {
if (argument instanceof Creature || argument.creature) {
// @ts-expect-error 2554
this.creature.faceHex(argument);
}
}
Expand Down Expand Up @@ -485,7 +492,7 @@ export class Ability {
*/

getTargets(hexes: Hex[]): Target[] {
const targets = {};
const targets: Record<number, Target> = {};
const targetsList: Target[] = [];

hexes.forEach((item) => {
Expand Down
6 changes: 3 additions & 3 deletions src/creature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -841,9 +841,9 @@ export class Creature {
*/
faceHex(
faceto: Hex | Creature,
facefrom: Hex | Creature,
ignoreCreaHex: boolean,
attackFix: boolean,
facefrom?: Hex | Creature,
ignoreCreaHex?: boolean,
attackFix?: boolean,
) {
if (!facefrom) {
facefrom = this.player.flipped ? this.hexagons[this.size - 1] : this.hexagons[0];
Expand Down
8 changes: 7 additions & 1 deletion src/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as $j from 'jquery';
import { getUrl } from './assetLoader';
import { Creature } from './creature';
import Game from './game';
import { AbilitySlot } from './ability';

/**
* Player Class
Expand Down Expand Up @@ -34,7 +35,12 @@ type PlayerName = `Player${1 | 2 | 3 | 4}`;

type PlayerColor = 'red' | 'blue' | 'orange' | 'green';

type ScoreEvent = { type: ScoreType; creature?: Creature; kills?: number };
export type ScoreEvent = {
type: ScoreType;
creature?: Creature;
kills?: number;
ability?: AbilitySlot;
};

type TotalScore = Record<ScoreType, number> & { total: number };

Expand Down

0 comments on commit 3c5cb88

Please sign in to comment.