pokerogue/src/system/arena-data.ts
Sirz Benjie 069e8a47d6
[Bug][Refactor] Fix loading arena tags (#6110)
* Improve type safety; add missing loadTag overrides to wish and neutralizing gas

* More automatic type safety for arena tags

* Fixup wording of lead comment in arena-tag.ts

* Apply kev's suggestions from code review

Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com>

* Allow abstract constructors for arena methods

* Address dean's comments from code review

* Add missing newline after interface definition

Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com>

* Apply suggestions from code review

Co-authored-by: Bertie690 <136088738+Bertie690@users.noreply.github.com>

* Apply suggestions from code review

Co-authored-by: Bertie690 <136088738+Bertie690@users.noreply.github.com>

* Format with biome

* Update tsdoc of ConditionalProtectTag

Co-authored-by: Bertie690 <136088738+Bertie690@users.noreply.github.com>

* Apply kev's suggestions from code review

Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com>

---------

Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com>
Co-authored-by: Bertie690 <136088738+Bertie690@users.noreply.github.com>
2025-07-22 11:26:27 -07:00

48 lines
1.7 KiB
TypeScript

import type { ArenaTag } from "#data/arena-tag";
import { loadArenaTag, SerializableArenaTag } from "#data/arena-tag";
import { Terrain } from "#data/terrain";
import { Weather } from "#data/weather";
import type { BiomeId } from "#enums/biome-id";
import { Arena } from "#field/arena";
import type { ArenaTagTypeData } from "#types/arena-tags";
import type { NonFunctionProperties } from "#types/type-helpers";
export interface SerializedArenaData {
biome: BiomeId;
weather: NonFunctionProperties<Weather> | null;
terrain: NonFunctionProperties<Terrain> | null;
tags?: ArenaTagTypeData[];
playerTerasUsed?: number;
}
export class ArenaData {
public biome: BiomeId;
public weather: Weather | null;
public terrain: Terrain | null;
public tags: ArenaTag[];
public playerTerasUsed: number;
constructor(source: Arena | SerializedArenaData) {
// Exclude any unserializable tags from the serialized data (such as ones only lasting 1 turn).
// NOTE: The filter has to be done _after_ map, data loaded from `ArenaTagTypeData`
// is not yet an instance of `ArenaTag`
this.tags =
source.tags
?.map((t: ArenaTag | ArenaTagTypeData) => loadArenaTag(t))
?.filter((tag): tag is SerializableArenaTag => tag instanceof SerializableArenaTag) ?? [];
this.playerTerasUsed = source.playerTerasUsed ?? 0;
if (source instanceof Arena) {
this.biome = source.biomeType;
this.weather = source.weather;
this.terrain = source.terrain;
return;
}
this.biome = source.biome;
this.weather = source.weather ? new Weather(source.weather.weatherType, source.weather.turnsLeft) : null;
this.terrain = source.terrain ? new Terrain(source.terrain.terrainType, source.terrain.turnsLeft) : null;
}
}