Skip to content

Commit 6135b1a

Browse files
committed
some server side cleanup
1 parent 040a172 commit 6135b1a

11 files changed

+78
-65
lines changed
Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,36 @@
11
package com.bernd;
22

33
import com.bernd.model.ActiveGame;
4-
import com.bernd.model.ActiveGameList;
4+
import com.bernd.model.Game;
55
import com.bernd.model.StatusMap;
6-
import org.springframework.stereotype.Component;
7-
86
import java.util.ArrayList;
9-
import java.util.LinkedHashMap;
107
import java.util.List;
11-
import java.util.Map;
128
import java.util.Set;
9+
import org.springframework.stereotype.Component;
1310

1411
@Component
1512
public class ActiveGames {
1613

1714
private final StatusMap statusMap;
18-
private final Map<String, ActiveGame> cache = new LinkedHashMap<>();
15+
private final Games games;
1916

20-
ActiveGames(StatusMap statusMap) {
17+
ActiveGames(
18+
StatusMap statusMap,
19+
Games games) {
2120
this.statusMap = statusMap;
21+
this.games = games;
2222
}
2323

24-
ActiveGame put(ActiveGame game) {
25-
cache.put(game.id(), game);
26-
return game;
27-
}
28-
29-
ActiveGameList games() {
24+
List<ActiveGame> games() {
3025
Set<String> active = statusMap.activeGames();
3126
List<ActiveGame> result = new ArrayList<>(active.size());
3227
for (String gameId : active) {
33-
ActiveGame game = cache.get(gameId);
34-
if (game != null) {
35-
result.add(game);
28+
Game game = games.get(gameId);
29+
if (game == null) {
30+
continue;
3631
}
32+
result.add(game.toActiveGame());
3733
}
38-
return new ActiveGameList(result);
34+
return result;
3935
}
4036
}
Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,31 @@
11
package com.bernd;
22

3+
import com.bernd.model.Game;
4+
import com.bernd.model.OpenGame;
35
import com.bernd.model.StatusMap;
46
import com.bernd.util.Sender;
5-
import org.springframework.scheduling.annotation.Scheduled;
6-
import org.springframework.stereotype.Component;
7-
87
import java.util.List;
98
import java.util.Map;
9+
import org.springframework.scheduling.annotation.Scheduled;
10+
import org.springframework.stereotype.Component;
1011

1112
@Component
1213
public class CleanupController {
1314

1415
private final Sender sender;
1516
private final StatusMap statusMap;
17+
private final OpenGames openGames;
18+
private final Games games;
1619

17-
CleanupController(Sender sender, StatusMap statusMap) {
20+
CleanupController(
21+
Sender sender,
22+
StatusMap statusMap,
23+
OpenGames openGames,
24+
Games games) {
1825
this.sender = sender;
1926
this.statusMap = statusMap;
27+
this.openGames = openGames;
28+
this.games = games;
2029
}
2130

2231
@Scheduled(fixedDelay = 40 * 1000)
@@ -27,5 +36,17 @@ public void runScheduled() {
2736
List<String> users = e.getValue();
2837
sender.sendUsers(room, users);
2938
}
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+
}
44+
}
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+
}
50+
}
3051
}
3152
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public OpenGame newGame(@RequestBody OpenGame game) {
148148
String principal = getPrincipal();
149149
OpenGame result = openGames.put(game.withUser(principal)
150150
.withId(RandomString.get()));
151-
operations.convertAndSend("/topic/lobby/open_games", openGames.games());
151+
operations.convertAndSend("/topic/lobby/open_games", Map.of("games", openGames.games()));
152152
return result;
153153
}
154154

