From 7be77c4da64a7233bc89a6091bb040dac1c9652e Mon Sep 17 00:00:00 2001 From: MarkDawson104 Date: Sun, 22 Sep 2024 12:31:20 +0100 Subject: [PATCH 1/5] Fixes #264 With a watchout, a json duplication may not be the best way to do this with typescript as it assumes this object is a data model. --- src/renderer/components/battle/BotParticipant.vue | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/renderer/components/battle/BotParticipant.vue b/src/renderer/components/battle/BotParticipant.vue index 9abcea07..ec86e504 100644 --- a/src/renderer/components/battle/BotParticipant.vue +++ b/src/renderer/components/battle/BotParticipant.vue @@ -3,7 +3,7 @@
-
{{ bot.name }}
+
{{ bot.name }} - {{ botNameAffix }}
version.id === props.battle.battleOptions.engineVersion); const ai = engineVersion?.ais.find((ai) => ai.name === props.bot.name); From 068f2e71dd07affb9fc9571cbe186d39da3e08f0 Mon Sep 17 00:00:00 2001 From: MarkDawson104 Date: Sun, 22 Sep 2024 12:38:22 +0100 Subject: [PATCH 2/5] Debug code cleanuip --- src/renderer/components/battle/BotParticipant.vue | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/renderer/components/battle/BotParticipant.vue b/src/renderer/components/battle/BotParticipant.vue index ec86e504..7416a755 100644 --- a/src/renderer/components/battle/BotParticipant.vue +++ b/src/renderer/components/battle/BotParticipant.vue @@ -3,7 +3,7 @@
-
{{ bot.name }} - {{ botNameAffix }}
+
{{ bot.name }}
Date: Sun, 22 Sep 2024 17:30:56 +0100 Subject: [PATCH 3/5] Coding improvements as suggested by @p2004a, thanks! --- src/renderer/components/battle/BotParticipant.vue | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/renderer/components/battle/BotParticipant.vue b/src/renderer/components/battle/BotParticipant.vue index 7416a755..045a7e45 100644 --- a/src/renderer/components/battle/BotParticipant.vue +++ b/src/renderer/components/battle/BotParticipant.vue @@ -65,7 +65,7 @@ function kickBot() { // Duplicates this bot and its settings and gives it a new player id. function duplicateBot() { - let duplicatedBot = JSON.parse(JSON.stringify(props.bot)); + const duplicatedBot = structuredClone(toRaw(props.bot)); duplicatedBot.playerId = props.battle.contenders.value.length; props.battle.addBot(duplicatedBot); } @@ -80,6 +80,7 @@ async function configureBot() { } function setBotOptions(options: Record) { + console.log("Set bot options"); props.battle.setBotOptions(props.bot.name, options); } From 959a9fc4a3bd538f001135114960565e24f46582 Mon Sep 17 00:00:00 2001 From: MarkDawson104 Date: Sun, 22 Sep 2024 17:47:13 +0100 Subject: [PATCH 4/5] Small tidy up. Fixed existing bug whereby we'd set bot options by name rather than playerId (resulting in the abiltiy to not uniquely set options for susequent bots). --- src/renderer/components/battle/BotParticipant.vue | 5 ++--- src/renderer/model/battle/abstract-battle.ts | 15 ++++++++++++++- src/renderer/model/battle/offline-battle.ts | 4 ++-- src/renderer/model/battle/spads-battle.ts | 2 +- 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/renderer/components/battle/BotParticipant.vue b/src/renderer/components/battle/BotParticipant.vue index 045a7e45..3cf2ea95 100644 --- a/src/renderer/components/battle/BotParticipant.vue +++ b/src/renderer/components/battle/BotParticipant.vue @@ -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"; @@ -80,8 +80,7 @@ async function configureBot() { } function setBotOptions(options: Record) { - console.log("Set bot options"); - props.battle.setBotOptions(props.bot.name, options); + props.battle.setBotOptions(props.bot.playerId, options); } diff --git a/src/renderer/model/battle/abstract-battle.ts b/src/renderer/model/battle/abstract-battle.ts index ddeba0e1..13cc2ebc 100644 --- a/src/renderer/model/battle/abstract-battle.ts +++ b/src/renderer/model/battle/abstract-battle.ts @@ -90,6 +90,19 @@ export abstract class AbstractBattle { }); } + // 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( @@ -137,7 +150,7 @@ export abstract class AbstractBattle { // eslint-disable-next-line @typescript-eslint/no-explicit-any public abstract setGameOptions(options: Record): void; // eslint-disable-next-line @typescript-eslint/no-explicit-any - public abstract setBotOptions(botName: string, options: Record): void; + public abstract setBotOptions(playerId: number, options: Record): void; public abstract addBot(bot: Bot): void; public abstract removeBot(bot: Bot): void; public abstract playerToSpectator(player: User): void; diff --git a/src/renderer/model/battle/offline-battle.ts b/src/renderer/model/battle/offline-battle.ts index 5ca3fe23..9451d1b8 100644 --- a/src/renderer/model/battle/offline-battle.ts +++ b/src/renderer/model/battle/offline-battle.ts @@ -72,8 +72,8 @@ export class OfflineBattle extends AbstractBattle { this.fixIds(); } - public setBotOptions(botName: string, options: Record) { - const bot = this.getParticipantByName(botName) as Bot; + public setBotOptions(playerId: number, options: Record) { + const bot = this.getBotParticipantByPlayerId(playerId) as Bot; bot.aiOptions = options; } diff --git a/src/renderer/model/battle/spads-battle.ts b/src/renderer/model/battle/spads-battle.ts index d0d479dc..80f502cc 100644 --- a/src/renderer/model/battle/spads-battle.ts +++ b/src/renderer/model/battle/spads-battle.ts @@ -364,7 +364,7 @@ export class SpadsBattle extends AbstractBattle { } // eslint-disable-next-line @typescript-eslint/no-explicit-any - public setBotOptions(botName: string, options: Record) { + public setBotOptions(playerId: number, options: Record) { console.warn("not implemented: setBotOptions"); // TODO } From 7bcc776ec4889c0f69de16a3a48d99e79e102fb7 Mon Sep 17 00:00:00 2001 From: MarkDawson104 Date: Mon, 23 Sep 2024 18:11:19 +0100 Subject: [PATCH 5/5] Changed getbotParticipantByPlayerId to instead be just bots as suggest by @Jazcash. --- src/renderer/model/battle/abstract-battle.ts | 11 ++++------- src/renderer/model/battle/offline-battle.ts | 2 +- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/renderer/model/battle/abstract-battle.ts b/src/renderer/model/battle/abstract-battle.ts index 13cc2ebc..664142da 100644 --- a/src/renderer/model/battle/abstract-battle.ts +++ b/src/renderer/model/battle/abstract-battle.ts @@ -91,13 +91,10 @@ export abstract class AbstractBattle { } // 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; - } + public getBotByPlayerId(playerId: number): Bot | undefined { + return this.bots.find((bot) => { + if (bot.playerId === playerId) { + return true; } return false; }); diff --git a/src/renderer/model/battle/offline-battle.ts b/src/renderer/model/battle/offline-battle.ts index 9451d1b8..55f847b6 100644 --- a/src/renderer/model/battle/offline-battle.ts +++ b/src/renderer/model/battle/offline-battle.ts @@ -73,7 +73,7 @@ export class OfflineBattle extends AbstractBattle { } public setBotOptions(playerId: number, options: Record) { - const bot = this.getBotParticipantByPlayerId(playerId) as Bot; + const bot = this.getBotByPlayerId(playerId) as Bot; bot.aiOptions = options; }