Skip to content

Commit

Permalink
Small tidy up. Fixed existing bug whereby we'd set bot options by nam…
Browse files Browse the repository at this point in the history
…e rather than playerId (resulting in the abiltiy to not uniquely set options for susequent bots).
  • Loading branch information
MarkDawson104 committed Sep 22, 2024
1 parent a7877be commit 959a9fc
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
5 changes: 2 additions & 3 deletions src/renderer/components/battle/BotParticipant.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import { Icon } from "@iconify/vue";
import robot from "@iconify-icons/mdi/robot";
import { MenuItem } from "primevue/menuitem";
import { Ref, ref } from "vue";
import { Ref, ref, toRaw } from "vue";
import LuaOptionsModal from "@/components/battle/LuaOptionsModal.vue";
import TeamParticipant from "@/components/battle/TeamParticipant.vue";
Expand Down Expand Up @@ -80,8 +80,7 @@ async function configureBot() {
}
function setBotOptions(options: Record<string, unknown>) {
console.log("Set bot options");
props.battle.setBotOptions(props.bot.name, options);
props.battle.setBotOptions(props.bot.playerId, options);
}
</script>

Expand Down
15 changes: 14 additions & 1 deletion src/renderer/model/battle/abstract-battle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,19 @@ export abstract class AbstractBattle<T extends BattleOptions = BattleOptions> {
});
}

// Returns a bot by it's PlayerId. Returns undefined if not found..
public getBotParticipantByPlayerId(playerId: number): User | Bot | undefined {
return this.participants.value.find((participant) => {
const isBot = !("userId" in participant);
if (isBot) {
if (participant.playerId === playerId) {
return true;
}
}
return false;
});
}

public open() {
this.watchStopHandles = [
watch(
Expand Down Expand Up @@ -137,7 +150,7 @@ export abstract class AbstractBattle<T extends BattleOptions = BattleOptions> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public abstract setGameOptions(options: Record<string, any>): void;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public abstract setBotOptions(botName: string, options: Record<string, any>): void;
public abstract setBotOptions(playerId: number, options: Record<string, any>): void;
public abstract addBot(bot: Bot): void;
public abstract removeBot(bot: Bot): void;
public abstract playerToSpectator(player: User): void;
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/model/battle/offline-battle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ export class OfflineBattle extends AbstractBattle {
this.fixIds();
}

public setBotOptions(botName: string, options: Record<string, unknown>) {
const bot = this.getParticipantByName(botName) as Bot;
public setBotOptions(playerId: number, options: Record<string, unknown>) {
const bot = this.getBotParticipantByPlayerId(playerId) as Bot;
bot.aiOptions = options;
}

Expand Down
2 changes: 1 addition & 1 deletion src/renderer/model/battle/spads-battle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ export class SpadsBattle extends AbstractBattle<SpadsBattleOptions> {
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
public setBotOptions(botName: string, options: Record<string, any>) {
public setBotOptions(playerId: number, options: Record<string, any>) {
console.warn("not implemented: setBotOptions");
// TODO
}
Expand Down

0 comments on commit 959a9fc

Please sign in to comment.