From 361e91997dc614f8dbcf646d0d64c29daa7bf982 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sun, 19 Jul 2020 15:25:12 +0200 Subject: [PATCH] First full ranked system implementation Signed-off-by: Lixfel --- src/de/steamwar/bungeecore/ArenaMode.java | 19 ++- .../steamwar/bungeecore/SubserverSystem.java | 11 +- .../bungeecore/commands/RankCommand.java | 31 +++++ .../bungeecore/commands/RankedCommand.java | 109 ++++++++++++++---- 4 files changed, 145 insertions(+), 25 deletions(-) diff --git a/src/de/steamwar/bungeecore/ArenaMode.java b/src/de/steamwar/bungeecore/ArenaMode.java index 5c1889e..3c895c9 100644 --- a/src/de/steamwar/bungeecore/ArenaMode.java +++ b/src/de/steamwar/bungeecore/ArenaMode.java @@ -35,6 +35,15 @@ public class ArenaMode { return chatNames; } + public static List getAllRankedChatNames(){ + List chatNames = new LinkedList<>(); + for(ArenaMode mode : byInternal.values()){ + if(mode.isRanked()) + chatNames.addAll(mode.chatNames); + } + return chatNames; + } + public static ArenaMode getByCheckSchemType(String checkSchemType){ return byCheckSchemType.get(checkSchemType); } @@ -50,6 +59,7 @@ public class ArenaMode { private final List maps; private final boolean historic; private final boolean ranked; + private final String schemType; private ArenaMode(String internalName, Configuration config){ this.internalName = internalName; @@ -57,8 +67,9 @@ public class ArenaMode { this.serverJar = config.getString("serverJar"); this.chatNames = config.getStringList("chatNames"); this.maps = config.getStringList("maps"); - this.historic = config.getBoolean("historic"); - this.ranked = config.getBoolean("ranked"); + this.historic = config.getBoolean("historic", false); + this.ranked = config.getBoolean("ranked", false); + this.schemType = config.getString("schemType", null); allModes.add(this); byInternal.put(internalName, this); @@ -113,4 +124,8 @@ public class ArenaMode { public boolean isRanked() { return ranked; } + + public String getSchemType() { + return schemType; + } } diff --git a/src/de/steamwar/bungeecore/SubserverSystem.java b/src/de/steamwar/bungeecore/SubserverSystem.java index 6787ae2..b491417 100644 --- a/src/de/steamwar/bungeecore/SubserverSystem.java +++ b/src/de/steamwar/bungeecore/SubserverSystem.java @@ -62,10 +62,13 @@ public class SubserverSystem { * For event, test and normal arenas: The UUID of the designated leader for the red team * or null (no designated leader). * + * @param ranked + * If the game is a ranked game. + * * @return * The new started subserver. */ - public static Subserver startArena(ArenaMode modus, String map, int eventFightID, int checkSchemID, String serverName, String mapName, UUID player1, UUID player2){ + public static Subserver startArena(ArenaMode modus, String map, int eventFightID, int checkSchemID, String serverName, String mapName, UUID player1, UUID player2, boolean ranked){ //Generate missing parameters int port = freePort(2500); @@ -98,6 +101,8 @@ public class SubserverSystem { cmd.add("-DredLeader=" + player2.toString()); if(checkSchemID != 0) cmd.add("-DcheckSchemID=" + checkSchemID); + if(ranked) + cmd.add("-Dranked=true"); cmd.add("-Xmx2G"); cmd.addAll(JVM_ARGS); cmd.add("-jar"); @@ -136,6 +141,10 @@ public class SubserverSystem { }); } + public static Subserver startArena(ArenaMode modus, String map, int eventFightID, int checkSchemID, String serverName, String mapName, UUID player1, UUID player2){ + return startArena(modus, map, eventFightID, checkSchemID, serverName, mapName, player1, player2, false); + } + public static Subserver startArena(ArenaMode modus, String map){ return startArena(modus, map, 0, 0, null, null, null, null); } diff --git a/src/de/steamwar/bungeecore/commands/RankCommand.java b/src/de/steamwar/bungeecore/commands/RankCommand.java index 4658e6e..c68cc98 100644 --- a/src/de/steamwar/bungeecore/commands/RankCommand.java +++ b/src/de/steamwar/bungeecore/commands/RankCommand.java @@ -1,6 +1,14 @@ package de.steamwar.bungeecore.commands; +import de.steamwar.bungeecore.ArenaMode; +import de.steamwar.bungeecore.BungeeCore; +import de.steamwar.bungeecore.sql.Elo; +import de.steamwar.bungeecore.sql.SteamwarUser; import net.md_5.bungee.api.CommandSender; +import net.md_5.bungee.api.connection.ProxiedPlayer; + +import java.util.ArrayList; +import java.util.List; public class RankCommand extends BasicCommand { public RankCommand() { @@ -9,6 +17,29 @@ public class RankCommand extends BasicCommand { @Override public void execute(CommandSender sender, String[] args) { + if(!(sender instanceof ProxiedPlayer)) + return; + ProxiedPlayer player = (ProxiedPlayer) sender; + BungeeCore.send(player, BungeeCore.CHAT_PREFIX + "§7§lPlatzierungen"); + SteamwarUser user = SteamwarUser.get(player.getUniqueId()); + for(ArenaMode mode : ArenaMode.getAllModes()){ + if(!mode.isRanked()) + continue; + int elo = Elo.getElo(user.getId(), mode.getSchemType()); + int placement = Elo.getPlacement(user.getId(), mode.getSchemType()); + + BungeeCore.send(player, "§e" + placement + "§8. §7mit §e" + elo + " §7Elo§8."); + } + } + + @Override + public Iterable onTabComplete(CommandSender commandSender, String[] args) { + if(args.length > 1) + return new ArrayList<>(); + + List result = ArenaMode.getAllRankedChatNames(); + result.removeIf(name -> !name.startsWith(args[0])); + return result; } } diff --git a/src/de/steamwar/bungeecore/commands/RankedCommand.java b/src/de/steamwar/bungeecore/commands/RankedCommand.java index f8f9aa0..6ef3999 100644 --- a/src/de/steamwar/bungeecore/commands/RankedCommand.java +++ b/src/de/steamwar/bungeecore/commands/RankedCommand.java @@ -1,13 +1,15 @@ package de.steamwar.bungeecore.commands; -import de.steamwar.bungeecore.ArenaMode; -import de.steamwar.bungeecore.BungeeCore; -import de.steamwar.bungeecore.Servertype; -import de.steamwar.bungeecore.Subserver; +import de.steamwar.bungeecore.*; import de.steamwar.bungeecore.sql.Elo; import de.steamwar.bungeecore.sql.SteamwarUser; +import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.CommandSender; import net.md_5.bungee.api.ProxyServer; +import net.md_5.bungee.api.chat.ClickEvent; +import net.md_5.bungee.api.chat.ComponentBuilder; +import net.md_5.bungee.api.chat.HoverEvent; +import net.md_5.bungee.api.chat.TextComponent; import net.md_5.bungee.api.connection.ProxiedPlayer; import java.util.*; @@ -15,7 +17,7 @@ import java.util.concurrent.TimeUnit; public class RankedCommand extends BasicCommand { - private static Map queues = new HashMap(); + private static Map queues = new HashMap<>(); public RankedCommand() { super("ranked", null); @@ -29,7 +31,7 @@ public class RankedCommand extends BasicCommand { @Override public void execute(CommandSender sender, String[] args) { if(args.length < 1){ - BungeeCore.send(sender, BungeeCore.CHAT_PREFIX + "§8/§7ranked §8[§eSpielmodus§8]"); + getModes(sender, "/ranked"); return; }else if(!(sender instanceof ProxiedPlayer)) return; @@ -43,13 +45,42 @@ public class RankedCommand extends BasicCommand { } ProxiedPlayer player = (ProxiedPlayer) sender; - queues.get(mode) + queues.get(mode).togglePlayer(player); + } + + @Override + public Iterable onTabComplete(CommandSender commandSender, String[] args) { + if(args.length > 1) + return new ArrayList<>(); + + List result = ArenaMode.getAllRankedChatNames(); + result.removeIf(name -> !name.startsWith(args[0])); + return result; + } + + static void getModes(CommandSender sender, String precommand){ + TextComponent start = new TextComponent(); + TextComponent current = start; + for(ArenaMode mode : ArenaMode.getAllModes()){ + if(!mode.hasChatName() || !mode.isRanked()) + continue; + String command = precommand + mode.getChatName(); + current.setBold(true); + current.setColor(ChatColor.GRAY); + current.setText(mode.getChatName() + " "); + current.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("§e" + command).create())); + current.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, command)); + if(current != start) + start.addExtra(current); + current = new TextComponent(); + } + + sender.sendMessage(start); } private void checkForGames(){ - for(WaitingQueue queue : queues.values()){ + for(WaitingQueue queue : queues.values()) queue.checkForGames(); - } } private static class WaitingQueue{ @@ -61,23 +92,63 @@ public class RankedCommand extends BasicCommand { this.mode = mode; } - private void addPlayer(ProxiedPlayer player){ - players.add(new WaitingPlayer(player, mode)); + private void togglePlayer(ProxiedPlayer player){ + for(WaitingPlayer wp : players){ + if(wp.player == player){ + BungeeCore.send(player, BungeeCore.CHAT_PREFIX + "§cRanglistenspiel-Warteschlange verlassen"); + players.remove(wp); + return; + } + } + + BungeeCore.send(player, BungeeCore.CHAT_PREFIX + "§aRanglistenspiel-Warteschlange betreten"); + BungeeCore.send(player, BungeeCore.CHAT_PREFIX + "§7Wiederhole den Befehl zum Verlassen der Warteschlange"); + players.add(new WaitingPlayer(player, mode.getSchemType())); + } + + private void removeFromAll(ProxiedPlayer player){ + for(WaitingQueue queue : queues.values()){ + Iterator it = queue.players.iterator(); + while(it.hasNext()){ + WaitingPlayer wp = it.next(); + if(wp.player == player){ + it.remove(); + break; + } + } + } } private void checkForGames(){ players.removeIf(wp -> !wp.doesStillWait()); + Map inRange = new HashMap<>(); + + //Find games for(WaitingPlayer wp1 : players){ + if(inRange.containsValue(wp1)) + continue; + for(WaitingPlayer wp2 : players){ - if(wp1 == wp2) + if(wp1 == wp2 || inRange.containsKey(wp2) || inRange.containsValue(wp2)) continue; - if(wp1.inRange(wp2)){ - //TODO: Open game opportunity - } + if(wp1.inRange(wp2)) + inRange.putIfAbsent(wp1, wp2); } wp1.increment(); } + + for(Map.Entry entry : inRange.entrySet()){ + WaitingPlayer wp1 = entry.getKey(); + WaitingPlayer wp2 = entry.getValue(); + BungeeCore.send(wp1.player, BungeeCore.CHAT_PREFIX + "§eGegner gefunden! Ranglistenspiel startet."); + BungeeCore.send(wp2.player, BungeeCore.CHAT_PREFIX + "§eGegner gefunden! Ranglistenspiel startet."); + removeFromAll(wp1.player); + removeFromAll(wp2.player); + + String serverName = wp1.player.getName() + " vs " + wp2.player.getName(); + SubserverSystem.startArena(mode, mode.getRandomMap(), 0, 0, serverName, serverName.replace(' ', '_'), wp1.player.getUniqueId(), wp2.player.getUniqueId(), true); + } } } @@ -105,21 +176,15 @@ public class RankedCommand extends BasicCommand { if(!player.isConnected()) return false; - //TODO: Check if has open match - Subserver subserver = Subserver.getSubserver(player); if(subserver == null) return true; if(subserver.getType() == Servertype.ARENA){ - BungeeCore.send(player, BungeeCore.CHAT_PREFIX + "§cRanglistenspiel-Warteschlange abgebrochen"); + BungeeCore.send(player, BungeeCore.CHAT_PREFIX + "§cRanglistenspiel-Warteschlange verlassen"); return false; } return true; } } - - private static class OpenMatch{ - //TODO: Open matches - } }