2026-06-04 10:11:00 +03:30
|
|
|
"use client";
|
|
|
|
|
|
|
|
|
|
import { create } from "zustand";
|
|
|
|
|
import { chooseCardAI, chooseTrumpAI } from "./hokm/ai";
|
|
|
|
|
import {
|
|
|
|
|
advanceAfterTrick,
|
|
|
|
|
chooseTrump as engineChooseTrump,
|
|
|
|
|
createInitialState,
|
|
|
|
|
dealForTrump,
|
|
|
|
|
playCard,
|
|
|
|
|
selectHakem,
|
|
|
|
|
startNextRound,
|
|
|
|
|
} from "./hokm/engine";
|
2026-06-04 13:13:48 +03:30
|
|
|
import { Card, GameState, Phase, Player, Rank, RoundResult, Seat, Suit, Team } from "./hokm/types";
|
2026-06-04 17:32:47 +03:30
|
|
|
import { avatarEmoji, RewardResult, ServerGameState } from "./online/types";
|
2026-06-04 13:13:48 +03:30
|
|
|
import type { OnlineService } from "./online/service";
|
2026-06-04 11:49:19 +03:30
|
|
|
import { sound } from "./sound";
|
2026-06-04 10:11:00 +03:30
|
|
|
|
|
|
|
|
const KOT_POINTS = 2;
|
|
|
|
|
|
|
|
|
|
// Animation/pacing timings (ms) — UI matches these.
|
|
|
|
|
export const TIMING = {
|
|
|
|
|
hakemDraw: 1500,
|
|
|
|
|
aiTrump: 1000,
|
|
|
|
|
aiPlay: 850,
|
|
|
|
|
trickPause: 1150,
|
|
|
|
|
roundPause: 2600,
|
|
|
|
|
} as const;
|
|
|
|
|
|
2026-06-04 10:49:54 +03:30
|
|
|
/** How long a player has to act before the system plays for them. */
|
|
|
|
|
export const TURN_MS = 20000;
|
|
|
|
|
/** Grace period to wait for a disconnected player to return. */
|
|
|
|
|
export const RECONNECT_MS = 15000;
|
|
|
|
|
/** Per-turn chance an online opponent briefly drops (mock). */
|
|
|
|
|
const DISCONNECT_CHANCE = 0.07;
|
|
|
|
|
|
2026-06-04 10:11:00 +03:30
|
|
|
export type GameMode = "ai" | "online";
|
|
|
|
|
|
|
|
|
|
export interface SeatPlayer {
|
|
|
|
|
name: string;
|
|
|
|
|
avatar: string; // emoji
|
|
|
|
|
level: number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface GameSettings {
|
|
|
|
|
names: [string, string, string, string];
|
|
|
|
|
targetScore: number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface OnlineMatchConfig {
|
|
|
|
|
players: { displayName: string; avatar: string; level: number }[]; // index = seat
|
|
|
|
|
targetScore: number;
|
|
|
|
|
stake: number;
|
|
|
|
|
ranked: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface MatchTally {
|
|
|
|
|
tricksTeam0: number;
|
|
|
|
|
kotFor: boolean; // your team kot'd opponents at least once
|
|
|
|
|
kotAgainst: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface GameStore {
|
|
|
|
|
game: GameState;
|
|
|
|
|
started: boolean;
|
|
|
|
|
mode: GameMode;
|
|
|
|
|
seatPlayers: SeatPlayer[];
|
|
|
|
|
matchMeta: { ranked: boolean; stake: number };
|
|
|
|
|
tally: MatchTally;
|
|
|
|
|
|
2026-06-04 10:49:54 +03:30
|
|
|
/** epoch ms by which the current actor must act (for the turn-timer UI). */
|
|
|
|
|
turnDeadline: number | null;
|
|
|
|
|
/** a seat that has dropped and we're waiting on (online). */
|
|
|
|
|
disconnectedSeat: Seat | null;
|
|
|
|
|
reconnectDeadline: number | null;
|
|
|
|
|
|
2026-06-04 13:13:48 +03:30
|
|
|
/** true when the match is driven by the live SignalR server. */
|
|
|
|
|
live: boolean;
|
2026-06-04 17:32:47 +03:30
|
|
|
/** reward pushed by the server for a server-run (ranked) match. */
|
|
|
|
|
serverReward: RewardResult | null;
|
2026-06-04 20:58:05 +03:30
|
|
|
/** the match is still alive but the player navigated away (resumable). */
|
|
|
|
|
paused: boolean;
|
2026-06-04 13:13:48 +03:30
|
|
|
|
2026-06-04 10:11:00 +03:30
|
|
|
newMatch: (settings: GameSettings) => void;
|
|
|
|
|
newOnlineMatch: (cfg: OnlineMatchConfig) => void;
|
2026-06-04 13:13:48 +03:30
|
|
|
enterServerMatch: (service: OnlineService) => void;
|
|
|
|
|
applyServerState: (s: ServerGameState) => void;
|
2026-06-04 10:11:00 +03:30
|
|
|
chooseTrump: (suit: Suit) => void;
|
|
|
|
|
playHuman: (card: Card) => void;
|
2026-06-04 20:58:05 +03:30
|
|
|
/** Leave the table without ending the match — keeps it resumable. */
|
|
|
|
|
minimize: () => void;
|
|
|
|
|
/** Return to a minimized match (re-arms local AI timers; live keeps streaming). */
|
|
|
|
|
resume: () => void;
|
2026-06-04 10:11:00 +03:30
|
|
|
reset: () => void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const AI_AVATARS = ["🦊", "🦁", "🦉", "🐯"];
|
|
|
|
|
|
|
|
|
|
let pending: ReturnType<typeof setTimeout> | null = null;
|
2026-06-04 13:13:48 +03:30
|
|
|
let liveUnsub: (() => void) | null = null;
|
2026-06-04 17:32:47 +03:30
|
|
|
let rewardUnsub: (() => void) | null = null;
|
2026-06-04 13:13:48 +03:30
|
|
|
let liveSvc: OnlineService | null = null;
|
2026-06-04 10:11:00 +03:30
|
|
|
function clearPending() {
|
|
|
|
|
if (pending) {
|
|
|
|
|
clearTimeout(pending);
|
|
|
|
|
pending = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function freshTally(): MatchTally {
|
|
|
|
|
return { tricksTeam0: 0, kotFor: false, kotAgainst: false };
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-04 13:13:48 +03:30
|
|
|
function mapCard(c: { suit: string; rank: number; id: string }): Card {
|
|
|
|
|
return { suit: c.suit as Suit, rank: c.rank as Rank, id: c.id };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Map a server GameStateDto onto the client GameState. */
|
|
|
|
|
function mapServerState(s: ServerGameState): GameState {
|
|
|
|
|
const players: Player[] = s.players.map((p) => ({
|
|
|
|
|
seat: p.seat as Seat,
|
|
|
|
|
name: p.name,
|
|
|
|
|
isHuman: p.isHuman,
|
|
|
|
|
team: p.team as Team,
|
|
|
|
|
hand: p.hand
|
|
|
|
|
? p.hand.map(mapCard)
|
|
|
|
|
: Array.from({ length: p.handCount }, (_, i) => ({
|
|
|
|
|
suit: "spades" as Suit,
|
|
|
|
|
rank: 2 as Rank,
|
|
|
|
|
id: `hidden-${p.seat}-${i}`,
|
|
|
|
|
})),
|
|
|
|
|
}));
|
|
|
|
|
return {
|
|
|
|
|
phase: s.phase as Phase,
|
|
|
|
|
players,
|
|
|
|
|
deck: [],
|
|
|
|
|
hakem: s.hakem as Seat | null,
|
|
|
|
|
trump: (s.trump as Suit) ?? null,
|
|
|
|
|
turn: s.turn as Seat | null,
|
|
|
|
|
currentTrick: s.currentTrick.map((pc) => ({ seat: pc.seat as Seat, card: mapCard(pc.card) })),
|
|
|
|
|
leadSeat: s.leadSeat as Seat | null,
|
|
|
|
|
roundTricks: [s.roundTricks[0] ?? 0, s.roundTricks[1] ?? 0],
|
|
|
|
|
matchScore: [s.matchScore[0] ?? 0, s.matchScore[1] ?? 0],
|
|
|
|
|
lastTrickWinner: s.lastTrickWinner as Seat | null,
|
|
|
|
|
lastRoundResult: s.lastRoundResult
|
|
|
|
|
? {
|
|
|
|
|
winningTeam: s.lastRoundResult.winningTeam as Team,
|
|
|
|
|
tricks: [s.lastRoundResult.tricks[0], s.lastRoundResult.tricks[1]],
|
|
|
|
|
kot: s.lastRoundResult.kot,
|
|
|
|
|
points: s.lastRoundResult.points,
|
|
|
|
|
}
|
|
|
|
|
: null,
|
|
|
|
|
matchWinner: s.matchWinner as Team | null,
|
|
|
|
|
hakemDraw: s.hakemDraw.map((pc) => ({ seat: pc.seat as Seat, card: mapCard(pc.card) })),
|
|
|
|
|
targetScore: s.targetScore,
|
|
|
|
|
dealId: s.dealId,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-04 10:11:00 +03:30
|
|
|
export const useGameStore = create<GameStore>((set, get) => {
|
|
|
|
|
function recordRound(result: RoundResult | null) {
|
|
|
|
|
if (!result) return;
|
|
|
|
|
const t = get().tally;
|
|
|
|
|
set({
|
|
|
|
|
tally: {
|
|
|
|
|
tricksTeam0: t.tricksTeam0 + result.tricks[0],
|
|
|
|
|
kotFor: t.kotFor || (result.winningTeam === 0 && result.kot),
|
|
|
|
|
kotAgainst: t.kotAgainst || (result.winningTeam === 1 && result.kot),
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-04 10:49:54 +03:30
|
|
|
function playSeatAI(seat: Seat) {
|
|
|
|
|
const cur = get().game;
|
|
|
|
|
if (cur.phase !== "playing" || cur.turn !== seat) return;
|
|
|
|
|
const card = chooseCardAI(cur, seat);
|
|
|
|
|
set({ game: playCard(cur, seat, card) });
|
2026-06-04 11:49:19 +03:30
|
|
|
sound.play("cardPlay");
|
2026-06-04 10:49:54 +03:30
|
|
|
scheduleAuto();
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-04 10:11:00 +03:30
|
|
|
function scheduleAuto() {
|
|
|
|
|
clearPending();
|
|
|
|
|
const g = get().game;
|
|
|
|
|
|
|
|
|
|
switch (g.phase) {
|
|
|
|
|
case "selecting-hakem":
|
2026-06-04 10:49:54 +03:30
|
|
|
set({ turnDeadline: null, disconnectedSeat: null, reconnectDeadline: null });
|
2026-06-04 10:11:00 +03:30
|
|
|
pending = setTimeout(() => {
|
|
|
|
|
set({ game: dealForTrump(get().game) });
|
2026-06-04 11:49:19 +03:30
|
|
|
sound.play("deal");
|
2026-06-04 10:11:00 +03:30
|
|
|
scheduleAuto();
|
|
|
|
|
}, TIMING.hakemDraw);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case "choosing-trump": {
|
|
|
|
|
const hakem = g.hakem!;
|
2026-06-04 10:49:54 +03:30
|
|
|
if (g.players[hakem].isHuman) {
|
|
|
|
|
// human hakem: timed choice, system auto-picks on timeout
|
|
|
|
|
set({ turnDeadline: Date.now() + TURN_MS, disconnectedSeat: null, reconnectDeadline: null });
|
|
|
|
|
pending = setTimeout(() => {
|
|
|
|
|
const cur = get().game;
|
|
|
|
|
if (cur.phase !== "choosing-trump") return;
|
|
|
|
|
const suit = chooseTrumpAI(cur.players[cur.hakem!].hand);
|
|
|
|
|
set({ game: engineChooseTrump(cur, suit), turnDeadline: null });
|
|
|
|
|
scheduleAuto();
|
|
|
|
|
}, TURN_MS);
|
|
|
|
|
} else {
|
|
|
|
|
set({ turnDeadline: null });
|
2026-06-04 10:11:00 +03:30
|
|
|
pending = setTimeout(() => {
|
|
|
|
|
const cur = get().game;
|
|
|
|
|
const suit = chooseTrumpAI(cur.players[cur.hakem!].hand);
|
|
|
|
|
set({ game: engineChooseTrump(cur, suit) });
|
2026-06-04 11:49:19 +03:30
|
|
|
sound.play("trump");
|
2026-06-04 10:11:00 +03:30
|
|
|
scheduleAuto();
|
|
|
|
|
}, TIMING.aiTrump);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "playing": {
|
|
|
|
|
const seat = g.turn!;
|
2026-06-04 10:49:54 +03:30
|
|
|
if (g.players[seat].isHuman) {
|
|
|
|
|
// human turn: timed; system plays a smart legal move on timeout
|
|
|
|
|
set({ turnDeadline: Date.now() + TURN_MS, disconnectedSeat: null, reconnectDeadline: null });
|
2026-06-04 10:11:00 +03:30
|
|
|
pending = setTimeout(() => {
|
|
|
|
|
const cur = get().game;
|
2026-06-04 10:49:54 +03:30
|
|
|
if (cur.phase !== "playing" || cur.turn !== seat) return;
|
|
|
|
|
set({ game: playCard(cur, seat, chooseCardAI(cur, seat)), turnDeadline: null });
|
2026-06-04 11:49:19 +03:30
|
|
|
sound.play("cardPlay");
|
2026-06-04 10:11:00 +03:30
|
|
|
scheduleAuto();
|
2026-06-04 10:49:54 +03:30
|
|
|
}, TURN_MS);
|
|
|
|
|
} else {
|
|
|
|
|
const st = get();
|
|
|
|
|
if (
|
|
|
|
|
st.mode === "online" &&
|
|
|
|
|
st.disconnectedSeat == null &&
|
|
|
|
|
Math.random() < DISCONNECT_CHANCE
|
|
|
|
|
) {
|
|
|
|
|
// simulate this opponent dropping; wait for them, then they return
|
|
|
|
|
set({
|
|
|
|
|
turnDeadline: null,
|
|
|
|
|
disconnectedSeat: seat,
|
|
|
|
|
reconnectDeadline: Date.now() + RECONNECT_MS,
|
|
|
|
|
});
|
|
|
|
|
const back = Math.floor(RECONNECT_MS * (0.4 + Math.random() * 0.45));
|
|
|
|
|
pending = setTimeout(() => {
|
|
|
|
|
set({ disconnectedSeat: null, reconnectDeadline: null });
|
|
|
|
|
playSeatAI(seat);
|
|
|
|
|
}, back);
|
|
|
|
|
} else {
|
|
|
|
|
set({ turnDeadline: null });
|
|
|
|
|
pending = setTimeout(() => playSeatAI(seat), TIMING.aiPlay);
|
|
|
|
|
}
|
2026-06-04 10:11:00 +03:30
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "trick-complete":
|
2026-06-04 10:49:54 +03:30
|
|
|
set({ turnDeadline: null, disconnectedSeat: null, reconnectDeadline: null });
|
2026-06-04 11:49:19 +03:30
|
|
|
sound.play("trickWin");
|
2026-06-04 10:11:00 +03:30
|
|
|
pending = setTimeout(() => {
|
|
|
|
|
const next = advanceAfterTrick(get().game, KOT_POINTS);
|
|
|
|
|
set({ game: next });
|
2026-06-04 11:49:19 +03:30
|
|
|
if (next.phase === "match-over") {
|
|
|
|
|
recordRound(next.lastRoundResult);
|
|
|
|
|
sound.play(next.matchWinner === 0 ? "win" : "lose");
|
|
|
|
|
} else if (next.phase === "round-over" && next.lastRoundResult?.kot) {
|
|
|
|
|
sound.play("kot");
|
|
|
|
|
}
|
2026-06-04 10:11:00 +03:30
|
|
|
scheduleAuto();
|
|
|
|
|
}, TIMING.trickPause);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case "round-over":
|
2026-06-04 10:49:54 +03:30
|
|
|
set({ turnDeadline: null, disconnectedSeat: null, reconnectDeadline: null });
|
2026-06-04 10:11:00 +03:30
|
|
|
pending = setTimeout(() => {
|
|
|
|
|
recordRound(get().game.lastRoundResult);
|
|
|
|
|
set({ game: startNextRound(get().game) });
|
|
|
|
|
scheduleAuto();
|
|
|
|
|
}, TIMING.roundPause);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
2026-06-04 10:49:54 +03:30
|
|
|
set({ turnDeadline: null });
|
2026-06-04 10:11:00 +03:30
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
game: createInitialState({ names: ["", "", "", ""], targetScore: 7 }),
|
|
|
|
|
started: false,
|
|
|
|
|
mode: "ai",
|
|
|
|
|
seatPlayers: [],
|
|
|
|
|
matchMeta: { ranked: false, stake: 0 },
|
|
|
|
|
tally: freshTally(),
|
2026-06-04 10:49:54 +03:30
|
|
|
turnDeadline: null,
|
|
|
|
|
disconnectedSeat: null,
|
|
|
|
|
reconnectDeadline: null,
|
2026-06-04 13:13:48 +03:30
|
|
|
live: false,
|
2026-06-04 17:32:47 +03:30
|
|
|
serverReward: null,
|
2026-06-04 20:58:05 +03:30
|
|
|
paused: false,
|
2026-06-04 10:11:00 +03:30
|
|
|
|
|
|
|
|
newMatch: (settings) => {
|
|
|
|
|
clearPending();
|
2026-06-04 11:49:19 +03:30
|
|
|
sound.init();
|
2026-06-04 10:11:00 +03:30
|
|
|
const initial = createInitialState(settings);
|
|
|
|
|
set({
|
|
|
|
|
game: selectHakem(initial),
|
|
|
|
|
started: true,
|
|
|
|
|
mode: "ai",
|
2026-06-04 20:58:05 +03:30
|
|
|
paused: false,
|
2026-06-04 10:11:00 +03:30
|
|
|
matchMeta: { ranked: false, stake: 0 },
|
|
|
|
|
tally: freshTally(),
|
2026-06-04 10:49:54 +03:30
|
|
|
turnDeadline: null,
|
|
|
|
|
disconnectedSeat: null,
|
|
|
|
|
reconnectDeadline: null,
|
2026-06-04 10:11:00 +03:30
|
|
|
seatPlayers: settings.names.map((name, i) => ({
|
|
|
|
|
name,
|
|
|
|
|
avatar: AI_AVATARS[i],
|
|
|
|
|
level: 0,
|
|
|
|
|
})),
|
|
|
|
|
});
|
|
|
|
|
scheduleAuto();
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
newOnlineMatch: (cfg) => {
|
|
|
|
|
clearPending();
|
2026-06-04 11:49:19 +03:30
|
|
|
sound.init();
|
2026-06-04 10:11:00 +03:30
|
|
|
const names = cfg.players.map((p) => p.displayName) as GameSettings["names"];
|
|
|
|
|
const initial = createInitialState({ names, targetScore: cfg.targetScore });
|
|
|
|
|
set({
|
|
|
|
|
game: selectHakem(initial),
|
|
|
|
|
started: true,
|
|
|
|
|
mode: "online",
|
2026-06-04 20:58:05 +03:30
|
|
|
paused: false,
|
2026-06-04 10:11:00 +03:30
|
|
|
matchMeta: { ranked: cfg.ranked, stake: cfg.stake },
|
|
|
|
|
tally: freshTally(),
|
2026-06-04 10:49:54 +03:30
|
|
|
turnDeadline: null,
|
|
|
|
|
disconnectedSeat: null,
|
|
|
|
|
reconnectDeadline: null,
|
2026-06-04 10:11:00 +03:30
|
|
|
seatPlayers: cfg.players.map((p) => ({
|
|
|
|
|
name: p.displayName,
|
|
|
|
|
avatar: avatarEmoji(p.avatar),
|
|
|
|
|
level: p.level,
|
|
|
|
|
})),
|
|
|
|
|
});
|
|
|
|
|
scheduleAuto();
|
|
|
|
|
},
|
|
|
|
|
|
2026-06-04 13:13:48 +03:30
|
|
|
enterServerMatch: (service) => {
|
|
|
|
|
clearPending();
|
|
|
|
|
sound.init();
|
|
|
|
|
liveSvc = service;
|
|
|
|
|
if (liveUnsub) liveUnsub();
|
2026-06-04 17:32:47 +03:30
|
|
|
if (rewardUnsub) rewardUnsub();
|
2026-06-04 13:13:48 +03:30
|
|
|
liveUnsub = service.onState((s) => get().applyServerState(s));
|
2026-06-04 17:32:47 +03:30
|
|
|
rewardUnsub = service.onReward((r) => set({ serverReward: r }));
|
2026-06-04 13:13:48 +03:30
|
|
|
set({
|
|
|
|
|
game: createInitialState({ names: ["شما", "", "", ""], targetScore: 7 }),
|
|
|
|
|
started: true,
|
|
|
|
|
mode: "online",
|
|
|
|
|
live: true,
|
2026-06-04 17:32:47 +03:30
|
|
|
serverReward: null,
|
2026-06-04 20:58:05 +03:30
|
|
|
paused: false,
|
2026-06-04 13:13:48 +03:30
|
|
|
matchMeta: { ranked: true, stake: 0 },
|
|
|
|
|
tally: freshTally(),
|
|
|
|
|
turnDeadline: null,
|
|
|
|
|
disconnectedSeat: null,
|
|
|
|
|
reconnectDeadline: null,
|
|
|
|
|
seatPlayers: [],
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
applyServerState: (s) => {
|
|
|
|
|
const prev = get().game;
|
|
|
|
|
const next = mapServerState(s);
|
|
|
|
|
const seatPlayers: SeatPlayer[] = [...s.seatPlayers]
|
|
|
|
|
.sort((a, b) => a.seat - b.seat)
|
|
|
|
|
.map((sp) => ({ name: sp.name, avatar: avatarEmoji(sp.avatar), level: sp.level }));
|
|
|
|
|
|
|
|
|
|
// accumulate the reward tally when the match score grows (a round ended)
|
|
|
|
|
const prevTotal = prev.matchScore[0] + prev.matchScore[1];
|
|
|
|
|
const newTotal = next.matchScore[0] + next.matchScore[1];
|
|
|
|
|
if (newTotal > prevTotal && next.lastRoundResult) recordRound(next.lastRoundResult);
|
|
|
|
|
|
|
|
|
|
// sounds on transitions
|
|
|
|
|
if (next.currentTrick.length > prev.currentTrick.length && next.currentTrick.length > 0)
|
|
|
|
|
sound.play("cardPlay");
|
|
|
|
|
if (next.phase === "trick-complete" && prev.phase !== "trick-complete") sound.play("trickWin");
|
|
|
|
|
if (next.trump && !prev.trump) sound.play("trump");
|
|
|
|
|
if (next.phase === "match-over" && prev.phase !== "match-over")
|
|
|
|
|
sound.play(next.matchWinner === 0 ? "win" : "lose");
|
|
|
|
|
else if (next.phase === "round-over" && prev.phase !== "round-over" && next.lastRoundResult?.kot)
|
|
|
|
|
sound.play("kot");
|
|
|
|
|
|
|
|
|
|
set({
|
|
|
|
|
game: next,
|
|
|
|
|
seatPlayers,
|
|
|
|
|
matchMeta: { ranked: s.ranked, stake: s.stake },
|
|
|
|
|
turnDeadline: s.turnDeadline ?? null,
|
|
|
|
|
disconnectedSeat: (s.disconnectedSeat ?? null) as Seat | null,
|
|
|
|
|
reconnectDeadline: s.disconnectedSeat != null ? Date.now() + RECONNECT_MS : null,
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
2026-06-04 10:11:00 +03:30
|
|
|
chooseTrump: (suit) => {
|
2026-06-04 13:13:48 +03:30
|
|
|
if (get().live) {
|
|
|
|
|
liveSvc?.chooseTrump(suit);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-06-04 10:11:00 +03:30
|
|
|
const g = get().game;
|
|
|
|
|
if (g.phase !== "choosing-trump") return;
|
2026-06-04 10:49:54 +03:30
|
|
|
set({ game: engineChooseTrump(g, suit), turnDeadline: null });
|
2026-06-04 11:49:19 +03:30
|
|
|
sound.play("trump");
|
2026-06-04 10:11:00 +03:30
|
|
|
scheduleAuto();
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
playHuman: (card) => {
|
2026-06-04 13:13:48 +03:30
|
|
|
if (get().live) {
|
|
|
|
|
liveSvc?.playCard(card.id);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-06-04 10:11:00 +03:30
|
|
|
const g = get().game;
|
|
|
|
|
if (g.phase !== "playing" || g.turn !== 0) return;
|
2026-06-04 10:49:54 +03:30
|
|
|
set({ game: playCard(g, 0, card), turnDeadline: null });
|
2026-06-04 11:49:19 +03:30
|
|
|
sound.play("cardPlay");
|
2026-06-04 10:11:00 +03:30
|
|
|
scheduleAuto();
|
|
|
|
|
},
|
|
|
|
|
|
2026-06-04 20:58:05 +03:30
|
|
|
minimize: () => {
|
|
|
|
|
// Keep the match alive and resumable. Single-player (AI) games pause their
|
|
|
|
|
// local timers so nothing happens while you're away; live (server-run)
|
|
|
|
|
// games keep streaming into the store via the still-active subscription.
|
|
|
|
|
if (!get().live) {
|
|
|
|
|
clearPending();
|
|
|
|
|
set({ turnDeadline: null, reconnectDeadline: null });
|
|
|
|
|
}
|
|
|
|
|
set({ paused: true });
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
resume: () => {
|
|
|
|
|
set({ paused: false });
|
|
|
|
|
// Re-arm the local driver for AI games; live games are already up to date.
|
|
|
|
|
if (!get().live && get().started && get().game.phase !== "match-over") scheduleAuto();
|
|
|
|
|
},
|
|
|
|
|
|
2026-06-04 10:11:00 +03:30
|
|
|
reset: () => {
|
|
|
|
|
clearPending();
|
2026-06-04 13:13:48 +03:30
|
|
|
if (liveUnsub) {
|
|
|
|
|
liveUnsub();
|
|
|
|
|
liveUnsub = null;
|
|
|
|
|
}
|
2026-06-04 17:32:47 +03:30
|
|
|
if (rewardUnsub) {
|
|
|
|
|
rewardUnsub();
|
|
|
|
|
rewardUnsub = null;
|
|
|
|
|
}
|
2026-06-04 13:13:48 +03:30
|
|
|
liveSvc = null;
|
2026-06-04 10:11:00 +03:30
|
|
|
set({
|
|
|
|
|
game: createInitialState({ names: ["", "", "", ""], targetScore: 7 }),
|
|
|
|
|
started: false,
|
|
|
|
|
mode: "ai",
|
2026-06-04 13:13:48 +03:30
|
|
|
live: false,
|
2026-06-04 17:32:47 +03:30
|
|
|
serverReward: null,
|
2026-06-04 20:58:05 +03:30
|
|
|
paused: false,
|
2026-06-04 10:11:00 +03:30
|
|
|
seatPlayers: [],
|
|
|
|
|
tally: freshTally(),
|
2026-06-04 10:49:54 +03:30
|
|
|
turnDeadline: null,
|
|
|
|
|
disconnectedSeat: null,
|
|
|
|
|
reconnectDeadline: null,
|
2026-06-04 10:11:00 +03:30
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
});
|