|
2 | 2 |
|
3 | 3 | public record UserStatus(
|
4 | 4 | String room,
|
5 |
| - long lastSeen) { |
| 5 | + long lastSeen, |
| 6 | + OpenGame openGame) { |
6 | 7 |
|
| 8 | + // max interval between heartbeats |
7 | 9 | private static final long USER_TIMEOUT = 45 * 1000;
|
8 | 10 |
|
9 | 11 | public static UserStatus create(String room) {
|
10 |
| - return new UserStatus(room, System.currentTimeMillis()); |
| 12 | + return new UserStatus( |
| 13 | + room, |
| 14 | + System.currentTimeMillis(), |
| 15 | + null); |
11 | 16 | }
|
12 | 17 |
|
13 | 18 | public UserStatus touch() {
|
14 |
| - return new UserStatus(room, System.currentTimeMillis()); |
| 19 | + return toBuilder().lastSeen(System.currentTimeMillis()).build(); |
15 | 20 | }
|
16 | 21 |
|
17 | 22 | public boolean isActive(long current) {
|
18 | 23 | return current < lastSeen + USER_TIMEOUT;
|
19 | 24 | }
|
| 25 | + |
| 26 | + public Builder toBuilder() { |
| 27 | + return new Builder(room, lastSeen, openGame); |
| 28 | + } |
| 29 | + |
| 30 | + public static final class Builder { |
| 31 | + private String room; |
| 32 | + private long lastSeen; |
| 33 | + private OpenGame openGame; |
| 34 | + |
| 35 | + private Builder( |
| 36 | + String room, |
| 37 | + long lastSeen, |
| 38 | + OpenGame openGame) { |
| 39 | + this.room = room; |
| 40 | + this.lastSeen = lastSeen; |
| 41 | + this.openGame = openGame; |
| 42 | + } |
| 43 | + |
| 44 | + public Builder room(String room) { |
| 45 | + this.room = room; |
| 46 | + return this; |
| 47 | + } |
| 48 | + |
| 49 | + public Builder lastSeen(long lastSeen) { |
| 50 | + this.lastSeen = lastSeen; |
| 51 | + return this; |
| 52 | + } |
| 53 | + |
| 54 | + public Builder openGame(OpenGame openGame) { |
| 55 | + this.openGame = openGame; |
| 56 | + return this; |
| 57 | + } |
| 58 | + |
| 59 | + public UserStatus build() { |
| 60 | + return new UserStatus(room, lastSeen, openGame); |
| 61 | + } |
| 62 | + } |
20 | 63 | }
|
0 commit comments