Added random map option to SinglePlayerModal and HostLobbyModal

This commit is contained in:
Todd
2025-03-02 02:46:48 -05:00
parent 389e905813
commit 5763abd30f
3 changed files with 90 additions and 8 deletions
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 MiB

+35 -3
View File
@@ -6,6 +6,7 @@ import { consolex } from "../core/Consolex";
import "./components/Difficulties"; import "./components/Difficulties";
import { DifficultyDescription } from "./components/Difficulties"; import { DifficultyDescription } from "./components/Difficulties";
import "./components/Maps"; import "./components/Maps";
import randomMap from "../../resources/images/RandomMap.png";
@customElement("host-lobby-modal") @customElement("host-lobby-modal")
export class HostLobbyModal extends LitElement { export class HostLobbyModal extends LitElement {
@@ -20,6 +21,7 @@ export class HostLobbyModal extends LitElement {
@state() private lobbyId = ""; @state() private lobbyId = "";
@state() private copySuccess = false; @state() private copySuccess = false;
@state() private players: string[] = []; @state() private players: string[] = [];
@state() private useRandomMap: boolean = false;
private playersInterval = null; private playersInterval = null;
@@ -365,11 +367,27 @@ export class HostLobbyModal extends LitElement {
<div @click=${() => this.handleMapSelection(value)}> <div @click=${() => this.handleMapSelection(value)}>
<map-display <map-display
.mapKey=${key} .mapKey=${key}
.selected=${this.selectedMap === value} .selected=${!this.useRandomMap &&
this.selectedMap === value}
></map-display> ></map-display>
</div> </div>
`, `,
)} )}
<div
class="option-card random-map ${this.useRandomMap
? "selected"
: ""}"
@click=${this.handleRandomMapToggle}
>
<div class="option-image">
<img
src=${randomMap}
alt="Random Map"
style="width:100%; aspect-ratio: 4/2; object-fit:cover; border-radius:8px;"
/>
</div>
<div class="option-card-title">Random</div>
</div>
</div> </div>
</div> </div>
@@ -544,9 +562,13 @@ export class HostLobbyModal extends LitElement {
this.playersInterval = null; this.playersInterval = null;
} }
} }
private async handleRandomMapToggle() {
this.useRandomMap = true;
this.putGameConfig();
}
private async handleMapSelection(value: GameMapType) { private async handleMapSelection(value: GameMapType) {
this.selectedMap = value; this.selectedMap = value;
this.useRandomMap = false;
this.putGameConfig(); this.putGameConfig();
} }
private async handleDifficultySelection(value: Difficulty) { private async handleDifficultySelection(value: Difficulty) {
@@ -599,9 +621,19 @@ export class HostLobbyModal extends LitElement {
}); });
} }
private getRandomMap(): GameMapType {
const maps = Object.values(GameMapType);
const randIdx = Math.floor(Math.random() * maps.length);
return maps[randIdx] as GameMapType;
}
private async startGame() { private async startGame() {
this.selectedMap = this.useRandomMap
? this.getRandomMap()
: this.selectedMap;
await this.putGameConfig();
consolex.log( consolex.log(
`Starting private game with map: ${GameMapType[this.selectedMap]}`, `Starting single player game with map: ${GameMapType[this.selectedMap]} ${this.useRandomMap ? " (Randomly selected)" : ""}`,
); );
this.close(); this.close();
const response = await fetch(`/start_private_lobby/${this.lobbyId}`, { const response = await fetch(`/start_private_lobby/${this.lobbyId}`, {
+55 -5
View File
@@ -6,6 +6,7 @@ import { consolex } from "../core/Consolex";
import "./components/Difficulties"; import "./components/Difficulties";
import { DifficultyDescription } from "./components/Difficulties"; import { DifficultyDescription } from "./components/Difficulties";
import "./components/Maps"; import "./components/Maps";
import randomMap from "../../resources/images/RandomMap.png";
@customElement("single-player-modal") @customElement("single-player-modal")
export class SinglePlayerModal extends LitElement { export class SinglePlayerModal extends LitElement {
@@ -17,6 +18,7 @@ export class SinglePlayerModal extends LitElement {
@state() private infiniteGold: boolean = false; @state() private infiniteGold: boolean = false;
@state() private infiniteTroops: boolean = false; @state() private infiniteTroops: boolean = false;
@state() private instantBuild: boolean = false; @state() private instantBuild: boolean = false;
@state() private useRandomMap: boolean = false;
static styles = css` static styles = css`
.modal-overlay { .modal-overlay {
@@ -230,6 +232,15 @@ export class SinglePlayerModal extends LitElement {
width: 80%; width: 80%;
} }
.random-map {
border: 2px solid rgba(255, 255, 255, 0.1);
background: rgba(30, 30, 30, 0.95);
}
.random-map.selected {
border: 2px solid '@4a9eff'
background: 'rgba(74, 158, 255, 0.1)'
}
@media screen and (max-width: 768px) { @media screen and (max-width: 768px) {
.modal-content { .modal-content {
max-height: calc(90vh - 42px); max-height: calc(90vh - 42px);
@@ -259,14 +270,34 @@ export class SinglePlayerModal extends LitElement {
.filter(([key]) => isNaN(Number(key))) .filter(([key]) => isNaN(Number(key)))
.map( .map(
([key, value]) => html` ([key, value]) => html`
<div @click=${() => this.handleMapSelection(value)}> <div
@click=${function () {
this.handleMapSelection(value);
}}
>
<map-display <map-display
.mapKey=${key} .mapKey=${key}
.selected=${this.selectedMap === value} .selected=${!this.useRandomMap &&
this.selectedMap === value}
></map-display> ></map-display>
</div> </div>
`, `,
)} )}
<div
class="option-card random-map ${this.useRandomMap
? "selected"
: ""}"
@click=${this.handleRandomMapToggle}
>
<div class="option-image">
<img
src=${randomMap}
alt="Random Map"
style="width:100%; aspect-ratio: 4/2; object-fit:cover; border-radius:8px;"
/>
</div>
<div class="option-card-title">Random</div>
</div>
</div> </div>
</div> </div>
@@ -329,7 +360,6 @@ export class SinglePlayerModal extends LitElement {
/> />
<div class="option-card-title">Disable Nations</div> <div class="option-card-title">Disable Nations</div>
</label> </label>
<label <label
for="instant-build" for="instant-build"
class="option-card ${this.instantBuild ? "selected" : ""}" class="option-card ${this.instantBuild ? "selected" : ""}"
@@ -385,14 +415,21 @@ export class SinglePlayerModal extends LitElement {
public open() { public open() {
this.isModalOpen = true; this.isModalOpen = true;
this.useRandomMap = false;
} }
public close() { public close() {
this.isModalOpen = false; this.isModalOpen = false;
} }
private handleRandomMapToggle() {
this.useRandomMap = true;
}
private handleMapSelection(value: GameMapType) { private handleMapSelection(value: GameMapType) {
this.selectedMap = value; this.selectedMap = value;
this.useRandomMap = false;
} }
private handleDifficultySelection(value: Difficulty) { private handleDifficultySelection(value: Difficulty) {
this.selectedDifficulty = value; this.selectedDifficulty = value;
} }
@@ -409,16 +446,29 @@ export class SinglePlayerModal extends LitElement {
private handleInfiniteGoldChange(e: Event) { private handleInfiniteGoldChange(e: Event) {
this.infiniteGold = Boolean((e.target as HTMLInputElement).checked); this.infiniteGold = Boolean((e.target as HTMLInputElement).checked);
} }
private handleInfiniteTroopsChange(e: Event) { private handleInfiniteTroopsChange(e: Event) {
this.infiniteTroops = Boolean((e.target as HTMLInputElement).checked); this.infiniteTroops = Boolean((e.target as HTMLInputElement).checked);
} }
private handleDisableNPCsChange(e: Event) { private handleDisableNPCsChange(e: Event) {
this.disableNPCs = Boolean((e.target as HTMLInputElement).checked); this.disableNPCs = Boolean((e.target as HTMLInputElement).checked);
} }
private getRandomMap(): GameMapType {
const maps = Object.values(GameMapType);
const randIdx = Math.floor(Math.random() * maps.length);
return maps[randIdx] as GameMapType;
}
private startGame() { private startGame() {
// If random map is selected, choose a random map now
const mapToUse = this.useRandomMap ? this.getRandomMap() : this.selectedMap;
consolex.log( consolex.log(
`Starting single player game with map: ${GameMapType[this.selectedMap]}`, `Starting single player game with map: ${GameMapType[mapToUse]}${this.useRandomMap ? " (Randomly selected)" : ""}`,
); );
this.dispatchEvent( this.dispatchEvent(
new CustomEvent("join-lobby", { new CustomEvent("join-lobby", {
detail: { detail: {
@@ -426,7 +476,7 @@ export class SinglePlayerModal extends LitElement {
lobby: { lobby: {
id: generateID(), id: generateID(),
}, },
map: this.selectedMap, map: mapToUse,
difficulty: this.selectedDifficulty, difficulty: this.selectedDifficulty,
disableNPCs: this.disableNPCs, disableNPCs: this.disableNPCs,
bots: this.bots, bots: this.bots,