diff --git a/lib/GameStore.ts b/lib/GameStore.ts index 5ed876f..cf104a2 100644 --- a/lib/GameStore.ts +++ b/lib/GameStore.ts @@ -1,18 +1,24 @@ import Deck from '@/lib/TarokkaDeck'; import generateID from '@/tools/simpleID'; +import parseMilliseconds from '@/tools/parseMilliseconds'; import { GameState, GameUpdate, Settings } from '@/types'; const deck = new Deck(); export default class GameStore { + private startTime: number; private dms: Map; private spectators: Map; private players: Map; constructor() { + this.startTime = Date.now(); + this.dms = new Map(); this.spectators = new Map(); this.players = new Map(); + + setInterval(() => this.log(), 15 * 60 * 1000); } createGameIDs() { @@ -120,6 +126,25 @@ export default class GameStore { return this.leaveGame(gameID, playerID); } + log() { + const now = Date.now(); + const uptime = now - this.startTime; + + const { days, hours, minutes, seconds } = parseMilliseconds(uptime); + + const dayLog = days ? ` ${days} ${days > 1 ? 'days' : 'day'}` : ''; + const hourLog = hours ? ` ${hours} ${hours > 1 ? 'hours' : 'hour'}` : ''; + const minuteLog = minutes ? ` ${minutes} ${minutes > 1 ? 'minutes' : 'minute'}` : ''; + + const uptimeLog = `Up${dayLog}${hourLog}${minuteLog} ${seconds} seconds`; + + console.log('-'.repeat(uptimeLog.length)); + console.log(uptimeLog); + console.log(`Games: ${this.dms.size}`); + console.log(`Players: ${this.players.size}`); + console.log('-'.repeat(uptimeLog.length)); + } + deleteGame(gameID: string): void { this.dms.delete(gameID); this.spectators.delete(gameID); diff --git a/tools/parseMilliseconds.ts b/tools/parseMilliseconds.ts new file mode 100644 index 0000000..b25bae2 --- /dev/null +++ b/tools/parseMilliseconds.ts @@ -0,0 +1,27 @@ +export interface ParsedMilliseconds { + days: number; + hours: number; + minutes: number; + seconds: number; +} + +export default function parseMilliseconds(timestamp: number): ParsedMilliseconds { + const SECOND = 1000; + const MINUTE = 60 * SECOND; + const HOUR = 60 * MINUTE; + const DAY = 24 * HOUR; + + const days = Math.floor(timestamp / DAY); + timestamp %= DAY; + + const hours = Math.floor(timestamp / HOUR); + timestamp %= HOUR; + + const minutes = Math.floor(timestamp / MINUTE); + timestamp %= MINUTE; + + const seconds = Math.floor(timestamp / SECOND); + timestamp %= SECOND; + + return { days, hours, minutes, seconds }; +}