Tweak system to only have game servers convert during normal saves

This commit is contained in:
AlexTheCoder 2017-11-07 01:17:56 -05:00 committed by Alexander Meech
parent 6325b522ed
commit e835747469
4 changed files with 131 additions and 61 deletions

View File

@ -12,6 +12,7 @@ public class PlayerStats
private final Object lock = new Object();
private Map<String, Long> _stats = new HashMap<>();
private Map<String, Long> _statsOld = new HashMap<>();
/**
* Add a value to the specified stat
@ -43,6 +44,14 @@ public class PlayerStats
return value;
}
}
void setStatOld(String statName, long value)
{
synchronized (lock)
{
_stats.put(statName, value);
}
}
/**
* Gets the value for the specified stat
@ -51,6 +60,24 @@ public class PlayerStats
* @return The value of the stat if it exists, or 0 if it does not
*/
public long getStat(String statName)
{
synchronized (lock)
{
long cur = _stats.getOrDefault(statName, 0L);
long old = _statsOld.getOrDefault(statName, 0L);
return cur + (old > cur ? old - cur : 0);
}
}
public long getStatOld(String statName)
{
synchronized (lock)
{
return _statsOld.getOrDefault(statName, 0L);
}
}
public long getJustCurrentStat(String statName)
{
synchronized (lock)
{

View File

@ -5,7 +5,6 @@ import java.sql.SQLException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.Future;
@ -20,10 +19,9 @@ import org.bukkit.plugin.java.JavaPlugin;
import mineplex.core.MiniDbClientPlugin;
import mineplex.core.account.CoreClient;
import mineplex.core.account.CoreClientManager;
import mineplex.core.account.ILoginProcessor;
import mineplex.core.account.permissions.Permission;
import mineplex.core.account.permissions.PermissionGroup;
import mineplex.core.common.util.F;
import mineplex.core.common.util.UtilPlayer;
import mineplex.core.common.util.UtilServer;
import mineplex.core.common.util.UtilTasks;
import mineplex.core.leaderboard.LeaderboardManager;
@ -65,6 +63,31 @@ public class StatsManager extends MiniDbClientPlugin<PlayerStats>
_coreClientManager = clientManager;
_leaderboard = new LeaderboardManager(plugin);
clientManager.addStoredProcedureLoginProcessor(new ILoginProcessor()
{
public String getName()
{
return "Stat Old Selector";
}
@Override
public void processLoginResultSet(String playerName, UUID uuid, int accountId, ResultSet resultSet) throws SQLException
{
PlayerStats stats = Get(uuid);
while (resultSet.next())
{
stats.setStatOld(resultSet.getString(1), resultSet.getLong(2));
}
}
@Override
public String getQuery(int accountId, String uuid, String name)
{
return "SELECT stats.name, value FROM accountStat INNER JOIN stats ON stats.id = accountStat.statId WHERE accountStat.accountId = '" + accountId + "';";
}
});
UtilScheduler.runAsyncEvery(UpdateType.SEC, () ->
{
@ -132,7 +155,6 @@ public class StatsManager extends MiniDbClientPlugin<PlayerStats>
});
}
/**
* Increments a stat for the given player by the specified amount
*
@ -144,12 +166,15 @@ public class StatsManager extends MiniDbClientPlugin<PlayerStats>
return;
CoreClient client = _coreClientManager.Get(player);
long oldCompositeValue = Get(player).getStat(statName);
long oldValue = Get(player).getJustCurrentStat(statName);
long oldOldValue = Get(player).getStatOld(statName);
final long incrementBy = value + (oldOldValue > oldValue ? (oldOldValue - oldValue) : 0L);
long newValue = Get(player).addStat(statName, incrementBy);
long oldValue = Get(player).getStat(statName);
long newValue = Get(player).addStat(statName, value);
UtilServer.getServer().getPluginManager().callEvent(new StatChangeEvent(player, statName, oldValue, newValue));
registerNewStat(statName, () -> addToQueue(statName, client, value));
UtilServer.getServer().getPluginManager().callEvent(new StatChangeEvent(player, statName, oldCompositeValue, newValue));
registerNewStat(statName, () -> addToQueue(statName, client, incrementBy));
}
@ -163,6 +188,10 @@ public class StatsManager extends MiniDbClientPlugin<PlayerStats>
{
if (value < 0)
return;
if (0 == 0)
{
return;
}
CoreClient client = _coreClientManager.Get(player);
@ -180,10 +209,25 @@ public class StatsManager extends MiniDbClientPlugin<PlayerStats>
{
registerNewStat(statName, () ->
{
Map<Integer, Map<Integer, Long>> uploadQueue = new HashMap<>();
uploadQueue.computeIfAbsent(accountId, key -> new HashMap<>()).put(_stats.get(statName), value);
_repository.saveStats(uploadQueue, false);
_repository.loadStatsFromOld(accountId, oldMap ->
{
runAsync(() ->
{
Map<Integer, Map<Integer, Long>> uploadQueue = new HashMap<>();
uploadQueue.computeIfAbsent(accountId, key -> new HashMap<>()).put(_stats.get(statName), value);
oldMap.forEach((stat, amountPair) ->
{
int statId = _stats.get(stat);
if (uploadQueue.get(accountId).containsKey(statId) && amountPair.getLeft() > amountPair.getRight())
{
long diff = amountPair.getLeft() - amountPair.getRight();
uploadQueue.get(accountId).merge(statId, diff, Long::sum);
}
});
_repository.saveStats(uploadQueue, false);
});
});
});
}
@ -197,6 +241,10 @@ public class StatsManager extends MiniDbClientPlugin<PlayerStats>
{
if (value < 0)
return;
if (0 == 0)
{
return;
}
registerNewStat(statName, () ->
{
@ -210,6 +258,10 @@ public class StatsManager extends MiniDbClientPlugin<PlayerStats>
@Deprecated
private void addToOverRidableQueue(String statName, CoreClient client, long value)
{
if (0 == 0)
{
return;
}
if (client.getAccountId() == -1)
{
System.out.println(String.format("Error: Tried to add %s/%s to overridable queue with -1 account id", client.getName(), client.getUniqueId()));
@ -336,43 +388,6 @@ public class StatsManager extends MiniDbClientPlugin<PlayerStats>
{
setStat(event.getPlayer(), stat, 0);
});
final UUID uuid = event.getPlayer().getUniqueId();
final int accountId = _coreClientManager.getAccountId(event.getPlayer());
UtilPlayer.message(event.getPlayer(), F.main(getName(), "We are currently converting to a different stat storage method, please wait a moment if your stats are not all present!"));
runAsync(() ->
{
_repository.loadStatsFromOld(accountId, statMap ->
{
if (Bukkit.getPlayer(uuid) == null)
{
return;
}
boolean conversionOccurred = false;
Map<String, Long> currentMap = Get(event.getPlayer()).getStats();
for (Entry<String, Long> oldEntry : statMap.entrySet())
{
if (currentMap.containsKey(oldEntry.getKey()))
{
if (currentMap.get(oldEntry.getKey()) < oldEntry.getValue())
{
conversionOccurred = true;
Long diff = oldEntry.getValue() - currentMap.get(oldEntry.getKey());
incrementStat(event.getPlayer(), oldEntry.getKey(), diff);
}
}
else
{
conversionOccurred = true;
incrementStat(event.getPlayer(), oldEntry.getKey(), oldEntry.getValue());
}
}
if (conversionOccurred)
{
UtilPlayer.message(event.getPlayer(), F.main(getName(), "Your old stats have been converted!"));
}
});
});
}
@Override

View File

@ -1,7 +1,8 @@
package mineplex.core.stats;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
@ -19,6 +20,7 @@ import org.jooq.Update;
import org.jooq.impl.DSL;
import org.jooq.types.ULong;
import mineplex.core.common.Pair;
import mineplex.core.common.util.UtilServer;
import mineplex.database.Tables;
import mineplex.serverdata.database.DBPool;
@ -36,18 +38,44 @@ public class StatsRepository extends RepositoryBase
super(DBPool.getAccount());
}
public void loadStatsFromOld(int accountId, Consumer<Map<String, Long>> callback)
public void loadStatsFromOld(int accountId, Consumer<Map<String, Pair<Long, Long>>> callback)
{
executeQuery("SELECT stats.name, value FROM accountStat INNER JOIN stats ON stats.id = accountStat.statId WHERE accountStat.accountId=?;", rs ->
try (Connection c = getConnection();
PreparedStatement oldStatement = c.prepareStatement("SELECT stats.name, value FROM accountStat INNER JOIN stats ON stats.id = accountStat.statId WHERE accountStat.accountId=?;");
PreparedStatement newStatement = c.prepareStatement("SELECT stats.name, value FROM accountStatsAllTime INNER JOIN stats ON stats.id = accountStatsAllTime.statId WHERE accountStatsAllTime.accountId=?;");
)
{
final Map<String, Long> statMap = new HashMap<>();
while (rs.next())
oldStatement.setInt(1, accountId);
newStatement.setInt(1, accountId);
try (ResultSet oldSet = oldStatement.executeQuery();
ResultSet newSet = newStatement.executeQuery();
)
{
statMap.put(rs.getString(1), rs.getLong(2));
final Map<String, Pair<Long, Long>> statMap = new HashMap<>();
while (oldSet.next())
{
statMap.put(oldSet.getString(1), Pair.create(oldSet.getLong(2), 0L));
}
while (newSet.next())
{
if (statMap.containsKey(newSet.getString(1)))
{
statMap.get(newSet.getString(1)).setRight(newSet.getLong(2));
}
else
{
statMap.put(oldSet.getString(1), Pair.create(0L, newSet.getLong(2)));
}
}
UtilServer.runSync(() -> callback.accept(statMap));
}
UtilServer.runSync(() -> callback.accept(statMap));
}, new ColumnInt("accountId", accountId));
}
catch (SQLException e)
{
e.printStackTrace();
UtilServer.runSync(() -> callback.accept(new HashMap<>()));
}
}
/**

View File

@ -75,13 +75,13 @@ public class MobaProgression implements Listener
_host.GetGems(caller).put("Fake Exp", new GemData(exp, false));
caller.sendMessage(F.main("Debug", "Gave you " + F.elem(exp) + " fake exp."));
});
host.registerDebugCommand("setmobalevel", Perm.DEBUG_SETMOBALEVEL_COMMAND, PermissionGroup.DEV, (caller, args) ->
/* host.registerDebugCommand("setmobalevel", Perm.DEBUG_SETMOBALEVEL_COMMAND, PermissionGroup.DEV, (caller, args) ->
{
MobaRole role = MobaRole.valueOf(args[0].toUpperCase());
int exp = getExpFor(Integer.parseInt(args[1]) - 1);
_host.getArcadeManager().GetStatsManager().setStat(caller, _host.GetName() + "." + role.getName() + ".ExpEarned", exp);
caller.sendMessage(F.main("Debug", "Set your " + role.getChatColor() + role.getName() + C.cGray + " level to " + F.elem(getLevel(exp) + 1) + "."));
});
});*/
host.registerDebugCommand("unlockhero", Perm.DEBUG_UNLOCK_HERO_COMMAND, PermissionGroup.DEV, (caller, args) ->
{
Donor donor = _host.getArcadeManager().GetDonation().Get(caller);