Skip to content

Commit 8b35d7c

Browse files
committed
remove class OpenGames
1 parent fbf6b6f commit 8b35d7c

File tree

8 files changed

+418
-381
lines changed

8 files changed

+418
-381
lines changed
Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.bernd;
22

33
import com.bernd.model.Game;
4-
import com.bernd.model.OpenGame;
54
import com.bernd.model.StatusMap;
65
import com.bernd.util.Sender;
76
import java.util.List;
@@ -12,41 +11,32 @@
1211
@Component
1312
public class CleanupController {
1413

15-
private final Sender sender;
16-
private final StatusMap statusMap;
17-
private final OpenGames openGames;
18-
private final Games games;
14+
private final Sender sender;
15+
private final StatusMap statusMap;
16+
private final Games games;
1917

20-
CleanupController(
21-
Sender sender,
22-
StatusMap statusMap,
23-
OpenGames openGames,
24-
Games games) {
25-
this.sender = sender;
26-
this.statusMap = statusMap;
27-
this.openGames = openGames;
28-
this.games = games;
29-
}
30-
31-
@Scheduled(fixedDelay = 40 * 1000)
32-
public void runScheduled() {
33-
Map<String, List<String>> updatedRooms = statusMap.removeInactiveUsers();
34-
for (Map.Entry<String, List<String>> e : updatedRooms.entrySet()) {
35-
String room = e.getKey();
36-
List<String> users = e.getValue();
37-
sender.sendUsers(room, users);
38-
}
39-
List<Game> games = this.games.games();
40-
for (Game game : games) {
41-
if (updatedRooms.getOrDefault(game.id(), List.of()).isEmpty()) {
42-
this.games.remove(game.id());
43-
}
18+
CleanupController(
19+
Sender sender,
20+
StatusMap statusMap,
21+
Games games) {
22+
this.sender = sender;
23+
this.statusMap = statusMap;
24+
this.games = games;
4425
}
45-
List<OpenGame> openGames = this.openGames.games();
46-
for (OpenGame game : openGames) {
47-
if (!statusMap.contains(game.user())) {
48-
this.openGames.remove(game.user());
49-
}
26+
27+
@Scheduled(fixedDelay = 40 * 1000)
28+
public void runScheduled() {
29+
Map<String, List<String>> updatedRooms = statusMap.removeInactiveUsers();
30+
for (Map.Entry<String, List<String>> e : updatedRooms.entrySet()) {
31+
String room = e.getKey();
32+
List<String> users = e.getValue();
33+
sender.sendUsers(room, users);
34+
}
35+
List<Game> games = this.games.games();
36+
for (Game game : games) {
37+
if (updatedRooms.getOrDefault(game.id(), List.of()).isEmpty()) {
38+
this.games.remove(game.id());
39+
}
40+
}
5041
}
51-
}
5242
}

src/main/java/com/bernd/GameController.java

Lines changed: 148 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.bernd.model.Move;
1010
import com.bernd.model.OpenGame;
1111
import com.bernd.model.StatusMap;
12+
import com.bernd.model.UserStatus;
1213
import com.bernd.model.ViewGame;
1314
import com.bernd.util.Auth;
1415
import com.bernd.util.RandomString;
@@ -37,167 +38,167 @@
3738
@Controller
3839
public class GameController {
3940

40-
private final MessageSendingOperations<String> operations;
41-
private final RoomManager roomManager;
42-
private final StatusMap statusMap;
43-
private final Games games;
44-
private final OpenGames openGames;
45-
private final ActiveGames activeGames;
46-
private final Chats chats;
41+
private final MessageSendingOperations<String> operations;
42+
private final RoomManager roomManager;
43+
private final StatusMap statusMap;
44+
private final Games games;
45+
private final ActiveGames activeGames;
46+
private final Chats chats;
4747

48-
GameController(
49-
MessageSendingOperations<String> operations,
50-
RoomManager roomManager,
51-
StatusMap statusMap,
52-
Games games,
53-
OpenGames openGames,
54-
ActiveGames activeGames,
55-
Chats chats) {
56-
this.operations = operations;
57-
this.roomManager = roomManager;
58-
this.statusMap = statusMap;
59-
this.games = games;
60-
this.openGames = openGames;
61-
this.activeGames = activeGames;
62-
this.chats = chats;
63-
}
64-
65-
@ResponseBody
66-
@GetMapping(value = "/api/game/{id}")
67-
public ViewGame getGame(@PathVariable String id, Principal p) {
68-
roomManager.updateStatus(Auth.getPrincipal(p), id);
69-
Game game = games.get(id);
70-
if (game == null) {
71-
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "no such game");
48+
GameController(
49+
MessageSendingOperations<String> operations,
50+
RoomManager roomManager,
51+
StatusMap statusMap,
52+
Games games,
53+
ActiveGames activeGames,
54+
Chats chats) {
55+
this.operations = operations;
56+
this.roomManager = roomManager;
57+
this.statusMap = statusMap;
58+
this.games = games;
59+
this.activeGames = activeGames;
60+
this.chats = chats;
7261
}
73-
return game.toView();
74-
}
7562

76-
@MessageMapping("/game/move")
77-
public void action(Move move, Principal p) {
78-
String id = statusMap.getRoom(getPrincipal(p));
79-
if (id == null) {
80-
return;
81-
}
82-
Game game = games.get(id);
83-
if (p == null || game == null) {
84-
return;
85-
}
86-
if (game.gameHasEnded()) {
87-
return;
88-
}
89-
int principalColor = getColorFromPrincipal(game, getPrincipal(p));
90-
if (principalColor == 0) {
91-
return;
92-
}
93-
int color = getColorFromGameState(game);
94-
Chat chat = chats.get(game.id());
95-
if (game.timesetting() != 0
96-
&& !game.isCounting()
97-
&& !game.gameHasEnded()
98-
&& System.currentTimeMillis() > game.updated() + game.timesetting() * 1000L) {
99-
games.put(game.withTimeoutState());
100-
String text = color == Board.W ? "B+Time" : "W+Time";
101-
ChatMessage message = new ChatMessage(chat.counter().getAndIncrement(), text, null, "status", null);
102-
chat.messages().add(message);
103-
operations.convertAndSend("/topic/chat/" + chat.id(), message);
104-
operations.convertAndSend("/topic/move/" + game.id(), game.getLastMove());
105-
return;
106-
}
107-
if (!game.isCounting() && !game.isSelfPlay() && color != principalColor) {
108-
return;
109-
}
110-
if (game.isForbidden(move)) {
111-
return;
63+
@ResponseBody
64+
@GetMapping(value = "/api/game/{id}")
65+
public ViewGame getGame(@PathVariable String id, Principal p) {
66+
roomManager.updateStatus(Auth.getPrincipal(p), id);
67+
Game game = games.get(id);
68+
if (game == null) {
69+
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "no such game");
70+
}
71+
return game.toView();
11272
}
113-
Game updated = game.update(move
114-
.withCount(game.moves().size())
115-
.withColor(game.isSelfPlay() ? color : principalColor));
116-
games.put(updated);
117-
Move lastMove = game.getLastMove();
118-
if (lastMove.end()) {
119-
ChatMessage message = new ChatMessage(chat.counter().getAndIncrement(), game.getScore(), null, "status", null);
120-
chat.messages().add(message);
121-
operations.convertAndSend("/topic/chat/" + chat.id(), message);
122-
}
123-
operations.convertAndSend("/topic/move/" + game.id(), lastMove);
124-
}
12573

126-
private int getColorFromGameState(Game game) {
127-
if (game.remainingHandicap() > 0) {
128-
return Board.B;
129-
}
130-
MoveList moves = game.moves();
131-
if (moves.isEmpty()) {
132-
return Board.B;
74+
@MessageMapping("/game/move")
75+
public void action(Move move, Principal p) {
76+
String id = statusMap.getRoom(getPrincipal(p));
77+
if (id == null) {
78+
return;
79+
}
80+
Game game = games.get(id);
81+
if (p == null || game == null) {
82+
return;
83+
}
84+
if (game.gameHasEnded()) {
85+
return;
86+
}
87+
int principalColor = getColorFromPrincipal(game, getPrincipal(p));
88+
if (principalColor == 0) {
89+
return;
90+
}
91+
int color = getColorFromGameState(game);
92+
Chat chat = chats.get(game.id());
93+
if (game.timesetting() != 0
94+
&& !game.isCounting()
95+
&& !game.gameHasEnded()
96+
&& System.currentTimeMillis() > game.updated() + game.timesetting() * 1000L) {
97+
games.put(game.withTimeoutState());
98+
String text = color == Board.W ? "B+Time" : "W+Time";
99+
ChatMessage message = new ChatMessage(chat.counter().getAndIncrement(), text, null, "status", null);
100+
chat.messages().add(message);
101+
operations.convertAndSend("/topic/chat/" + chat.id(), message);
102+
operations.convertAndSend("/topic/move/" + game.id(), game.getLastMove());
103+
return;
104+
}
105+
if (!game.isCounting() && !game.isSelfPlay() && color != principalColor) {
106+
return;
107+
}
108+
if (game.isForbidden(move)) {
109+
return;
110+
}
111+
Game updated = game.update(move
112+
.withCount(game.moves().size())
113+
.withColor(game.isSelfPlay() ? color : principalColor));
114+
games.put(updated);
115+
Move lastMove = game.getLastMove();
116+
if (lastMove.end()) {
117+
ChatMessage message = new ChatMessage(chat.counter().getAndIncrement(), game.getScore(), null, "status", null);
118+
chat.messages().add(message);
119+
operations.convertAndSend("/topic/chat/" + chat.id(), message);
120+
}
121+
operations.convertAndSend("/topic/move/" + game.id(), lastMove);
133122
}
134-
return moves.get(moves.size() - 1).color() ^ COLORS;
135-
}
136123

137-
private static int getColorFromPrincipal(Game game, String principal) {
138-
if (game.isBlack(principal)) {
139-
return Board.B;
124+
private int getColorFromGameState(Game game) {
125+
if (game.remainingHandicap() > 0) {
126+
return Board.B;
127+
}
128+
MoveList moves = game.moves();
129+
if (moves.isEmpty()) {
130+
return Board.B;
131+
}
132+
return moves.get(moves.size() - 1).color() ^ COLORS;
140133
}
141-
if (game.isWhite(principal)) {
142-
return Board.W;
134+
135+
private static int getColorFromPrincipal(Game game, String principal) {
136+
if (game.isBlack(principal)) {
137+
return Board.B;
138+
}
139+
if (game.isWhite(principal)) {
140+
return Board.W;
141+
}
142+
return 0;
143143
}
144-
return 0;
145-
}
146144

147-
@ResponseBody
148-
@PostMapping(value = "/api/create", consumes = "application/json")
149-
public OpenGame newGame(@RequestBody OpenGame game) {
150-
String principal = getPrincipal();
151-
OpenGame result = openGames.put(game.withUser(principal)
152-
.withId(RandomString.get()));
153-
operations.convertAndSend("/topic/lobby/open_games", Map.of("games", openGames.games()));
154-
return result;
155-
}
145+
@ResponseBody
146+
@PostMapping(value = "/api/create", consumes = "application/json")
147+
public OpenGame newGame(@RequestBody OpenGame game) {
148+
String principal = getPrincipal();
149+
UserStatus result = statusMap.openGame(game.withUser(principal)
150+
.withId(RandomString.get()));
151+
if (result == null) {
152+
return null;
153+
}
154+
operations.convertAndSend("/topic/lobby/open_games", Map.of("games", statusMap.openGames()));
155+
return result.openGame();
156+
}
156157

157-
@PostMapping(value = "/api/lobby/start", consumes = "application/json")
158-
public ResponseEntity<?> start(@RequestBody AcceptRequest acceptRequest) {
159-
String principal = getPrincipal();
160-
openGames.remove(acceptRequest.opponent());
161-
OpenGame openGame = openGames.remove(principal);
162-
Game fullGame = games.put(openGame.accept(acceptRequest));
163-
Chat chat = chats.get(openGame.id());
158+
@PostMapping(value = "/api/lobby/start", consumes = "application/json")
159+
public ResponseEntity<?> start(@RequestBody AcceptRequest acceptRequest) {
160+
String principal = getPrincipal();
161+
statusMap.removeOpenGame(acceptRequest.opponent());
162+
OpenGame openGame = statusMap.removeOpenGame(principal);
163+
Game fullGame = games.put(openGame.accept(acceptRequest));
164+
Chat chat = chats.get(openGame.id());
164165

165-
ChatMessage startMessage = ChatMessage.createStartMessage(chat, fullGame);
166-
chat.messages().add(startMessage);
167-
operations.convertAndSend("/topic/chat/" + chat.id(), startMessage);
168-
operations.convertAndSend("/topic/gamestart", Map.of(
169-
"players", List.of(principal, acceptRequest.opponent()),
170-
"id", openGame.id()));
171-
operations.convertAndSend("/topic/lobby/open_games", Map.of("games", openGames.games()));
172-
operations.convertAndSend("/topic/lobby/active_games", Map.of("games", activeGames.games()));
173-
return ResponseEntity.ok().build();
174-
}
166+
ChatMessage startMessage = ChatMessage.createStartMessage(chat, fullGame);
167+
chat.messages().add(startMessage);
168+
operations.convertAndSend("/topic/chat/" + chat.id(), startMessage);
169+
operations.convertAndSend("/topic/gamestart", Map.of(
170+
"players", List.of(principal, acceptRequest.opponent()),
171+
"id", openGame.id()));
172+
operations.convertAndSend("/topic/lobby/open_games", Map.of("games", statusMap.openGames()));
173+
operations.convertAndSend("/topic/lobby/active_games", Map.of("games", activeGames.games()));
174+
return ResponseEntity.ok().build();
175+
}
175176

176-
@ResponseBody
177-
@PostMapping(value = "/api/challenge", consumes = "application/json")
178-
public Map<String, List<AcceptRequest>> challenge(@RequestBody AcceptRequest acceptRequest) {
179-
String principal = getPrincipal();
180-
openGames.remove(principal);
181-
OpenGame openGame = openGames.addRequest(acceptRequest.game().user(), acceptRequest, principal);
182-
operations.convertAndSend("/topic/lobby/requests", Map.of("requests", openGame.requests()));
183-
return Map.of("requests", openGame.requests());
184-
}
177+
@ResponseBody
178+
@PostMapping(value = "/api/challenge", consumes = "application/json")
179+
public Map<String, List<AcceptRequest>> challenge(@RequestBody AcceptRequest acceptRequest) {
180+
String principal = getPrincipal();
181+
statusMap.removeOpenGame(principal);
182+
UserStatus status = statusMap.addRequest(acceptRequest.game().user(), acceptRequest, principal);
183+
operations.convertAndSend("/topic/lobby/requests", Map.of("requests", status.requests()));
184+
return Map.of("requests", status.requests());
185+
}
185186

186-
@ResponseBody
187-
@GetMapping(value = "/api/lobby/requests")
188-
public Map<String, List<AcceptRequest>> getRequests() {
189-
String principal = getPrincipal();
190-
return Map.of("requests", openGames.getRequests(principal));
191-
}
187+
@ResponseBody
188+
@GetMapping(value = "/api/lobby/requests")
189+
public Map<String, List<AcceptRequest>> getRequests() {
190+
String principal = getPrincipal();
191+
return Map.of("requests", statusMap.getRequests(principal));
192+
}
192193

193-
@GetMapping("/api/sgf/{id}/{black}_vs_{white}.sgf")
194-
public ResponseEntity<String> getSgf(
195-
@PathVariable String id) {
196-
Game game = games.get(id);
197-
if (game == null) {
198-
return ResponseEntity.notFound().build();
194+
@GetMapping("/api/sgf/{id}/{black}_vs_{white}.sgf")
195+
public ResponseEntity<String> getSgf(
196+
@PathVariable String id) {
197+
Game game = games.get(id);
198+
if (game == null) {
199+
return ResponseEntity.notFound().build();
200+
}
201+
String sgf = SgfCreator.createSgf(game, LocalDate.now());
202+
return ResponseEntity.ok().contentType(MediaType.TEXT_PLAIN).body(sgf);
199203
}
200-
String sgf = SgfCreator.createSgf(game, LocalDate.now());
201-
return ResponseEntity.ok().contentType(MediaType.TEXT_PLAIN).body(sgf);
202-
}
203204
}

0 commit comments

Comments
 (0)