Fix outdated stats being loaded when players transfer servers

This commit is contained in:
AlexTheCoder 2017-11-25 19:47:03 -05:00 committed by Alexander Meech
parent 1380ef8e33
commit 02dc0458d9
5 changed files with 85 additions and 39 deletions

View File

@ -32,7 +32,7 @@ public abstract class MiniClientPlugin<DataType> extends MiniPlugin
{ {
synchronized (_clientDataLock) synchronized (_clientDataLock)
{ {
saveData(event.GetName(), event.getAccountId()); saveData(event.GetName(), event.getUniqueId(), event.getAccountId());
_clientData.remove(event.getUniqueId()); _clientData.remove(event.getUniqueId());
} }
} }
@ -57,7 +57,7 @@ public abstract class MiniClientPlugin<DataType> extends MiniPlugin
} }
} }
public void saveData(String name, int accountId) {} public void saveData(String name, UUID uuid, int accountId) {}
public DataType Get(Player player) public DataType Get(Player player)
{ {

View File

@ -387,11 +387,10 @@ public class QuestManager extends MiniDbClientPlugin<QuestClientData>
return new QuestClientData(); return new QuestClientData();
} }
@SuppressWarnings("deprecation")
@Override @Override
public void saveData(String name, int accountId) public void saveData(String name, UUID uuid, int accountId)
{ {
Hologram hologram = Get(name).getHologram(); Hologram hologram = Get(uuid).getHologram();
if (hologram != null) if (hologram != null)
{ {

View File

@ -14,6 +14,18 @@ public class PlayerStats
private Map<String, Long> _stats = new HashMap<>(); private Map<String, Long> _stats = new HashMap<>();
private Map<String, Long> _statsOld = new HashMap<>(); private Map<String, Long> _statsOld = new HashMap<>();
private final boolean _temporary;
public PlayerStats(boolean temporary)
{
_temporary = temporary;
}
public boolean isTemporary()
{
return _temporary;
}
/** /**
* Add a value to the specified stat * Add a value to the specified stat
* *

View File

@ -1,7 +1,6 @@
package mineplex.core.stats; package mineplex.core.stats;
import java.sql.ResultSet; import java.util.Collections;
import java.sql.SQLException;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map; import java.util.Map;
@ -13,13 +12,14 @@ import java.util.function.Consumer;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.player.AsyncPlayerPreLoginEvent;
import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import mineplex.core.MiniDbClientPlugin; import mineplex.core.MiniClientPlugin;
import mineplex.core.account.CoreClient; import mineplex.core.account.CoreClient;
import mineplex.core.account.CoreClientManager; import mineplex.core.account.CoreClientManager;
import mineplex.core.account.ILoginProcessor;
import mineplex.core.account.permissions.Permission; import mineplex.core.account.permissions.Permission;
import mineplex.core.account.permissions.PermissionGroup; import mineplex.core.account.permissions.PermissionGroup;
import mineplex.core.common.util.UtilServer; import mineplex.core.common.util.UtilServer;
@ -35,7 +35,7 @@ import mineplex.core.utils.UtilScheduler;
/** /**
* This manager handles player statistics * This manager handles player statistics
*/ */
public class StatsManager extends MiniDbClientPlugin<PlayerStats> public class StatsManager extends MiniClientPlugin<PlayerStats>//MiniDbClientPlugin<PlayerStats>
{ {
public enum Perm implements Permission public enum Perm implements Permission
{ {
@ -55,16 +55,19 @@ public class StatsManager extends MiniDbClientPlugin<PlayerStats>
private final Map<CoreClient, Map<String, Long>> _statUploadQueue = new HashMap<>(); private final Map<CoreClient, Map<String, Long>> _statUploadQueue = new HashMap<>();
private final Map<CoreClient, Map<String, Long>> _statUploadQueueOverRidable = new HashMap<>(); private final Map<CoreClient, Map<String, Long>> _statUploadQueueOverRidable = new HashMap<>();
private final Set<UUID> _loading = Collections.synchronizedSet(new HashSet<>());
public StatsManager(JavaPlugin plugin, CoreClientManager clientManager) public StatsManager(JavaPlugin plugin, CoreClientManager clientManager)
{ {
super("Stats Manager", plugin, clientManager); //super("Stats Manager", plugin, clientManager);
super("Stats Manager", plugin);
_repository = new StatsRepository(); _repository = new StatsRepository();
_coreClientManager = clientManager; _coreClientManager = clientManager;
_leaderboard = new LeaderboardManager(this); _leaderboard = new LeaderboardManager(this);
clientManager.addStoredProcedureLoginProcessor(new ILoginProcessor() /*clientManager.addStoredProcedureLoginProcessor(new ILoginProcessor()
{ {
public String getName() public String getName()
{ {
@ -87,7 +90,7 @@ public class StatsManager extends MiniDbClientPlugin<PlayerStats>
{ {
return "SELECT stats.name, value FROM accountStat INNER JOIN stats ON stats.id = accountStat.statId WHERE accountStat.accountId = '" + accountId + "';"; return "SELECT stats.name, value FROM accountStat INNER JOIN stats ON stats.id = accountStat.statId WHERE accountStat.accountId = '" + accountId + "';";
} }
}); });*/
UtilScheduler.runAsyncEvery(UpdateType.SEC, () -> UtilScheduler.runAsyncEvery(UpdateType.SEC, () ->
{ {
@ -166,12 +169,18 @@ public class StatsManager extends MiniDbClientPlugin<PlayerStats>
return; return;
CoreClient client = _coreClientManager.Get(player); CoreClient client = _coreClientManager.Get(player);
PlayerStats snapshot = Get(player);
long oldCompositeValue = Get(player).getStat(statName); if (snapshot.isTemporary())
long oldValue = Get(player).getJustCurrentStat(statName); {
long oldOldValue = Get(player).getStatOld(statName); return;
}
long oldCompositeValue = snapshot.getStat(statName);
long oldValue = snapshot.getJustCurrentStat(statName);
long oldOldValue = snapshot.getStatOld(statName);
final long incrementBy = value + (oldOldValue > oldValue ? (oldOldValue - oldValue) : 0L); final long incrementBy = value + (oldOldValue > oldValue ? (oldOldValue - oldValue) : 0L);
long newValue = Get(player).addStat(statName, incrementBy); long newValue = snapshot.addStat(statName, incrementBy);
UtilServer.getServer().getPluginManager().callEvent(new StatChangeEvent(player, statName, oldCompositeValue, newValue)); UtilServer.getServer().getPluginManager().callEvent(new StatChangeEvent(player, statName, oldCompositeValue, newValue));
registerNewStat(statName, () -> addToQueue(statName, client, incrementBy)); registerNewStat(statName, () -> addToQueue(statName, client, incrementBy));
@ -371,23 +380,31 @@ public class StatsManager extends MiniDbClientPlugin<PlayerStats>
@EventHandler @EventHandler
private void onPlayerJoin(PlayerJoinEvent event) private void onPlayerJoin(PlayerJoinEvent event)
{ {
Set<String> statsToReset = new HashSet<>(); final UUID uuid = event.getPlayer().getUniqueId();
final int accountId = _coreClientManager.Get(event.getPlayer()).getAccountId();
Get(event.getPlayer()).getStats().forEach((stat, amount) -> runAsync(() ->
{ {
if (!_stats.containsKey(stat)) _repository.loadStatsFromOld(accountId, data ->
return;
if (amount == -1)
{ {
statsToReset.add(stat); PlayerStats stats = new PlayerStats(false);
} data.forEach((stat, values) ->
}); {
stats.addStat(stat, values.getRight());
stats.setStatOld(stat, values.getLeft());
});
statsToReset.forEach(stat -> if (_loading.remove(uuid))
{ {
setStat(event.getPlayer(), stat, 0); Set(uuid, stats);
}); }
});
}, 5000); //Load 5 seconds later
}
@EventHandler(priority = EventPriority.LOWEST)
public void onLogin(AsyncPlayerPreLoginEvent event)
{
_loading.add(event.getUniqueId());
} }
@Override @Override
@ -402,10 +419,26 @@ public class StatsManager extends MiniDbClientPlugin<PlayerStats>
@Override @Override
protected PlayerStats addPlayer(UUID uuid) protected PlayerStats addPlayer(UUID uuid)
{ {
return new PlayerStats(); return new PlayerStats(false);
} }
@Override @Override
public PlayerStats Get(UUID uuid)
{
if (_loading.contains(uuid))
{
return new PlayerStats(true);
}
return super.Get(uuid);
}
@Override
public void saveData(String name, UUID uuid, int accountId)
{
_loading.remove(uuid);
}
/* @Override
public void processLoginResultSet(String playerName, UUID uuid, int accountId, ResultSet resultSet) throws SQLException public void processLoginResultSet(String playerName, UUID uuid, int accountId, ResultSet resultSet) throws SQLException
{ {
PlayerStats playerStats = new PlayerStats(); PlayerStats playerStats = new PlayerStats();
@ -425,10 +458,12 @@ public class StatsManager extends MiniDbClientPlugin<PlayerStats>
Set(uuid, playerStats); Set(uuid, playerStats);
} }
*/
@Override /* @Override
public String getQuery(int accountId, String uuid, String name) public String getQuery(int accountId, String uuid, String name)
{ {
return "SELECT stats.name, value FROM accountStatsAllTime INNER JOIN stats ON stats.id = accountStatsAllTime.statId WHERE accountStatsAllTime.accountId = '" + accountId + "';"; return "SELECT stats.name, value FROM accountStatsAllTime INNER JOIN stats ON stats.id = accountStatsAllTime.statId WHERE accountStatsAllTime.accountId = '" + accountId + "';";
} }
*/
} }

View File

@ -219,7 +219,7 @@ public class StatsRepository extends RepositoryBase
if (result.isNotEmpty()) if (result.isNotEmpty())
{ {
playerStats = new PlayerStats(); playerStats = new PlayerStats(false);
for (Record2<String, ULong> record : result) for (Record2<String, ULong> record : result)
{ {
playerStats.addStat(record.value1(), record.value2().longValue()); playerStats.addStat(record.value1(), record.value2().longValue());