@@ -158,7 +158,6 @@ public ResponseEntity<?> start(@RequestBody AcceptRequest acceptRequest) {
158158
openGames.remove(acceptRequest.opponent());
159159
OpenGame openGame = openGames.remove(principal);
160160
Game fullGame = games.put(openGame.accept(acceptRequest));
161-
activeGames.put(ActiveGame.fromGame(fullGame));
162161
Chat chat = chats.get(openGame.id());
163162

164163
ChatMessage startMessage = ChatMessage.createStartMessage(chat, fullGame);
@@ -167,8 +166,8 @@ public ResponseEntity<?> start(@RequestBody AcceptRequest acceptRequest) {
167166
operations.convertAndSend("/topic/gamestart", Map.of(
168167
"players", List.of(principal, acceptRequest.opponent()),
169168
"id", openGame.id()));
170-
operations.convertAndSend("/topic/lobby/open_games", openGames.games());
171-
operations.convertAndSend("/topic/lobby/active_games", activeGames.games());
169+
operations.convertAndSend("/topic/lobby/open_games", Map.of("games", openGames.games()));
170+
operations.convertAndSend("/topic/lobby/active_games", Map.of("games", activeGames.games()));
172171
return ResponseEntity.ok().build();
173172
}
174173

src/main/java/com/bernd/Games.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,8 @@ Game put(Game game) {
2222
List<Game> games() {
2323
return List.copyOf(map.values());
2424
}
25+
26+
void remove(String id) {
27+
map.remove(id);
28+
}
2529
}

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22

33
import com.bernd.game.MoveList;
44
import com.bernd.model.ActiveGame;
5-
import com.bernd.model.ActiveGameList;
65
import com.bernd.model.Chat;
76
import com.bernd.model.ChatMessage;
87
import com.bernd.model.Game;
98
import com.bernd.model.MatchRequest;
10-
import com.bernd.model.OpenGameList;
9+
import com.bernd.model.OpenGame;
1110
import com.bernd.model.ViewGame;
1211
import com.bernd.util.Auth;
1312
import com.bernd.util.RandomString;
13+
import java.util.List;
14+
import java.util.Map;
1415
import org.springframework.http.ResponseEntity;
1516
import org.springframework.messaging.core.MessageSendingOperations;
1617
import org.springframework.stereotype.Controller;
@@ -44,21 +45,21 @@ public class LobbyController {
4445
@GetMapping(value = "/api/lobby/hello")
4546
public ResponseEntity<?> sayHello() {
4647
String principal = Auth.getPrincipal();
47-
operations.convertAndSend("/topic/lobby/open_games", openGames.games());
48-
operations.convertAndSend("/topic/lobby/active_games", activeGames.games());
48+
operations.convertAndSend("/topic/lobby/open_games", Map.of("games", openGames.games()));
49+
operations.convertAndSend("/topic/lobby/active_games", Map.of("games", activeGames.games()));
4950
return ResponseEntity.ok().build();
5051
}
5152

5253
@ResponseBody
5354
@GetMapping(value = "/api/lobby/active_games")
54-
public ActiveGameList getActiveGames() {
55-
return activeGames.games();
55+
public Map<String, List<ActiveGame>> getActiveGames() {
56+
return Map.of("games", activeGames.games());
5657
}
5758

5859
@ResponseBody
5960
@GetMapping(value = "/api/lobby/open_games")
60-
public OpenGameList getOpenGames() {
61-
return openGames.games();
61+
public Map<String, List<OpenGame>> getOpenGames() {
62+
return Map.of("games", openGames.games());
6263
}
6364

6465
@ResponseBody
@@ -77,7 +78,6 @@ public ViewGame startEdit(@RequestBody MatchRequest request) {
7778
request.handicap(),
7879
new int[]{-1, -1},
7980
MoveList.create(request.dim())));
80-
activeGames.put(ActiveGame.fromGame(game));
8181
Chat chat = chats.get(game.id());
8282
ChatMessage startMessage = ChatMessage.createStartMessage(chat, game);
8383
chat.messages().add(startMessage);

src/main/java/com/bernd/OpenGames.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.bernd.model.AcceptRequest;
44
import com.bernd.model.OpenGame;
5-
import com.bernd.model.OpenGameList;
65
import java.util.LinkedHashMap;
76
import java.util.List;
87
import java.util.Map;
@@ -27,8 +26,8 @@ OpenGame addRequest(String name, AcceptRequest request, String opponent) {
2726
return result;
2827
}
2928

30-
OpenGameList games() {
31-
return new OpenGameList(List.copyOf(map.values()));
29+
List<OpenGame> games() {
30+
return List.copyOf(map.values());
3231
}
3332

3433
OpenGame remove(String name) {

src/main/java/com/bernd/SessionDisconnectEventListener.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.bernd.util.RoomManager;
44
import java.security.Principal;
5+
import java.util.Map;
56
import org.springframework.context.ApplicationListener;
67
import org.springframework.messaging.core.MessageSendingOperations;
78
import org.springframework.stereotype.Component;
@@ -32,6 +33,6 @@ public void onApplicationEvent(SessionDisconnectEvent event) {
3233
String name = user.getName();
3334
roomManager.logout(name);
3435
openGames.remove(name);
35-
operations.convertAndSend("/topic/lobby/open_games", openGames.games());
36+
operations.convertAndSend("/topic/lobby/open_games", Map.of("games", openGames.games()));
3637
}
3738
}

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

Lines changed: 0 additions & 6 deletions
This file was deleted.

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@
77
import com.bernd.game.Toggle;
88
import com.bernd.util.BoardUpdateImpl;
99
import com.bernd.util.Util;
10+
import java.util.Objects;
1011
import org.apache.logging.log4j.LogManager;
1112
import org.apache.logging.log4j.Logger;
1213

13-
import java.util.Objects;
14-
1514
import static com.bernd.game.Board.removeDeadStonesAround;
1615
import static com.bernd.util.Util.COLORS;
1716

@@ -235,4 +234,12 @@ public boolean isTimeout() {
235234
public boolean isCounting() {
236235
return state == STATE_COUNTING;
237236
}
237+
238+
public ActiveGame toActiveGame() {
239+
return new ActiveGame(
240+
id,
241+
black,
242+
white,
243+
board().length);
244+
}
238245
}

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

Lines changed: 0 additions & 6 deletions
This file was deleted.

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

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,35 +45,36 @@ public List<String> usersInRoom(String room) {
4545
}
4646

4747
/**
48-
* @return all changed rooms, where inactive users have been removed
48+
* @return a map where key=roomId and value=users
4949
*/
5050
public Map<String, List<String>> removeInactiveUsers() {
5151
long current = System.currentTimeMillis();
5252
Map<String, List<String>> tmp = new HashMap<>();
53-
List<RemoveTask> removeTasks = new ArrayList<>();
53+
List<String> usersToRemove = new ArrayList<>();
5454
for (Map.Entry<String, UserStatus> e : map.entrySet()) {
5555
UserStatus status = e.getValue();
5656
String room = status.room();
5757
String user = e.getKey();
5858
if (status.isActive(current)) {
5959
tmp.computeIfAbsent(room, key -> new ArrayList<>()).add(user);
6060
} else {
61-
removeTasks.add(new RemoveTask(user, room));
61+
usersToRemove.add(user);
6262
}
6363
}
64-
Map<String, List<String>> result = new LinkedHashMap<>(Math.max(8, (int) (tmp.size() * 1.5)));
65-
for (RemoveTask task : removeTasks) {
66-
result.put(task.room, tmp.getOrDefault(task.room, List.of()));
67-
map.remove(task.user);
64+
for (String user : usersToRemove) {
65+
UserStatus status = map.get(user);
66+
if (status == null) {
67+
continue;
68+
}
69+
map.remove(user);
6870
}
69-
return result;
71+
return tmp;
7072
}
7173

7274
public Set<String> activeGames() {
7375
Set<String> result = new LinkedHashSet<>();
74-
long current = System.currentTimeMillis();
7576
for (UserStatus status : map.values()) {
76-
if (status.isActive(current)) {
77+
if (!"lobby".equals(status.room())) {
7778
result.add(status.room());
7879
}
7980
}
@@ -83,7 +84,4 @@ public Set<String> activeGames() {
8384
public boolean contains(String user) {
8485
return map.containsKey(user);
8586
}
86-
87-
private record RemoveTask(String user, String room) {
88-
}
8987
}

0 commit comments

Comments
 (0)