improving game
This commit is contained in:
@@ -8,13 +8,16 @@ const PlayerSchema = z.object({
|
||||
y: z.number(),
|
||||
});
|
||||
|
||||
const GameStateSchema = z.record(z.string(), PlayerSchema);
|
||||
const GameStateSchema = z.object({
|
||||
players: z.record(z.string(), PlayerSchema),
|
||||
tick_number: z.number(),
|
||||
});
|
||||
|
||||
type GameState = z.infer<typeof GameStateSchema>;
|
||||
type Player = z.infer<typeof PlayerSchema>;
|
||||
|
||||
export const BoardDisplay = ({ playerName }: { playerName: string }) => {
|
||||
const { channel, isJoined, joinGame } = useGameChannelContext();
|
||||
const [players, setPlayers] = useState<GameState>({});
|
||||
const [players, setPlayers] = useState<Record<string, Player>>({});
|
||||
|
||||
useEffect(() => {
|
||||
if (channel && !isJoined) {
|
||||
@@ -25,18 +28,15 @@ export const BoardDisplay = ({ playerName }: { playerName: string }) => {
|
||||
useEffect(() => {
|
||||
if (!channel) return;
|
||||
|
||||
const ref = channel.on(
|
||||
"game_state",
|
||||
(payload: { game_state: unknown }) => {
|
||||
const result = GameStateSchema.safeParse(payload.game_state);
|
||||
if (result.success) {
|
||||
setPlayers(result.data);
|
||||
} else {
|
||||
console.error("Invalid game state received:", result.error);
|
||||
setPlayers({});
|
||||
}
|
||||
},
|
||||
);
|
||||
const ref = channel.on("game_state", (payload: { game_state: unknown }) => {
|
||||
const result = GameStateSchema.safeParse(payload.game_state);
|
||||
if (result.success) {
|
||||
setPlayers(result.data.players);
|
||||
} else {
|
||||
console.error("Invalid game state received:", result.error);
|
||||
setPlayers({});
|
||||
}
|
||||
});
|
||||
|
||||
return () => {
|
||||
channel.off("game_state", ref);
|
||||
|
||||
@@ -4,7 +4,6 @@ export const SessionOverride: FC = () => {
|
||||
return (
|
||||
<div className="fixed inset-0 bg-navy-900/95 flex items-center justify-center z-50">
|
||||
<div className="bg-navy-800 border-2 border-accent-600 rounded-lg p-8 max-w-md text-center">
|
||||
<div className="text-6xl mb-4">⚠️</div>
|
||||
<h2 className="text-2xl font-bold text-accent-500 mb-4">
|
||||
Session Overridden
|
||||
</h2>
|
||||
|
||||
Reference in New Issue
Block a user