diff --git a/src/client/graphics/layers/EventsDisplay.ts b/src/client/graphics/layers/EventsDisplay.ts index 293590d3a..5a8eb98a4 100644 --- a/src/client/graphics/layers/EventsDisplay.ts +++ b/src/client/graphics/layers/EventsDisplay.ts @@ -328,12 +328,12 @@ export class EventsDisplay extends LitElement implements Layer { const myPlayer = this.game.playerByClientID(this.clientID); if (!myPlayer) return; - const recipient = update.recipientID == AllPlayers ? AllPlayers : this.game.playerBySmallID(update.recipientID) - const sender = this.game.playerBySmallID(update.senderID) as PlayerView + const recipient = update.emoji.recipientID == AllPlayers ? AllPlayers : this.game.playerBySmallID(update.emoji.recipientID) + const sender = this.game.playerBySmallID(update.emoji.senderID) as PlayerView if (recipient == myPlayer) { this.addEvent({ - description: `${sender.displayName()}:${update.message}`, + description: `${sender.displayName()}:${update.emoji.message}`, unsafeDescription: true, type: MessageType.INFO, highlight: true, @@ -341,7 +341,7 @@ export class EventsDisplay extends LitElement implements Layer { }); } else if (sender === myPlayer && recipient !== AllPlayers) { this.addEvent({ - description: `Sent ${(recipient as PlayerView).displayName()}: ${update.message}`, + description: `Sent ${(recipient as PlayerView).displayName()}: ${update.emoji.message}`, unsafeDescription: true, type: MessageType.INFO, highlight: true, diff --git a/src/core/game/Game.ts b/src/core/game/Game.ts index 90c4e186a..6c3583378 100644 --- a/src/core/game/Game.ts +++ b/src/core/game/Game.ts @@ -82,14 +82,6 @@ export class Nation { ) { } } -export class EmojiMessage { - constructor( - public readonly sender: Player, - public readonly recipient: Player | typeof AllPlayers, - public readonly emoji: string, - public readonly createdAt: Tick - ) { } -} export class Cell { public index: number @@ -430,6 +422,7 @@ export interface PlayerUpdate { allies: number[] isTraitor: boolean targets: number[] + outgoingEmojis: EmojiMessage[] } @@ -464,14 +457,18 @@ export interface TargetPlayerUpdate { targetID: number } -export interface EmojiUpdate { - type: GameUpdateType.EmojiUpdate +export interface EmojiMessage { message: string senderID: number recipientID: number | typeof AllPlayers createdAt: Tick } +export interface EmojiUpdate { + type: GameUpdateType.EmojiUpdate + emoji: EmojiMessage +} + export interface DisplayMessageUpdate { type: GameUpdateType.DisplayEvent message: string diff --git a/src/core/game/GameImpl.ts b/src/core/game/GameImpl.ts index 08316047d..d351ecc3f 100644 --- a/src/core/game/GameImpl.ts +++ b/src/core/game/GameImpl.ts @@ -1,5 +1,5 @@ import { Config } from "../configuration/Config"; -import { Cell, Execution, MutableGame, Game, MutablePlayer, PlayerID, PlayerInfo, Player, TerraNullius, Unit, MutableAllianceRequest, Alliance, Nation, UnitType, UnitInfo, DefenseBonus, GameUpdate, GameUpdateType, AllPlayers, GameUpdates, TerrainType } from "./Game"; +import { Cell, Execution, MutableGame, Game, MutablePlayer, PlayerID, PlayerInfo, Player, TerraNullius, Unit, MutableAllianceRequest, Alliance, Nation, UnitType, UnitInfo, DefenseBonus, GameUpdate, GameUpdateType, AllPlayers, GameUpdates, TerrainType, EmojiMessage } from "./Game"; import { NationMap } from "./TerrainMapLoader"; import { PlayerImpl } from "./PlayerImpl"; import { TerraNulliusImpl } from "./TerraNulliusImpl"; @@ -443,15 +443,10 @@ export class GameImpl implements MutableGame { }) } - sendEmojiUpdate(sender: Player, recipient: Player | typeof AllPlayers, emoji: string): void { - const recipientID = recipient === AllPlayers ? recipient : recipient.smallID(); - + sendEmojiUpdate(msg: EmojiMessage): void { this.addUpdate({ type: GameUpdateType.EmojiUpdate, - message: emoji, - senderID: sender.smallID(), - recipientID: recipientID, - createdAt: this._ticks + emoji: msg }) } diff --git a/src/core/game/PlayerImpl.ts b/src/core/game/PlayerImpl.ts index ab8ddd650..939728380 100644 --- a/src/core/game/PlayerImpl.ts +++ b/src/core/game/PlayerImpl.ts @@ -1,4 +1,4 @@ -import { MutablePlayer, PlayerInfo, PlayerID, PlayerType, Player, TerraNullius, Cell, Execution, AllianceRequest, MutableAllianceRequest, MutableAlliance, Alliance, Tick, EmojiMessage, AllPlayers, Gold, UnitType, Unit, MutableUnit, Relation, PlayerUpdate, GameUpdateType } from "./Game"; +import { MutablePlayer, PlayerInfo, PlayerID, PlayerType, Player, TerraNullius, Cell, Execution, AllianceRequest, MutableAllianceRequest, MutableAlliance, Alliance, Tick, AllPlayers, Gold, UnitType, Unit, MutableUnit, Relation, PlayerUpdate, GameUpdateType, EmojiMessage } from "./Game"; import { ClientID } from "../Schemas"; import { assertNever, closestOceanShoreFromPlayer, distSortUnit, simpleHash, sourceDstOceanShore, within } from "../Util"; import { CellString, GameImpl } from "./GameImpl"; @@ -7,6 +7,7 @@ import { MessageType } from './Game'; import { renderTroops } from "../../client/Utils"; import { TerraNulliusImpl } from "./TerraNulliusImpl"; import { manhattanDistFN, TileRef } from "./GameMap"; +import { Emoji } from "discord.js"; interface Target { tick: Tick @@ -77,7 +78,8 @@ export class PlayerImpl implements MutablePlayer { targetTroopRatio: this.targetTroopRatio(), allies: this.alliances().map(a => a.other(this).smallID()), isTraitor: this.isTraitor(), - targets: this.targets().map(p => p.smallID()) + targets: this.targets().map(p => p.smallID()), + outgoingEmojis: this.outgoingEmojis() } } @@ -318,9 +320,14 @@ export class PlayerImpl implements MutablePlayer { if (recipient == this) { throw Error(`Cannot send emoji to oneself: ${this}`) } - const msg = new EmojiMessage(this, recipient, emoji, this.mg.ticks()) + const msg: EmojiMessage = { + message: emoji, + senderID: this.smallID(), + recipientID: recipient == AllPlayers ? recipient : recipient.smallID(), + createdAt: this.mg.ticks() + } this.outgoingEmojis_.push(msg) - this.mg.sendEmojiUpdate(this, recipient, emoji) + this.mg.sendEmojiUpdate(msg) } outgoingEmojis(): EmojiMessage[] { @@ -330,7 +337,8 @@ export class PlayerImpl implements MutablePlayer { } canSendEmoji(recipient: Player | typeof AllPlayers): boolean { - const prevMsgs = this.outgoingEmojis_.filter(msg => msg.recipient == recipient) + const recipientID = recipient == AllPlayers ? AllPlayers : recipient.smallID() + const prevMsgs = this.outgoingEmojis_.filter(msg => msg.recipientID == recipientID) for (const msg of prevMsgs) { if (this.mg.ticks() - msg.createdAt < this.mg.config().emojiMessageCooldown()) { return false