Implement checks to handle doubled statistics issue, and complete final tweaks of stat conversion system

This commit is contained in:
AlexTheCoder 2017-11-28 04:09:49 -05:00 committed by Alexander Meech
parent 02dc0458d9
commit fedbf2397b
3 changed files with 81 additions and 11 deletions

View File

@ -22,6 +22,8 @@ import mineplex.core.account.CoreClient;
import mineplex.core.account.CoreClientManager; import mineplex.core.account.CoreClientManager;
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.F;
import mineplex.core.common.util.UtilPlayer;
import mineplex.core.common.util.UtilServer; import mineplex.core.common.util.UtilServer;
import mineplex.core.common.util.UtilTasks; import mineplex.core.common.util.UtilTasks;
import mineplex.core.leaderboard.LeaderboardManager; import mineplex.core.leaderboard.LeaderboardManager;
@ -382,23 +384,42 @@ public class StatsManager extends MiniClientPlugin<PlayerStats>//MiniDbClientPlu
{ {
final UUID uuid = event.getPlayer().getUniqueId(); final UUID uuid = event.getPlayer().getUniqueId();
final int accountId = _coreClientManager.Get(event.getPlayer()).getAccountId(); final int accountId = _coreClientManager.Get(event.getPlayer()).getAccountId();
UtilPlayer.message(event.getPlayer(), F.main(getName(), "Loading your stats..."));
runAsync(() -> runAsync(() ->
{ {
_repository.loadStatsFromOld(accountId, data -> _repository.loadStatsFromOld(accountId, data ->
{ {
PlayerStats stats = new PlayerStats(false); PlayerStats stats = new PlayerStats(false);
Map<String, Long> decrement = new HashMap<>();
data.forEach((stat, values) -> data.forEach((stat, values) ->
{ {
stats.addStat(stat, values.getRight());
stats.setStatOld(stat, values.getLeft()); stats.setStatOld(stat, values.getLeft());
if (values.getLeft() > 0 && values.getRight() >= (values.getLeft() * 2))
{
decrement.put(stat, -1 * values.getLeft());
stats.addStat(stat, values.getRight() - values.getLeft());
}
else
{
stats.addStat(stat, values.getRight());
}
}); });
if (_loading.remove(uuid)) if (_loading.remove(uuid))
{ {
Set(uuid, stats); Set(uuid, stats);
if (!decrement.isEmpty())
{
decrement.forEach((stat, value) ->
{
addToQueue(stat, _coreClientManager.Get(uuid), value);
});
}
UtilPlayer.message(event.getPlayer(), F.main(getName(), "Your stats have been loaded!"));
} }
}); });
}, 5000); //Load 5 seconds later }, 5 * 20); //Load 5 seconds later
} }
@EventHandler(priority = EventPriority.LOWEST) @EventHandler(priority = EventPriority.LOWEST)

View File

@ -20,12 +20,13 @@ import org.jooq.Update;
import org.jooq.impl.DSL; import org.jooq.impl.DSL;
import org.jooq.types.ULong; import org.jooq.types.ULong;
import com.mysql.jdbc.exceptions.jdbc4.MySQLDataException;
import mineplex.core.common.Pair; import mineplex.core.common.Pair;
import mineplex.core.common.util.UtilServer; import mineplex.core.common.util.UtilServer;
import mineplex.database.Tables; import mineplex.database.Tables;
import mineplex.serverdata.database.DBPool; import mineplex.serverdata.database.DBPool;
import mineplex.serverdata.database.RepositoryBase; import mineplex.serverdata.database.RepositoryBase;
import mineplex.serverdata.database.column.ColumnInt;
import mineplex.serverdata.database.column.ColumnVarChar; import mineplex.serverdata.database.column.ColumnVarChar;
public class StatsRepository extends RepositoryBase public class StatsRepository extends RepositoryBase
@ -54,17 +55,37 @@ public class StatsRepository extends RepositoryBase
final Map<String, Pair<Long, Long>> statMap = new HashMap<>(); final Map<String, Pair<Long, Long>> statMap = new HashMap<>();
while (oldSet.next()) while (oldSet.next())
{ {
statMap.put(oldSet.getString(1), Pair.create(oldSet.getLong(2), 0L)); String statName = oldSet.getString(1);
long oldValue;
try
{
oldValue = oldSet.getLong(2);
}
catch (MySQLDataException ex)
{
oldValue = 0;
}
statMap.put(statName, Pair.create(oldValue, 0L));
} }
while (newSet.next()) while (newSet.next())
{ {
if (statMap.containsKey(newSet.getString(1))) String statName = newSet.getString(1);
long newValue;
try
{ {
statMap.get(newSet.getString(1)).setRight(newSet.getLong(2)); newValue = newSet.getLong(2);
}
catch (MySQLDataException ex)
{
newValue = 0;
}
if (statMap.containsKey(statName))
{
statMap.get(statName).setRight(newValue);
} }
else else
{ {
statMap.put(oldSet.getString(1), Pair.create(0L, newSet.getLong(2))); statMap.put(statName, Pair.create(0L, newValue));
} }
} }

View File

@ -16,6 +16,7 @@ import java.util.Map.Entry;
import com.google.common.io.Files; import com.google.common.io.Files;
import com.mineplex.statconverter.database.mysql.DBPool; import com.mineplex.statconverter.database.mysql.DBPool;
import com.mysql.jdbc.exceptions.jdbc4.MySQLDataException;
public class Main public class Main
{ {
@ -130,7 +131,16 @@ public class Main
{ {
while (rs.next()) while (rs.next())
{ {
oldStats.put(rs.getInt("statId"), rs.getLong("value")); long oldValue;
try
{
oldValue = rs.getLong("value");
}
catch (MySQLDataException ex)
{
oldValue = 0;
}
oldStats.put(rs.getInt("statId"), oldValue);
} }
} }
try (Statement s = c.createStatement(); try (Statement s = c.createStatement();
@ -139,7 +149,16 @@ public class Main
{ {
while (rs.next()) while (rs.next())
{ {
newStats.put(rs.getInt("statId"), rs.getLong("value")); long newValue;
try
{
newValue = rs.getLong("value");
}
catch (MySQLDataException ex)
{
newValue = 0;
}
newStats.put(rs.getInt("statId"), newValue);
} }
} }
@ -157,8 +176,17 @@ public class Main
{ {
if (newStats.get(oldEntry.getKey()) < oldEntry.getValue()) if (newStats.get(oldEntry.getKey()) < oldEntry.getValue())
{ {
Long diff = oldEntry.getValue() - newStats.get(oldEntry.getKey()); Long delta = oldEntry.getValue() - newStats.get(oldEntry.getKey());
updateStatement.setLong(1, diff); updateStatement.setLong(1, delta);
updateStatement.setInt(2, accountId);
updateStatement.setInt(3, oldEntry.getKey());
updateStatement.addBatch();
updates++;
}
else if (oldEntry.getValue() > 0 && newStats.get(oldEntry.getKey()) >= (oldEntry.getValue() * 2))
{
Long delta = -1 * oldEntry.getValue();
updateStatement.setLong(1, delta);
updateStatement.setInt(2, accountId); updateStatement.setInt(2, accountId);
updateStatement.setInt(3, oldEntry.getKey()); updateStatement.setInt(3, oldEntry.getKey());
updateStatement.addBatch(); updateStatement.addBatch();