From 5000a913485ce341e6f3aff1c6666160c2d1ccb7 Mon Sep 17 00:00:00 2001 From: flx-sta <50131232+flx-sta@users.noreply.github.com> Date: Tue, 13 Aug 2024 23:12:42 +0200 Subject: [PATCH] resolve some open strict-null bangs TODOs (#3530) --- src/battle-scene.ts | 6 +++--- src/battle.ts | 8 ++++---- src/data/ability.ts | 26 +++++++++++++------------- src/data/battle-anims.ts | 4 ++-- src/data/daily-run.ts | 2 +- src/inputs-controller.ts | 2 +- src/messages.ts | 3 ++- src/phases.ts | 10 +++++++--- 8 files changed, 33 insertions(+), 28 deletions(-) diff --git a/src/battle-scene.ts b/src/battle-scene.ts index 656af1391fc..b2e5635b642 100644 --- a/src/battle-scene.ts +++ b/src/battle-scene.ts @@ -944,7 +944,7 @@ export default class BattleScene extends SceneBase { } randBattleSeedInt(range: integer, min: integer = 0): integer { - return this.currentBattle.randSeedInt(this, range, min); + return this.currentBattle?.randSeedInt(this, range, min); } reset(clearScene: boolean = false, clearData: boolean = false, reloadI18n: boolean = false): void { @@ -2397,7 +2397,7 @@ export default class BattleScene extends SceneBase { } party.forEach((enemyPokemon: EnemyPokemon, i: integer) => { - const isBoss = enemyPokemon.isBoss() || (this.currentBattle.battleType === BattleType.TRAINER && this.currentBattle.trainer?.config.isBoss); + const isBoss = enemyPokemon.isBoss() || (this.currentBattle.battleType === BattleType.TRAINER && !!this.currentBattle.trainer?.config.isBoss); let upgradeChance = 32; if (isBoss) { upgradeChance /= 2; @@ -2405,7 +2405,7 @@ export default class BattleScene extends SceneBase { if (isFinalBoss) { upgradeChance /= 8; } - const modifierChance = this.gameMode.getEnemyModifierChance(isBoss!); // TODO: is this bang correct? + const modifierChance = this.gameMode.getEnemyModifierChance(isBoss); let pokemonModifierChance = modifierChance; if (this.currentBattle.battleType === BattleType.TRAINER && this.currentBattle.trainer) pokemonModifierChance = Math.ceil(pokemonModifierChance * this.currentBattle.trainer.getPartyMemberModifierChanceMultiplier(i)); // eslint-disable-line diff --git a/src/battle.ts b/src/battle.ts index 0b3b256f3df..a82f1a3db9b 100644 --- a/src/battle.ts +++ b/src/battle.ts @@ -74,14 +74,14 @@ export default class Battle { this.gameMode = gameMode; this.waveIndex = waveIndex; this.battleType = battleType; - this.trainer = trainer!; //TODO: is this bang correct? + this.trainer = trainer ?? null; this.initBattleSpec(); this.enemyLevels = battleType !== BattleType.TRAINER ? new Array(double ? 2 : 1).fill(null).map(() => this.getLevelForWave()) : trainer?.getPartyLevels(this.waveIndex); this.enemyParty = []; this.seenEnemyPartyMemberIds = new Set(); - this.double = double!; //TODO: is this bang correct? + this.double = !!double; this.enemySwitchCounter = 0; this.turn = 0; this.playerParticipantIds = new Set(); @@ -208,9 +208,9 @@ export default class Battle { return `encounter_${this.trainer?.getEncounterBgm()}`; } if (scene.musicPreference === 0) { - return this.trainer?.getBattleBgm()!; // TODO: is this bang correct? + return this.trainer?.getBattleBgm() ?? null; } else { - return this.trainer?.getMixedBattleBgm()!; // TODO: is this bang correct? + return this.trainer?.getMixedBattleBgm() ?? null; } } else if (this.gameMode.isClassic && this.waveIndex > 195 && this.battleSpec !== BattleSpec.FINAL_BOSS) { return "end_summit"; diff --git a/src/data/ability.ts b/src/data/ability.ts index 054e32ad0ff..1840254d618 100644 --- a/src/data/ability.ts +++ b/src/data/ability.ts @@ -325,7 +325,7 @@ export class TypeImmunityAbAttr extends PreDefendAbAttr { super(); this.immuneType = immuneType; - this.condition = condition!; // TODO: is this bang correct? + this.condition = condition ?? null; } /** @@ -1507,7 +1507,7 @@ export class BattleStatMultiplierAbAttr extends AbAttr { this.battleStat = battleStat; this.multiplier = multiplier; - this.condition = condition!; // TODO: is this bang correct? + this.condition = condition ?? null; } applyBattleStat(pokemon: Pokemon, passive: boolean, battleStat: BattleStat, statValue: Utils.NumberHolder, args: any[]): boolean | Promise { @@ -1560,7 +1560,7 @@ export class PostAttackStealHeldItemAbAttr extends PostAttackAbAttr { constructor(stealCondition?: PokemonAttackCondition) { super(); - this.stealCondition = stealCondition!; // TODO: is this bang correct? + this.stealCondition = stealCondition ?? null; } applyPostAttackAfterMoveTypeCheck(pokemon: Pokemon, passive: boolean, defender: Pokemon, move: Move, hitResult: HitResult, args: any[]): Promise { @@ -1649,7 +1649,7 @@ export class PostDefendStealHeldItemAbAttr extends PostDefendAbAttr { constructor(condition?: PokemonDefendCondition) { super(); - this.condition = condition!; // TODO: is this bang correct? + this.condition = condition ?? null; } applyPostDefend(pokemon: Pokemon, passive: boolean, attacker: Pokemon, move: Move, hitResult: HitResult, args: any[]): Promise { @@ -2400,11 +2400,11 @@ export class ProtectStatAbAttr extends PreStatChangeAbAttr { constructor(protectedStat?: BattleStat) { super(); - this.protectedStat = protectedStat!; // TODO: is this bang correct? + this.protectedStat = protectedStat ?? null; } applyPreStatChange(pokemon: Pokemon, passive: boolean, stat: BattleStat, cancelled: Utils.BooleanHolder, args: any[]): boolean { - if (this.protectedStat === undefined || stat === this.protectedStat) { + if (!this.protectedStat || stat === this.protectedStat) { cancelled.value = true; return true; } @@ -2746,7 +2746,7 @@ export class SuppressWeatherEffectAbAttr extends PreWeatherEffectAbAttr { constructor(affectsImmutable?: boolean) { super(); - this.affectsImmutable = affectsImmutable!; // TODO: is this bang correct? + this.affectsImmutable = !!affectsImmutable; } applyPreWeatherEffect(pokemon: Pokemon, passive: boolean, weather: Weather, cancelled: Utils.BooleanHolder, args: any[]): boolean { @@ -2810,11 +2810,11 @@ function getAnticipationCondition(): AbAttrCondition { return true; } // move is a OHKO - if (move!.getMove().hasAttr(OneHitKOAttr)) { // TODO: is this bang correct? + if (move?.getMove().hasAttr(OneHitKOAttr)) { return true; } // edge case for hidden power, type is computed - if (move!.getMove().id === Moves.HIDDEN_POWER) { // TODO: is this bang correct? + if (move?.getMove().id === Moves.HIDDEN_POWER) { const iv_val = Math.floor(((opponent.ivs[Stat.HP] & 1) +(opponent.ivs[Stat.ATK] & 1) * 2 +(opponent.ivs[Stat.DEF] & 1) * 4 @@ -2862,13 +2862,13 @@ export class ForewarnAbAttr extends PostSummonAbAttr { let movePower = 0; for (const opponent of pokemon.getOpponents()) { for (const move of opponent.moveset) { - if (move!.getMove() instanceof StatusMove) { // TODO: is this bang correct? + if (move?.getMove() instanceof StatusMove) { movePower = 1; - } else if (move!.getMove().hasAttr(OneHitKOAttr)) { // TODO: is this bang correct? + } else if (move?.getMove().hasAttr(OneHitKOAttr)) { movePower = 150; - } else if (move!.getMove().id === Moves.COUNTER || move!.getMove().id === Moves.MIRROR_COAT || move!.getMove().id === Moves.METAL_BURST) { // TODO: are those bangs correct? + } else if (move?.getMove().id === Moves.COUNTER || move?.getMove().id === Moves.MIRROR_COAT || move?.getMove().id === Moves.METAL_BURST) { movePower = 120; - } else if (move!.getMove().power === -1) { // TODO: is this bang correct? + } else if (move?.getMove().power === -1) { movePower = 80; } else { movePower = move!.getMove().power; // TODO: is this bang correct? diff --git a/src/data/battle-anims.ts b/src/data/battle-anims.ts index 634c39b7d66..c86f3db5085 100644 --- a/src/data/battle-anims.ts +++ b/src/data/battle-anims.ts @@ -683,8 +683,8 @@ export abstract class BattleAnim { private dstLine: number[]; constructor(user?: Pokemon, target?: Pokemon) { - this.user = user!; // TODO: is this bang correct? - this.target = target!; // TODO: is this bang correct? + this.user = user ?? null; + this.target = target ?? null; this.sprites = []; } diff --git a/src/data/daily-run.ts b/src/data/daily-run.ts index b7e03b4b189..b875877f99e 100644 --- a/src/data/daily-run.ts +++ b/src/data/daily-run.ts @@ -19,7 +19,7 @@ export function fetchDailyRunSeed(): Promise { return; } return response.text(); - }).then(seed => resolve(seed!)) // TODO: is this bang correct? + }).then(seed => resolve(seed ?? null)) .catch(err => reject(err)); }); } diff --git a/src/inputs-controller.ts b/src/inputs-controller.ts index 1af376ed6f4..7b0cb9cb050 100644 --- a/src/inputs-controller.ts +++ b/src/inputs-controller.ts @@ -343,7 +343,7 @@ export class InputsController { // Sometimes, gamepads are undefined. For some reason. this.gamepads = this.scene.input.gamepad?.gamepads.filter(function (el) { return el !== null; - })!; // TODO: is this bang correct? + }) ?? []; for (const [index, thisGamepad] of this.gamepads.entries()) { thisGamepad.index = index; // Overwrite the gamepad index, in case we had undefined gamepads earlier diff --git a/src/messages.ts b/src/messages.ts index b9bf94802f7..1cd6a5966b6 100644 --- a/src/messages.ts +++ b/src/messages.ts @@ -10,7 +10,8 @@ import i18next from "i18next"; export function getPokemonNameWithAffix(pokemon: Pokemon | undefined): string { if (!pokemon) { return "Missigno"; - } // TODO: little easter-egg, lol + } + switch (pokemon.scene.currentBattle.battleSpec) { case BattleSpec.DEFAULT: return !pokemon.isPlayer() diff --git a/src/phases.ts b/src/phases.ts index 1d1a5e2473a..46d5ef36e3c 100644 --- a/src/phases.ts +++ b/src/phases.ts @@ -195,7 +195,7 @@ export class TitlePhase extends Phase { this.scene.playBgm("title", true); - this.scene.gameData.getSession(loggedInUser!.lastSessionSlot).then(sessionData => { // TODO: is this bang correct? + this.scene.gameData.getSession(loggedInUser?.lastSessionSlot ?? -1).then(sessionData => { if (sessionData) { this.lastSessionData = sessionData; const biomeKey = getBiomeKey(sessionData.arena.biome); @@ -394,7 +394,11 @@ export class TitlePhase extends Phase { // If Online, calls seed fetch from db to generate daily run. If Offline, generates a daily run based on current date. if (!Utils.isLocal) { fetchDailyRunSeed().then(seed => { - generateDaily(seed!); // TODO: is this bang correct? + if (seed) { + generateDaily(seed); + } else { + throw new Error("Daily run seed is null!"); + } }).catch(err => { console.error("Failed to load daily run:\n", err); }); @@ -468,7 +472,7 @@ export class ReloadSessionPhase extends Phase { constructor(scene: BattleScene, systemDataStr?: string) { super(scene); - this.systemDataStr = systemDataStr!; // TODO: is this bang correct? + this.systemDataStr = systemDataStr ?? null; } start(): void {