Skip to content

Commit 053f198

Browse files
committed
prune active games list
1 parent cdc1616 commit 053f198

File tree

3 files changed

+40
-11
lines changed

3 files changed

+40
-11
lines changed

src/main/java/com/bernd/ActiveGames.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,39 @@
22

33
import com.bernd.model.ActiveGame;
44
import com.bernd.model.ActiveGameList;
5+
import com.bernd.model.StatusMap;
56
import org.springframework.stereotype.Component;
67

8+
import java.util.ArrayList;
79
import java.util.LinkedHashMap;
810
import java.util.List;
911
import java.util.Map;
12+
import java.util.Set;
1013

1114
@Component
1215
public class ActiveGames {
16+
17+
private final StatusMap statusMap;
1318
private final Map<String, ActiveGame> map = new LinkedHashMap<>();
1419

20+
ActiveGames(StatusMap statusMap) {
21+
this.statusMap = statusMap;
22+
}
23+
1524
ActiveGame put(ActiveGame game) {
1625
map.put(game.id(), game);
1726
return game;
1827
}
1928

2029
ActiveGameList games() {
21-
return new ActiveGameList(List.copyOf(map.values()));
22-
}
23-
24-
ActiveGame remove(String name) {
25-
return map.remove(name);
30+
Set<String> active = statusMap.activeGames();
31+
List<ActiveGame> result = new ArrayList<>(active.size());
32+
for (String gameId : active) {
33+
ActiveGame game = map.get(gameId);
34+
if (game != null) {
35+
result.add(game);
36+
}
37+
}
38+
return new ActiveGameList(result);
2639
}
2740
}

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
import java.util.ArrayList;
66
import java.util.HashMap;
77
import java.util.LinkedHashMap;
8+
import java.util.LinkedHashSet;
89
import java.util.List;
910
import java.util.Map;
11+
import java.util.Set;
1012

1113
@Component
1214
public final class StatusMap {
1315

14-
private static final long USER_TIMEOUT = 45 * 1000;
15-
1616
private final Map<String, UserStatus> map = new LinkedHashMap<>();
1717

1818
public UserStatus put(String user, UserStatus status) {
@@ -37,20 +37,31 @@ public Map<String, List<String>> prune() {
3737
UserStatus status = e.getValue();
3838
String room = status.room();
3939
String user = e.getKey();
40-
if (status.lastSeen() + USER_TIMEOUT < current) {
41-
remove.add(new RemoveItem(user, room));
42-
} else {
40+
if (status.isActive(current)) {
4341
tmp.computeIfAbsent(room, key -> new ArrayList<>()).add(user);
42+
} else {
43+
remove.add(new RemoveItem(user, room));
4444
}
4545
}
4646
Map<String, List<String>> result = new LinkedHashMap<>(Math.max(8, (int) (tmp.size() * 1.5)));
4747
for (RemoveItem item : remove) {
48-
result.put(item.room, tmp.get(item.room));
48+
result.put(item.room, tmp.getOrDefault(item.room, List.of()));
4949
map.remove(item.user);
5050
}
5151
return result;
5252
}
5353

54+
public Set<String> activeGames() {
55+
Set<String> result = new LinkedHashSet<>();
56+
long current = System.currentTimeMillis();
57+
for (UserStatus status : map.values()) {
58+
if (status.isActive(current)) {
59+
result.add(status.room());
60+
}
61+
}
62+
return result;
63+
}
64+
5465
private record RemoveItem(String user, String room) {
5566
}
5667
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
package com.bernd.model;
22

33
public record UserStatus(String room, long lastSeen) {
4+
private static final long USER_TIMEOUT = 45 * 1000;
45
public static UserStatus create(String room) {
56
return new UserStatus(room, System.currentTimeMillis());
67
}
8+
9+
public boolean isActive(long current) {
10+
return current < lastSeen + USER_TIMEOUT;
11+
}
712
}

0 commit comments

Comments
 (0)