Skip to content

Commit f33aa03

Browse files
committed
Resolve review comments
1 parent 858511b commit f33aa03

9 files changed

Lines changed: 80 additions & 51 deletions

File tree

src/client/graphics/layers/EventsDisplay.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
} from "../../../core/game/Game";
1313
import {
1414
AllianceExpiredUpdate,
15+
AllianceExtensionUpdate,
1516
AllianceRequestReplyUpdate,
1617
AllianceRequestUpdate,
1718
BrokeAllianceUpdate,
@@ -175,26 +176,18 @@ export class EventsDisplay extends LitElement implements Layer {
175176
[GameUpdateType.Emoji, this.onEmojiMessageEvent.bind(this)],
176177
[GameUpdateType.UnitIncoming, this.onUnitIncomingEvent.bind(this)],
177178
[GameUpdateType.AllianceExpired, this.onAllianceExpiredEvent.bind(this)],
179+
[
180+
GameUpdateType.AllianceExtension,
181+
this.onAllianceExtensionEvent.bind(this),
182+
],
178183
] as const;
179184

180185
constructor() {
181186
super();
182187
this.events = [];
183188
}
184189

185-
init() {
186-
this.eventBus.on(SendAllianceExtensionIntentEvent, (e) => {
187-
const myPlayer = this.game.myPlayer();
188-
if (!myPlayer) return;
189-
const alliance = myPlayer
190-
.alliances()
191-
.find((a) => a.other === e.recipient.id());
192-
if (alliance) {
193-
this.removeAllianceRenewalEvents(alliance.id);
194-
this.requestUpdate();
195-
}
196-
});
197-
}
190+
init() {}
198191

199192
tick() {
200193
this.active = true;
@@ -622,6 +615,11 @@ export class EventsDisplay extends LitElement implements Layer {
622615
});
623616
}
624617

