import { LitElement, html } from "lit"; import { customElement, property, state } from "lit/decorators.js"; import { EventBus } from "../../../core/EventBus"; import { GameView, PlayerView } from "../../../core/game/GameView"; import { Layer } from "./Layer"; import { MouseUpEvent } from "../../InputHandler"; import { AllPlayers, Player, PlayerActions, UnitType, } from "../../../core/game/Game"; import { TileRef } from "../../../core/game/GameMap"; import { renderNumber, renderTroops } from "../../Utils"; import targetIcon from "../../../../resources/images/TargetIconWhite.svg"; import emojiIcon from "../../../../resources/images/EmojiIconWhite.svg"; import donateIcon from "../../../../resources/images/DonateIconWhite.svg"; import traitorIcon from "../../../../resources/images/TraitorIconWhite.svg"; import allianceIcon from "../../../../resources/images/AllianceIconWhite.svg"; import { SendAllianceRequestIntentEvent, SendBreakAllianceIntentEvent, SendDonateIntentEvent, SendEmojiIntentEvent, SendTargetPlayerIntentEvent, SendEmbargoIntentEvent, } from "../../Transport"; import { EmojiTable } from "./EmojiTable"; @customElement("player-panel") export class PlayerPanel extends LitElement implements Layer { public g: GameView; public eventBus: EventBus; public emojiTable: EmojiTable; private actions: PlayerActions = null; private tile: TileRef = null; @state() private isVisible: boolean = false; public show(actions: PlayerActions, tile: TileRef) { this.actions = actions; this.tile = tile; this.isVisible = true; this.requestUpdate(); } public hide() { this.isVisible = false; this.requestUpdate(); } private handleClose(e: Event) { e.stopPropagation(); this.hide(); } private handleAllianceClick( e: Event, myPlayer: PlayerView, other: PlayerView, ) { e.stopPropagation(); this.eventBus.emit(new SendAllianceRequestIntentEvent(myPlayer, other)); this.hide(); } private handleBreakAllianceClick( e: Event, myPlayer: PlayerView, other: PlayerView, ) { e.stopPropagation(); this.eventBus.emit(new SendBreakAllianceIntentEvent(myPlayer, other)); this.hide(); } private handleDonateClick(e: Event, myPlayer: PlayerView, other: PlayerView) { e.stopPropagation(); this.eventBus.emit(new SendDonateIntentEvent(myPlayer, other, null)); this.hide(); } private handleEmbargoClick( e: Event, myPlayer: PlayerView, other: PlayerView, ) { e.stopPropagation(); this.eventBus.emit(new SendEmbargoIntentEvent(myPlayer, other, "start")); this.hide(); } private handleStopEmbargoClick( e: Event, myPlayer: PlayerView, other: PlayerView, ) { e.stopPropagation(); this.eventBus.emit(new SendEmbargoIntentEvent(myPlayer, other, "stop")); this.hide(); } private handleEmojiClick(e: Event, myPlayer: PlayerView, other: PlayerView) { e.stopPropagation(); this.emojiTable.showTable((emoji: string) => { if (myPlayer == other) { this.eventBus.emit(new SendEmojiIntentEvent(AllPlayers, emoji)); } else { this.eventBus.emit(new SendEmojiIntentEvent(other, emoji)); } this.emojiTable.hideTable(); this.hide(); }); } private handleTargetClick(e: Event, other: PlayerView) { e.stopPropagation(); this.eventBus.emit(new SendTargetPlayerIntentEvent(other.id())); this.hide(); } createRenderRoot() { return this; } init() { this.eventBus.on(MouseUpEvent, (e: MouseEvent) => this.hide()); } tick() { this.requestUpdate(); } getTotalNukesSent(): number { const stats = this.actions.interaction?.stats; if (!stats) { return 0; } let sum = 0; const nukes = stats.sentNukes[this.g.myPlayer().id()]; if (!nukes) { return 0; } for (const nukeType in nukes) { if (nukeType != UnitType.MIRVWarhead) { sum += nukes[nukeType]; } } return sum; } render() { if (!this.isVisible) { return html``; } const myPlayer = this.g.myPlayer(); if (myPlayer == null) { return; } let other = this.g.owner(this.tile); if (!other.isPlayer()) { throw new Error("Tile is not owned by a player"); } other = other as PlayerView; const canDonate = this.actions.interaction?.canDonate; const canSendAllianceRequest = this.actions.interaction?.canSendAllianceRequest; const canSendEmoji = other == myPlayer ? this.actions.canSendEmojiAllPlayers : this.actions.interaction?.canSendEmoji; const canBreakAlliance = this.actions.interaction?.canBreakAlliance; const canTarget = this.actions.interaction?.canTarget; const canEmbargo = this.actions.interaction?.canEmbargo; return html`