Skip to content

Commit 3fd38e7

Browse files
authored
Add clanTag to GameRecord for archiving (#2314)
## Description: This will be used to determine clan winners in the api layer. ## Please complete the following: - [x] I have added screenshots for all UI updates - [x] I process any text displayed to the user through translateText() and I've added it to the en.json file - [x] I have added relevant tests to the test directory - [x] I confirm I have thoroughly tested these changes and take full responsibility for any bugs introduced ## Please put your Discord username so you can be contacted if a bug or regression is found: evan
1 parent b8fab0d commit 3fd38e7

File tree

6 files changed

+21
-9
lines changed

6 files changed

+21
-9
lines changed

jest.config.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@ export default {
1111
},
1212
transform: {
1313
"^.+\\.tsx?$": ["@swc/jest"],
14+
"^.+\\.mjs$": ["@swc/jest"],
15+
"^.+\\.js$": ["@swc/jest"],
1416
},
15-
transformIgnorePatterns: ["node_modules/(?!(node:)/)"],
17+
transformIgnorePatterns: [
18+
"node_modules/(?!(nanoid|@jsep|fastpriorityqueue|@datastructures-js)/)",
19+
],
1620
collectCoverageFrom: ["src/**/*.ts", "!src/**/*.d.ts"],
1721
coverageThreshold: {
1822
global: {

src/client/LocalServer.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
import {
1515
createPartialGameRecord,
1616
decompressGameRecord,
17+
getClanTag,
1718
replacer,
1819
} from "../core/Util";
1920
import { LobbyConfig } from "./ClientGameRunner";
@@ -188,6 +189,7 @@ export class LocalServer {
188189
clientID: this.lobbyConfig.clientID,
189190
stats: this.allPlayersStats[this.lobbyConfig.clientID],
190191
cosmetics: this.lobbyConfig.gameStartInfo?.players[0].cosmetics,
192+
clanTag: getClanTag(this.lobbyConfig.playerName) ?? undefined,
191193
},
192194
];
193195
if (this.lobbyConfig.gameStartInfo === undefined) {

src/core/Schemas.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,7 @@ export const ClientMessageSchema = z.discriminatedUnion("type", [
544544

545545
export const PlayerRecordSchema = PlayerSchema.extend({
546546
persistentID: PersistentIdSchema.nullable(), // WARNING: PII
547+
clanTag: z.string().optional(),
547548
stats: PlayerStatsSchema,
548549
});
549550
export type PlayerRecord = z.infer<typeof PlayerRecordSchema>;

src/core/Util.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,3 +320,12 @@ export function sigmoid(
320320
): number {
321321
return 1 / (1 + Math.exp(-decayRate * (value - midpoint)));
322322
}
323+
324+
// Compute clan from name
325+
export function getClanTag(name: string): string | null {
326+
if (!name.includes("[") || !name.includes("]")) {
327+
return null;
328+
}
329+
const clanMatch = name.match(/\[([a-zA-Z0-9]{2,5})\]/);
330+
return clanMatch ? clanMatch[1].toUpperCase() : null;
331+
}

src/core/game/Game.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Config } from "../configuration/Config";
22
import { AllPlayersStats, ClientID } from "../Schemas";
3+
import { getClanTag } from "../Util";
34
import { GameMap, TileRef } from "./GameMap";
45
import {
56
GameUpdate,
@@ -407,13 +408,7 @@ export class PlayerInfo {
407408
public readonly id: PlayerID,
408409
public readonly nation?: Nation | null,
409410
) {
410-
// Compute clan from name
411-
if (!name.includes("[") || !name.includes("]")) {
412-
this.clan = null;
413-
} else {
414-
const clanMatch = name.match(/\[([a-zA-Z0-9]{2,5})\]/);
415-
this.clan = clanMatch ? clanMatch[1].toUpperCase() : null;
416-
}
411+
this.clan = getClanTag(name);
417412
}
418413
}
419414

src/server/GameServer.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
ServerTurnMessage,
2222
Turn,
2323
} from "../core/Schemas";
24-
import { createPartialGameRecord } from "../core/Util";
24+
import { createPartialGameRecord, getClanTag } from "../core/Util";
2525
import { archive, finalizeGameRecord } from "./Archive";
2626
import { Client } from "./Client";
2727
export enum GamePhase {
@@ -686,6 +686,7 @@ export class GameServer {
686686
this.allClients.get(player.clientID)?.persistentID ?? "",
687687
stats,
688688
cosmetics: player.cosmetics,
689+
clanTag: getClanTag(player.username) ?? undefined,
689690
} satisfies PlayerRecord;
690691
},
691692
);

0 commit comments

Comments
 (0)