mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-30 21:53:28 +00:00
store client ips in bigquery table
This commit is contained in:
+9
-8
@@ -42,6 +42,7 @@ export type ClientPingMessage = z.infer<typeof ClientPingMessageSchema>
|
||||
export type ClientIntentMessage = z.infer<typeof ClientIntentMessageSchema>
|
||||
export type ClientJoinMessage = z.infer<typeof ClientJoinMessageSchema>
|
||||
|
||||
export type PlayerRecord = z.infer<typeof PlayerRecordSchema>
|
||||
export type GameRecord = z.infer<typeof GameRecordSchema>
|
||||
|
||||
const PlayerTypeSchema = z.nativeEnum(PlayerType);
|
||||
@@ -105,11 +106,6 @@ export const BoatAttackIntentSchema = BaseIntentSchema.extend({
|
||||
y: z.number(),
|
||||
})
|
||||
|
||||
export const UpdateNameIntentSchema = BaseIntentSchema.extend({
|
||||
type: z.literal('updateName'),
|
||||
name: z.string(),
|
||||
})
|
||||
|
||||
export const AllianceRequestIntentSchema = BaseIntentSchema.extend({
|
||||
type: z.literal('allianceRequest'),
|
||||
requestor: z.string(),
|
||||
@@ -228,20 +224,25 @@ export const ClientIntentMessageSchema = ClientBaseMessageSchema.extend({
|
||||
|
||||
export const ClientJoinMessageSchema = ClientBaseMessageSchema.extend({
|
||||
type: z.literal('join'),
|
||||
clientIP: z.string().nullable(),
|
||||
lastTurn: z.number() // The last turn the client saw.
|
||||
})
|
||||
|
||||
export const ClientMessageSchema = z.union([ClientPingMessageSchema, ClientIntentMessageSchema, ClientJoinMessageSchema]);
|
||||
|
||||
export const PlayerRecordSchema = z.object({
|
||||
clientID: z.string(),
|
||||
username: z.string(),
|
||||
ip: z.string().nullable(),
|
||||
})
|
||||
|
||||
export const GameRecordSchema = z.object({
|
||||
id: z.string(),
|
||||
gameConfig: GameConfigSchema,
|
||||
players: z.array(PlayerRecordSchema),
|
||||
startTimestampMS: z.number(),
|
||||
endTimestampMS: z.number(),
|
||||
durationSeconds: z.number(),
|
||||
date: z.string(),
|
||||
usernames: z.array(z.string()),
|
||||
num_turns: z.number(),
|
||||
turns: z.array(TurnSchema)
|
||||
})
|
||||
})
|
||||
|
||||
+19
-7
@@ -5,7 +5,7 @@ import DOMPurify from 'dompurify';
|
||||
|
||||
import { Cell, Game, Player, TerraNullius, Tile, Unit } from "./game/Game";
|
||||
import { number } from 'zod';
|
||||
import { GameConfig, GameID, GameRecord, Turn } from './Schemas';
|
||||
import { GameConfig, GameID, GameRecord, PlayerRecord, Turn } from './Schemas';
|
||||
import { customAlphabet, nanoid } from 'nanoid';
|
||||
|
||||
export function manhattanDist(c1: Cell, c2: Cell): number {
|
||||
@@ -232,7 +232,15 @@ export function onlyImages(html: string) {
|
||||
});
|
||||
}
|
||||
|
||||
export function CreateGameRecord(id: GameID, gameConfig: GameConfig, turns: Turn[], start: number, end: number): GameRecord {
|
||||
export function CreateGameRecord(
|
||||
id: GameID,
|
||||
gameConfig: GameConfig,
|
||||
// username does not need to be set.
|
||||
players: PlayerRecord[],
|
||||
turns: Turn[],
|
||||
start: number,
|
||||
end: number
|
||||
): GameRecord {
|
||||
const record: GameRecord = {
|
||||
id: id,
|
||||
gameConfig: gameConfig,
|
||||
@@ -241,18 +249,22 @@ export function CreateGameRecord(id: GameID, gameConfig: GameConfig, turns: Turn
|
||||
date: new Date().toISOString().split('T')[0],
|
||||
turns: []
|
||||
}
|
||||
const usernames = new Set<string>()
|
||||
|
||||
for (const turn of turns) {
|
||||
if (turn.intents.length != 0) {
|
||||
record.turns.push(turn)
|
||||
for (const intent of turn.intents) {
|
||||
if (intent.type == 'spawn') {
|
||||
usernames.add(intent.name)
|
||||
if (intent.type == "spawn") {
|
||||
for (const playerRecord of players) {
|
||||
if (playerRecord.clientID == intent.clientID) {
|
||||
playerRecord.username = intent.name
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
record.usernames = Array.from(usernames)
|
||||
record.players = players
|
||||
record.durationSeconds = Math.floor((record.endTimestampMS - record.startTimestampMS) / 1000)
|
||||
record.num_turns = turns.length
|
||||
return record;
|
||||
@@ -262,7 +274,7 @@ export function assertNever(x: never): never {
|
||||
throw new Error('Unexpected value: ' + x);
|
||||
}
|
||||
|
||||
export function generateGameID(): GameID {
|
||||
export function generateID(): GameID {
|
||||
const nanoid = customAlphabet('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 8)
|
||||
return nanoid()
|
||||
}
|
||||
Reference in New Issue
Block a user