|
| 1 | +import { CODE_LENGTH } from '../../shared/constants'; |
| 2 | +import { SCENARIOS } from '../../shared/map-data'; |
| 3 | +import { byId } from '../dom'; |
| 4 | +import type { AIDifficulty, UIEvent } from './events'; |
| 5 | +import { parseJoinInput } from './formatters'; |
| 6 | +import { buildWaitingScreenCopy } from './screens'; |
| 7 | + |
| 8 | +export interface LobbyViewDeps { |
| 9 | + emit: (event: UIEvent) => void; |
| 10 | + showMenu: () => void; |
| 11 | + showScenarioSelect: () => void; |
| 12 | + copyText?: (text: string) => Promise<void> | undefined; |
| 13 | +} |
| 14 | + |
| 15 | +export class LobbyView { |
| 16 | + private aiDifficulty: AIDifficulty = 'normal'; |
| 17 | + private pendingAIGame = false; |
| 18 | + |
| 19 | + constructor(private readonly deps: LobbyViewDeps) { |
| 20 | + this.bindMenuControls(); |
| 21 | + this.bindDifficultyButtons(); |
| 22 | + this.buildScenarioList(); |
| 23 | + this.bindJoinControls(); |
| 24 | + this.bindCopyButton(); |
| 25 | + } |
| 26 | + |
| 27 | + onMenuShown(): void { |
| 28 | + this.pendingAIGame = false; |
| 29 | + } |
| 30 | + |
| 31 | + setMenuLoading(loading: boolean): void { |
| 32 | + const btn = byId<HTMLButtonElement>('createBtn'); |
| 33 | + |
| 34 | + btn.disabled = loading; |
| 35 | + btn.textContent = loading ? 'CREATING...' : 'Create Game'; |
| 36 | + } |
| 37 | + |
| 38 | + showWaiting(code: string): void { |
| 39 | + const copy = buildWaitingScreenCopy(code, false); |
| 40 | + byId('gameCode').textContent = copy.codeText; |
| 41 | + byId('waitingStatus').textContent = copy.statusText; |
| 42 | + } |
| 43 | + |
| 44 | + showConnecting(): void { |
| 45 | + const copy = buildWaitingScreenCopy('', true); |
| 46 | + byId('gameCode').textContent = copy.codeText; |
| 47 | + byId('waitingStatus').textContent = copy.statusText; |
| 48 | + } |
| 49 | + |
| 50 | + private bindMenuControls(): void { |
| 51 | + byId('createBtn').addEventListener('click', () => { |
| 52 | + this.deps.showScenarioSelect(); |
| 53 | + }); |
| 54 | + |
| 55 | + byId('singlePlayerBtn').addEventListener('click', () => { |
| 56 | + this.pendingAIGame = true; |
| 57 | + this.deps.showScenarioSelect(); |
| 58 | + }); |
| 59 | + |
| 60 | + byId('backBtn').addEventListener('click', () => { |
| 61 | + this.deps.emit({ type: 'backToMenu' }); |
| 62 | + this.deps.showMenu(); |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + private bindDifficultyButtons(): void { |
| 67 | + const buttons = Array.from( |
| 68 | + document.querySelectorAll<HTMLElement>('.btn-difficulty'), |
| 69 | + ); |
| 70 | + |
| 71 | + for (const btn of buttons) { |
| 72 | + btn.addEventListener('click', (event: Event) => { |
| 73 | + event.stopPropagation(); |
| 74 | + |
| 75 | + this.aiDifficulty = btn.dataset.difficulty as AIDifficulty; |
| 76 | + |
| 77 | + for (const button of buttons) { |
| 78 | + button.classList.remove('active'); |
| 79 | + } |
| 80 | + |
| 81 | + btn.classList.add('active'); |
| 82 | + }); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private submitJoin(rawValue: string): void { |
| 87 | + const parsed = parseJoinInput(rawValue, CODE_LENGTH); |
| 88 | + if (!parsed) { |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + this.deps.emit({ |
| 93 | + type: 'join', |
| 94 | + code: parsed.code, |
| 95 | + playerToken: parsed.playerToken, |
| 96 | + }); |
| 97 | + } |
| 98 | + |
| 99 | + private bindJoinControls(): void { |
| 100 | + byId('joinBtn').addEventListener('click', () => { |
| 101 | + this.submitJoin(byId<HTMLInputElement>('codeInput').value); |
| 102 | + }); |
| 103 | + |
| 104 | + byId('codeInput').addEventListener('keydown', (event) => { |
| 105 | + if (event.key === 'Enter') { |
| 106 | + this.submitJoin((event.target as HTMLInputElement).value); |
| 107 | + } |
| 108 | + }); |
| 109 | + } |
| 110 | + |
| 111 | + private bindCopyButton(): void { |
| 112 | + byId('copyBtn').addEventListener('click', () => { |
| 113 | + const code = byId('gameCode').textContent ?? ''; |
| 114 | + const url = `${window.location.origin}/?code=${code}`; |
| 115 | + const copyText = |
| 116 | + this.deps.copyText ?? |
| 117 | + ((text: string) => navigator.clipboard?.writeText(text)); |
| 118 | + const copyPromise = copyText(url); |
| 119 | + |
| 120 | + copyPromise |
| 121 | + ?.then(() => { |
| 122 | + byId('copyBtn').textContent = 'Copied!'; |
| 123 | + |
| 124 | + setTimeout(() => { |
| 125 | + byId('copyBtn').textContent = 'Copy Link'; |
| 126 | + }, 2000); |
| 127 | + }) |
| 128 | + .catch(() => {}); |
| 129 | + }); |
| 130 | + } |
| 131 | + |
| 132 | + private buildScenarioList(): void { |
| 133 | + const container = byId('scenarioList'); |
| 134 | + |
| 135 | + for (const [key, def] of Object.entries(SCENARIOS)) { |
| 136 | + const btn = document.createElement('button'); |
| 137 | + btn.className = 'btn btn-scenario'; |
| 138 | + btn.dataset.scenario = key; |
| 139 | + |
| 140 | + const tags = (def.tags ?? []) |
| 141 | + .map((tag) => `<span class="scenario-tag">${tag}</span>`) |
| 142 | + .join(''); |
| 143 | + |
| 144 | + btn.innerHTML = |
| 145 | + `<div class="scenario-name">${def.name}${tags}</div>` + |
| 146 | + `<div class="scenario-desc">${def.description}</div>`; |
| 147 | + |
| 148 | + btn.addEventListener('click', () => { |
| 149 | + if (this.pendingAIGame) { |
| 150 | + this.pendingAIGame = false; |
| 151 | + this.deps.emit({ |
| 152 | + type: 'startSinglePlayer', |
| 153 | + scenario: key, |
| 154 | + difficulty: this.aiDifficulty, |
| 155 | + }); |
| 156 | + return; |
| 157 | + } |
| 158 | + |
| 159 | + this.deps.emit({ |
| 160 | + type: 'selectScenario', |
| 161 | + scenario: key, |
| 162 | + }); |
| 163 | + }); |
| 164 | + |
| 165 | + container.appendChild(btn); |
| 166 | + } |
| 167 | + } |
| 168 | +} |
0 commit comments