Files
OpenFrontIO/src/core/game/GameUpdates.ts
T
evanpelle 500b5fcfde Clean up and refactor the Unit class (#769)
## Description:

* Merged similar fields so they can be reused (eg warshipTarget =>
targetUnit)
* simplified isCooldown api
* added "touch" method to send update to UI layer
* standardized on "undefined"


## Please complete the following:

- [ ] I have added screenshots for all UI updates
- [x] I confirm I have thoroughly tested these changes and take full
responsibility for any bugs introduced
- [x] I understand that submitting code with bugs that could have been
caught through manual testing blocks releases and new features for all
contributors

## Please put your Discord username so you can be contacted if a bug or
regression is found:

<DISCORD USERNAME>
evan

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **Refactor**
- Unified and simplified how unit targets and cooldowns are managed
across all unit types, resulting in more consistent in-game behavior for
nukes, warships, trade ships, and SAM launchers.
- Updated naming and logic for unit targeting and cooldowns, improving
clarity in status displays and interactions.
- Reorganized unit interface and streamlined cooldown handling for
smoother gameplay experience.
- **Bug Fixes**
- Corrected visual indicators for nukes and warships to accurately
reflect their targets.
- **Tests**
- Updated automated tests to align with the new cooldown and targeting
logic.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-05-17 17:45:10 -07:00

195 lines
3.9 KiB
TypeScript

import { AllPlayersStats, ClientID, PlayerStats } from "../Schemas";
import {
EmojiMessage,
GameUpdates,
MessageType,
NameViewData,
PlayerID,
PlayerType,
Team,
Tick,
UnitType,
} from "./Game";
import { TileRef, TileUpdate } from "./GameMap";
export interface GameUpdateViewData {
tick: number;
updates: GameUpdates;
packedTileUpdates: BigUint64Array;
playerNameViewData: Record<number, NameViewData>;
}
export interface ErrorUpdate {
errMsg: string;
stack?: string;
}
export enum GameUpdateType {
Tile,
Unit,
Player,
DisplayEvent,
DisplayChatEvent,
AllianceRequest,
AllianceRequestReply,
BrokeAlliance,
AllianceExpired,
TargetPlayer,
Emoji,
Win,
Hash,
UnitIncoming,
}
export type GameUpdate =
| TileUpdateWrapper
| UnitUpdate
| PlayerUpdate
| AllianceRequestUpdate
| AllianceRequestReplyUpdate
| BrokeAllianceUpdate
| AllianceExpiredUpdate
| DisplayMessageUpdate
| DisplayChatMessageUpdate
| TargetPlayerUpdate
| EmojiUpdate
| WinUpdate
| HashUpdate
| UnitIncomingUpdate;
export interface TileUpdateWrapper {
type: GameUpdateType.Tile;
update: TileUpdate;
}
export interface UnitUpdate {
type: GameUpdateType.Unit;
unitType: UnitType;
troops: number;
id: number;
ownerID: number;
lastOwnerID?: number;
// TODO: make these tilerefs
pos: TileRef;
lastPos: TileRef;
isActive: boolean;
targetUnitId?: number; // Only for trade ships
targetTile?: TileRef; // Only for nukes
health?: number;
constructionType?: UnitType;
ticksLeftInCooldown?: Tick;
}
export interface AttackUpdate {
attackerID: number;
targetID: number;
troops: number;
id: string;
retreating: boolean;
}
export interface PlayerUpdate {
type: GameUpdateType.Player;
nameViewData?: NameViewData;
clientID: ClientID | null;
flag: string | undefined;
name: string;
displayName: string;
id: PlayerID;
team?: Team;
smallID: number;
playerType: PlayerType;
isAlive: boolean;
tilesOwned: number;
gold: number;
population: number;
workers: number;
troops: number;
targetTroopRatio: number;
allies: number[];
embargoes: Set<PlayerID>;
isTraitor: boolean;
targets: number[];
outgoingEmojis: EmojiMessage[];
outgoingAttacks: AttackUpdate[];
incomingAttacks: AttackUpdate[];
outgoingAllianceRequests: PlayerID[];
stats: PlayerStats;
hasSpawned: boolean;
}
export interface AllianceRequestUpdate {
type: GameUpdateType.AllianceRequest;
requestorID: number;
recipientID: number;
createdAt: Tick;
}
export interface AllianceRequestReplyUpdate {
type: GameUpdateType.AllianceRequestReply;
request: AllianceRequestUpdate;
accepted: boolean;
}
export interface BrokeAllianceUpdate {
type: GameUpdateType.BrokeAlliance;
traitorID: number;
betrayedID: number;
}
export interface AllianceExpiredUpdate {
type: GameUpdateType.AllianceExpired;
player1ID: number;
player2ID: number;
}
export interface TargetPlayerUpdate {
type: GameUpdateType.TargetPlayer;
playerID: number;
targetID: number;
}
export interface EmojiUpdate {
type: GameUpdateType.Emoji;
emoji: EmojiMessage;
}
export interface DisplayMessageUpdate {
type: GameUpdateType.DisplayEvent;
message: string;
messageType: MessageType;
playerID: number | null;
}
export type DisplayChatMessageUpdate = {
type: GameUpdateType.DisplayChatEvent;
key: string;
category: string;
variables?: Record<string, string>;
playerID: number | null;
isFrom: boolean;
recipient: string;
};
export interface WinUpdate {
type: GameUpdateType.Win;
allPlayersStats: AllPlayersStats;
// Player id or team name.
winner: number | Team;
winnerType: "player" | "team";
}
export interface HashUpdate {
type: GameUpdateType.Hash;
tick: Tick;
hash: number;
}
export interface UnitIncomingUpdate {
type: GameUpdateType.UnitIncoming;
unitID: number;
message: string;
messageType: MessageType;
playerID: number;
}