changes to sync
This commit is contained in:
@@ -4,6 +4,7 @@ import eu.jonahbauer.wizard.common.machine.TimeoutContext;
|
||||
import eu.jonahbauer.wizard.common.messages.observer.ObserverMessage;
|
||||
import eu.jonahbauer.wizard.common.messages.observer.StateMessage;
|
||||
import eu.jonahbauer.wizard.common.messages.player.PlayerMessage;
|
||||
import eu.jonahbauer.wizard.core.machine.states.TransientState;
|
||||
import eu.jonahbauer.wizard.core.machine.states.game.Created;
|
||||
import eu.jonahbauer.wizard.core.machine.states.game.Error;
|
||||
import eu.jonahbauer.wizard.core.messages.Observer;
|
||||
@@ -57,7 +58,9 @@ public final class Game extends TimeoutContext<GameState, Game> {
|
||||
|
||||
@Override
|
||||
protected void onTransition(GameState from, GameState to) {
|
||||
notify(new StateMessage(to != null ? Util.toSnakeCase(to.getClass().getSimpleName()) : "null"));
|
||||
if (!(to instanceof TransientState)) {
|
||||
notify(new StateMessage(to != null ? Util.toSnakeCase(to.getClass().getSimpleName()) : "null"));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -3,12 +3,14 @@ package eu.jonahbauer.wizard.core.machine;
|
||||
import eu.jonahbauer.wizard.common.machine.TimeoutState;
|
||||
import eu.jonahbauer.wizard.common.messages.player.PlayerMessage;
|
||||
import eu.jonahbauer.wizard.core.machine.states.GameData;
|
||||
import eu.jonahbauer.wizard.core.machine.states.SyncState;
|
||||
import lombok.Getter;
|
||||
import org.jetbrains.annotations.Unmodifiable;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static eu.jonahbauer.wizard.core.machine.states.GameData.CURRENT_PLAYER;
|
||||
import static eu.jonahbauer.wizard.core.machine.states.GameData.PLAYERS;
|
||||
@@ -44,6 +46,14 @@ public abstract class GameState implements TimeoutState<GameState, Game> {
|
||||
protected final long getSyncTimeout(Game game, boolean absolute) {
|
||||
return (absolute ? System.currentTimeMillis() : 0) + game.getConfig().syncTimeout();
|
||||
}
|
||||
|
||||
protected final Optional<GameState> sync(Function<GameData, GameState> state) {
|
||||
return sync(getData(), state);
|
||||
}
|
||||
|
||||
protected final Optional<GameState> sync(GameData data, Function<GameData, GameState> state) {
|
||||
return Optional.of(new SyncState(data, state));
|
||||
}
|
||||
//</editor-fold>
|
||||
|
||||
public Optional<GameState> onMessage(Game game, UUID player, PlayerMessage message) {
|
||||
|
@@ -205,6 +205,11 @@ public final class GameData {
|
||||
return this;
|
||||
}
|
||||
|
||||
public GameData keepAll() {
|
||||
Arrays.fill(required, true);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns {@code this} if this map contains a mapping for each of the specified keys or throws a
|
||||
* {@link NoSuchElementException} otherwise.
|
||||
|
@@ -11,15 +11,18 @@ import java.util.HashSet;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static eu.jonahbauer.wizard.common.messages.observer.UserInputMessage.Action.SYNC;
|
||||
import static eu.jonahbauer.wizard.core.machine.states.GameData.PLAYERS;
|
||||
|
||||
public abstract class SyncState extends GameState {
|
||||
public final class SyncState extends GameState implements TransientState {
|
||||
private final transient Set<UUID> ready = new HashSet<>();
|
||||
private final Function<GameData, GameState> nextState;
|
||||
|
||||
public SyncState(GameData data) {
|
||||
super(data);
|
||||
public SyncState(GameData data, Function<GameData, GameState> nextState) {
|
||||
super(data.keepAll());
|
||||
this.nextState = nextState;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -34,7 +37,7 @@ public abstract class SyncState extends GameState {
|
||||
ready.add(player);
|
||||
|
||||
if (ready.size() == get(PLAYERS).size()) {
|
||||
return Optional.of(getNextState());
|
||||
return Optional.of(nextState.apply(getData()));
|
||||
} else {
|
||||
return Optional.empty();
|
||||
}
|
||||
@@ -46,8 +49,6 @@ public abstract class SyncState extends GameState {
|
||||
@Override
|
||||
public Optional<GameState> onTimeout(Game game) {
|
||||
game.notify(new TimeoutMessage());
|
||||
return Optional.of(getNextState());
|
||||
return Optional.of(nextState.apply(getData()));
|
||||
}
|
||||
|
||||
protected abstract GameState getNextState();
|
||||
}
|
||||
|
@@ -0,0 +1,4 @@
|
||||
package eu.jonahbauer.wizard.core.machine.states;
|
||||
|
||||
public interface TransientState {
|
||||
}
|
@@ -37,11 +37,12 @@ public final class Dealing extends RoundState {
|
||||
}
|
||||
|
||||
Card trumpCard = deck.draw();
|
||||
return transition(new DeterminingTrump(
|
||||
return sync(
|
||||
getData().with(
|
||||
HANDS, Map.copyOf(hands),
|
||||
TRUMP_CARD, trumpCard
|
||||
)
|
||||
));
|
||||
),
|
||||
DeterminingTrump::new
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@@ -103,6 +103,6 @@ public final class DeterminingTrump extends RoundState {
|
||||
} else {
|
||||
game.notify(new TrumpMessage(get(TRUMP_CARD), trumpSuit));
|
||||
}
|
||||
return transition(new TrumpDetermined(data));
|
||||
return sync(data, Predicting::new);
|
||||
}
|
||||
}
|
||||
|
@@ -1,17 +1,19 @@
|
||||
package eu.jonahbauer.wizard.core.machine.states.round;
|
||||
|
||||
import eu.jonahbauer.wizard.core.machine.Game;
|
||||
import eu.jonahbauer.wizard.core.machine.GameState;
|
||||
import eu.jonahbauer.wizard.core.machine.states.GameData;
|
||||
import eu.jonahbauer.wizard.core.machine.states.SyncState;
|
||||
|
||||
public final class StartingRound extends SyncState {
|
||||
import java.util.Optional;
|
||||
|
||||
public final class StartingRound extends RoundState {
|
||||
|
||||
public StartingRound(GameData data) {
|
||||
super(RoundState.requirements(data));
|
||||
super(data);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected GameState getNextState() {
|
||||
return new Dealing(getData());
|
||||
public Optional<GameState> onEnter(Game context) {
|
||||
return sync(Dealing::new);
|
||||
}
|
||||
}
|
||||
|
@@ -1,19 +0,0 @@
|
||||
package eu.jonahbauer.wizard.core.machine.states.round;
|
||||
|
||||
import eu.jonahbauer.wizard.core.machine.GameState;
|
||||
import eu.jonahbauer.wizard.core.machine.states.GameData;
|
||||
import eu.jonahbauer.wizard.core.machine.states.SyncState;
|
||||
|
||||
import static eu.jonahbauer.wizard.core.machine.states.GameData.*;
|
||||
|
||||
public final class TrumpDetermined extends SyncState {
|
||||
|
||||
public TrumpDetermined(GameData data) {
|
||||
super(RoundState.requirements(data).requireEach(PLAYERS, HANDS).require(PREDICTIONS, TRUMP_SUIT, CURRENT_PLAYER));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected GameState getNextState() {
|
||||
return new Predicting(getData());
|
||||
}
|
||||
}
|
@@ -1,18 +1,21 @@
|
||||
package eu.jonahbauer.wizard.core.machine.states.trick;
|
||||
|
||||
import eu.jonahbauer.wizard.core.machine.Game;
|
||||
import eu.jonahbauer.wizard.core.machine.GameState;
|
||||
import eu.jonahbauer.wizard.core.machine.states.GameData;
|
||||
import eu.jonahbauer.wizard.core.machine.states.SyncState;
|
||||
import eu.jonahbauer.wizard.core.machine.states.round.RoundState;
|
||||
|
||||
public final class StartingTrick extends SyncState {
|
||||
import java.util.Optional;
|
||||
|
||||
public final class StartingTrick extends TrickState {
|
||||
|
||||
public StartingTrick(GameData data) {
|
||||
super(RoundState.requirements(TrickState.requirements(data)));
|
||||
super(data);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected GameState getNextState() {
|
||||
return new PlayingCard(getData());
|
||||
public Optional<GameState> onEnter(Game context) {
|
||||
return sync(PlayingCard::new);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user