Files
bridzik/frontend/src/store/gameStore.ts
T

92 lines
2.6 KiB
TypeScript

import { create } from 'zustand';
import type {
Account,
GameDetail,
GameInfo,
GameStatusPayload,
Hand,
HistoryGame,
MyPlayer,
Registration,
} from '../types';
interface GameStore {
games: GameInfo[];
onlineCount: number;
availableCount: number;
account: Account | null;
registration: Registration | null;
history: HistoryGame[];
gameDetail: GameDetail | null;
myPlayer: MyPlayer | null;
gameStatus: GameStatusPayload | null;
hand: Hand;
error: string | null;
setGames: (games: GameInfo[]) => void;
setOnlineCount: (count: number) => void;
setAvailableCount: (count: number) => void;
setAccount: (account: Account | null) => void;
setRegistration: (registration: Registration | null) => void;
setHistory: (history: HistoryGame[]) => void;
setGameDetail: (detail: GameDetail | null) => void;
setMyPlayer: (player: MyPlayer | null) => void;
setGameStatus: (status: GameStatusPayload) => void;
setHand: (hand: Hand) => void;
setError: (error: string | null) => void;
clearError: () => void;
updatePlayerConnection: (order: number, connected: boolean) => void;
reset: () => void;
logout: () => void;
}
export const useGameStore = create<GameStore>((set) => ({
games: [],
onlineCount: 0,
availableCount: 0,
account: null,
registration: null,
history: [],
gameDetail: null,
myPlayer: null,
gameStatus: null,
hand: {},
error: null,
setGames: (games) => set({ games }),
setOnlineCount: (onlineCount) => set({ onlineCount }),
setAvailableCount: (availableCount) => set({ availableCount }),
setAccount: (account) => set({ account }),
setRegistration: (registration) => set({ registration }),
setHistory: (history) => set({ history }),
setGameDetail: (gameDetail) => set({ gameDetail }),
setMyPlayer: (myPlayer) => set({ myPlayer }),
setGameStatus: (gameStatus) => set({ gameStatus }),
setHand: (hand) => set({ hand }),
setError: (error) => set({ error }),
clearError: () => set({ error: null }),
updatePlayerConnection: (order, connected) =>
set((state) => ({
gameStatus: state.gameStatus
? {
...state.gameStatus,
players: state.gameStatus.players.map((p) =>
p.order === order ? { ...p, connected } : p
),
}
: null,
})),
reset: () => set({ myPlayer: null, gameStatus: null, hand: {}, error: null }),
logout: () =>
set({
account: null,
registration: null,
history: [],
gameDetail: null,
myPlayer: null,
gameStatus: null,
hand: {},
error: null,
}),
}));