File size: 6,524 Bytes
c670567 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 | export type Role = 'Mafia' | 'Detective' | 'Doctor' | 'Villager';
export type Phase =
| 'night'
| 'dawn'
| 'discussion'
| 'hot_seat'
| 'vote'
| 'resolution'
| 'game_over';
export interface PlayerView {
id: string;
name: string;
seat: number;
alive: boolean;
isHuman: boolean;
persona: string;
modelSpec: string;
architecture: string;
team: 'town' | 'mafia' | null;
role: Role | null;
claimedRole: Role | null;
claimConfidence: string;
keyQuote: string;
lastVote: string | null;
publicStatus: string;
avatar?: string | null;
}
export interface EventView {
seq: number;
type: string;
phase: Phase;
day: number;
actor: string | null;
payload: Record<string, unknown>;
}
export interface Suggestion {
id: string;
intent: string;
tone: string;
message: string;
}
export interface MafiaGameView {
gameId: string;
seed: number;
mode: string;
phase: Phase;
phaseLabel: string;
day: number;
winner: 'town' | 'mafia' | null;
aliveCount: number;
human: {
id: string;
name: string;
role: Role;
team: 'town' | 'mafia';
alive: boolean;
legalActions: string[];
privateInfo: Record<string, unknown>;
};
players: PlayerView[];
events: EventView[];
scene: {
id: string;
sceneKey: string;
title: string;
subtitle: string;
objective: string;
soundCue: string;
};
suggestions: Suggestion[];
lastReceipt: EventView | null;
public: Record<string, unknown>;
votes: Record<string, string>;
lockedVotes: string[];
hotSeatTarget: string | null;
dawnMessage: string;
pendingHumanFloor: boolean;
targetChoices: { id: string; label: string }[];
roleChoices: Role[];
metrics: Record<string, unknown>;
humanAvatar: string;
}
export interface ReadyView {
ready: boolean;
agentMode: string;
moderator: string;
playerArchitecture: string;
checks: { name: string; target?: string; ready: boolean; error?: string }[];
}
export interface NewGameOptions {
seed: number;
humanName: string;
humanRole?: string;
agentMode: string;
humanAvatar?: string;
}
export const seatTexturePrefix: Record<string, string> = {
p1: 'player',
p2: 'nora',
p3: 'kai',
p4: 'mira',
p5: 'jules',
p6: 'lena',
p7: 'owen',
};
export const roleCardKey: Record<Role, string> = {
Mafia: 'role_card_mafia',
Detective: 'role_card_detective',
Doctor: 'role_card_doctor',
Villager: 'role_card_villager',
};
declare global {
interface Window {
__MAFIA_VIEW__?: MafiaGameView;
__MAFIA_READY__?: ReadyView;
__MAFIA_PLAYER_NAME__?: string;
__MAFIA_AGENT_MODE__?: string;
__MAFIA_AVATAR_ID__?: string;
}
}
async function requestJson<T>(path: string, init?: RequestInit): Promise<T> {
const response = await fetch(path, {
...init,
headers: {
'content-type': 'application/json',
...(init?.headers ?? {}),
},
});
if (!response.ok) {
const text = await response.text();
throw new Error(text || `${response.status} ${response.statusText}`);
}
return response.json() as Promise<T>;
}
export class BackendClient {
async ready(agentMode: string): Promise<ReadyView> {
return requestJson<ReadyView>('/api/ready', {
method: 'POST',
body: JSON.stringify({ agent_mode: agentMode }),
});
}
async newGame(options: NewGameOptions): Promise<MafiaGameView> {
return requestJson<MafiaGameView>('/api/game', {
method: 'POST',
body: JSON.stringify({
seed: options.seed,
human_name: options.humanName,
human_role: options.humanRole ?? 'Random',
agent_mode: options.agentMode,
human_avatar: options.humanAvatar ?? 'player',
}),
});
}
async advance(gameId: string, maxSteps = 1): Promise<MafiaGameView> {
return requestJson<MafiaGameView>(`/api/game/${gameId}/advance`, {
method: 'POST',
body: JSON.stringify({ max_steps: maxSteps }),
});
}
async message(gameId: string, message: string): Promise<MafiaGameView> {
return requestJson<MafiaGameView>(`/api/game/${gameId}/message`, {
method: 'POST',
body: JSON.stringify({ message, source: 'human_typed' }),
});
}
async approveSuggestion(gameId: string, suggestion: Suggestion): Promise<MafiaGameView> {
return requestJson<MafiaGameView>(`/api/game/${gameId}/approve-suggestion`, {
method: 'POST',
body: JSON.stringify({
suggestion_id: suggestion.id,
message: suggestion.message,
}),
});
}
async claim(gameId: string, role: Role): Promise<MafiaGameView> {
return requestJson<MafiaGameView>(`/api/game/${gameId}/claim`, {
method: 'POST',
body: JSON.stringify({ role }),
});
}
async accuse(gameId: string, target: string): Promise<MafiaGameView> {
return requestJson<MafiaGameView>(`/api/game/${gameId}/accuse`, {
method: 'POST',
body: JSON.stringify({ target }),
});
}
async startVote(gameId: string): Promise<MafiaGameView> {
return requestJson<MafiaGameView>(`/api/game/${gameId}/start-vote`, {
method: 'POST',
body: JSON.stringify({}),
});
}
async vote(gameId: string, target: string): Promise<MafiaGameView> {
return requestJson<MafiaGameView>(`/api/game/${gameId}/vote`, {
method: 'POST',
body: JSON.stringify({ target }),
});
}
async nightAction(gameId: string, target: string): Promise<MafiaGameView> {
return requestJson<MafiaGameView>(`/api/game/${gameId}/night-action`, {
method: 'POST',
body: JSON.stringify({ target }),
});
}
async passFloor(gameId: string): Promise<MafiaGameView> {
return requestJson<MafiaGameView>(`/api/game/${gameId}/pass-floor`, {
method: 'POST',
body: JSON.stringify({}),
});
}
async suggestions(gameId: string, target?: string | null): Promise<Suggestion[]> {
const data = await requestJson<{ suggestions: Suggestion[] }>(`/api/game/${gameId}/suggestions`, {
method: 'POST',
body: JSON.stringify({ target }),
});
return data.suggestions ?? [];
}
}
export function actorName(view: MafiaGameView, id: string | null | undefined): string {
if (!id) return 'Moderator';
return view.players.find((player) => player.id === id)?.name ?? id;
}
export function targetLabel(view: MafiaGameView, id: string | null | undefined): string {
if (!id) return 'Unknown';
const player = view.players.find((item) => item.id === id);
return player ? `${player.seat}. ${player.name}` : id;
}
|