Skip to content

Commit e8cdd30

Browse files
committed
wip game requests
1 parent af36a61 commit e8cdd30

File tree

3 files changed

+125
-1
lines changed

3 files changed

+125
-1
lines changed

src/main/client/src/feature/lobby/Lobby.jsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ import {
2929
import {
3030
ActiveGames,
3131
} from "./ActiveGames.jsx"
32+
import {
33+
Requests,
34+
} from "./Requests.jsx"
3235
import {
3336
useAuthStore,
3437
} from "src/store.js"
@@ -49,6 +52,7 @@ import {
4952
const detailData = [
5053
["open", "Open Games"],
5154
["active", "Active Games"],
55+
["requests", "Game Requests"],
5256
]
5357

5458
export function Lobby() {
@@ -118,6 +122,9 @@ export function Lobby() {
118122
{detail === "active" && (
119123
<ActiveGames />
120124
)}
125+
{detail === "requests" && (
126+
<Requests lobbyState={lobbyState} />
127+
)}
121128
</div>
122129
<LobbyPanel />
123130
</div>
@@ -202,6 +209,7 @@ function NewGameDialog({zNewGame, lobbyState, setLobbyState, onNewGame, onStartE
202209
</form>
203210
)
204211
}
212+
205213
function DetailNavigation({detail, setDetail}) {
206214
return (
207215
<div className={twJoin(
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import {
2+
useRef,
3+
useState,
4+
useEffect,
5+
useContext,
6+
} from "react"
7+
import {
8+
twJoin,
9+
} from "tailwind-merge"
10+
import {
11+
useNavigate,
12+
} from "react-router-dom"
13+
import {
14+
base,
15+
StompContext,
16+
tfetch,
17+
doTry,
18+
} from "src/util.js"
19+
import {
20+
useAuthStore,
21+
} from "src/store.js"
22+
23+
export function Requests({lobbyState}) {
24+
let [requests, setRequests] = useState([])
25+
let openGameId = lobbyState.openGameId
26+
let stompClient = useContext(StompContext)
27+
let navigate = useNavigate()
28+
let auth = useAuthStore(state => state.auth)
29+
let initialized = useRef()
30+
useEffect(() => {
31+
if (initialized.current) {
32+
return
33+
}
34+
initialized.current = true
35+
doTry(async () => {
36+
let r = await tfetch("/api/lobby/requests", {
37+
headers: {
38+
"Authorization": "Bearer " + auth.token,
39+
},
40+
})
41+
setRequests(r.requests.filter(request => request.gameId === openGameId))
42+
})
43+
let sub1 = stompClient.subscribe("/topic/lobby/requests", (message) => {
44+
let r = JSON.parse(message.body)
45+
setRequests(r.requests.filter(request => request.gameId === openGameId))
46+
})
47+
return () => {
48+
sub1.unsubscribe()
49+
}
50+
}, [auth, initialized, stompClient, navigate, openGameId])
51+
if (!openGameId) {
52+
return <div />
53+
}
54+
return (
55+
<div>
56+
<div className="grid grid-cols-[max-content_max-content_max-content_max-content]">
57+
{requests.map((request) => (
58+
<Request
59+
lobbyState={lobbyState}
60+
request={request}
61+
key={request.id} />
62+
))}
63+
</div>
64+
</div>
65+
)
66+
}
67+
68+
function Request({lobbyState, request}) {
69+
let navigate = useNavigate()
70+
let auth = useAuthStore(state => state.auth)
71+
let openGameId = lobbyState.openGameId
72+
let classes = twJoin(
73+
"contents",
74+
"*:py-3",
75+
"cursor-pointer *:hover:bg-sky-200 *:hover:text-black",
76+
)
77+
return (
78+
<div
79+
onClick={() => {
80+
doTry(async () => {
81+
let r = await tfetch("/api/lobby/start", {
82+
headers: {
83+
"method": "POST",
84+
"Authorization": "Bearer " + auth.token,
85+
},
86+
body: JSON.stringify({
87+
gameId: openGameId,
88+
request: request.id,
89+
}),
90+
})
91+
navigate(base + "/game/" + r.id)
92+
})
93+
}}
94+
className={classes}
95+
key={request.id}>
96+
<div className="pl-3 pr-1 rounded-l-lg">
97+
{request.white}
98+
</div>
99+
<div className="px-1">
100+
{request.black}
101+
</div>
102+
<div className="px-1">
103+
{request.dim}x{request.dim}
104+
</div>
105+
<div className="pl-1 pr-3 rounded-r-lg">
106+
H{request.handi}
107+
</div>
108+
</div>
109+
)
110+
}

src/main/client/src/feature/lobby/lobbyState.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,16 @@ import {
55
export function initialState() {
66
return {
77
stack: [], // newgame, accept
8-
proposals: [],
8+
openGameId: "",
99
}
1010
}
1111

12+
export function setOpenGameId(state, openGameId) {
13+
return produce(state, draft => {
14+
draft.openGameId = openGameId
15+
})
16+
}
17+
1218
function setOpen(state, el, kind, data) {
1319
if (!el) {
1420
return state

0 commit comments

Comments
 (0)