60 lines
2.2 KiB
Java
60 lines
2.2 KiB
Java
package games.dmg.spigottyrant;
|
|
|
|
import java.time.Duration;
|
|
import java.time.Instant;
|
|
import java.util.Optional;
|
|
import java.util.Set;
|
|
import java.util.UUID;
|
|
|
|
public record GameState(
|
|
GameLifecycle lifecycle,
|
|
Optional<UUID> tyrantId,
|
|
Optional<UUID> vigilanteId,
|
|
Optional<PendingSelection> pendingTyrant,
|
|
Optional<PendingSelection> pendingVigilante,
|
|
Optional<Instant> pausedAt,
|
|
Duration accumulatedPausedTime,
|
|
int tyrantLevel,
|
|
int unspentChoices,
|
|
Set<TyrantUnlock> purchases
|
|
) {
|
|
public GameState {
|
|
lifecycle = lifecycle == null ? GameLifecycle.UNSTARTED : lifecycle;
|
|
tyrantId = tyrantId == null ? Optional.empty() : tyrantId;
|
|
vigilanteId = vigilanteId == null ? Optional.empty() : vigilanteId;
|
|
pendingTyrant = pendingTyrant == null ? Optional.empty() : pendingTyrant;
|
|
pendingVigilante = pendingVigilante == null ? Optional.empty() : pendingVigilante;
|
|
pausedAt = pausedAt == null ? Optional.empty() : pausedAt;
|
|
accumulatedPausedTime = accumulatedPausedTime == null
|
|
? Duration.ZERO : accumulatedPausedTime;
|
|
purchases = purchases == null ? Set.of() : Set.copyOf(purchases);
|
|
if (tyrantId.isPresent() && tyrantId.equals(vigilanteId)) {
|
|
throw new IllegalArgumentException("Tyrant and Vigilante must be different players");
|
|
}
|
|
if (tyrantLevel < 0 || unspentChoices < 0) {
|
|
throw new IllegalArgumentException("progression values must not be negative");
|
|
}
|
|
if (accumulatedPausedTime.isNegative()) {
|
|
throw new IllegalArgumentException("accumulated paused time must not be negative");
|
|
}
|
|
if ((lifecycle == GameLifecycle.PAUSED) != pausedAt.isPresent()) {
|
|
throw new IllegalArgumentException("pausedAt must be present exactly while paused");
|
|
}
|
|
}
|
|
|
|
public static GameState empty() {
|
|
return new GameState(
|
|
GameLifecycle.UNSTARTED,
|
|
Optional.empty(),
|
|
Optional.empty(),
|
|
Optional.empty(),
|
|
Optional.empty(),
|
|
Optional.empty(),
|
|
Duration.ZERO,
|
|
0,
|
|
0,
|
|
Set.of()
|
|
);
|
|
}
|
|
}
|