Skip to content

Commit

Permalink
some server side cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
h908714124 committed Oct 10, 2024
1 parent 040a172 commit 6135b1a
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 65 deletions.
30 changes: 13 additions & 17 deletions src/main/java/com/bernd/ActiveGames.java
Original file line number Diff line number Diff line change
@@ -1,40 +1,36 @@
package com.bernd;

import com.bernd.model.ActiveGame;
import com.bernd.model.ActiveGameList;
import com.bernd.model.Game;
import com.bernd.model.StatusMap;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.stereotype.Component;

@Component
public class ActiveGames {

private final StatusMap statusMap;
private final Map<String, ActiveGame> cache = new LinkedHashMap<>();
private final Games games;

ActiveGames(StatusMap statusMap) {
ActiveGames(
StatusMap statusMap,
Games games) {
this.statusMap = statusMap;
this.games = games;
}

ActiveGame put(ActiveGame game) {
cache.put(game.id(), game);
return game;
}

ActiveGameList games() {
List<ActiveGame> games() {
Set<String> active = statusMap.activeGames();
List<ActiveGame> result = new ArrayList<>(active.size());
for (String gameId : active) {
ActiveGame game = cache.get(gameId);
if (game != null) {
result.add(game);
Game game = games.get(gameId);
if (game == null) {
continue;
}
result.add(game.toActiveGame());
}
return new ActiveGameList(result);
return result;
}
}
29 changes: 25 additions & 4 deletions src/main/java/com/bernd/CleanupController.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
package com.bernd;

import com.bernd.model.Game;
import com.bernd.model.OpenGame;
import com.bernd.model.StatusMap;
import com.bernd.util.Sender;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class CleanupController {

private final Sender sender;
private final StatusMap statusMap;
private final OpenGames openGames;
private final Games games;

CleanupController(Sender sender, StatusMap statusMap) {
CleanupController(
Sender sender,
StatusMap statusMap,
OpenGames openGames,
Games games) {
this.sender = sender;
this.statusMap = statusMap;
this.openGames = openGames;
this.games = games;
}

@Scheduled(fixedDelay = 40 * 1000)
Expand All @@ -27,5 +36,17 @@ public void runScheduled() {
List<String> users = e.getValue();
sender.sendUsers(room, users);
}
List<Game> games = this.games.games();
for (Game game : games) {
if (updatedRooms.getOrDefault(game.id(), List.of()).isEmpty()) {
this.games.remove(game.id());
}
}
List<OpenGame> openGames = this.openGames.games();
for (OpenGame game : openGames) {
if (!statusMap.contains(game.user())) {
this.openGames.remove(game.user());
}
}
}
}
7 changes: 3 additions & 4 deletions src/main/java/com/bernd/GameController.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public OpenGame newGame(@RequestBody OpenGame game) {
String principal = getPrincipal();
OpenGame result = openGames.put(game.withUser(principal)
.withId(RandomString.get()));
operations.convertAndSend("/topic/lobby/open_games", openGames.games());
operations.convertAndSend("/topic/lobby/open_games", Map.of("games", openGames.games()));
return result;
}

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

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

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/bernd/Games.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ Game put(Game game) {
List<Game> games() {
return List.copyOf(map.values());
}

void remove(String id) {
map.remove(id);
}
}
18 changes: 9 additions & 9 deletions src/main/java/com/bernd/LobbyController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

import com.bernd.game.MoveList;
import com.bernd.model.ActiveGame;
import com.bernd.model.ActiveGameList;
import com.bernd.model.Chat;
import com.bernd.model.ChatMessage;
import com.bernd.model.Game;
import com.bernd.model.MatchRequest;
import com.bernd.model.OpenGameList;
import com.bernd.model.OpenGame;
import com.bernd.model.ViewGame;
import com.bernd.util.Auth;
import com.bernd.util.RandomString;
import java.util.List;
import java.util.Map;
import org.springframework.http.ResponseEntity;
import org.springframework.messaging.core.MessageSendingOperations;
import org.springframework.stereotype.Controller;
Expand Down Expand Up @@ -44,21 +45,21 @@ public class LobbyController {
@GetMapping(value = "/api/lobby/hello")
public ResponseEntity<?> sayHello() {
String principal = Auth.getPrincipal();
operations.convertAndSend("/topic/lobby/open_games", openGames.games());
operations.convertAndSend("/topic/lobby/active_games", activeGames.games());
operations.convertAndSend("/topic/lobby/open_games", Map.of("games", openGames.games()));
operations.convertAndSend("/topic/lobby/active_games", Map.of("games", activeGames.games()));
return ResponseEntity.ok().build();
}

@ResponseBody
@GetMapping(value = "/api/lobby/active_games")
public ActiveGameList getActiveGames() {
return activeGames.games();
public Map<String, List<ActiveGame>> getActiveGames() {
return Map.of("games", activeGames.games());
}

@ResponseBody
@GetMapping(value = "/api/lobby/open_games")
public OpenGameList getOpenGames() {
return openGames.games();
public Map<String, List<OpenGame>> getOpenGames() {
return Map.of("games", openGames.games());
}

@ResponseBody
Expand All @@ -77,7 +78,6 @@ public ViewGame startEdit(@RequestBody MatchRequest request) {
request.handicap(),
new int[]{-1, -1},
MoveList.create(request.dim())));
activeGames.put(ActiveGame.fromGame(game));
Chat chat = chats.get(game.id());
ChatMessage startMessage = ChatMessage.createStartMessage(chat, game);
chat.messages().add(startMessage);
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/com/bernd/OpenGames.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.bernd.model.AcceptRequest;
import com.bernd.model.OpenGame;
import com.bernd.model.OpenGameList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -27,8 +26,8 @@ OpenGame addRequest(String name, AcceptRequest request, String opponent) {
return result;
}

OpenGameList games() {
return new OpenGameList(List.copyOf(map.values()));
List<OpenGame> games() {
return List.copyOf(map.values());
}

OpenGame remove(String name) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/bernd/SessionDisconnectEventListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.bernd.util.RoomManager;
import java.security.Principal;
import java.util.Map;
import org.springframework.context.ApplicationListener;
import org.springframework.messaging.core.MessageSendingOperations;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -32,6 +33,6 @@ public void onApplicationEvent(SessionDisconnectEvent event) {
String name = user.getName();
roomManager.logout(name);
openGames.remove(name);
operations.convertAndSend("/topic/lobby/open_games", openGames.games());
operations.convertAndSend("/topic/lobby/open_games", Map.of("games", openGames.games()));
}
}
6 changes: 0 additions & 6 deletions src/main/java/com/bernd/model/ActiveGameList.java

This file was deleted.

11 changes: 9 additions & 2 deletions src/main/java/com/bernd/model/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
import com.bernd.game.Toggle;
import com.bernd.util.BoardUpdateImpl;
import com.bernd.util.Util;
import java.util.Objects;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Objects;

import static com.bernd.game.Board.removeDeadStonesAround;
import static com.bernd.util.Util.COLORS;

Expand Down Expand Up @@ -235,4 +234,12 @@ public boolean isTimeout() {
public boolean isCounting() {
return state == STATE_COUNTING;
}

public ActiveGame toActiveGame() {
return new ActiveGame(
id,
black,
white,
board().length);
}
}
6 changes: 0 additions & 6 deletions src/main/java/com/bernd/model/OpenGameList.java

This file was deleted.

24 changes: 11 additions & 13 deletions src/main/java/com/bernd/model/StatusMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,35 +45,36 @@ public List<String> usersInRoom(String room) {
}

/**
* @return all changed rooms, where inactive users have been removed
* @return a map where key=roomId and value=users
*/
public Map<String, List<String>> removeInactiveUsers() {
long current = System.currentTimeMillis();
Map<String, List<String>> tmp = new HashMap<>();
List<RemoveTask> removeTasks = new ArrayList<>();
List<String> usersToRemove = new ArrayList<>();
for (Map.Entry<String, UserStatus> e : map.entrySet()) {
UserStatus status = e.getValue();
String room = status.room();
String user = e.getKey();
if (status.isActive(current)) {
tmp.computeIfAbsent(room, key -> new ArrayList<>()).add(user);
} else {
removeTasks.add(new RemoveTask(user, room));
usersToRemove.add(user);
}
}
Map<String, List<String>> result = new LinkedHashMap<>(Math.max(8, (int) (tmp.size() * 1.5)));
for (RemoveTask task : removeTasks) {
result.put(task.room, tmp.getOrDefault(task.room, List.of()));
map.remove(task.user);
for (String user : usersToRemove) {
UserStatus status = map.get(user);
if (status == null) {
continue;
}
map.remove(user);
}
return result;
return tmp;
}

public Set<String> activeGames() {
Set<String> result = new LinkedHashSet<>();
long current = System.currentTimeMillis();
for (UserStatus status : map.values()) {
if (status.isActive(current)) {
if (!"lobby".equals(status.room())) {
result.add(status.room());
}
}
Expand All @@ -83,7 +84,4 @@ public Set<String> activeGames() {
public boolean contains(String user) {
return map.containsKey(user);
}

private record RemoveTask(String user, String room) {
}
}

0 comments on commit 6135b1a

Please sign in to comment.