mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-21 09:40:44 +00:00
aa4b490e68
## Summary The WebGL renderer was adapted from an external extension and carried a lot of machinery this integration never uses (replay playback, its own input/event system, a GL radial menu). This PR is two mechanical cleanup passes with **no behavior change**: delete the dead code, then untangle the `GameView` naming collision. **78 files, +142 / −2,197.** ### Pass 1 — remove dead extension baggage - **Replay/copy mode**: `FrameData.tileMode` was hard-coded `"live"`; the copy branches in `frame/Upload.ts`, `UploadOptions` (never passed), `applyFullFrame`/`applyFullTiles`/`applyDelta` on the facade and `GPURenderer`, `HeatManager.resetForSeek`, and the seek-upload methods on `TerritoryPass`/`TrailPass` were all unreachable. Also deletes `types/Replay.ts`, `types/FrameSource.ts`, `types/GameUpdates.ts`, `types/Game.ts` (imported only by the types barrel). - **FrameEvents**: trimmed from 14 fields to the 3 actually populated and read (`deadUnits`, `conquestEvents`, `bonusEvents`). The other 11 fed the extension's stats system and were never written or read here. - **GL radial menu**: `RadialMenuPass`, its 4 shaders, and ~10 API methods on facade + renderer had zero callers — the game uses the DOM/d3 radial menu in `hud/layers/RadialMenu.ts`. The pass was constructed and drawn every frame for nothing. - **Facade event system**: `GameViewEventMap` defined 10 event types (`click`, `hover`, `scroll`, …) but only `contextrestored` was ever emitted — input actually flows through `InputHandler` → EventBus → controllers. Replaced the listener map with a single `onContextRestored` callback and deleted `Events.ts`. Also fixed the stale header comment claiming the facade handles user interaction. - **Unused API surface**: removed ~20 facade/renderer methods with zero callers (camera passthroughs like `panTo`/`zoomTo`/`fitMap`/`screenToWorld`, hit-testing queries, SAM replay setters, `setSelectedUnit`, `clearFx`/`setFxTimeFn`, `onFrame`/`afterRender`/fps tracking). Deliberately left alone: `Camera`'s pan/zoom primitives (building blocks for a possible future camera unification) and the `timeFn` plumbing inside the FX passes (deeply embedded as defaults; only the dead renderer-level wrappers were removed). ### Pass 2 — untangle the three GameViews - `render/gl/GameView.ts` → **`MapRenderer.ts`** (class `MapRenderer`). Every importer was already aliasing it as `WebGLGameView` to dodge the collision with the simulation-mirror `GameView` in `client/view/`, so this removes aliasing rather than adding churn. `render/CLAUDE.md` updated. - Deleted the `src/core/game/GameView.ts` back-compat shim (its own TODO asked for this). All 51 importers now import from `src/client/view/` directly via a new 3-line barrel `view/index.ts`. ## Test plan - `tsc --noEmit` clean, `eslint` clean - Full test suite passes (1,385 + 65 server tests) - Manual verification via headless Chromium: started a singleplayer game and confirmed the renderer works end-to-end — terrain draws, spawn-phase overlay shows, territories fill with borders after spawning, player names/flags render, no renderer console errors 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
334 lines
9.7 KiB
TypeScript
334 lines
9.7 KiB
TypeScript
import { LitElement, html } from "lit";
|
|
import { customElement, query } from "lit/decorators.js";
|
|
|
|
import { PlayerType } from "../../../core/game/Game";
|
|
import { GameView, PlayerView } from "../../view";
|
|
|
|
import quickChatData from "resources/QuickChat.json";
|
|
import { EventBus } from "../../../core/EventBus";
|
|
import { CloseViewEvent } from "../../InputHandler";
|
|
import { SendQuickChatEvent } from "../../Transport";
|
|
import { translateText } from "../../Utils";
|
|
|
|
export type QuickChatPhrase = {
|
|
key: string;
|
|
requiresPlayer: boolean;
|
|
};
|
|
|
|
export type QuickChatPhrases = Record<string, QuickChatPhrase[]>;
|
|
|
|
export const quickChatPhrases: QuickChatPhrases = quickChatData;
|
|
|
|
@customElement("chat-modal")
|
|
export class ChatModal extends LitElement {
|
|
@query("o-modal") private modalEl!: HTMLElement & {
|
|
open: () => void;
|
|
close: () => void;
|
|
};
|
|
|
|
createRenderRoot() {
|
|
return this;
|
|
}
|
|
|
|
private players: PlayerView[] = [];
|
|
|
|
private playerSearchQuery: string = "";
|
|
private previewText: string | null = null;
|
|
private requiresPlayerSelection: boolean = false;
|
|
private selectedCategory: string | null = null;
|
|
private selectedPhraseText: string | null = null;
|
|
private selectedPhraseTemplate: string | null = null;
|
|
private selectedQuickChatKey: string | null = null;
|
|
private selectedPlayer: PlayerView | null = null;
|
|
|
|
private recipient: PlayerView;
|
|
private sender: PlayerView;
|
|
public eventBus: EventBus;
|
|
|
|
public g: GameView;
|
|
|
|
quickChatPhrases: Record<
|
|
string,
|
|
Array<{ text: string; requiresPlayer: boolean }>
|
|
> = {
|
|
help: [{ text: "Please give me troops!", requiresPlayer: false }],
|
|
attack: [{ text: "Attack [P1]!", requiresPlayer: true }],
|
|
defend: [{ text: "Defend [P1]!", requiresPlayer: true }],
|
|
greet: [{ text: "Hello!", requiresPlayer: false }],
|
|
misc: [{ text: "Let's go!", requiresPlayer: false }],
|
|
};
|
|
|
|
public categories = [
|
|
{ id: "help" },
|
|
{ id: "attack" },
|
|
{ id: "defend" },
|
|
{ id: "greet" },
|
|
{ id: "misc" },
|
|
{ id: "warnings" },
|
|
];
|
|
|
|
private getPhrasesForCategory(categoryId: string) {
|
|
return quickChatPhrases[categoryId] ?? [];
|
|
}
|
|
|
|
render() {
|
|
return html`
|
|
<o-modal title="${translateText("chat.title")}">
|
|
<div class="chat-columns">
|
|
<div class="chat-column">
|
|
<div class="column-title">${translateText("chat.category")}</div>
|
|
${this.categories.map(
|
|
(category) => html`
|
|
<button
|
|
class="chat-option-button ${this.selectedCategory ===
|
|
category.id
|
|
? "selected"
|
|
: ""}"
|
|
@click=${() => this.selectCategory(category.id)}
|
|
>
|
|
${translateText(`chat.cat.${category.id}`)}
|
|
</button>
|
|
`,
|
|
)}
|
|
</div>
|
|
|
|
${this.selectedCategory
|
|
? html`
|
|
<div class="chat-column">
|
|
<div class="column-title">
|
|
${translateText("chat.phrase")}
|
|
</div>
|
|
<div class="phrase-scroll-area">
|
|
${this.getPhrasesForCategory(this.selectedCategory).map(
|
|
(phrase) => html`
|
|
<button
|
|
class="chat-option-button ${this
|
|
.selectedPhraseText ===
|
|
translateText(
|
|
`chat.${this.selectedCategory}.${phrase.key}`,
|
|
)
|
|
? "selected"
|
|
: ""}"
|
|
@click=${() => this.selectPhrase(phrase)}
|
|
>
|
|
${this.renderPhrasePreview(phrase)}
|
|
</button>
|
|
`,
|
|
)}
|
|
</div>
|
|
</div>
|
|
`
|
|
: null}
|
|
${this.requiresPlayerSelection || this.selectedPlayer
|
|
? html`
|
|
<div class="chat-column">
|
|
<div class="column-title">
|
|
${translateText("chat.player")}
|
|
</div>
|
|
|
|
<input
|
|
class="player-search-input"
|
|
type="text"
|
|
placeholder="${translateText("chat.search")}"
|
|
.value=${this.playerSearchQuery}
|
|
@input=${this.onPlayerSearchInput}
|
|
/>
|
|
|
|
<div class="player-scroll-area">
|
|
${this.getSortedFilteredPlayers().map(
|
|
(player) => html`
|
|
<button
|
|
class="chat-option-button ${this.selectedPlayer ===
|
|
player
|
|
? "selected"
|
|
: ""}"
|
|
style="border: 2px solid ${player
|
|
.territoryColor()
|
|
.toHex()};"
|
|
@click=${() => this.selectPlayer(player)}
|
|
>
|
|
${player.displayName()}
|
|
</button>
|
|
`,
|
|
)}
|
|
</div>
|
|
</div>
|
|
`
|
|
: null}
|
|
</div>
|
|
|
|
<div class="chat-preview">
|
|
${this.previewText
|
|
? translateText(this.previewText)
|
|
: translateText("chat.build")}
|
|
</div>
|
|
<div class="chat-send">
|
|
<button
|
|
class="chat-send-button"
|
|
@click=${this.sendChatMessage}
|
|
?disabled=${!this.previewText ||
|
|
(this.requiresPlayerSelection && !this.selectedPlayer)}
|
|
>
|
|
${translateText("chat.send")}
|
|
</button>
|
|
</div>
|
|
</o-modal>
|
|
`;
|
|
}
|
|
|
|
initEventBus(eventBus: EventBus) {
|
|
this.eventBus = eventBus;
|
|
eventBus.on(CloseViewEvent, (e) => {
|
|
if (!this.hidden) {
|
|
this.close();
|
|
}
|
|
});
|
|
}
|
|
|
|
private selectCategory(categoryId: string) {
|
|
this.selectedCategory = categoryId;
|
|
this.selectedPhraseText = null;
|
|
this.previewText = null;
|
|
this.requiresPlayerSelection = false;
|
|
this.requestUpdate();
|
|
}
|
|
|
|
private selectPhrase(phrase: QuickChatPhrase) {
|
|
this.selectedQuickChatKey = this.getFullQuickChatKey(
|
|
this.selectedCategory!,
|
|
phrase.key,
|
|
);
|
|
this.selectedPhraseTemplate = translateText(
|
|
`chat.${this.selectedCategory}.${phrase.key}`,
|
|
);
|
|
this.selectedPhraseText = translateText(
|
|
`chat.${this.selectedCategory}.${phrase.key}`,
|
|
);
|
|
this.previewText = `chat.${this.selectedCategory}.${phrase.key}`;
|
|
this.requiresPlayerSelection = phrase.requiresPlayer;
|
|
this.requestUpdate();
|
|
}
|
|
|
|
private renderPhrasePreview(phrase: { key: string }) {
|
|
return translateText(`chat.${this.selectedCategory}.${phrase.key}`);
|
|
}
|
|
|
|
private selectPlayer(player: PlayerView) {
|
|
if (this.previewText) {
|
|
this.previewText =
|
|
this.selectedPhraseTemplate?.replace("[P1]", player.displayName()) ??
|
|
null;
|
|
this.selectedPlayer = player;
|
|
this.requiresPlayerSelection = false;
|
|
this.requestUpdate();
|
|
}
|
|
}
|
|
|
|
private sendChatMessage() {
|
|
console.log("Sent message:", this.previewText);
|
|
console.log("Sender:", this.sender);
|
|
console.log("Recipient:", this.recipient);
|
|
console.log("Key:", this.selectedQuickChatKey);
|
|
|
|
if (this.sender && this.recipient && this.selectedQuickChatKey) {
|
|
this.eventBus.emit(
|
|
new SendQuickChatEvent(
|
|
this.recipient,
|
|
this.selectedQuickChatKey,
|
|
this.selectedPlayer?.id(),
|
|
),
|
|
);
|
|
}
|
|
|
|
this.previewText = null;
|
|
this.selectedCategory = null;
|
|
this.requiresPlayerSelection = false;
|
|
this.close();
|
|
|
|
this.requestUpdate();
|
|
}
|
|
|
|
private onPlayerSearchInput(e: Event) {
|
|
const target = e.target as HTMLInputElement;
|
|
this.playerSearchQuery = target.value.toLowerCase();
|
|
this.requestUpdate();
|
|
}
|
|
|
|
private getSortedFilteredPlayers(): PlayerView[] {
|
|
const sorted = [...this.players].sort((a, b) =>
|
|
a.displayName().localeCompare(b.displayName()),
|
|
);
|
|
const filtered = sorted.filter((p) =>
|
|
p.displayName().toLowerCase().includes(this.playerSearchQuery),
|
|
);
|
|
const others = sorted.filter(
|
|
(p) => !p.displayName().toLowerCase().includes(this.playerSearchQuery),
|
|
);
|
|
return [...filtered, ...others];
|
|
}
|
|
|
|
private getFullQuickChatKey(category: string, phraseKey: string): string {
|
|
return `${category}.${phraseKey}`;
|
|
}
|
|
|
|
public open(sender?: PlayerView, recipient?: PlayerView) {
|
|
if (sender && recipient) {
|
|
console.log("Sent message:", recipient);
|
|
console.log("Sent message:", sender);
|
|
this.players = this.g
|
|
.players()
|
|
.filter((p) => p.isAlive() && p.type() !== PlayerType.Bot);
|
|
|
|
this.recipient = recipient;
|
|
this.sender = sender;
|
|
}
|
|
this.requestUpdate();
|
|
this.modalEl?.open();
|
|
}
|
|
|
|
public close() {
|
|
this.selectedCategory = null;
|
|
this.selectedPhraseText = null;
|
|
this.previewText = null;
|
|
this.requiresPlayerSelection = false;
|
|
this.modalEl?.close();
|
|
}
|
|
|
|
public setRecipient(value: PlayerView) {
|
|
this.recipient = value;
|
|
}
|
|
|
|
public setSender(value: PlayerView) {
|
|
this.sender = value;
|
|
}
|
|
|
|
public openWithSelection(
|
|
categoryId: string,
|
|
phraseKey: string,
|
|
sender?: PlayerView,
|
|
recipient?: PlayerView,
|
|
) {
|
|
if (sender && recipient) {
|
|
this.players = this.g
|
|
.players()
|
|
.filter((p) => p.isAlive() && p.type() !== PlayerType.Bot);
|
|
|
|
this.recipient = recipient;
|
|
this.sender = sender;
|
|
}
|
|
|
|
this.selectCategory(categoryId);
|
|
|
|
const phrase = this.getPhrasesForCategory(categoryId).find(
|
|
(p) => p.key === phraseKey,
|
|
);
|
|
|
|
if (phrase) {
|
|
this.selectPhrase(phrase);
|
|
}
|
|
|
|
this.requestUpdate();
|
|
this.modalEl?.open();
|
|
}
|
|
}
|