Refactor an Instance model out of the GameRunner

This commit is contained in:
Torrie Fischer 2022-09-11 17:34:16 +02:00
parent 83e6139acc
commit 9b0bdfddc8
11 changed files with 66 additions and 33 deletions

View File

@ -10,7 +10,7 @@ import org.bukkit.plugin.ServicePriority;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import gg.malloc.defense.engine.GameRunner; import gg.malloc.defense.model.Instance;
import java.util.List; import java.util.List;
import java.util.ArrayList; import java.util.ArrayList;
@ -30,7 +30,7 @@ public class GameEconomy extends AbstractEconomy {
@Override @Override
public EconomyResponse withdrawPlayer(String playerName, double amount) { public EconomyResponse withdrawPlayer(String playerName, double amount) {
GameRunner runner = m_plugin.getRunnerForPlayer(playerName); Instance runner = m_plugin.getRunnerForPlayer(playerName);
if (runner != null) { if (runner != null) {
Player p = m_plugin.getServer().getPlayer(playerName); Player p = m_plugin.getServer().getPlayer(playerName);
if (!has(playerName, amount)) { if (!has(playerName, amount)) {
@ -48,7 +48,7 @@ public class GameEconomy extends AbstractEconomy {
@Override @Override
public EconomyResponse depositPlayer(String playerName, double amount) { public EconomyResponse depositPlayer(String playerName, double amount) {
GameRunner runner = m_plugin.getRunnerForPlayer(playerName); Instance runner = m_plugin.getRunnerForPlayer(playerName);
if (runner != null) { if (runner != null) {
Player p = m_plugin.getServer().getPlayer(playerName); Player p = m_plugin.getServer().getPlayer(playerName);
runner.spendMoney(p, -(int)Math.floor(amount)); runner.spendMoney(p, -(int)Math.floor(amount));
@ -61,7 +61,7 @@ public class GameEconomy extends AbstractEconomy {
@Override @Override
public double getBalance(String playerName) { public double getBalance(String playerName) {
GameRunner runner = m_plugin.getRunnerForPlayer(playerName); Instance runner = m_plugin.getRunnerForPlayer(playerName);
if (runner != null) { if (runner != null) {
Player p = m_plugin.getServer().getPlayer(playerName); Player p = m_plugin.getServer().getPlayer(playerName);
return runner.getState().getPlayerBalance(p); return runner.getState().getPlayerBalance(p);

View File

@ -26,6 +26,7 @@ import java.util.Collection;
import gg.malloc.defense.model.Arena; import gg.malloc.defense.model.Arena;
import gg.malloc.defense.model.Game; import gg.malloc.defense.model.Game;
import gg.malloc.defense.model.Instance;
import gg.malloc.defense.games.LinearGame; import gg.malloc.defense.games.LinearGame;
import gg.malloc.defense.games.ScaledWaves; import gg.malloc.defense.games.ScaledWaves;
@ -50,6 +51,14 @@ public class Plugin extends JavaPlugin {
GameEventHandler m_handler = new GameEventHandler(); GameEventHandler m_handler = new GameEventHandler();
GameEconomy m_economy = new GameEconomy(this); GameEconomy m_economy = new GameEconomy(this);
public Collection<Instance> getInstances() {
ArrayList<Instance> ret = new ArrayList<>();
for(GameRunner runner : m_runningGames.values()) {
ret.add(runner);
}
return ret;
}
public Collection<String> arenaNames() { public Collection<String> arenaNames() {
return m_arenas.keySet(); return m_arenas.keySet();
} }

View File

@ -5,7 +5,7 @@ import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import gg.malloc.defense.engine.GameRunner; import gg.malloc.defense.model.Instance;
import gg.malloc.defense.Plugin; import gg.malloc.defense.Plugin;
@ -19,7 +19,7 @@ public class AddPlayerCommand implements CommandExecutor {
@Override @Override
public boolean onCommand(CommandSender sender, Command command, String s, String[] args) { public boolean onCommand(CommandSender sender, Command command, String s, String[] args) {
String arenaName = args[0]; String arenaName = args[0];
GameRunner runner = m_plugin.getRunnerForArenaName(arenaName); Instance runner = m_plugin.getRunnerForArenaName(arenaName);
if (runner == null) { if (runner == null) {
sender.sendMessage("No such arena '" + arenaName + "'"); sender.sendMessage("No such arena '" + arenaName + "'");
return true; return true;

View File

@ -5,7 +5,7 @@ import org.bukkit.command.TabExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import gg.malloc.defense.engine.GameRunner; import gg.malloc.defense.model.Instance;
import gg.malloc.defense.Plugin; import gg.malloc.defense.Plugin;
@ -46,7 +46,7 @@ public class JoinGameCommand implements TabExecutor {
Player player = (Player)sender; Player player = (Player)sender;
String arenaName = args[0]; String arenaName = args[0];
sender.sendMessage("Joining arena '" + arenaName + "'..."); sender.sendMessage("Joining arena '" + arenaName + "'...");
GameRunner runner = m_plugin.getRunnerForArenaName(arenaName); Instance runner = m_plugin.getRunnerForArenaName(arenaName);
if (runner == null) { if (runner == null) {
sender.sendMessage("No such arena '" + arenaName + "'"); sender.sendMessage("No such arena '" + arenaName + "'");
return true; return true;

View File

@ -5,7 +5,7 @@ import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import gg.malloc.defense.engine.GameRunner; import gg.malloc.defense.model.Instance;
import gg.malloc.defense.Plugin; import gg.malloc.defense.Plugin;
@ -20,7 +20,7 @@ public class LeaveGameCommand implements CommandExecutor {
public boolean onCommand(CommandSender sender, Command command, String s, String[] args) { public boolean onCommand(CommandSender sender, Command command, String s, String[] args) {
if (sender instanceof Player) { if (sender instanceof Player) {
Player player = (Player)sender; Player player = (Player)sender;
GameRunner runner = m_plugin.getRunnerForPlayer(player); Instance runner = m_plugin.getRunnerForPlayer(player);
if (runner == null) { if (runner == null) {
sender.sendMessage("You are not currently in a game."); sender.sendMessage("You are not currently in a game.");
return true; return true;

View File

@ -10,7 +10,7 @@ import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.ComponentBuilder; import net.md_5.bungee.api.chat.ComponentBuilder;
import gg.malloc.defense.engine.GameRunner; import gg.malloc.defense.model.Instance;
import gg.malloc.defense.Plugin; import gg.malloc.defense.Plugin;
@ -28,7 +28,7 @@ public class ListGamesCommand implements CommandExecutor {
String stageName = "Ready to play"; String stageName = "Ready to play";
ChatColor stageColor = ChatColor.WHITE; ChatColor stageColor = ChatColor.WHITE;
if (m_plugin.hasRunnerForArenaName(arenaName)) { if (m_plugin.hasRunnerForArenaName(arenaName)) {
GameRunner runner = m_plugin.getRunnerForArenaName(arenaName); Instance runner = m_plugin.getRunnerForArenaName(arenaName);
switch(runner.getStage()) { switch(runner.getStage()) {
case Idle: stageColor = ChatColor.WHITE;stageName = "Open";break; case Idle: stageColor = ChatColor.WHITE;stageName = "Open";break;
case Warmup: stageColor = ChatColor.YELLOW;stageName = "" + runner.getPlayers().size() + " players warming up";break; case Warmup: stageColor = ChatColor.YELLOW;stageName = "" + runner.getPlayers().size() + " players warming up";break;

View File

@ -5,7 +5,7 @@ import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import gg.malloc.defense.engine.GameRunner; import gg.malloc.defense.model.Instance;
import gg.malloc.defense.Plugin; import gg.malloc.defense.Plugin;
@ -20,7 +20,7 @@ public class PlayerReadyCommand implements CommandExecutor {
public boolean onCommand(CommandSender sender, Command command, String s, String[] args) { public boolean onCommand(CommandSender sender, Command command, String s, String[] args) {
if (sender instanceof Player) { if (sender instanceof Player) {
Player player = (Player)sender; Player player = (Player)sender;
GameRunner runner = m_plugin.getRunnerForPlayer(player); Instance runner = m_plugin.getRunnerForPlayer(player);
if (runner != null) { if (runner != null) {
runner.togglePlayerReady(player); runner.togglePlayerReady(player);
} else { } else {

View File

@ -5,7 +5,7 @@ import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor; import org.bukkit.command.TabExecutor;
import org.bukkit.World; import org.bukkit.World;
import gg.malloc.defense.engine.GameRunner; import gg.malloc.defense.model.Instance;
import gg.malloc.defense.Plugin; import gg.malloc.defense.Plugin;
@ -23,8 +23,8 @@ public class SetStageCommand implements TabExecutor {
ArrayList<String> ret = new ArrayList<>(); ArrayList<String> ret = new ArrayList<>();
if (args.length == 1) { if (args.length == 1) {
String proposal = args[0].toLowerCase(); String proposal = args[0].toLowerCase();
GameRunner.Stage stages[] = GameRunner.Stage.Idle.getDeclaringClass().getEnumConstants(); Instance.Stage stages[] = Instance.Stage.Idle.getDeclaringClass().getEnumConstants();
for(GameRunner.Stage stage : stages) { for(Instance.Stage stage : stages) {
if (stage.toString().toLowerCase().startsWith(proposal)) { if (stage.toString().toLowerCase().startsWith(proposal)) {
ret.add(stage.toString()); ret.add(stage.toString());
} }
@ -50,16 +50,16 @@ public class SetStageCommand implements TabExecutor {
sender.sendMessage("Usage: setstage <stage> <arena>"); sender.sendMessage("Usage: setstage <stage> <arena>");
return true; return true;
} }
GameRunner runner = m_plugin.getRunnerForArenaName(args[1]); Instance runner = m_plugin.getRunnerForArenaName(args[1]);
if (runner == null) { if (runner == null) {
sender.sendMessage("Unknown arena " + args[1]); sender.sendMessage("Unknown arena " + args[1]);
return true; return true;
} }
String stateName = args[0].toLowerCase(); String stateName = args[0].toLowerCase();
boolean ret = false; boolean ret = false;
GameRunner.Stage decodedStage = null; Instance.Stage decodedStage = null;
GameRunner.Stage stages[] = GameRunner.Stage.Idle.getDeclaringClass().getEnumConstants(); Instance.Stage stages[] = Instance.Stage.Idle.getDeclaringClass().getEnumConstants();
for(GameRunner.Stage stage : stages) { for(Instance.Stage stage : stages) {
if (stage.toString().toLowerCase().equals(stateName)) { if (stage.toString().toLowerCase().equals(stateName)) {
decodedStage = stage; decodedStage = stage;
break; break;

View File

@ -4,6 +4,7 @@ import gg.malloc.defense.model.Arena;
import gg.malloc.defense.model.Game; import gg.malloc.defense.model.Game;
import gg.malloc.defense.model.Spawner; import gg.malloc.defense.model.Spawner;
import gg.malloc.defense.model.Waypoint; import gg.malloc.defense.model.Waypoint;
import gg.malloc.defense.model.Instance;
import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.ClickEvent; import net.md_5.bungee.api.chat.ClickEvent;
@ -20,6 +21,7 @@ import java.util.logging.Logger;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
import java.util.Collection; import java.util.Collection;
import java.util.UUID;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.GameRule; import org.bukkit.GameRule;
@ -37,7 +39,7 @@ import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile; import org.bukkit.entity.Projectile;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
public class GameRunner { public class GameRunner implements Instance {
GameState m_state; GameState m_state;
Arena m_arena; Arena m_arena;
@ -53,14 +55,6 @@ public class GameRunner {
Logger m_log; Logger m_log;
public enum Stage {
Idle,
Warmup,
Countdown,
Playing,
GameOver
}
TickTask m_fuseTask; TickTask m_fuseTask;
TickTask m_countdownTask; TickTask m_countdownTask;
TickTask m_lobbyReturnTask; TickTask m_lobbyReturnTask;
@ -262,6 +256,12 @@ public class GameRunner {
return true; return true;
} }
UUID m_id = UUID.randomUUID();
public UUID getId() {
return m_id;
}
public Collection<Player> getPlayers() { public Collection<Player> getPlayers() {
return m_players.getPlayers(); return m_players.getPlayers();
} }

View File

@ -0,0 +1,26 @@
package gg.malloc.defense.model;
import java.util.Collection;
import java.util.UUID;
import org.bukkit.entity.Player;
public interface Instance {
public enum Stage {
Idle,
Warmup,
Countdown,
Playing,
GameOver
}
UUID getId();
Stage getStage();
boolean requestTransition(Stage stage);
State getState();
void addPlayer(Player p);
void removePlayer(Player p);
Collection<Player> getPlayers();
void togglePlayerReady(Player p);
void spendMoney(Player p, int amount);
}

View File

@ -1,11 +1,9 @@
package gg.malloc.defense.model; package gg.malloc.defense.model;
import gg.malloc.defense.engine.GameRunner;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
public interface State { public interface State {
GameRunner.Stage getStage(); Instance.Stage getStage();
Progress fuseProgress(); Progress fuseProgress();
Progress waveProgress(); Progress waveProgress();