mirror of
https://github.com/proxiodev/RedisBungee.git
synced 2024-11-23 04:28:01 +00:00
One more attempt to fix player count. Now it's calculated with Lua.
This commit is contained in:
parent
565de9b4c0
commit
12ad0c792b
@ -20,7 +20,6 @@ import net.md_5.bungee.config.ConfigurationProvider;
|
|||||||
import net.md_5.bungee.config.YamlConfiguration;
|
import net.md_5.bungee.config.YamlConfiguration;
|
||||||
import redis.clients.jedis.*;
|
import redis.clients.jedis.*;
|
||||||
import redis.clients.jedis.exceptions.JedisConnectionException;
|
import redis.clients.jedis.exceptions.JedisConnectionException;
|
||||||
import redis.clients.jedis.exceptions.JedisException;
|
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
@ -60,6 +59,7 @@ public final class RedisBungee extends Plugin {
|
|||||||
private ScheduledTask heartbeatTask;
|
private ScheduledTask heartbeatTask;
|
||||||
private boolean usingLua;
|
private boolean usingLua;
|
||||||
private LuaManager.Script serverToPlayersScript;
|
private LuaManager.Script serverToPlayersScript;
|
||||||
|
private LuaManager.Script getPlayerCountScript;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetch the {@link RedisBungeeAPI} object created on plugin start.
|
* Fetch the {@link RedisBungeeAPI} object created on plugin start.
|
||||||
@ -121,15 +121,10 @@ public final class RedisBungee extends Plugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
final Multimap<String, UUID> serversToPlayers() {
|
final Multimap<String, UUID> serversToPlayers() {
|
||||||
if (usingLua) {
|
|
||||||
Collection<String> data = (Collection<String>) serverToPlayersScript.eval(ImmutableList.<String>of(), getServerIds());
|
Collection<String> data = (Collection<String>) serverToPlayersScript.eval(ImmutableList.<String>of(), getServerIds());
|
||||||
|
|
||||||
ImmutableMultimap.Builder<String, UUID> builder = ImmutableMultimap.builder();
|
ImmutableMultimap.Builder<String, UUID> builder = ImmutableMultimap.builder();
|
||||||
|
|
||||||
// TODO: This seems pretty slow, but execution times over the long term seem to stay below that of the
|
|
||||||
// Java implementation, at least. If you have a better idea, I want to see it!
|
|
||||||
String key = null;
|
String key = null;
|
||||||
|
|
||||||
for (String s : data) {
|
for (String s : data) {
|
||||||
if (key == null) {
|
if (key == null) {
|
||||||
key = s;
|
key = s;
|
||||||
@ -141,31 +136,11 @@ public final class RedisBungee extends Plugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return builder.build();
|
return builder.build();
|
||||||
} else {
|
|
||||||
ImmutableMultimap.Builder<String, UUID> multimapBuilder = ImmutableMultimap.builder();
|
|
||||||
for (UUID p : getPlayers()) {
|
|
||||||
String name = dataManager.getServer(p);
|
|
||||||
if (name != null)
|
|
||||||
multimapBuilder.put(name, p);
|
|
||||||
}
|
|
||||||
return multimapBuilder.build();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
final int getCount() {
|
final int getCount() {
|
||||||
int c = 0;
|
Long count = (Long) getPlayerCountScript.eval(ImmutableList.<String>of(), ImmutableList.<String>of());
|
||||||
if (pool != null) {
|
return count.intValue();
|
||||||
try (Jedis rsc = pool.getResource()) {
|
|
||||||
for (String i : getServerIds()) {
|
|
||||||
c += rsc.scard("proxy:" + i + ":usersOnline");
|
|
||||||
}
|
|
||||||
} catch (JedisConnectionException e) {
|
|
||||||
// Redis server has disappeared!
|
|
||||||
getLogger().log(Level.SEVERE, "Unable to get connection from pool - did your Redis server go away?", e);
|
|
||||||
throw new RuntimeException("Unable to get total player count", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return c;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Set<String> getLocalPlayersAsUuidStrings() {
|
private Set<String> getLocalPlayersAsUuidStrings() {
|
||||||
@ -250,6 +225,7 @@ public final class RedisBungee extends Plugin {
|
|||||||
} else {
|
} else {
|
||||||
LuaManager manager = new LuaManager(this);
|
LuaManager manager = new LuaManager(this);
|
||||||
serverToPlayersScript = manager.createScript(IOUtil.readInputStreamAsString(getResourceAsStream("lua/server_to_players.lua")));
|
serverToPlayersScript = manager.createScript(IOUtil.readInputStreamAsString(getResourceAsStream("lua/server_to_players.lua")));
|
||||||
|
getPlayerCountScript = manager.createScript(IOUtil.readInputStreamAsString(getResourceAsStream("lua/get_player_count.lua")));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
24
src/main/resources/lua/get_player_count.lua
Normal file
24
src/main/resources/lua/get_player_count.lua
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
local c = redis.call
|
||||||
|
|
||||||
|
local curTime = c("TIME")
|
||||||
|
local time = tonumber(curTime[1])
|
||||||
|
|
||||||
|
local heartbeats = c("HGETALL", "heartbeats")
|
||||||
|
local total = 0
|
||||||
|
local key
|
||||||
|
|
||||||
|
for _, v in pairs(heartbeats) do
|
||||||
|
if not key then
|
||||||
|
key = v
|
||||||
|
else
|
||||||
|
local n = tonumber(v)
|
||||||
|
if n then
|
||||||
|
if n + 30 >= time then
|
||||||
|
total = total + c("SCARD", "proxy:" .. key .. ":usersOnline")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
key = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return total
|
Loading…
Reference in New Issue
Block a user