From 70bbaed6e5fbd58e5c021d78aacee6222817d8cd Mon Sep 17 00:00:00 2001 From: James Ketrenos Date: Mon, 6 Oct 2025 11:29:41 -0700 Subject: [PATCH] Working on refactoring --- server/routes/games.ts | 56 +++--------------------------------- server/routes/games/types.ts | 26 +++++++++++++++++ server/routes/games/utils.ts | 30 +++++++++++++++++++ 3 files changed, 60 insertions(+), 52 deletions(-) create mode 100644 server/routes/games/types.ts create mode 100644 server/routes/games/utils.ts diff --git a/server/routes/games.ts b/server/routes/games.ts index 42634b0..89496c1 100755 --- a/server/routes/games.ts +++ b/server/routes/games.ts @@ -9,15 +9,8 @@ import { layout, staticData } from '../util/layout.js'; import basePath from '../basepath'; import { getValidRoads, getValidCorners, isRuleEnabled } from '../util/validLocations.js'; - -interface Player { - order: number; - orderRoll?: number; - position?: string; - orderStatus?: string; - tied?: boolean; - [key: string]: unknown; -} +import { Player } from './games/types.js'; +import { normalizeIncoming, shuffleArray } from './games/utils.js'; const router = express.Router(); @@ -34,31 +27,7 @@ const debug = { update: false }; -// Normalize incoming websocket messages to a canonical { type, data } -// shape. Some clients historically sent the payload as { type, data } while -// others used a flatter shape. This helper accepts either a string or an -// already-parsed object and returns a stable object so handlers don't need -// to defensively check multiple nested locations. -function normalizeIncoming(msg: unknown): { type: string | null, data: unknown } { - if (!msg) return { type: null, data: null }; - let parsed: unknown = null; - try { - if (typeof msg === 'string') { - parsed = JSON.parse(msg); - } else { - parsed = msg; - } - } catch (e) { - // if parsing failed, return nulls so the caller can log/ignore - return { type: null, data: null }; - } - if (!parsed) return { type: null, data: null }; - const type = (parsed as any).type || (parsed as any).action || null; - // Prefer parsed.data when present, but allow flattened payloads where - // properties like `name` live at the root. - const data = parsed.data || (Object.keys(parsed).length ? Object.assign({}, parsed) : null); - return { type, data }; -} +// normalizeIncoming imported from './games/utils.ts' let gameDB; @@ -66,24 +35,7 @@ require("../db/games").then(function(db) { gameDB = db; }); -function shuffleArray(array) { - var currentIndex = array.length, temporaryValue, randomIndex; - - // While there remain elements to shuffle... - while (0 !== currentIndex) { - - // Pick a remaining element... - randomIndex = Math.floor(Math.random() * currentIndex); - currentIndex -= 1; - - // And swap it with the current element. - temporaryValue = array[currentIndex]; - array[currentIndex] = array[randomIndex]; - array[randomIndex] = temporaryValue; - } - - return array; -} +// shuffleArray imported from './games/utils.ts' const games = {}; diff --git a/server/routes/games/types.ts b/server/routes/games/types.ts new file mode 100644 index 0000000..b24ca3a --- /dev/null +++ b/server/routes/games/types.ts @@ -0,0 +1,26 @@ +export interface Player { + order: number; + orderRoll?: number; + position?: string; + orderStatus?: string; + tied?: boolean; + [key: string]: any; +} + +export interface Game { + id?: string | number; + placements?: any; + rules?: any; + state?: string; + robber?: number; + players?: Player[]; + [key: string]: any; +} + +export interface Session { + id?: string | number; + userId?: number; + [key: string]: any; +} + +export type IncomingMessage = { type: string | null; data: any }; diff --git a/server/routes/games/utils.ts b/server/routes/games/utils.ts new file mode 100644 index 0000000..564e32f --- /dev/null +++ b/server/routes/games/utils.ts @@ -0,0 +1,30 @@ +export function normalizeIncoming(msg: unknown): { type: string | null, data: unknown } { + if (!msg) return { type: null, data: null }; + let parsed: unknown = null; + try { + if (typeof msg === 'string') { + parsed = JSON.parse(msg); + } else { + parsed = msg; + } + } catch (e) { + return { type: null, data: null }; + } + if (!parsed) return { type: null, data: null }; + const type = (parsed as any).type || (parsed as any).action || null; + const data = (parsed as any).data || (Object.keys(parsed as any).length ? Object.assign({}, parsed as any) : null); + return { type, data }; +} + +export function shuffleArray(array: T[]): T[] { + let currentIndex = array.length, temporaryValue: T | undefined, randomIndex: number; + while (0 !== currentIndex) { + randomIndex = Math.floor(Math.random() * currentIndex); + currentIndex -= 1; + // use non-null assertions because we're swapping indices that exist + temporaryValue = array[currentIndex] as T; + array[currentIndex] = array[randomIndex] as T; + array[randomIndex] = temporaryValue as T; + } + return array; +}