mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-29 03:44:40 +00:00
store client ips in bigquery table
This commit is contained in:
@@ -7,7 +7,7 @@ import { createRenderer, GameRenderer } from "./graphics/GameRenderer";
|
||||
import { InputHandler, MouseUpEvent, ZoomEvent, DragEvent, MouseDownEvent } from "./InputHandler"
|
||||
import { ClientID, ClientIntentMessageSchema, ClientJoinMessageSchema, ClientMessageSchema, GameConfig, GameID, Intent, ServerMessage, ServerMessageSchema, ServerSyncMessage, Turn } from "../core/Schemas";
|
||||
import { createMiniMap, loadTerrainMap, TerrainMapImpl } from "../core/game/TerrainMapLoader";
|
||||
import { and, bfs, dist, manhattanDist } from "../core/Util";
|
||||
import { and, bfs, dist, generateID, manhattanDist } from "../core/Util";
|
||||
import { WinCheckExecution } from "../core/execution/WinCheckExecution";
|
||||
import { SendAttackIntentEvent, SendSpawnIntentEvent, Transport } from "./Transport";
|
||||
import { createCanvas } from "./graphics/Utils";
|
||||
@@ -20,14 +20,13 @@ export interface LobbyConfig {
|
||||
gameType: GameType
|
||||
playerName: () => string
|
||||
gameID: GameID
|
||||
ip: string | null
|
||||
map: GameMap | null
|
||||
difficulty: Difficulty | null
|
||||
}
|
||||
|
||||
export function joinLobby(lobbyConfig: LobbyConfig, onjoin: () => void): () => void {
|
||||
const clientID = uuidv4()
|
||||
const playerID = uuidv4()
|
||||
const clientID = generateID()
|
||||
const playerID = generateID()
|
||||
const eventBus = new EventBus()
|
||||
const config = getConfig()
|
||||
|
||||
@@ -45,7 +44,6 @@ export function joinLobby(lobbyConfig: LobbyConfig, onjoin: () => void): () => v
|
||||
gameConfig,
|
||||
eventBus,
|
||||
lobbyConfig.gameID,
|
||||
lobbyConfig.ip,
|
||||
clientID,
|
||||
playerID,
|
||||
config,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Config } from "../core/configuration/Config";
|
||||
import { ClientMessage, ClientMessageSchema, GameConfig, GameID, GameRecordSchema, Intent, ServerMessage, ServerStartGameMessageSchema, ServerTurnMessageSchema, Turn } from "../core/Schemas";
|
||||
import { CreateGameRecord, generateGameID } from "../core/Util";
|
||||
import { ClientID, ClientMessage, ClientMessageSchema, GameConfig, GameID, GameRecordSchema, Intent, PlayerRecord, ServerMessage, ServerStartGameMessageSchema, ServerTurnMessageSchema, Turn } from "../core/Schemas";
|
||||
import { CreateGameRecord, generateID } from "../core/Util";
|
||||
|
||||
export class LocalServer {
|
||||
|
||||
@@ -13,8 +13,8 @@ export class LocalServer {
|
||||
|
||||
private gameID: GameID
|
||||
|
||||
constructor(private config: Config, private gameConfig: GameConfig, private clientConnect: () => void, private clientMessage: (message: ServerMessage) => void) {
|
||||
this.gameID = generateGameID()
|
||||
constructor(private clientID: ClientID, private config: Config, private gameConfig: GameConfig, private clientConnect: () => void, private clientMessage: (message: ServerMessage) => void) {
|
||||
this.gameID = generateID()
|
||||
}
|
||||
|
||||
start() {
|
||||
@@ -52,7 +52,11 @@ export class LocalServer {
|
||||
public endGame() {
|
||||
console.log('local server ending game')
|
||||
clearInterval(this.endTurnIntervalID)
|
||||
const record = CreateGameRecord(this.gameID, this.gameConfig, this.turns, this.startedAt, Date.now())
|
||||
const players: PlayerRecord[] = [{
|
||||
ip: null,
|
||||
clientID: this.clientID
|
||||
}]
|
||||
const record = CreateGameRecord(this.gameID, this.gameConfig, players, this.turns, this.startedAt, Date.now())
|
||||
// Clear turns because beacon only supports up to 64kb
|
||||
record.turns = []
|
||||
// For unload events, sendBeacon is the only reliable method
|
||||
|
||||
@@ -16,8 +16,6 @@ import { JoinPrivateLobbyModal } from "./JoinPrivateLobbyModal";
|
||||
class Client {
|
||||
private gameStop: () => void
|
||||
|
||||
private ip: Promise<string | null> = null
|
||||
|
||||
private usernameInput: UsernameInput | null = null;
|
||||
|
||||
private joinModal: JoinPrivateLobbyModal
|
||||
@@ -37,7 +35,6 @@ class Client {
|
||||
});
|
||||
|
||||
setFavicon()
|
||||
this.ip = getClientIP()
|
||||
document.addEventListener('join-lobby', this.handleJoinLobby.bind(this));
|
||||
document.addEventListener('leave-lobby', this.handleLeaveLobby.bind(this));
|
||||
document.addEventListener('single-player', this.handleSinglePlayer.bind(this));
|
||||
@@ -65,8 +62,6 @@ class Client {
|
||||
private async handleJoinLobby(event: CustomEvent) {
|
||||
const lobby = event.detail.lobby
|
||||
console.log(`joining lobby ${lobby.id}`)
|
||||
const clientIP = await this.ip
|
||||
console.log(`got ip ${clientIP}`)
|
||||
if (this.gameStop != null) {
|
||||
console.log('joining lobby, stopping existing game')
|
||||
this.gameStop()
|
||||
@@ -76,7 +71,6 @@ class Client {
|
||||
gameType: event.detail.gameType,
|
||||
playerName: (): string => this.usernameInput.getCurrentUsername(),
|
||||
gameID: lobby.id,
|
||||
ip: clientIP,
|
||||
map: event.detail.map,
|
||||
difficulty: event.detail.difficulty,
|
||||
},
|
||||
@@ -98,37 +92,6 @@ class Client {
|
||||
}
|
||||
}
|
||||
|
||||
async function getClientIP(timeoutMs: number = 1000): Promise<string | null> {
|
||||
const controller = new AbortController();
|
||||
const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
|
||||
|
||||
try {
|
||||
const response: Response = await fetch('https://api.ipify.org?format=json', {
|
||||
signal: controller.signal
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
|
||||
const data: { ip: string } = await response.json();
|
||||
return data.ip;
|
||||
} catch (error) {
|
||||
if (error instanceof Error) {
|
||||
if (error.name === 'AbortError') {
|
||||
console.error('Request timed out');
|
||||
} else {
|
||||
console.error('Error fetching IP:', error.message);
|
||||
}
|
||||
} else {
|
||||
console.error('An unknown error occurred');
|
||||
}
|
||||
return null;
|
||||
} finally {
|
||||
clearTimeout(timeoutId);
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize the client when the DOM is loaded
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
new Client().initialize();
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { LitElement, html, css } from 'lit';
|
||||
import { customElement, property, state } from 'lit/decorators.js';
|
||||
import { Difficulty, GameMap, GameType } from '../core/game/Game';
|
||||
import { generateGameID as generateGameID } from '../core/Util';
|
||||
import { generateID as generateID } from '../core/Util';
|
||||
|
||||
@customElement('single-player-modal')
|
||||
export class SinglePlayerModal extends LitElement {
|
||||
@@ -128,7 +128,7 @@ export class SinglePlayerModal extends LitElement {
|
||||
detail: {
|
||||
gameType: GameType.Singleplayer,
|
||||
lobby: {
|
||||
id: generateGameID(),
|
||||
id: generateID(),
|
||||
},
|
||||
map: this.selectedMap,
|
||||
difficulty: this.selectedDifficulty
|
||||
|
||||
@@ -102,7 +102,6 @@ export class Transport {
|
||||
private gameConfig: GameConfig | null,
|
||||
private eventBus: EventBus,
|
||||
private gameID: GameID,
|
||||
private clientIP: string | null,
|
||||
private clientID: ClientID,
|
||||
private playerID: PlayerID,
|
||||
private config: Config,
|
||||
@@ -152,7 +151,7 @@ export class Transport {
|
||||
}
|
||||
|
||||
private connectLocal(onconnect: () => void, onmessage: (message: ServerMessage) => void) {
|
||||
this.localServer = new LocalServer(this.config, this.gameConfig, onconnect, onmessage)
|
||||
this.localServer = new LocalServer(this.clientID, this.config, this.gameConfig, onconnect, onmessage)
|
||||
this.localServer.start()
|
||||
}
|
||||
|
||||
@@ -195,7 +194,6 @@ export class Transport {
|
||||
type: "join",
|
||||
gameID: this.gameID,
|
||||
clientID: this.clientID,
|
||||
clientIP: this.clientIP,
|
||||
lastTurn: numTurns
|
||||
})
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user