Working on refactoring
This commit is contained in:
parent
a2cb68b421
commit
70bbaed6e5
@ -9,15 +9,8 @@ import { layout, staticData } from '../util/layout.js';
|
|||||||
import basePath from '../basepath';
|
import basePath from '../basepath';
|
||||||
|
|
||||||
import { getValidRoads, getValidCorners, isRuleEnabled } from '../util/validLocations.js';
|
import { getValidRoads, getValidCorners, isRuleEnabled } from '../util/validLocations.js';
|
||||||
|
import { Player } from './games/types.js';
|
||||||
interface Player {
|
import { normalizeIncoming, shuffleArray } from './games/utils.js';
|
||||||
order: number;
|
|
||||||
orderRoll?: number;
|
|
||||||
position?: string;
|
|
||||||
orderStatus?: string;
|
|
||||||
tied?: boolean;
|
|
||||||
[key: string]: unknown;
|
|
||||||
}
|
|
||||||
|
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
@ -34,31 +27,7 @@ const debug = {
|
|||||||
update: false
|
update: false
|
||||||
};
|
};
|
||||||
|
|
||||||
// Normalize incoming websocket messages to a canonical { type, data }
|
// normalizeIncoming imported from './games/utils.ts'
|
||||||
// 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 };
|
|
||||||
}
|
|
||||||
|
|
||||||
let gameDB;
|
let gameDB;
|
||||||
|
|
||||||
@ -66,24 +35,7 @@ require("../db/games").then(function(db) {
|
|||||||
gameDB = db;
|
gameDB = db;
|
||||||
});
|
});
|
||||||
|
|
||||||
function shuffleArray(array) {
|
// shuffleArray imported from './games/utils.ts'
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
const games = {};
|
const games = {};
|
||||||
|
26
server/routes/games/types.ts
Normal file
26
server/routes/games/types.ts
Normal 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 };
|
30
server/routes/games/utils.ts
Normal file
30
server/routes/games/utils.ts
Normal 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;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user