Implement round and player state management services along with /hg roundstate command.
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
package com.alttd.hunger_games.services;
|
||||
|
||||
import com.alttd.hunger_games.data_objects.PLAYER_STATE;
|
||||
import com.alttd.hunger_games.data_objects.ROUND_STATE;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class PlayerService implements RoundListener {
|
||||
|
||||
private final RoundService roundService;
|
||||
private ROUND_STATE roundState;
|
||||
|
||||
@Override
|
||||
public void stateChange(ROUND_STATE roundState) {
|
||||
this.roundState = roundState;
|
||||
//TODO: Implement player state change logic
|
||||
}
|
||||
|
||||
@Override
|
||||
public void roundReset() {
|
||||
//TODO: Implement player reset logic
|
||||
}
|
||||
|
||||
public Optional<PLAYER_STATE> registerPlayer(Player player) {
|
||||
if (roundState == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
switch (roundState) {
|
||||
case PLAYER_REGISTRATION -> {
|
||||
}
|
||||
case COUNTDOWN -> {
|
||||
}
|
||||
case KILL_PHASE -> {
|
||||
}
|
||||
case FINALE -> {
|
||||
}
|
||||
case ENDED -> {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
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));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.alttd.hunger_games.services;
|
||||
|
||||
import com.alttd.hunger_games.data_objects.ROUND_STATE;
|
||||
|
||||
public interface RoundListener {
|
||||
|
||||
void stateChange(ROUND_STATE roundState);
|
||||
|
||||
void roundReset();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.alttd.hunger_games.services;
|
||||
|
||||
import com.alttd.hunger_games.data_objects.PLAYER_STATE;
|
||||
import com.alttd.hunger_games.data_objects.ROUND_STATE;
|
||||
import lombok.Getter;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class RoundService implements RoundListener {
|
||||
|
||||
@Getter
|
||||
private ROUND_STATE roundState;
|
||||
private final HashMap<UUID, PLAYER_STATE> players = new HashMap<>();
|
||||
|
||||
public RoundService(Round round) {
|
||||
this.roundState = round.register(this);
|
||||
}
|
||||
|
||||
public Set<UUID> getPlayers(PLAYER_STATE playerState) {
|
||||
return players.entrySet().stream()
|
||||
.filter(entry -> entry.getValue().equals(playerState))
|
||||
.map(Map.Entry::getKey)
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
protected void setPlayerState(UUID uuid, PLAYER_STATE playerState) {
|
||||
players.put(uuid, playerState);
|
||||
}
|
||||
|
||||
public PLAYER_STATE getPlayerState(UUID uuid) {
|
||||
return players.get(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stateChange(ROUND_STATE roundState) {
|
||||
this.roundState = roundState;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void roundReset() {
|
||||
players.clear();
|
||||
roundState = null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user