Skip to content

Commit 5b2fbea

Browse files
author
Christopher Strauss
committed
setting timeout
1 parent 623c662 commit 5b2fbea

File tree

10 files changed

+90
-25
lines changed

10 files changed

+90
-25
lines changed

src/main/client/src/feature/game/state.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export function initialState() {
3535
viewPos: Number.NaN,
3636
winnerByTime: 0,
3737
dim: 0,
38+
timesetting: 0,
3839
handicap: 0,
3940
queueStatus: "behind",
4041
black: "",
@@ -272,6 +273,7 @@ export function createGameState(game, auth) {
272273
black: game.black,
273274
white: game.white,
274275
dim: game.dim,
276+
timesetting: game.timesetting,
275277
handicap: game.handicap,
276278
counting: counting,
277279
baseBoard: baseBoard,

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

Lines changed: 76 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,20 @@ export function Lobby() {
4949
let stompClient = useContext(StompContext)
5050
let navigate = useNavigate()
5151
let auth = useAuthStore(state => state.auth)
52+
let newGameRef = useRef()
53+
let onClickOutside = useCallback((event) => {
54+
if (!isNewGameOpen) {
55+
return
56+
}
57+
let el = newGameRef.current
58+
if (!el) {
59+
return
60+
}
61+
let rect = el.getBoundingClientRect()
62+
if (event.clientX > rect.right || event.clientX < rect.left || event.clientY > rect.bottom || event.clientY < rect.top) {
63+
setNewGameOpen(false)
64+
}
65+
}, [isNewGameOpen] )
5266
let onNewGame = useCallback((d) => doTry(async () => {
5367
let response = await tfetch("/api/create", {
5468
method: "POST",
@@ -77,28 +91,31 @@ export function Lobby() {
7791
navigate(base + "/game/" + response.id)
7892
}), [auth, navigate])
7993
return (
80-
<div className="h-full">
94+
<div onClick={onClickOutside} className="h-full">
8195
<div className={twJoin(
82-
"mt-2 inline-flex py-2 pr-4 gap-x-1 border-r-2 border-y-2",
83-
isNewGameOpen && "rounded-r-full border-slate-600",
96+
"mt-2 py-2 pr-4 gap-x-1",
97+
isNewGameOpen && "",
8498
!isNewGameOpen && "border-transparent",
8599
)}>
86100
{isNewGameOpen ? (
87101
<NewGameDialog
102+
onRef={ref => newGameRef.current = ref}
88103
onNewGame={onNewGame}
89104
onStartEdit={onStartEdit}
90105
setNewGameOpen={setNewGameOpen} />
91-
) : (
92-
<button className={twJoin(
106+
) : "" }
107+
<button disabled={isNewGameOpen} className={!isNewGameOpen ? twJoin(
93108
"ml-2 border-2 border-transparent px-4 py-2 rounded-lg",
94109
"hover:border-sky-700",
110+
) : twJoin(
111+
"ml-2 border-2 bg-gray-400 border-transparent px-4 py-2 rounded-lg",
95112
)}
96113
onClick={() => {
97114
setNewGameOpen(true)
98115
}}>
99116
New Game
100117
</button>
101-
)}
118+
102119
</div>
103120
<div className="mt-2 grid gap-x-4 grid-cols-[max-content_auto]">
104121
<DetailNavigation detail={detail} setDetail={setDetail} />
@@ -114,33 +131,70 @@ export function Lobby() {
114131
)
115132
}
116133

117-
function NewGameDialog({onNewGame, onStartEdit, setNewGameOpen}) {
134+
function NewGameDialog({onNewGame, onStartEdit, setNewGameOpen, onRef}) {
118135
let dimRef = useRef(9)
136+
let timeRef = useRef(0)
119137
let [edit, setEdit] = useState(false)
120138
return (
121139
<form className="contents" onSubmit={(e) => {
122140
e.preventDefault()
123-
let game = {dim: dimRef.current}
141+
let game = {dim: dimRef.current, timesetting: timeRef.current}
124142
if (edit) {
125143
onStartEdit(game)
126144
} else {
127145
onNewGame(game)
128146
}
129147
}}>
130-
<Button type="submit" className="ml-2">OK</Button>
131-
<input id="dim-9" type="radio" name="dim" value="9" className="ml-2" defaultChecked={true} onClick={() => dimRef.current = 9} />
132-
<label htmlFor="dim-9" className="pt-[0.625rem] pr-1" >9x9</label>
133-
<input id="dim-13" type="radio" name="dim" value="13" onClick={() => dimRef.current = 13} />
134-
<label htmlFor="dim-13" className="pt-[0.625rem] pr-1">13x13</label>
135-
<input id="dim-19" type="radio" name="dim" value="19" onClick={() => dimRef.current = 19} />
136-
<label htmlFor="dim-19" className="pt-[0.625rem] pr-1">19x19</label>
137-
<input id="cb-edit" type="checkbox" name="edit" checked={edit} onChange={() => setEdit(!edit)} />
138-
<label htmlFor="cb-edit" className="pt-[0.625rem] ml-1">Edit</label>
139-
<button onClick={() => setNewGameOpen(false)} className="ml-1 text-stone-100 hover:text-stone-300">
140-
<IconContext.Provider value={{ size: "1.25em" }}>
141-
<CgClose />
142-
</IconContext.Provider>
143-
</button>
148+
<div className="flex justify-start">
149+
<div ref={onRef} className="absolute ml-40 bg-slate-800 w-96 border-2 border-slate-600 rounded-lg">
150+
<div className="absolute top-1 right-0 mr-2">
151+
<button onClick={() => setNewGameOpen(false)} className="ml-1 text-stone-100 hover:text-stone-300">
152+
<IconContext.Provider value={{ size: "1.25em" }}>
153+
<CgClose />
154+
</IconContext.Provider>
155+
</button>
156+
</div>
157+
<div className="grid grid-cols-3 gap-1">
158+
<div className="col-span-3 pt-3">
159+
<label className="italic text-gray-400 ml-2">Dimension</label>
160+
</div>
161+
<div>
162+
<input id="dim-9" type="radio" name="dim" value="9" className="ml-2" defaultChecked={true} onClick={() => dimRef.current = 9} />
163+
<label htmlFor="dim-9" className="pt-[0.625rem] pr-1" >9x9</label>
164+
</div>
165+
<div>
166+
<input id="dim-13" type="radio" name="dim" value="13" onClick={() => dimRef.current = 13} />
167+
<label htmlFor="dim-13" className="pt-[0.625rem] pr-1">13x13</label>
168+
</div>
169+
<div>
170+
<input id="dim-19" type="radio" name="dim" value="19" onClick={() => dimRef.current = 19} />
171+
<label htmlFor="dim-19" className="pt-[0.625rem] pr-5">19x19</label>
172+
</div>
173+
<div className="col-span-3">
174+
<label className="italic text-gray-400 ml-2">Time</label>
175+
</div>
176+
<div>
177+
<input id="time-0" type="radio" name="time" value="0" className="ml-2" defaultChecked={true} onClick={() => timeRef.current = 0} />
178+
<label htmlFor="time-0" className="pt-[0.625rem] pr-1" >0s</label>
179+
</div>
180+
<div>
181+
<input id="time-10" type="radio" name="time" value="10" onClick={() => timeRef.current = 10} />
182+
<label htmlFor="time-10" className="pt-[0.625rem] pr-1">10s</label>
183+
</div>
184+
<div>
185+
<input id="time-30" type="radio" name="time" value="30" onClick={() => timeRef.current = 30} />
186+
<label htmlFor="time-30" className="pt-[0.625rem] pr-1">30s</label>
187+
</div>
188+
</div>
189+
<div className="flex flex-row w-full pt-2 pr-2 pb-2">
190+
<input className="ml-2" id="cb-edit" type="checkbox" name="edit" checked={edit} onChange={() => setEdit(!edit)} />
191+
<label htmlFor="cb-edit" className="pt-[0.625rem] ml-1">Edit</label>
192+
<div className="grow" />
193+
<Button type="submit" className="ml-2">OK</Button>
194+
</div>
195+
</div>
196+
</div>
197+
144198
</form>
145199
)
146200
}

src/main/java/com/bernd/LobbyController.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public ViewGame startEdit(@RequestBody MatchRequest request) {
7272
false,
7373
createEmptyBoard(request.dim()),
7474
request.dim(),
75+
request.timesetting(),
7576
request.handicap(),
7677
new int[]{-1, -1},
7778
MoveList.create(request.dim())));

src/main/java/com/bernd/model/Game.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public record Game(
2020
boolean counting,
2121
int[][] board,
2222
int dim,
23+
int timesetting,
2324
int handicap,
2425
int[] forbidden,
2526
MoveList moves

src/main/java/com/bernd/model/GameBuilder.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public Game build() {
4646
counting,
4747
board,
4848
game.dim(),
49+
game.timesetting(),
4950
game.handicap(),
5051
forbidden,
5152
game.moves()

src/main/java/com/bernd/model/MatchRequest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
public record MatchRequest(
44
int dim,
5+
int timesetting,
56
boolean editMode,
67
int handicap) {
78
}

src/main/java/com/bernd/model/OpenGame.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ public record OpenGame(
88
String id,
99
String user,
1010
int dim,
11+
int timesetting,
1112
int handicap) {
1213

1314
public OpenGame withId(String id) {
14-
return new OpenGame(id, user, dim, handicap);
15+
return new OpenGame(id, user, dim, timesetting, handicap);
1516
}
1617

1718
public OpenGame withUser(String name) {
18-
return new OpenGame(id, name, dim, handicap);
19+
return new OpenGame(id, name, dim, timesetting, handicap);
1920
}
2021

2122
public Game accept(String opponent, AcceptRequest acceptRequest) {
@@ -28,6 +29,7 @@ public Game accept(String opponent, AcceptRequest acceptRequest) {
2829
false,
2930
createEmptyBoard(dim),
3031
dim,
32+
timesetting,
3133
acceptRequest.handicap(),
3234
new int[]{-1, -1},
3335
MoveList.create(dim));

src/main/java/com/bernd/model/ViewGame.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public record ViewGame(
77
String black,
88
String white,
99
int dim,
10+
int timesetting,
1011
int handicap,
1112
List<Move> moves
1213
) {
@@ -17,6 +18,7 @@ static ViewGame fromGame(Game game) {
1718
game.black(),
1819
game.white(),
1920
game.dim(),
21+
game.timesetting(),
2022
game.handicap(),
2123
game.moves().moves());
2224
}

src/test/java/com/bernd/model/GameBuilderTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ void unchanged() {
1818
createEmptyBoard(9),
1919
9,
2020
2,
21+
0,
2122
new int[]{-1, -1},
2223
MoveList.create(2));
2324
Game game2 = GameBuilder.builder(game).build();

src/test/java/com/bernd/util/SgfCreatorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ void testCreate() {
1717
MoveList moveList = MoveList.create(20);
1818
moveList.add(new Move(Board.B, 0, null, 3, 4));
1919
moveList.add(new Move(Board.W, 0, null, 6, 4));
20-
Game game = new Game("1234", "B", "W", false, new int[9][], 9, 0, null, moveList);
20+
Game game = new Game("1234", "B", "W", false, new int[9][], 9, 0, 0, new int[]{-1, -1}, moveList);
2121
String sgf = SgfCreator.createSgf(game, LocalDate.of(2024, Month.AUGUST, 30));
2222
assertEquals("(;FF[4]CA[UTF-8]GM[1]DT[2024-08-30]GN[1234]PB[B]PW[W]RE[?]SZ[9];B[de];W[ge])",
2323
sgf.replace("\n", ""));

0 commit comments

Comments
 (0)