finish minigame balancer
This commit is contained in:
@@ -1,13 +1,18 @@
|
|||||||
package us.ajg0702.queue.common.queues;
|
package us.ajg0702.queue.common.queues;
|
||||||
|
|
||||||
import com.google.common.collect.ImmutableList;
|
import com.google.common.collect.ImmutableList;
|
||||||
|
import jdk.nashorn.internal.runtime.Debug;
|
||||||
import us.ajg0702.queue.api.players.AdaptedPlayer;
|
import us.ajg0702.queue.api.players.AdaptedPlayer;
|
||||||
import us.ajg0702.queue.api.players.QueuePlayer;
|
import us.ajg0702.queue.api.players.QueuePlayer;
|
||||||
|
import us.ajg0702.queue.api.queues.Balancer;
|
||||||
import us.ajg0702.queue.api.queues.QueueServer;
|
import us.ajg0702.queue.api.queues.QueueServer;
|
||||||
import us.ajg0702.queue.api.server.AdaptedServer;
|
import us.ajg0702.queue.api.server.AdaptedServer;
|
||||||
import us.ajg0702.queue.api.server.AdaptedServerPing;
|
import us.ajg0702.queue.api.server.AdaptedServerPing;
|
||||||
import us.ajg0702.queue.common.QueueMain;
|
import us.ajg0702.queue.common.QueueMain;
|
||||||
import us.ajg0702.queue.common.players.QueuePlayerImpl;
|
import us.ajg0702.queue.common.players.QueuePlayerImpl;
|
||||||
|
import us.ajg0702.queue.common.queues.balancers.DefaultBalancer;
|
||||||
|
import us.ajg0702.queue.common.queues.balancers.MinigameBalancer;
|
||||||
|
import us.ajg0702.queue.common.utils.Debugger;
|
||||||
import us.ajg0702.utils.common.GenUtils;
|
import us.ajg0702.utils.common.GenUtils;
|
||||||
import us.ajg0702.utils.common.Messages;
|
import us.ajg0702.utils.common.Messages;
|
||||||
|
|
||||||
@@ -30,6 +35,32 @@ public class QueueServerImpl implements QueueServer {
|
|||||||
this.servers = servers;
|
this.servers = servers;
|
||||||
this.main = main;
|
this.main = main;
|
||||||
|
|
||||||
|
List<String> types = main.getConfig().getStringList("balancer-types");
|
||||||
|
for(String type : types) {
|
||||||
|
int colon = type.indexOf(":");
|
||||||
|
if(colon == -1) continue;
|
||||||
|
String groupName = type.substring(0, colon);
|
||||||
|
String balancerType = type.substring(colon+1);
|
||||||
|
|
||||||
|
if(groupName.equals(name)) {
|
||||||
|
boolean valid = true;
|
||||||
|
switch(balancerType.toLowerCase(Locale.ROOT)) {
|
||||||
|
case "minigame":
|
||||||
|
balancer = new MinigameBalancer(this, main);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
balancerType = "default";
|
||||||
|
balancer = new DefaultBalancer(this, main);
|
||||||
|
}
|
||||||
|
Debugger.debug("Using "+balancerType.toLowerCase(Locale.ROOT)+" balancer for "+name);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(balancer == null) {
|
||||||
|
balancer = new DefaultBalancer(this, main);
|
||||||
|
Debugger.debug("Using default balancer for "+name);
|
||||||
|
}
|
||||||
|
|
||||||
for(QueuePlayer queuePlayer : previousPlayers) {
|
for(QueuePlayer queuePlayer : previousPlayers) {
|
||||||
if(queuePlayer.getPlayer() == null) {
|
if(queuePlayer.getPlayer() == null) {
|
||||||
addPlayer(
|
addPlayer(
|
||||||
@@ -64,6 +95,8 @@ public class QueueServerImpl implements QueueServer {
|
|||||||
|
|
||||||
private List<Integer> supportedProtocols = new ArrayList<>();
|
private List<Integer> supportedProtocols = new ArrayList<>();
|
||||||
|
|
||||||
|
private Balancer balancer;
|
||||||
|
|
||||||
|
|
||||||
private int playerCount;
|
private int playerCount;
|
||||||
private int maxPlayers;
|
private int maxPlayers;
|
||||||
@@ -386,36 +419,7 @@ public class QueueServerImpl implements QueueServer {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public AdaptedServer getIdealServer(AdaptedPlayer player) {
|
public AdaptedServer getIdealServer(AdaptedPlayer player) {
|
||||||
HashMap<AdaptedServer, AdaptedServerPing> serverInfos = pings;
|
return getBalancer().getIdealServer(player);
|
||||||
AdaptedServer selected = null;
|
|
||||||
int selectednum = 0;
|
|
||||||
if(serverInfos.keySet().size() == 1) {
|
|
||||||
selected = serverInfos.keySet().iterator().next();
|
|
||||||
} else {
|
|
||||||
for(AdaptedServer si : serverInfos.keySet()) {
|
|
||||||
AdaptedServerPing sp = serverInfos.get(si);
|
|
||||||
if(sp == null) continue;
|
|
||||||
int online = sp.getPlayerCount();
|
|
||||||
if(selected == null) {
|
|
||||||
selected = si;
|
|
||||||
selectednum = online;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if(selectednum > online && main.getQueueManager().findServer(si.getName()).isJoinable(player)) {
|
|
||||||
selected = si;
|
|
||||||
selectednum = online;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(selected == null && serverInfos.size() > 0) {
|
|
||||||
selected = serverInfos.keySet().iterator().next();
|
|
||||||
}
|
|
||||||
if(selected == null) {
|
|
||||||
main.getLogger().warning("Unable to find ideal server, using random server from group.");
|
|
||||||
int r = GenUtils.randomInt(0, getServers().size()-1);
|
|
||||||
selected = getServers().get(r);
|
|
||||||
}
|
|
||||||
return selected;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -433,6 +437,11 @@ public class QueueServerImpl implements QueueServer {
|
|||||||
supportedProtocols = new ArrayList<>(list);
|
supportedProtocols = new ArrayList<>(list);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Balancer getBalancer() {
|
||||||
|
return balancer;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean canJoinFull(AdaptedPlayer player) {
|
public boolean canJoinFull(AdaptedPlayer player) {
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -0,0 +1,55 @@
|
|||||||
|
package us.ajg0702.queue.common.queues.balancers;
|
||||||
|
|
||||||
|
import us.ajg0702.queue.api.players.AdaptedPlayer;
|
||||||
|
import us.ajg0702.queue.api.queues.Balancer;
|
||||||
|
import us.ajg0702.queue.api.queues.QueueServer;
|
||||||
|
import us.ajg0702.queue.api.server.AdaptedServer;
|
||||||
|
import us.ajg0702.queue.api.server.AdaptedServerPing;
|
||||||
|
import us.ajg0702.queue.common.QueueMain;
|
||||||
|
import us.ajg0702.utils.common.GenUtils;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
public class DefaultBalancer implements Balancer {
|
||||||
|
|
||||||
|
private final QueueServer server;
|
||||||
|
private final QueueMain main;
|
||||||
|
public DefaultBalancer(QueueServer server, QueueMain main) {
|
||||||
|
this.server = server;
|
||||||
|
this.main = main;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public AdaptedServer getIdealServer(AdaptedPlayer player) {
|
||||||
|
HashMap<AdaptedServer, AdaptedServerPing> serverInfos = server.getLastPings();
|
||||||
|
AdaptedServer selected = null;
|
||||||
|
int selectednum = 0;
|
||||||
|
if(serverInfos.keySet().size() == 1) {
|
||||||
|
selected = serverInfos.keySet().iterator().next();
|
||||||
|
} else {
|
||||||
|
for(AdaptedServer si : serverInfos.keySet()) {
|
||||||
|
AdaptedServerPing sp = serverInfos.get(si);
|
||||||
|
if(sp == null) continue;
|
||||||
|
int online = sp.getPlayerCount();
|
||||||
|
if(selected == null) {
|
||||||
|
selected = si;
|
||||||
|
selectednum = online;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if(selectednum > online && main.getQueueManager().findServer(si.getName()).isJoinable(player)) {
|
||||||
|
selected = si;
|
||||||
|
selectednum = online;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(selected == null && serverInfos.size() > 0) {
|
||||||
|
selected = serverInfos.keySet().iterator().next();
|
||||||
|
}
|
||||||
|
if(selected == null) {
|
||||||
|
main.getLogger().warning("Unable to find ideal server, using random server from group.");
|
||||||
|
int r = GenUtils.randomInt(0, server.getServers().size()-1);
|
||||||
|
selected = server.getServers().get(r);
|
||||||
|
}
|
||||||
|
return selected;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
package us.ajg0702.queue.common.queues.balancers;
|
||||||
|
|
||||||
|
import us.ajg0702.queue.api.players.AdaptedPlayer;
|
||||||
|
import us.ajg0702.queue.api.queues.Balancer;
|
||||||
|
import us.ajg0702.queue.api.queues.QueueServer;
|
||||||
|
import us.ajg0702.queue.api.server.AdaptedServer;
|
||||||
|
import us.ajg0702.queue.api.server.AdaptedServerPing;
|
||||||
|
import us.ajg0702.queue.common.QueueMain;
|
||||||
|
import us.ajg0702.utils.common.GenUtils;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
public class MinigameBalancer implements Balancer {
|
||||||
|
|
||||||
|
private final QueueServer server;
|
||||||
|
private final QueueMain main;
|
||||||
|
public MinigameBalancer(QueueServer server, QueueMain main) {
|
||||||
|
this.server = server;
|
||||||
|
this.main = main;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public AdaptedServer getIdealServer(AdaptedPlayer player) {
|
||||||
|
HashMap<AdaptedServer, AdaptedServerPing> serverInfos = server.getLastPings();
|
||||||
|
AdaptedServer selected = null;
|
||||||
|
int selectednum = 0;
|
||||||
|
if(serverInfos.keySet().size() == 1) {
|
||||||
|
selected = serverInfos.keySet().iterator().next();
|
||||||
|
} else {
|
||||||
|
for(AdaptedServer si : serverInfos.keySet()) {
|
||||||
|
AdaptedServerPing sp = serverInfos.get(si);
|
||||||
|
if(sp == null) continue;
|
||||||
|
int online = sp.getPlayerCount();
|
||||||
|
int max = sp.getMaxPlayers();
|
||||||
|
if(selected == null) {
|
||||||
|
selected = si;
|
||||||
|
selectednum = online;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if(selectednum < online && online < max && main.getQueueManager().findServer(si.getName()).isJoinable(player)) {
|
||||||
|
selected = si;
|
||||||
|
selectednum = online;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(selected == null && serverInfos.size() > 0) {
|
||||||
|
selected = serverInfos.keySet().iterator().next();
|
||||||
|
}
|
||||||
|
if(selected == null) {
|
||||||
|
main.getLogger().warning("Unable to find ideal server, using random server from group.");
|
||||||
|
int r = GenUtils.randomInt(0, server.getServers().size()-1);
|
||||||
|
selected = server.getServers().get(r);
|
||||||
|
}
|
||||||
|
return selected;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package us.ajg0702.queue.common.utils;
|
||||||
|
|
||||||
|
import us.ajg0702.queue.api.AjQueueAPI;
|
||||||
|
|
||||||
|
public class Debugger {
|
||||||
|
public static void debug(String message) {
|
||||||
|
AjQueueAPI api = AjQueueAPI.getInstance();
|
||||||
|
if(!api.getConfig().getBoolean("debug")) return;
|
||||||
|
api.getLogger().info("[debug] "+message);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
# Dont touch this number please
|
# Dont touch this number please
|
||||||
config-version: 29
|
config-version: 30
|
||||||
|
|
||||||
|
|
||||||
# This is the main config for ajQueue.
|
# This is the main config for ajQueue.
|
||||||
@@ -271,9 +271,13 @@ slash-servers: []
|
|||||||
|
|
||||||
# What balancer should we use?
|
# What balancer should we use?
|
||||||
# If a group is not specified here, then the default one is used
|
# If a group is not specified here, then the default one is used
|
||||||
# Example entry: - bedwars: minigame
|
# Example entry: - "bedwars:minigame"
|
||||||
# Balancers:
|
# Balancers:
|
||||||
# default - Will send the player to the server in the group with the least number of players
|
# default - Will send the player to the server in the group with the least number of players
|
||||||
# minigame - Will send the player to the server with the most players, until that server is full, which it will then send to the next full server
|
# minigame - Will send the player to the server with the most players, until that server is full, which it will then send to the next server
|
||||||
balancer-types:
|
balancer-types:
|
||||||
- bedwars: minigame
|
- "bedwars:minigame"
|
||||||
|
|
||||||
|
|
||||||
|
# Should we print some extra stuff to the console that might help diagnose some issues?
|
||||||
|
debug: false
|
||||||
Reference in New Issue
Block a user