Refactor an Instance model out of the GameRunner
This commit is contained in:
parent
83e6139acc
commit
9b0bdfddc8
@ -10,7 +10,7 @@ import org.bukkit.plugin.ServicePriority;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
|
||||
import gg.malloc.defense.engine.GameRunner;
|
||||
import gg.malloc.defense.model.Instance;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
@ -30,7 +30,7 @@ public class GameEconomy extends AbstractEconomy {
|
||||
|
||||
@Override
|
||||
public EconomyResponse withdrawPlayer(String playerName, double amount) {
|
||||
GameRunner runner = m_plugin.getRunnerForPlayer(playerName);
|
||||
Instance runner = m_plugin.getRunnerForPlayer(playerName);
|
||||
if (runner != null) {
|
||||
Player p = m_plugin.getServer().getPlayer(playerName);
|
||||
if (!has(playerName, amount)) {
|
||||
@ -48,7 +48,7 @@ public class GameEconomy extends AbstractEconomy {
|
||||
|
||||
@Override
|
||||
public EconomyResponse depositPlayer(String playerName, double amount) {
|
||||
GameRunner runner = m_plugin.getRunnerForPlayer(playerName);
|
||||
Instance runner = m_plugin.getRunnerForPlayer(playerName);
|
||||
if (runner != null) {
|
||||
Player p = m_plugin.getServer().getPlayer(playerName);
|
||||
runner.spendMoney(p, -(int)Math.floor(amount));
|
||||
@ -61,7 +61,7 @@ public class GameEconomy extends AbstractEconomy {
|
||||
|
||||
@Override
|
||||
public double getBalance(String playerName) {
|
||||
GameRunner runner = m_plugin.getRunnerForPlayer(playerName);
|
||||
Instance runner = m_plugin.getRunnerForPlayer(playerName);
|
||||
if (runner != null) {
|
||||
Player p = m_plugin.getServer().getPlayer(playerName);
|
||||
return runner.getState().getPlayerBalance(p);
|
||||
|
@ -26,6 +26,7 @@ import java.util.Collection;
|
||||
|
||||
import gg.malloc.defense.model.Arena;
|
||||
import gg.malloc.defense.model.Game;
|
||||
import gg.malloc.defense.model.Instance;
|
||||
|
||||
import gg.malloc.defense.games.LinearGame;
|
||||
import gg.malloc.defense.games.ScaledWaves;
|
||||
@ -50,6 +51,14 @@ public class Plugin extends JavaPlugin {
|
||||
GameEventHandler m_handler = new GameEventHandler();
|
||||
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() {
|
||||
return m_arenas.keySet();
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import gg.malloc.defense.engine.GameRunner;
|
||||
import gg.malloc.defense.model.Instance;
|
||||
|
||||
import gg.malloc.defense.Plugin;
|
||||
|
||||
@ -19,7 +19,7 @@ public class AddPlayerCommand implements CommandExecutor {
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String s, String[] args) {
|
||||
String arenaName = args[0];
|
||||
GameRunner runner = m_plugin.getRunnerForArenaName(arenaName);
|
||||
Instance runner = m_plugin.getRunnerForArenaName(arenaName);
|
||||
if (runner == null) {
|
||||
sender.sendMessage("No such arena '" + arenaName + "'");
|
||||
return true;
|
||||
|
@ -5,7 +5,7 @@ import org.bukkit.command.TabExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import gg.malloc.defense.engine.GameRunner;
|
||||
import gg.malloc.defense.model.Instance;
|
||||
|
||||
import gg.malloc.defense.Plugin;
|
||||
|
||||
@ -46,7 +46,7 @@ public class JoinGameCommand implements TabExecutor {
|
||||
Player player = (Player)sender;
|
||||
String arenaName = args[0];
|
||||
sender.sendMessage("Joining arena '" + arenaName + "'...");
|
||||
GameRunner runner = m_plugin.getRunnerForArenaName(arenaName);
|
||||
Instance runner = m_plugin.getRunnerForArenaName(arenaName);
|
||||
if (runner == null) {
|
||||
sender.sendMessage("No such arena '" + arenaName + "'");
|
||||
return true;
|
||||
|
@ -5,7 +5,7 @@ import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import gg.malloc.defense.engine.GameRunner;
|
||||
import gg.malloc.defense.model.Instance;
|
||||
|
||||
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) {
|
||||
if (sender instanceof Player) {
|
||||
Player player = (Player)sender;
|
||||
GameRunner runner = m_plugin.getRunnerForPlayer(player);
|
||||
Instance runner = m_plugin.getRunnerForPlayer(player);
|
||||
if (runner == null) {
|
||||
sender.sendMessage("You are not currently in a game.");
|
||||
return true;
|
||||
|
@ -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.chat.ComponentBuilder;
|
||||
|
||||
import gg.malloc.defense.engine.GameRunner;
|
||||
import gg.malloc.defense.model.Instance;
|
||||
|
||||
import gg.malloc.defense.Plugin;
|
||||
|
||||
@ -28,7 +28,7 @@ public class ListGamesCommand implements CommandExecutor {
|
||||
String stageName = "Ready to play";
|
||||
ChatColor stageColor = ChatColor.WHITE;
|
||||
if (m_plugin.hasRunnerForArenaName(arenaName)) {
|
||||
GameRunner runner = m_plugin.getRunnerForArenaName(arenaName);
|
||||
Instance runner = m_plugin.getRunnerForArenaName(arenaName);
|
||||
switch(runner.getStage()) {
|
||||
case Idle: stageColor = ChatColor.WHITE;stageName = "Open";break;
|
||||
case Warmup: stageColor = ChatColor.YELLOW;stageName = "" + runner.getPlayers().size() + " players warming up";break;
|
||||
|
@ -5,7 +5,7 @@ import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import gg.malloc.defense.engine.GameRunner;
|
||||
import gg.malloc.defense.model.Instance;
|
||||
|
||||
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) {
|
||||
if (sender instanceof Player) {
|
||||
Player player = (Player)sender;
|
||||
GameRunner runner = m_plugin.getRunnerForPlayer(player);
|
||||
Instance runner = m_plugin.getRunnerForPlayer(player);
|
||||
if (runner != null) {
|
||||
runner.togglePlayerReady(player);
|
||||
} else {
|
||||
|
@ -5,7 +5,7 @@ import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.TabExecutor;
|
||||
import org.bukkit.World;
|
||||
|
||||
import gg.malloc.defense.engine.GameRunner;
|
||||
import gg.malloc.defense.model.Instance;
|
||||
|
||||
import gg.malloc.defense.Plugin;
|
||||
|
||||
@ -23,8 +23,8 @@ public class SetStageCommand implements TabExecutor {
|
||||
ArrayList<String> ret = new ArrayList<>();
|
||||
if (args.length == 1) {
|
||||
String proposal = args[0].toLowerCase();
|
||||
GameRunner.Stage stages[] = GameRunner.Stage.Idle.getDeclaringClass().getEnumConstants();
|
||||
for(GameRunner.Stage stage : stages) {
|
||||
Instance.Stage stages[] = Instance.Stage.Idle.getDeclaringClass().getEnumConstants();
|
||||
for(Instance.Stage stage : stages) {
|
||||
if (stage.toString().toLowerCase().startsWith(proposal)) {
|
||||
ret.add(stage.toString());
|
||||
}
|
||||
@ -50,16 +50,16 @@ public class SetStageCommand implements TabExecutor {
|
||||
sender.sendMessage("Usage: setstage <stage> <arena>");
|
||||
return true;
|
||||
}
|
||||
GameRunner runner = m_plugin.getRunnerForArenaName(args[1]);
|
||||
Instance runner = m_plugin.getRunnerForArenaName(args[1]);
|
||||
if (runner == null) {
|
||||
sender.sendMessage("Unknown arena " + args[1]);
|
||||
return true;
|
||||
}
|
||||
String stateName = args[0].toLowerCase();
|
||||
boolean ret = false;
|
||||
GameRunner.Stage decodedStage = null;
|
||||
GameRunner.Stage stages[] = GameRunner.Stage.Idle.getDeclaringClass().getEnumConstants();
|
||||
for(GameRunner.Stage stage : stages) {
|
||||
Instance.Stage decodedStage = null;
|
||||
Instance.Stage stages[] = Instance.Stage.Idle.getDeclaringClass().getEnumConstants();
|
||||
for(Instance.Stage stage : stages) {
|
||||
if (stage.toString().toLowerCase().equals(stateName)) {
|
||||
decodedStage = stage;
|
||||
break;
|
||||
|
@ -4,6 +4,7 @@ import gg.malloc.defense.model.Arena;
|
||||
import gg.malloc.defense.model.Game;
|
||||
import gg.malloc.defense.model.Spawner;
|
||||
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.ClickEvent;
|
||||
@ -20,6 +21,7 @@ import java.util.logging.Logger;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.Collection;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.GameRule;
|
||||
@ -37,7 +39,7 @@ import org.bukkit.entity.Player;
|
||||
import org.bukkit.entity.Projectile;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
public class GameRunner {
|
||||
public class GameRunner implements Instance {
|
||||
GameState m_state;
|
||||
|
||||
Arena m_arena;
|
||||
@ -53,14 +55,6 @@ public class GameRunner {
|
||||
|
||||
Logger m_log;
|
||||
|
||||
public enum Stage {
|
||||
Idle,
|
||||
Warmup,
|
||||
Countdown,
|
||||
Playing,
|
||||
GameOver
|
||||
}
|
||||
|
||||
TickTask m_fuseTask;
|
||||
TickTask m_countdownTask;
|
||||
TickTask m_lobbyReturnTask;
|
||||
@ -262,6 +256,12 @@ public class GameRunner {
|
||||
return true;
|
||||
}
|
||||
|
||||
UUID m_id = UUID.randomUUID();
|
||||
|
||||
public UUID getId() {
|
||||
return m_id;
|
||||
}
|
||||
|
||||
public Collection<Player> getPlayers() {
|
||||
return m_players.getPlayers();
|
||||
}
|
||||
|
26
src/main/java/gg/malloc/defense/model/Instance.java
Normal file
26
src/main/java/gg/malloc/defense/model/Instance.java
Normal 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);
|
||||
}
|
@ -1,11 +1,9 @@
|
||||
package gg.malloc.defense.model;
|
||||
|
||||
import gg.malloc.defense.engine.GameRunner;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public interface State {
|
||||
GameRunner.Stage getStage();
|
||||
Instance.Stage getStage();
|
||||
|
||||
Progress fuseProgress();
|
||||
Progress waveProgress();
|
||||
|
Loading…
Reference in New Issue
Block a user