feat(state): add validated persistent game state
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
package games.dmg.spigottyrant;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
public record PersistentState(GameState game, Map<UUID, PlayerState> players) {
|
||||
public PersistentState {
|
||||
game = game == null ? GameState.empty() : game;
|
||||
players = players == null ? Map.of() : Map.copyOf(players);
|
||||
for (Map.Entry<UUID, PlayerState> entry : players.entrySet()) {
|
||||
if (!entry.getKey().equals(entry.getValue().playerId())) {
|
||||
throw new IllegalArgumentException("player map key must match player state ID");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static PersistentState empty() {
|
||||
return new PersistentState(GameState.empty(), Map.of());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user