diff --git a/src/client/HostLobbyModal.ts b/src/client/HostLobbyModal.ts index d532ef3a4..6e12a4ad3 100644 --- a/src/client/HostLobbyModal.ts +++ b/src/client/HostLobbyModal.ts @@ -19,6 +19,7 @@ import "./components/Difficulties"; import { DifficultyDescription } from "./components/Difficulties"; import "./components/Maps"; import { JoinLobbyEvent } from "./Main"; +import { renderUnitTypeOptions } from "./utilities/RenderUnitTypeOptions"; @customElement("host-lobby-modal") export class HostLobbyModal extends LitElement { @@ -314,59 +315,10 @@ export class HostLobbyModal extends LitElement {
- ${[ - [UnitType.City, "unit_type.city"], - [UnitType.DefensePost, "unit_type.defense_post"], - [UnitType.Port, "unit_type.port"], - [UnitType.Warship, "unit_type.warship"], - [UnitType.MissileSilo, "unit_type.missile_silo"], - [UnitType.SAMLauncher, "unit_type.sam_launcher"], - [UnitType.AtomBomb, "unit_type.atom_bomb"], - [UnitType.HydrogenBomb, "unit_type.hydrogen_bomb"], - [UnitType.MIRV, "unit_type.mirv"], - ].map( - ([unitType, translationKey]: [UnitType, string]) => html` - - `, - )} + ${renderUnitTypeOptions({ + disabledUnits: this.disabledUnits, + toggleUnit: this.toggleUnit.bind(this), + })}
@@ -545,6 +497,15 @@ export class HostLobbyModal extends LitElement { return response; } + private toggleUnit(unit: UnitType, checked: boolean): void { + consolex.log(`Toggling unit type: ${unit} to ${checked}`); + this.disabledUnits = checked + ? [...this.disabledUnits, unit] + : this.disabledUnits.filter((u) => u !== unit); + + this.putGameConfig(); + } + private getRandomMap(): GameMapType { const maps = Object.values(GameMapType); const randIdx = Math.floor(Math.random() * maps.length); diff --git a/src/client/SinglePlayerModal.ts b/src/client/SinglePlayerModal.ts index 09a6d4334..509f564f6 100644 --- a/src/client/SinglePlayerModal.ts +++ b/src/client/SinglePlayerModal.ts @@ -21,6 +21,7 @@ import "./components/Maps"; import { FlagInput } from "./FlagInput"; import { JoinLobbyEvent } from "./Main"; import { UsernameInput } from "./UsernameInput"; +import { renderUnitTypeOptions } from "./utilities/RenderUnitTypeOptions"; @customElement("single-player-modal") export class SinglePlayerModal extends LitElement { @@ -39,7 +40,7 @@ export class SinglePlayerModal extends LitElement { @state() private gameMode: GameMode = GameMode.FFA; @state() private teamCount: number | typeof Duos = 2; - @state() private disabledUnits: string[] = []; + @state() private disabledUnits: UnitType[] = []; render() { return html` @@ -284,48 +285,10 @@ export class SinglePlayerModal extends LitElement {
- ${[ - [UnitType.City, "unit_type.city"], - [UnitType.DefensePost, "unit_type.defense_post"], - [UnitType.Port, "unit_type.port"], - [UnitType.Warship, "unit_type.warship"], - [UnitType.MissileSilo, "unit_type.missile_silo"], - [UnitType.SAMLauncher, "unit_type.sam_launcher"], - [UnitType.AtomBomb, "unit_type.atom_bomb"], - [UnitType.HydrogenBomb, "unit_type.hydrogen_bomb"], - [UnitType.MIRV, "unit_type.mirv"], - ].map( - ([unitType, translationKey]) => html` - - `, - )} + ${renderUnitTypeOptions({ + disabledUnits: this.disabledUnits, + toggleUnit: this.toggleUnit.bind(this), + })}
@@ -403,6 +366,13 @@ export class SinglePlayerModal extends LitElement { return maps[randIdx] as GameMapType; } + private toggleUnit(unit: UnitType, checked: boolean): void { + consolex.log(`Toggling unit type: ${unit} to ${checked}`); + this.disabledUnits = checked + ? [...this.disabledUnits, unit] + : this.disabledUnits.filter((u) => u !== unit); + } + private startGame() { // If random map is selected, choose a random map now if (this.useRandomMap) { diff --git a/src/client/utilities/RenderUnitTypeOptions.ts b/src/client/utilities/RenderUnitTypeOptions.ts new file mode 100644 index 000000000..c74aaf7ef --- /dev/null +++ b/src/client/utilities/RenderUnitTypeOptions.ts @@ -0,0 +1,48 @@ +// renderUnitTypeOptions.ts +import { html, TemplateResult } from "lit"; +import { UnitType } from "../../core/game/Game"; +import { translateText } from "../Utils"; + +export interface UnitTypeRenderContext { + disabledUnits: UnitType[]; + toggleUnit: (unit: UnitType, checked: boolean) => void; +} + +const unitOptions: { type: UnitType; translationKey: string }[] = [ + { type: UnitType.City, translationKey: "unit_type.city" }, + { type: UnitType.DefensePost, translationKey: "unit_type.defense_post" }, + { type: UnitType.Port, translationKey: "unit_type.port" }, + { type: UnitType.Warship, translationKey: "unit_type.warship" }, + { type: UnitType.MissileSilo, translationKey: "unit_type.missile_silo" }, + { type: UnitType.SAMLauncher, translationKey: "unit_type.sam_launcher" }, + { type: UnitType.AtomBomb, translationKey: "unit_type.atom_bomb" }, + { type: UnitType.HydrogenBomb, translationKey: "unit_type.hydrogen_bomb" }, + { type: UnitType.MIRV, translationKey: "unit_type.mirv" }, +]; + +export function renderUnitTypeOptions({ + disabledUnits, + toggleUnit, +}: UnitTypeRenderContext): TemplateResult[] { + return unitOptions.map( + ({ type, translationKey }) => html` + + `, + ); +}