57 lines
1.5 KiB
Java
57 lines
1.5 KiB
Java
|
|
package com.alttd.hunger_games.services;
|
||
|
|
|
||
|
|
import com.alttd.hunger_games.data_objects.ROUND_STATE;
|
||
|
|
|
||
|
|
import java.util.ArrayList;
|
||
|
|
import java.util.List;
|
||
|
|
import java.util.Optional;
|
||
|
|
|
||
|
|
public class Round {
|
||
|
|
|
||
|
|
private static Round instance = null;
|
||
|
|
private final List<RoundListener> listeners = new ArrayList<>();
|
||
|
|
private ROUND_STATE roundState = null;
|
||
|
|
|
||
|
|
private Round() {
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
public static Round createSingletonInstance() throws IllegalStateException {
|
||
|
|
if (instance != null) {
|
||
|
|
throw new IllegalStateException("Round is already initialized.");
|
||
|
|
}
|
||
|
|
instance = new Round();
|
||
|
|
return instance;
|
||
|
|
}
|
||
|
|
|
||
|
|
public ROUND_STATE register(RoundListener listener) {
|
||
|
|
listeners.add(listener);
|
||
|
|
return roundState;
|
||
|
|
}
|
||
|
|
|
||
|
|
public void unregister(RoundListener listener) {
|
||
|
|
listeners.remove(listener);
|
||
|
|
}
|
||
|
|
|
||
|
|
public void reset() {
|
||
|
|
listeners.forEach(RoundListener::roundReset);
|
||
|
|
}
|
||
|
|
|
||
|
|
public void start() {
|
||
|
|
roundState = ROUND_STATE.PLAYER_REGISTRATION;
|
||
|
|
listeners.forEach(roundListener -> roundListener.stateChange(roundState));
|
||
|
|
}
|
||
|
|
|
||
|
|
public void nextStage() {
|
||
|
|
Optional<ROUND_STATE> optionalNextRoundState = roundState.next();
|
||
|
|
if (optionalNextRoundState.isEmpty()) {
|
||
|
|
roundState = null;
|
||
|
|
reset();
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
roundState = optionalNextRoundState.get();
|
||
|
|
listeners.forEach(roundListener -> roundListener.stateChange(roundState));
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|