import { LitElement, TemplateResult, html } from "lit"; import { customElement, state } from "lit/decorators.js"; import { translateText } from "../../../client/Utils"; import { Pattern } from "../../../core/CosmeticSchemas"; import { EventBus } from "../../../core/EventBus"; import { GameUpdateType } from "../../../core/game/GameUpdates"; import { GameView } from "../../../core/game/GameView"; import "../../components/PatternButton"; import { fetchPatterns, handlePurchase } from "../../Cosmetics"; import { getUserMe } from "../../jwt"; import { SendWinnerEvent } from "../../Transport"; import { GutterAdModalEvent } from "./GutterAdModal"; import { Layer } from "./Layer"; @customElement("win-modal") export class WinModal extends LitElement implements Layer { public game: GameView; public eventBus: EventBus; private hasShownDeathModal = false; @state() isVisible = false; @state() showButtons = false; @state() private patternContent: TemplateResult | null = null; private _title: string; // Override to prevent shadow DOM creation createRenderRoot() { return this; } constructor() { super(); } render() { return html`

${this._title || ""}

${this.innerHtml()}
`; } innerHtml() { return this.renderPatternButton(); } renderPatternButton() { return html`

${translateText("win_modal.support_openfront")}

${translateText("win_modal.territory_pattern")}

${this.patternContent}
`; } async loadPatternContent() { const me = await getUserMe(); const patterns = await fetchPatterns(me !== false ? me : null); const purchasable = Array.from(patterns.values()).filter( (p) => p.product !== null, ); if (purchasable.length === 0) { this.patternContent = html``; return; } const pattern = purchasable[Math.floor(Math.random() * purchasable.length)]; this.patternContent = html` {}} .onPurchase=${(priceId: string) => handlePurchase(priceId)} > `; } steamWishlist(): TemplateResult { return html`

${translateText("win_modal.wishlist")}

`; } async show() { await this.loadPatternContent(); this.eventBus.emit(new GutterAdModalEvent(true)); setTimeout(() => { this.isVisible = true; this.requestUpdate(); }, 1500); setTimeout(() => { this.showButtons = true; this.requestUpdate(); }, 3000); } hide() { this.eventBus.emit(new GutterAdModalEvent(false)); this.isVisible = false; this.showButtons = false; this.requestUpdate(); } private _handleExit() { this.hide(); window.location.href = "/"; } init() {} tick() { const myPlayer = this.game.myPlayer(); if ( !this.hasShownDeathModal && myPlayer && !myPlayer.isAlive() && !this.game.inSpawnPhase() && myPlayer.hasSpawned() ) { this.hasShownDeathModal = true; this._title = translateText("win_modal.died"); this.show(); } const updates = this.game.updatesSinceLastTick(); const winUpdates = updates !== null ? updates[GameUpdateType.Win] : []; winUpdates.forEach((wu) => { if (wu.winner === undefined) { // ... } else if (wu.winner[0] === "team") { this.eventBus.emit(new SendWinnerEvent(wu.winner, wu.allPlayersStats)); if (wu.winner[1] === this.game.myPlayer()?.team()) { this._title = translateText("win_modal.your_team"); } else { this._title = translateText("win_modal.other_team", { team: wu.winner[1], }); } this.show(); } else { const winner = this.game.playerByClientID(wu.winner[1]); if (!winner?.isPlayer()) return; const winnerClient = winner.clientID(); if (winnerClient !== null) { this.eventBus.emit( new SendWinnerEvent(["player", winnerClient], wu.allPlayersStats), ); } if ( winnerClient !== null && winnerClient === this.game.myPlayer()?.clientID() ) { this._title = translateText("win_modal.you_won"); } else { this._title = translateText("win_modal.other_won", { player: winner.name(), }); } this.show(); } }); } renderLayer(/* context: CanvasRenderingContext2D */) {} shouldTransform(): boolean { return false; } }