-
Notifications
You must be signed in to change notification settings - Fork 0
/
Demo.tsx
186 lines (168 loc) · 6.02 KB
/
Demo.tsx
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
import { yDoc, useProviderInit, usePresence, useYAwarenessInit, useYAwareness } from "@/store";
import { Game, Player } from "@/types";
import { getRandomColor, makeGame, getSaturedColor, throttle } from "@/utils";
import { useYArray } from "jotai-yjs";
import {
Box,
Button,
Center,
chakra,
Circle,
CloseButton,
Editable,
EditableInput,
EditablePreview,
EditableProps,
Flex,
SimpleGrid,
Spinner,
Stack,
} from "@chakra-ui/react";
import { removeItemMutate } from "@pastable/core";
import { useSnapshot } from "valtio";
export const Demo = () => {
useProviderInit();
useYAwarenessInit();
const gamesSource = useYArray<Game>(yDoc, "games");
const games = useSnapshot(gamesSource);
const [presence, setPresence] = usePresence();
const makeNewGame = () => gamesSource.push(makeGame(presence));
const updateName = (username: Player["username"]) => setPresence((player) => ({ ...player, username }));
const updateColor = (color: Player["color"]) => setPresence((player) => ({ ...player, color }));
const handleUpdateColor = throttle((e) => updateColor(e.target.value), 1000);
const updateRandomColor = () => setPresence((player) => ({ ...player, color: getRandomColor() }));
if (!presence) {
return (
<Center>
<Spinner />
</Center>
);
}
return (
<Stack w="100%">
<Center flexDir="column" m="8">
<Stack h="100%">
<Stack direction="row" alignItems="center">
<chakra.span>(Editable) Username: </chakra.span>
<EditableName defaultValue={presence.username} onSubmit={updateName} />
</Stack>
<input type="color" onChange={handleUpdateColor} />
<Button onClick={updateRandomColor}>Random color</Button>
<Button onClick={makeNewGame}>New game</Button>
</Stack>
</Center>
<SimpleGrid columns={[1, 1, 2, 3, 3, 4]} w="100%" spacing="8">
{games.map((game: Game, gameIndex) => {
const gameSrc = gamesSource[gameIndex];
const gameId = game.id;
return <DuelGameWidget key={gameId} game={gameSrc} />;
})}
</SimpleGrid>
<PlayerList />
</Stack>
);
};
const PlayerList = () => {
const awareness = useYAwareness();
const players = Array.from(awareness.entries()).filter(([_id, player]) => player.id);
return (
<Box pos="fixed" top="100px" right="0">
<Stack>
{players.map(([id, presence]) => (
<Box key={id} py="2" px="4" w="150px" bgColor={presence.color} pos="relative">
<Box
pos="absolute"
top="0"
right="100%"
h="100%"
w="20px"
bgColor={getSaturedColor(presence.color)}
/>
<chakra.span color="black">{presence.username}</chakra.span>
</Box>
))}
</Stack>
</Box>
);
};
const DuelGameWidget = ({ game }) => {
const gameSnap = useSnapshot<Game>(game);
const [hostPlayer, opponentPlayer] = gameSnap.players || [];
const gamesSource = useYArray<Game>(yDoc, "games");
const deleteGame = () => removeItemMutate(gamesSource, "id", game.id);
const [presence] = usePresence();
const joinGame = () => game.players.push(presence);
const isHost = presence.id === hostPlayer.id;
return (
<Flex bgColor="gray.400" w="100%" h="200px" p="15px" rounded={8} pos="relative">
{<CloseButton pos="absolute" bottom="100%" left="100%" bgColor="gray.100" onClick={deleteGame} />}
<PlayerSlot>
<PlayerSlotContent player={hostPlayer} />
</PlayerSlot>
<Center w="80px" flexShrink={0}>
<VsCircle />
</Center>
<PlayerSlot>
{opponentPlayer ? (
<PlayerSlotContent player={opponentPlayer} />
) : isHost ? (
<PlayerSlotWaitingForOpponent />
) : (
<PlayerSlotJoinGame onJoin={joinGame} />
)}
</PlayerSlot>
</Flex>
);
};
const PlayerSlot = ({ children }) => (
<Box w="100%" bgColor="gray.600" rounded={8}>
{children}
</Box>
);
const PlayerSlotContent = ({ player }: { player: Player }) => {
return (
<Stack justifyContent="center" alignItems="center" h="100%" spacing="1">
<Circle size={"65px"} bgColor="gray.300" />
<chakra.span textTransform="uppercase" color="gray.300">
{player.username}
</chakra.span>
<chakra.span textTransform="uppercase" color="gray.300" fontSize="small">
{player.elo} ELO
</chakra.span>
</Stack>
);
};
const PlayerSlotJoinGame = ({ onJoin }) => {
return (
<Center h="100%">
<Button colorScheme="yellow" onClick={onJoin}>
Join game
</Button>
</Center>
);
};
const PlayerSlotWaitingForOpponent = () => {
return (
<Center h="100%">
<Button colorScheme="yellow" disabled h="50px" mx="4" fontSize="sm">
Waiting for
<br /> an opponent...
</Button>
</Center>
);
};
const VsCircle = () => (
<Circle size={"40px"} bgColor="gray.300">
<chakra.span textTransform="uppercase" color="gray.900" fontSize="small">
VS
</chakra.span>
</Circle>
);
const EditableName = (props: EditableProps) => {
return (
<Editable {...props} textTransform="uppercase">
<EditablePreview />
<EditableInput w="12ch" textTransform="uppercase" textAlign="center" />
</Editable>
);
};