V1.0.0
Two Keys: co-op mystery games for couples. Eight games on four engines (story, defuse, sync, labyrinth), two-device and couch play, mobile-first UI. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
import { rankFor, type GameResult, type Role, type RoomState } from '@two-keys/shared';
|
||||
import type { Game } from '../content/schemas';
|
||||
import { engineView, gameMeta } from '../content/sanitize';
|
||||
import { totalKeys } from '../engines/index';
|
||||
import type { PlayerRec, Room } from './store';
|
||||
|
||||
export function computeResult(room: Room, game: Game): GameResult | null {
|
||||
if (room.phase !== 'finished' || !room.result || room.startedAt === null || room.finishedAt === null) {
|
||||
return null;
|
||||
}
|
||||
const elapsedSeconds = Math.round((room.finishedAt - room.startedAt) / 1000);
|
||||
const finalSeconds = elapsedSeconds + room.penaltySeconds;
|
||||
const keys = totalKeys(game);
|
||||
return {
|
||||
success: room.result.success,
|
||||
elapsedSeconds,
|
||||
penaltySeconds: room.penaltySeconds,
|
||||
finalSeconds,
|
||||
hintsUsed: room.hintsUsed,
|
||||
wrongGuesses: room.wrongGuesses,
|
||||
keysEarned: room.keys.length,
|
||||
totalKeys: keys,
|
||||
rank: rankFor({
|
||||
success: room.result.success,
|
||||
hintsUsed: room.hintsUsed,
|
||||
finalSeconds,
|
||||
estimatedMinutes: game.estimatedMinutes,
|
||||
keysEarned: room.keys.length,
|
||||
totalKeys: keys,
|
||||
}),
|
||||
ending: room.result.ending,
|
||||
reveal: room.result.reveal,
|
||||
};
|
||||
}
|
||||
|
||||
/** The full, sanitized state for one player. This is the only thing sockets ever receive. */
|
||||
export function buildState(
|
||||
room: Room,
|
||||
player: PlayerRec,
|
||||
game: Game,
|
||||
isOnline: (token: string) => boolean,
|
||||
now = Date.now(),
|
||||
): RoomState {
|
||||
const roles: Role[] = player.seat === 'both' ? ['A', 'B'] : player.seat ? [player.seat] : [];
|
||||
const views: RoomState['views'] = {};
|
||||
if (room.phase !== 'lobby' && room.engine) {
|
||||
for (const role of roles) views[role] = engineView(game, room, role, now);
|
||||
}
|
||||
|
||||
const partner = room.players.find((p) => p !== player);
|
||||
return {
|
||||
code: room.code,
|
||||
mode: room.mode,
|
||||
phase: room.phase,
|
||||
game: gameMeta(game),
|
||||
you: { seat: player.seat, ready: player.ready },
|
||||
partner: partner
|
||||
? {
|
||||
online: isOnline(partner.token),
|
||||
seat: partner.seat === 'both' ? null : partner.seat,
|
||||
ready: partner.ready,
|
||||
viewing: partner.viewing,
|
||||
}
|
||||
: null,
|
||||
notepad: room.notepad,
|
||||
keys: [...room.keys],
|
||||
timer: {
|
||||
startedAt: room.startedAt,
|
||||
finishedAt: room.finishedAt,
|
||||
penaltySeconds: room.penaltySeconds,
|
||||
serverNow: now,
|
||||
},
|
||||
stats: { hintsUsed: room.hintsUsed, wrongGuesses: room.wrongGuesses },
|
||||
views,
|
||||
result: computeResult(room, game),
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user