618+
private onAllianceExtensionEvent(update: AllianceExtensionUpdate) {
619+
this.removeAllianceRenewalEvents(update.allianceID);
620+
this.requestUpdate();
621+
}
622+
625623
onTargetPlayerEvent(event: TargetPlayerUpdate) {
626624
const other = this.game.playerBySmallID(event.playerID) as PlayerView;
627625
const myPlayer = this.game.myPlayer() as PlayerView;

src/client/graphics/layers/PlayerPanel.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ export class PlayerPanel extends LitElement implements Layer {
122122
const myPlayer = this.g.myPlayer();
123123
if (myPlayer !== null && myPlayer.isAlive()) {
124124
this.actions = await myPlayer.actions(this.tile);
125-
if (this.actions?.interaction?.allianceExpiresAt !== undefined) {
126-
const expiresAt = this.actions.interaction.allianceExpiresAt;
125+
if (this.actions?.interaction?.allianceInfo?.expiresAt !== undefined) {
126+
const expiresAt = this.actions.interaction.allianceInfo.expiresAt;
127127
const remainingTicks = expiresAt - this.g.ticks();
128128
const remainingSeconds = Math.max(0, Math.floor(remainingTicks / 10)); // 10 ticks per second
129129

src/client/graphics/layers/RadialMenu.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,8 +1287,8 @@ export class RadialMenu implements Layer {
12871287
params: MenuElementParams,
12881288
): string {
12891289
const interaction = params.playerActions?.interaction;
1290-
const myAgreed = interaction?.myPlayerAgreedToExtend ?? false;
1291-
const otherAgreed = interaction?.otherPlayerAgreedToExtend ?? false;
1290+
const myAgreed = interaction?.allianceInfo?.myPlayerAgreedToExtend ?? false;
1291+
const otherAgreed = interaction?.allianceInfo?.otherAgreedToExtend ?? false;
12921292
return `${disabled}:${myAgreed}:${otherAgreed}`;
12931293
}
12941294

@@ -1307,8 +1307,8 @@ export class RadialMenu implements Layer {
13071307
}
13081308

13091309
const interaction = params.playerActions?.interaction;
1310-
const myAgreed = interaction?.myPlayerAgreedToExtend ?? false;
1311-
const otherAgreed = interaction?.otherPlayerAgreedToExtend ?? false;
1310+
const myAgreed = interaction?.allianceInfo?.myPlayerAgreedToExtend ?? false;
1311+
const otherAgreed = interaction?.allianceInfo?.otherAgreedToExtend ?? false;
13121312

13131313
const ns = "http://www.w3.org/2000/svg";
13141314
const smallSize = iconSize * 0.8;

src/client/graphics/layers/RadialMenuElements.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ const allyExtendElement: MenuElement = {
218218
id: "ally_extend",
219219
name: "extend",
220220
displayed: (params: MenuElementParams) =>
221-
!!params.playerActions?.interaction?.inAllianceExtensionWindow,
221+
!!params.playerActions?.interaction?.allianceInfo?.inExtensionWindow,
222222
disabled: (params: MenuElementParams) =>
223223
!params.playerActions?.interaction?.canExtendAlliance,
224224
color: COLORS.ally,
@@ -230,10 +230,10 @@ const allyExtendElement: MenuElement = {
230230
},
231231
timerFraction: (params: MenuElementParams) => {
232232
const interaction = params.playerActions?.interaction;
233-
if (!interaction?.allianceExpiresAt) return 1;
233+
if (!interaction?.allianceInfo?.expiresAt) return 1;
234234
const remaining = Math.max(
235235
0,
236-
interaction.allianceExpiresAt - params.game.ticks(),
236+
interaction.allianceInfo.expiresAt - params.game.ticks(),
237237
);
238238
const extensionWindow = Math.max(
239239
1,
@@ -664,7 +664,7 @@ export const rootMenuElement: MenuElement = {
664664
(tileOwner as PlayerView).id() === params.myPlayer.id();
665665

666666
const inExtensionWindow =
667-
params.playerActions.interaction?.inAllianceExtensionWindow;
667+
params.playerActions.interaction?.allianceInfo?.inExtensionWindow;
668668

669669
const menuItems: (MenuElement | null)[] = [
670670
infoMenuElement,

src/core/GameRunner.ts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -212,20 +212,8 @@ export class GameRunner {
212212
canDonateTroops: player.canDonateTroops(other),
213213
canEmbargo: !player.hasEmbargoAgainst(other),
214214
};
215-
const alliance = player.allianceWith(other as Player);
216-
if (alliance) {
217-
actions.interaction.allianceExpiresAt = alliance.expiresAt();
218-
const inWindow =
219-
alliance.expiresAt() <=
220-
this.game.ticks() +
221-
this.game.config().allianceExtensionPromptOffset();
222-
actions.interaction.inAllianceExtensionWindow = inWindow;
223-
actions.interaction.myPlayerAgreedToExtend =
224-
alliance.agreedToExtend(player);
225-
actions.interaction.otherPlayerAgreedToExtend = alliance.agreedToExtend(
226-
other as Player,
227-
);
228-
}
215+
actions.interaction.allianceInfo =
216+
player.allianceInfo(other as Player) ?? undefined;
229217
}
230218

231219
return actions;

src/core/execution/alliance/AllianceExtensionExecution.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
Player,
66
PlayerID,
77
} from "../../game/Game";
8+
import { GameUpdateType } from "../../game/GameUpdates";
89

910
export class AllianceExtensionExecution implements Execution {
1011
constructor(
@@ -42,6 +43,12 @@ export class AllianceExtensionExecution implements Execution {
4243
// Mark this player's intent to extend
4344
alliance.addExtensionRequest(this.from);
4445

46+
mg.addUpdate({
47+
type: GameUpdateType.AllianceExtension,
48+
playerID: this.from.smallID(),
49+
allianceID: alliance.id(),
50+
});
51+
4552
if (alliance.bothAgreedToExtend()) {
4653
alliance.extend();
4754

src/core/game/Game.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,7 @@ export interface Player {
661661
allies(): Player[];
662662
isAlliedWith(other: Player): boolean;
663663
allianceWith(other: Player): MutableAlliance | null;
664+
allianceInfo(other: Player): AllianceInfo | null;
664665
canSendAllianceRequest(other: Player): boolean;
665666
canExtendAlliance(other: Player): boolean;
666667
breakAlliance(alliance: Alliance): void;
@@ -860,6 +861,13 @@ export interface PlayerBorderTiles {
860861
borderTiles: ReadonlySet<TileRef>;
861862
}
862863

864+
export interface AllianceInfo {
865+
expiresAt: Tick;
866+
inExtensionWindow: boolean;
867+
myPlayerAgreedToExtend: boolean;
868+
otherAgreedToExtend: boolean;
869+
}
870+
863871
export interface PlayerInteraction {
864872
sharedBorder: boolean;
865873
canSendEmoji: boolean;
@@ -870,10 +878,7 @@ export interface PlayerInteraction {
870878
canDonateGold: boolean;
871879
canDonateTroops: boolean;
872880
canEmbargo: boolean;
873-
allianceExpiresAt?: Tick;
874-
inAllianceExtensionWindow?: boolean;
875-
myPlayerAgreedToExtend?: boolean;
876-
otherPlayerAgreedToExtend?: boolean;
881+
allianceInfo?: AllianceInfo;
877882
}
878883

879884
export interface EmojiMessage {

src/core/game/PlayerImpl.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
import { AttackImpl } from "./AttackImpl";
1313
import {
1414
Alliance,
15+
AllianceInfo,
1516
AllianceRequest,
1617
AllPlayers,
1718
Attack,
@@ -402,6 +403,22 @@ export class PlayerImpl implements Player {
402403
);
403404
}
404405

406+
allianceInfo(other: Player): AllianceInfo | null {
407+
const alliance = this.allianceWith(other);
408+
if (!alliance) {
409+
return null;
410+
}
411+
const inExtensionWindow =
412+
alliance.expiresAt() <=
413+
this.mg.ticks() + this.mg.config().allianceExtensionPromptOffset();
414+
return {
415+
expiresAt: alliance.expiresAt(),
416+
inExtensionWindow,
417+
myPlayerAgreedToExtend: alliance.agreedToExtend(this),
418+
otherAgreedToExtend: alliance.agreedToExtend(other),
419+
};
420+
}
421+
405422
canExtendAlliance(other: Player): boolean {
406423
if (other === this) {
407424
return false;

tests/client/graphics/RadialMenuElements.test.ts

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -364,10 +364,13 @@ describe("RadialMenuElements", () => {
364364
mockPlayerActions.interaction = {
365365
...mockPlayerActions.interaction,
366366
canBreakAlliance: true,
367-
inAllianceExtensionWindow: true,
368367
canExtendAlliance: false,
369-
myPlayerAgreedToExtend: true,
370-
otherPlayerAgreedToExtend: false,
368+
allianceInfo: {
369+
expiresAt: 100,
370+
inExtensionWindow: true,
371+
myPlayerAgreedToExtend: true,
372+
otherAgreedToExtend: false,
373+
},
371374
};
372375

373376
const subMenu = rootMenuElement.subMenu!(mockParams);
@@ -387,8 +390,13 @@ describe("RadialMenuElements", () => {
387390
mockPlayerActions.interaction = {
388391
...mockPlayerActions.interaction,
389392
canBreakAlliance: true,
390-
inAllianceExtensionWindow: false,
391393
canExtendAlliance: false,
394+
allianceInfo: {
395+
expiresAt: 100,
396+
inExtensionWindow: false,
397+
myPlayerAgreedToExtend: false,
398+
otherAgreedToExtend: false,
399+
},
392400
};
393401

394402
const subMenu = rootMenuElement.subMenu!(mockParams);
@@ -408,10 +416,13 @@ describe("RadialMenuElements", () => {
408416
mockPlayerActions.interaction = {
409417
...mockPlayerActions.interaction,
410418
canBreakAlliance: true,
411-
inAllianceExtensionWindow: true,
412419
canExtendAlliance: false,
413-
myPlayerAgreedToExtend: true,
414-
otherPlayerAgreedToExtend: false,
420+
allianceInfo: {
421+
expiresAt: 100,
422+
inExtensionWindow: true,
423+
myPlayerAgreedToExtend: true,
424+
otherAgreedToExtend: false,
425+
},
415426
};
416427

417428
const subMenu = rootMenuElement.subMenu!(mockParams);
@@ -432,10 +443,13 @@ describe("RadialMenuElements", () => {
432443
mockPlayerActions.interaction = {
433444
...mockPlayerActions.interaction,
434445
canBreakAlliance: true,
435-
inAllianceExtensionWindow: true,
436446
canExtendAlliance: true,
437-
myPlayerAgreedToExtend: false,
438-
otherPlayerAgreedToExtend: false,
447+
allianceInfo: {
448+
expiresAt: 100,
449+
inExtensionWindow: true,
450+
myPlayerAgreedToExtend: false,
451+
otherAgreedToExtend: false,
452+
},
439453
};
440454

441455
const subMenu = rootMenuElement.subMenu!(mockParams);

0 commit comments

Comments
 (0)