2025-01-24 16:49:17 +00:00
|
|
|
package com.alttd.ctf.game;
|
|
|
|
|
|
|
|
|
|
import com.alttd.ctf.game.phases.ClassSelectionPhase;
|
|
|
|
|
import com.alttd.ctf.game_class.implementations.Fighter;
|
|
|
|
|
import com.alttd.ctf.team.Team;
|
|
|
|
|
import com.alttd.ctf.team.TeamPlayer;
|
|
|
|
|
import org.bukkit.Material;
|
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
|
|
|
|
|
|
import java.time.Duration;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
|
import java.util.concurrent.ScheduledExecutorService;
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
|
|
public class GameManager {
|
|
|
|
|
|
|
|
|
|
ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
|
|
|
|
|
private final HashMap<GamePhase, GamePhaseExecutor> phases;
|
|
|
|
|
private RunningGame runningGame;
|
|
|
|
|
private final HashMap<Integer, Team> teams = new HashMap<>();
|
|
|
|
|
|
|
|
|
|
public GameManager() {
|
|
|
|
|
phases = new HashMap<>();
|
|
|
|
|
//TODO initialize this somewhere else (maybe load it from a json file?)
|
|
|
|
|
phases.put(GamePhase.CLASS_SELECTION, new ClassSelectionPhase(this, new Fighter(List.of(Material.LEATHER_CHESTPLATE), List.of(), new ItemStack(Material.STONE), 15, 3, 5)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Optional<GamePhase> getGamePhase() {
|
|
|
|
|
return runningGame == null ? Optional.empty() : Optional.of(runningGame.getCurrentPhase());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void registerPlayer(Team team, Player player) {
|
|
|
|
|
unregisterPlayer(player);
|
|
|
|
|
teams.get(team.getId()).addPlayer(player.getUniqueId());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void unregisterPlayer(Player player) {
|
|
|
|
|
teams.values().forEach(team -> team.removePlayer(player.getUniqueId()));
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-24 19:12:27 +00:00
|
|
|
public void registerTeam(Team team) {
|
|
|
|
|
teams.put(team.getId(), team);
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-24 16:49:17 +00:00
|
|
|
public Collection<Team> getTeams() {
|
|
|
|
|
return teams.values();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Optional<Team> getTeam(@NotNull UUID uuid) {
|
|
|
|
|
return getTeams().stream().filter(filterTeam -> filterTeam.getPlayer(uuid).isPresent()).findAny();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Optional<TeamPlayer> getTeamPlayer(@NotNull UUID uuid) {
|
|
|
|
|
return getTeams().stream()
|
|
|
|
|
.map(team -> team.getPlayer(uuid))
|
|
|
|
|
.filter(Optional::isPresent)
|
|
|
|
|
.findFirst()
|
|
|
|
|
.orElseGet(Optional::empty);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void start(Duration duration) {
|
|
|
|
|
if (runningGame != null) {
|
|
|
|
|
runningGame.end();
|
|
|
|
|
executorService.shutdown();
|
|
|
|
|
executorService = Executors.newSingleThreadScheduledExecutor();
|
|
|
|
|
}
|
|
|
|
|
runningGame = new RunningGame(this, duration);
|
|
|
|
|
executorService.scheduleAtFixedRate(runningGame, 0, 1, TimeUnit.SECONDS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected GamePhaseExecutor getPhaseExecutor(GamePhase gamePhase) {
|
|
|
|
|
GamePhaseExecutor gamePhaseExecutor = phases.get(gamePhase);
|
|
|
|
|
if (gamePhaseExecutor == null) {
|
|
|
|
|
throw new IllegalArgumentException("No phase executor found for phase " + gamePhase);
|
|
|
|
|
}
|
|
|
|
|
return gamePhaseExecutor;
|
|
|
|
|
}
|
|
|
|
|
}
|