1
0

Working on refactoring

This commit is contained in:
James Ketr 2025-10-06 11:29:41 -07:00
parent a2cb68b421
commit 70bbaed6e5
3 changed files with 60 additions and 52 deletions

View File

@ -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 = {};

View File

@ -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 };

View File

@ -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<T>(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;
}