Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #264 #268

Merged
merged 5 commits into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 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 All @@ -43,6 +43,10 @@ const actions: MenuItem[] = [
label: "Configure",
command: configureBot,
},
{
label: "Duplicate",
command: duplicateBot,
},
{
label: "Kick",
command: kickBot,
Expand All @@ -59,6 +63,13 @@ function kickBot() {
props.battle.removeBot(props.bot);
}

// Duplicates this bot and its settings and gives it a new player id.
function duplicateBot() {
const duplicatedBot = structuredClone(toRaw(props.bot));
duplicatedBot.playerId = props.battle.contenders.value.length;
props.battle.addBot(duplicatedBot);
}

async function configureBot() {
const engineVersion = api.content.engine.installedVersions.find((version) => version.id === props.battle.battleOptions.engineVersion);
const ai = engineVersion?.ais.find((ai) => ai.name === props.bot.name);
Expand All @@ -69,7 +80,7 @@ async function configureBot() {
}

function setBotOptions(options: Record<string, unknown>) {
props.battle.setBotOptions(props.bot.name, options);
props.battle.setBotOptions(props.bot.playerId, options);
}
</script>

Expand Down
12 changes: 11 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,16 @@ export abstract class AbstractBattle<T extends BattleOptions = BattleOptions> {
});
}

// Returns a bot by it's PlayerId. Returns undefined if not found..
public getBotByPlayerId(playerId: number): Bot | undefined {
return this.bots.find((bot) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could have been just (bot) => bot.playerId === playerId but whatever.

if (bot.playerId === playerId) {
return true;
}
return false;
});
}

public open() {
this.watchStopHandles = [
watch(
Expand Down Expand Up @@ -137,7 +147,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.getBotByPlayerId(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
Loading