Restructured the providers, now using SecureRandom

This commit is contained in:
Jaime Martínez Rincón 2017-12-28 22:37:18 +01:00
parent 2c0b36c0cd
commit 8d47f52265
9 changed files with 96 additions and 33 deletions

View File

@ -2,7 +2,13 @@ package com.jaimemartz.playerbalancer.connection;
import com.jaimemartz.playerbalancer.PlayerBalancer; import com.jaimemartz.playerbalancer.PlayerBalancer;
import com.jaimemartz.playerbalancer.connection.provider.AbstractProvider; import com.jaimemartz.playerbalancer.connection.provider.AbstractProvider;
import com.jaimemartz.playerbalancer.connection.provider.types.*; import com.jaimemartz.playerbalancer.connection.provider.types.NullProvider;
import com.jaimemartz.playerbalancer.connection.provider.types.progressive.ProgressiveFillerProvider;
import com.jaimemartz.playerbalancer.connection.provider.types.progressive.ProgressiveLowestProvider;
import com.jaimemartz.playerbalancer.connection.provider.types.progressive.ProgressiveProvider;
import com.jaimemartz.playerbalancer.connection.provider.types.random.RandomFillerProvider;
import com.jaimemartz.playerbalancer.connection.provider.types.random.RandomLowestProvider;
import com.jaimemartz.playerbalancer.connection.provider.types.random.RandomProvider;
import com.jaimemartz.playerbalancer.section.ServerSection; import com.jaimemartz.playerbalancer.section.ServerSection;
import net.md_5.bungee.api.config.ServerInfo; import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.connection.ProxiedPlayer;
@ -28,8 +34,8 @@ public enum ProviderType {
} }
}, },
LOWEST { RANDOM_LOWEST {
LowestProvider provider = new LowestProvider(); RandomLowestProvider provider = new RandomLowestProvider();
@Override @Override
public ServerInfo requestTarget(PlayerBalancer plugin, ServerSection section, List<ServerInfo> servers, ProxiedPlayer player) { public ServerInfo requestTarget(PlayerBalancer plugin, ServerSection section, List<ServerInfo> servers, ProxiedPlayer player) {
@ -37,8 +43,8 @@ public enum ProviderType {
} }
}, },
BALANCED { RANDOM_FILLER {
BalancedProvider provider = new BalancedProvider(); RandomFillerProvider provider = new RandomFillerProvider();
@Override @Override
public ServerInfo requestTarget(PlayerBalancer plugin, ServerSection section, List<ServerInfo> servers, ProxiedPlayer player) { public ServerInfo requestTarget(PlayerBalancer plugin, ServerSection section, List<ServerInfo> servers, ProxiedPlayer player) {
@ -55,8 +61,17 @@ public enum ProviderType {
} }
}, },
FILLER { PROGRESSIVE_LOWEST {
FillerProvider provider = new FillerProvider(); ProgressiveLowestProvider provider = new ProgressiveLowestProvider();
@Override
public ServerInfo requestTarget(PlayerBalancer plugin, ServerSection section, List<ServerInfo> servers, ProxiedPlayer player) {
return provider.requestTarget(plugin, section, servers, player);
}
},
PROGRESSIVE_FILLER {
ProgressiveFillerProvider provider = new ProgressiveFillerProvider();
@Override @Override
public ServerInfo requestTarget(PlayerBalancer plugin, ServerSection section, List<ServerInfo> servers, ProxiedPlayer player) { public ServerInfo requestTarget(PlayerBalancer plugin, ServerSection section, List<ServerInfo> servers, ProxiedPlayer player) {

View File

@ -1,4 +1,4 @@
package com.jaimemartz.playerbalancer.connection.provider.types; package com.jaimemartz.playerbalancer.connection.provider.types.progressive;
import com.jaimemartz.playerbalancer.PlayerBalancer; import com.jaimemartz.playerbalancer.PlayerBalancer;
import com.jaimemartz.playerbalancer.connection.provider.AbstractProvider; import com.jaimemartz.playerbalancer.connection.provider.AbstractProvider;
@ -9,7 +9,7 @@ import net.md_5.bungee.api.connection.ProxiedPlayer;
import java.util.List; import java.util.List;
public class FillerProvider extends AbstractProvider { public class ProgressiveFillerProvider extends AbstractProvider {
@Override @Override
public ServerInfo requestTarget(PlayerBalancer plugin, ServerSection section, List<ServerInfo> servers, ProxiedPlayer player) { public ServerInfo requestTarget(PlayerBalancer plugin, ServerSection section, List<ServerInfo> servers, ProxiedPlayer player) {
int max = Integer.MIN_VALUE; int max = Integer.MIN_VALUE;

View File

@ -1,4 +1,4 @@
package com.jaimemartz.playerbalancer.connection.provider.types; package com.jaimemartz.playerbalancer.connection.provider.types.progressive;
import com.jaimemartz.playerbalancer.PlayerBalancer; import com.jaimemartz.playerbalancer.PlayerBalancer;
import com.jaimemartz.playerbalancer.connection.provider.AbstractProvider; import com.jaimemartz.playerbalancer.connection.provider.AbstractProvider;
@ -8,7 +8,7 @@ import net.md_5.bungee.api.connection.ProxiedPlayer;
import java.util.List; import java.util.List;
public class LowestProvider extends AbstractProvider { public class ProgressiveLowestProvider extends AbstractProvider {
@Override @Override
public ServerInfo requestTarget(PlayerBalancer plugin, ServerSection section, List<ServerInfo> servers, ProxiedPlayer player) { public ServerInfo requestTarget(PlayerBalancer plugin, ServerSection section, List<ServerInfo> servers, ProxiedPlayer player) {
int min = Integer.MAX_VALUE; int min = Integer.MAX_VALUE;

View File

@ -1,4 +1,4 @@
package com.jaimemartz.playerbalancer.connection.provider.types; package com.jaimemartz.playerbalancer.connection.provider.types.progressive;
import com.jaimemartz.playerbalancer.PlayerBalancer; import com.jaimemartz.playerbalancer.PlayerBalancer;
import com.jaimemartz.playerbalancer.connection.provider.AbstractProvider; import com.jaimemartz.playerbalancer.connection.provider.AbstractProvider;
@ -8,7 +8,6 @@ import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.connection.ProxiedPlayer;
import java.util.List; import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
public class ProgressiveProvider extends AbstractProvider { public class ProgressiveProvider extends AbstractProvider {
@Override @Override
@ -20,6 +19,6 @@ public class ProgressiveProvider extends AbstractProvider {
} }
} }
return servers.get(ThreadLocalRandom.current().nextInt(servers.size())); return null;
} }
} }

View File

@ -0,0 +1,35 @@
package com.jaimemartz.playerbalancer.connection.provider.types.random;
import com.jaimemartz.playerbalancer.PlayerBalancer;
import com.jaimemartz.playerbalancer.connection.provider.AbstractProvider;
import com.jaimemartz.playerbalancer.section.ServerSection;
import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import java.util.ArrayList;
import java.util.List;
import static com.jaimemartz.playerbalancer.utils.RandomUtils.random;
public class RandomFillerProvider extends AbstractProvider {
@Override
public ServerInfo requestTarget(PlayerBalancer plugin, ServerSection section, List<ServerInfo> servers, ProxiedPlayer player) {
List<ServerInfo> results = new ArrayList<>();
int max = Integer.MIN_VALUE;
for (ServerInfo server : servers) {
int count = plugin.getNetworkManager().getPlayers(server);
if (count >= max) {
if (count > max) {
max = count;
results.clear();
}
results.add(server);
}
}
return random(results);
}
}

View File

@ -1,4 +1,4 @@
package com.jaimemartz.playerbalancer.connection.provider.types; package com.jaimemartz.playerbalancer.connection.provider.types.random;
import com.jaimemartz.playerbalancer.PlayerBalancer; import com.jaimemartz.playerbalancer.PlayerBalancer;
import com.jaimemartz.playerbalancer.connection.provider.AbstractProvider; import com.jaimemartz.playerbalancer.connection.provider.AbstractProvider;
@ -8,9 +8,10 @@ import net.md_5.bungee.api.connection.ProxiedPlayer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
public class BalancedProvider extends AbstractProvider { import static com.jaimemartz.playerbalancer.utils.RandomUtils.random;
public class RandomLowestProvider extends AbstractProvider {
@Override @Override
public ServerInfo requestTarget(PlayerBalancer plugin, ServerSection section, List<ServerInfo> servers, ProxiedPlayer player) { public ServerInfo requestTarget(PlayerBalancer plugin, ServerSection section, List<ServerInfo> servers, ProxiedPlayer player) {
List<ServerInfo> results = new ArrayList<>(); List<ServerInfo> results = new ArrayList<>();
@ -28,6 +29,6 @@ public class BalancedProvider extends AbstractProvider {
} }
} }
return results.get(ThreadLocalRandom.current().nextInt(results.size())); return random(results);
} }
} }

View File

@ -1,4 +1,4 @@
package com.jaimemartz.playerbalancer.connection.provider.types; package com.jaimemartz.playerbalancer.connection.provider.types.random;
import com.jaimemartz.playerbalancer.PlayerBalancer; import com.jaimemartz.playerbalancer.PlayerBalancer;
import com.jaimemartz.playerbalancer.connection.provider.AbstractProvider; import com.jaimemartz.playerbalancer.connection.provider.AbstractProvider;
@ -7,11 +7,12 @@ import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.connection.ProxiedPlayer;
import java.util.List; import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import static com.jaimemartz.playerbalancer.utils.RandomUtils.random;
public class RandomProvider extends AbstractProvider { public class RandomProvider extends AbstractProvider {
@Override @Override
public ServerInfo requestTarget(PlayerBalancer plugin, ServerSection section, List<ServerInfo> servers, ProxiedPlayer player) { public ServerInfo requestTarget(PlayerBalancer plugin, ServerSection section, List<ServerInfo> servers, ProxiedPlayer player) {
return servers.get(ThreadLocalRandom.current().nextInt(servers.size())); return random(servers);
} }
} }

View File

@ -0,0 +1,12 @@
package com.jaimemartz.playerbalancer.utils;
import java.security.SecureRandom;
import java.util.List;
public class RandomUtils {
private static final SecureRandom instance = new SecureRandom();
public static <T> T random(List<T> list) {
return list.get(instance.nextInt(list.size()));
}
}

View File

@ -42,21 +42,21 @@ messages {
features { features {
balancer { balancer {
# Here you have an example of what you can do with the sections # Here you have an example of what you can do with the sections
# The plugin will print out info telling you if your config is right or not
# If a section does not have a provider it will be inherit from the parent
# The best way to understand this is to play around with it # The best way to understand this is to play around with it
# You can use regex to match a set of servers instead of adding each server
# You can have as many sections as you want, there is no limit here # You can have as many sections as you want, there is no limit here
# When connecting to a server in a section, you get redirected # If a section does not have a provider it will try to inherit it from the parent
# When connecting to a server on a section while not being on it already, you get distributed
# You can use regex to match a set of servers instead of adding each server individually
# Providers you can use: # Providers you can use: (you can request more!)
# NONE: Returns no server (no one will be able to connect to this section) # NONE: Returns no server (no one will be able to connect to this section)
# BALANCED: Returns a server between the ones with the least players online # RANDOM: Returns a random server, generated by SecureRandom
# LOWEST: Returns the first server found with the least players online # RANDOM_LOWEST: Returns a random server between the ones with the least players online
# RANDOM: Returns a server selected by a RNG algorithm (random) # RANDOM_FILLER: Returns a random server between the ones with the most players online that is not full
# PROGRESSIVE: Returns the first server found that is not full # PROGRESSIVE: Returns the first server that is not full
# FILLER: Returns the server with the most players online that is not full # PROGRESSIVE_LOWEST: Returns the first server with the least players online
# EXTERNAL: Returns the server determined by a provider created by other plugin # PROGRESSIVE_FILLER: Returns the first server with the most players online that is not full
# EXTERNAL: Returns the server calculated by a provider created by other plugin
sections { sections {
auth-lobbies { auth-lobbies {