|
9 | 9 | import com.bernd.model.Move; |
10 | 10 | import com.bernd.model.OpenGame; |
11 | 11 | import com.bernd.model.StatusMap; |
| 12 | +import com.bernd.model.UserStatus; |
12 | 13 | import com.bernd.model.ViewGame; |
13 | 14 | import com.bernd.util.Auth; |
14 | 15 | import com.bernd.util.RandomString; |
|
37 | 38 | @Controller |
38 | 39 | public class GameController { |
39 | 40 |
|
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; |
47 | 47 |
|
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; |
72 | 61 | } |
73 | | - return game.toView(); |
74 | | - } |
75 | 62 |
|
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(); |
112 | 72 | } |
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 | | - } |
125 | 73 |
|
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); |
133 | 122 | } |
134 | | - return moves.get(moves.size() - 1).color() ^ COLORS; |
135 | | - } |
136 | 123 |
|
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; |
140 | 133 | } |
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; |
143 | 143 | } |
144 | | - return 0; |
145 | | - } |
146 | 144 |
|
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 | + } |
156 | 157 |
|
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()); |
164 | 165 |
|
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 | + } |
175 | 176 |
|
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 | + } |
185 | 186 |
|
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 | + } |
192 | 193 |
|
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); |
199 | 203 | } |
200 | | - String sgf = SgfCreator.createSgf(game, LocalDate.now()); |
201 | | - return ResponseEntity.ok().contentType(MediaType.TEXT_PLAIN).body(sgf); |
202 | | - } |
203 | 204 | } |
0 commit comments