Merge pull request #99 from groffta/randmap

Added random option to map selection for SinglePlayerModal and HostLobbyModal.
This commit is contained in:
evanpelle
2025-03-02 20:13:14 -08:00
committed by GitHub
3 changed files with 93 additions and 7 deletions
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 MiB

+37 -3
View File
@@ -8,6 +8,8 @@ import { DifficultyDescription } from "./components/Difficulties";
import "./components/Maps";
import { generateID } from "../core/Util";
import { getConfig, getServerConfig } from "../core/configuration/Config";
import randomMap from "../../resources/images/RandomMap.png";
@customElement("host-lobby-modal")
export class HostLobbyModal extends LitElement {
@@ -22,6 +24,7 @@ export class HostLobbyModal extends LitElement {
@state() private lobbyId = "";
@state() private copySuccess = false;
@state() private players: string[] = [];
@state() private useRandomMap: boolean = false;
private playersInterval = null;
@@ -372,11 +375,27 @@ export class HostLobbyModal extends LitElement {
<div @click=${() => this.handleMapSelection(value)}>
<map-display
.mapKey=${key}
.selected=${this.selectedMap === value}
.selected=${!this.useRandomMap &&
this.selectedMap === value}
></map-display>
</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>
@@ -551,9 +570,13 @@ export class HostLobbyModal extends LitElement {
this.playersInterval = null;
}
}
private async handleRandomMapToggle() {
this.useRandomMap = true;
this.putGameConfig();
}
private async handleMapSelection(value: GameMapType) {
this.selectedMap = value;
this.useRandomMap = false;
this.putGameConfig();
}
private async handleDifficultySelection(value: Difficulty) {
@@ -609,9 +632,20 @@ 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() {
if (this.useRandomMap) {
this.selectedMap = this.getRandomMap();
}
await this.putGameConfig();
consolex.log(
`Starting private game with map: ${GameMapType[this.selectedMap]}`,
`Starting private game with map: ${GameMapType[this.selectedMap]} ${this.useRandomMap ? " (Randomly selected)" : ""}`,
);
this.close();
const response = await fetch(
+56 -4
View File
@@ -6,6 +6,7 @@ import { consolex } from "../core/Consolex";
import "./components/Difficulties";
import { DifficultyDescription } from "./components/Difficulties";
import "./components/Maps";
import randomMap from "../../resources/images/RandomMap.png";
@customElement("single-player-modal")
export class SinglePlayerModal extends LitElement {
@@ -17,6 +18,7 @@ export class SinglePlayerModal extends LitElement {
@state() private infiniteGold: boolean = false;
@state() private infiniteTroops: boolean = false;
@state() private instantBuild: boolean = false;
@state() private useRandomMap: boolean = false;
static styles = css`
.modal-overlay {
@@ -230,6 +232,15 @@ export class SinglePlayerModal extends LitElement {
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) {
.modal-content {
max-height: calc(90vh - 42px);
@@ -264,14 +275,34 @@ export class SinglePlayerModal extends LitElement {
.filter(([key]) => isNaN(Number(key)))
.map(
([key, value]) => html`
<div @click=${() => this.handleMapSelection(value)}>
<div
@click=${function () {
this.handleMapSelection(value);
}}
>
<map-display
.mapKey=${key}
.selected=${this.selectedMap === value}
.selected=${!this.useRandomMap &&
this.selectedMap === value}
></map-display>
</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>
@@ -334,7 +365,6 @@ export class SinglePlayerModal extends LitElement {
/>
<div class="option-card-title">Disable Nations</div>
</label>
<label
for="instant-build"
class="option-card ${this.instantBuild ? "selected" : ""}"
@@ -390,14 +420,21 @@ export class SinglePlayerModal extends LitElement {
public open() {
this.isModalOpen = true;
this.useRandomMap = false;
}
public close() {
this.isModalOpen = false;
}
private handleRandomMapToggle() {
this.useRandomMap = true;
}
private handleMapSelection(value: GameMapType) {
this.selectedMap = value;
this.useRandomMap = false;
}
private handleDifficultySelection(value: Difficulty) {
this.selectedDifficulty = value;
}
@@ -414,16 +451,31 @@ export class SinglePlayerModal extends LitElement {
private handleInfiniteGoldChange(e: Event) {
this.infiniteGold = Boolean((e.target as HTMLInputElement).checked);
}
private handleInfiniteTroopsChange(e: Event) {
this.infiniteTroops = Boolean((e.target as HTMLInputElement).checked);
}
private handleDisableNPCsChange(e: Event) {
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() {
// If random map is selected, choose a random map now
if (this.useRandomMap) {
this.selectedMap = this.getRandomMap();
}
consolex.log(
`Starting single player game with map: ${GameMapType[this.selectedMap]}`,
`Starting single player game with map: ${GameMapType[this.selectedMap]}${this.useRandomMap ? " (Randomly selected)" : ""}`,
);
this.dispatchEvent(
new CustomEvent("join-lobby", {
detail: {