added sync at start of round and trick
This commit is contained in:
@@ -28,6 +28,11 @@ public abstract class GameState implements TimeoutState<GameState, Game> {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
protected final Optional<GameState> syncTimeout(Game game) {
|
||||
game.timeout(this, getSyncTimeout(game, false));
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
protected final Optional<GameState> transition(GameState state) {
|
||||
return Optional.of(state);
|
||||
}
|
||||
@@ -35,6 +40,10 @@ public abstract class GameState implements TimeoutState<GameState, Game> {
|
||||
protected final long getTimeout(Game game, boolean absolute) {
|
||||
return (absolute ? System.currentTimeMillis() : 0) + game.getConfig().timeout();
|
||||
}
|
||||
|
||||
protected final long getSyncTimeout(Game game, boolean absolute) {
|
||||
return (absolute ? System.currentTimeMillis() : 0) + game.getConfig().syncTimeout();
|
||||
}
|
||||
//</editor-fold>
|
||||
|
||||
public Optional<GameState> onMessage(Game game, UUID player, PlayerMessage message) {
|
||||
|
@@ -1,18 +1,52 @@
|
||||
package eu.jonahbauer.wizard.core.machine.states.round;
|
||||
|
||||
import eu.jonahbauer.wizard.core.machine.states.GameData;
|
||||
import eu.jonahbauer.wizard.common.messages.observer.TimeoutMessage;
|
||||
import eu.jonahbauer.wizard.common.messages.observer.UserInputMessage;
|
||||
import eu.jonahbauer.wizard.common.messages.player.ContinueMessage;
|
||||
import eu.jonahbauer.wizard.common.messages.player.PlayerMessage;
|
||||
import eu.jonahbauer.wizard.core.machine.Game;
|
||||
import eu.jonahbauer.wizard.core.machine.GameState;
|
||||
import eu.jonahbauer.wizard.core.machine.states.GameData;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
import static eu.jonahbauer.wizard.common.messages.observer.UserInputMessage.Action.SYNC;
|
||||
import static eu.jonahbauer.wizard.core.machine.states.GameData.PLAYERS;
|
||||
|
||||
public final class StartingRound extends RoundState {
|
||||
private final transient Set<UUID> ready = new HashSet<>();
|
||||
|
||||
public StartingRound(GameData data) {
|
||||
super(data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<GameState> onEnter(Game game) {
|
||||
return transition(new Dealing(getData()));
|
||||
game.notify(new UserInputMessage(null, SYNC, getSyncTimeout(game, true)));
|
||||
return syncTimeout(game);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<GameState> onMessage(Game game, UUID player, PlayerMessage message) {
|
||||
if (message instanceof ContinueMessage) {
|
||||
ready.add(player);
|
||||
|
||||
if (ready.size() == get(PLAYERS).size()) {
|
||||
return Optional.of(new Dealing(getData()));
|
||||
} else {
|
||||
return Optional.empty();
|
||||
}
|
||||
} else {
|
||||
return super.onMessage(game, player, message);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<GameState> onTimeout(Game game) {
|
||||
game.notify(new TimeoutMessage());
|
||||
return Optional.of(new Dealing(getData()));
|
||||
}
|
||||
}
|
||||
|
@@ -1,18 +1,52 @@
|
||||
package eu.jonahbauer.wizard.core.machine.states.trick;
|
||||
|
||||
import eu.jonahbauer.wizard.common.messages.observer.TimeoutMessage;
|
||||
import eu.jonahbauer.wizard.common.messages.observer.UserInputMessage;
|
||||
import eu.jonahbauer.wizard.common.messages.player.ContinueMessage;
|
||||
import eu.jonahbauer.wizard.common.messages.player.PlayerMessage;
|
||||
import eu.jonahbauer.wizard.core.machine.Game;
|
||||
import eu.jonahbauer.wizard.core.machine.states.GameData;
|
||||
import eu.jonahbauer.wizard.core.machine.GameState;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
import static eu.jonahbauer.wizard.common.messages.observer.UserInputMessage.Action.SYNC;
|
||||
import static eu.jonahbauer.wizard.core.machine.states.GameData.PLAYERS;
|
||||
|
||||
public final class StartingTrick extends TrickState {
|
||||
private final transient Set<UUID> ready = new HashSet<>();
|
||||
|
||||
public StartingTrick(GameData data) {
|
||||
super(data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<GameState> onEnter(Game game) {
|
||||
return transition(new PlayingCard(getData()));
|
||||
game.notify(new UserInputMessage(null, SYNC, getSyncTimeout(game, true)));
|
||||
return syncTimeout(game);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<GameState> onMessage(Game game, UUID player, PlayerMessage message) {
|
||||
if (message instanceof ContinueMessage) {
|
||||
ready.add(player);
|
||||
|
||||
if (ready.size() == get(PLAYERS).size()) {
|
||||
return Optional.of(new PlayingCard(getData()));
|
||||
} else {
|
||||
return Optional.empty();
|
||||
}
|
||||
} else {
|
||||
return super.onMessage(game, player, message);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<GameState> onTimeout(Game game) {
|
||||
game.notify(new TimeoutMessage());
|
||||
return Optional.of(new PlayingCard(getData()));
|
||||
}
|
||||
}
|
||||
|
@@ -16,4 +16,9 @@ public class GameConfiguration {
|
||||
Set<Card> cards;
|
||||
boolean allowExactPredictions;
|
||||
@With long timeout;
|
||||
@With long syncTimeout;
|
||||
|
||||
public GameConfiguration(Set<Card> cards, boolean allowExactPredictions, long timeout) {
|
||||
this(cards, allowExactPredictions, timeout, 10_000);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user