Removed migration code from game servers

This commit is contained in:
AlexTheCoder 2018-01-30 00:54:31 -05:00 committed by Alexander Meech
parent 4832bcbe46
commit a35550867a
2 changed files with 23 additions and 50 deletions

View File

@ -4,14 +4,21 @@ import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import javax.annotation.concurrent.GuardedBy;
import javax.annotation.concurrent.ThreadSafe;
/** /**
* Represents a player's statistic information. This object is thread-safe * Represents a player's statistic information. This object is thread-safe
*/ */
@ThreadSafe
public class PlayerStats public class PlayerStats
{ {
private final Object lock = new Object(); private final Object _lock = new Object();
@GuardedBy("_lock")
private Map<String, Long> _stats = new HashMap<>(); private Map<String, Long> _stats = new HashMap<>();
@GuardedBy("_lock")
private Map<String, Long> _statsOld = new HashMap<>(); private Map<String, Long> _statsOld = new HashMap<>();
private final boolean _temporary; private final boolean _temporary;
@ -35,7 +42,7 @@ public class PlayerStats
*/ */
long addStat(String statName, long value) long addStat(String statName, long value)
{ {
synchronized (lock) synchronized (_lock)
{ {
return _stats.merge(statName, Math.max(0, value), Long::sum); return _stats.merge(statName, Math.max(0, value), Long::sum);
} }
@ -50,7 +57,7 @@ public class PlayerStats
*/ */
long setStat(String statName, long value) long setStat(String statName, long value)
{ {
synchronized (lock) synchronized (_lock)
{ {
_stats.put(statName, value); _stats.put(statName, value);
return value; return value;
@ -59,7 +66,7 @@ public class PlayerStats
void setStatOld(String statName, long value) void setStatOld(String statName, long value)
{ {
synchronized (lock) synchronized (_lock)
{ {
_statsOld.put(statName, value); _statsOld.put(statName, value);
} }
@ -73,7 +80,7 @@ public class PlayerStats
*/ */
public long getStat(String statName) public long getStat(String statName)
{ {
synchronized (lock) synchronized (_lock)
{ {
long cur = _stats.getOrDefault(statName, 0L); long cur = _stats.getOrDefault(statName, 0L);
long old = _statsOld.getOrDefault(statName, 0L); long old = _statsOld.getOrDefault(statName, 0L);
@ -83,7 +90,7 @@ public class PlayerStats
public long getStatOld(String statName) public long getStatOld(String statName)
{ {
synchronized (lock) synchronized (_lock)
{ {
return _statsOld.getOrDefault(statName, 0L); return _statsOld.getOrDefault(statName, 0L);
} }
@ -91,7 +98,7 @@ public class PlayerStats
public long getJustCurrentStat(String statName) public long getJustCurrentStat(String statName)
{ {
synchronized (lock) synchronized (_lock)
{ {
return _stats.getOrDefault(statName, 0L); return _stats.getOrDefault(statName, 0L);
} }
@ -102,7 +109,7 @@ public class PlayerStats
*/ */
public Map<String, Long> getStats() public Map<String, Long> getStats()
{ {
synchronized (lock) synchronized (_lock)
{ {
// make it unmodifiable so that people who try to edit it will get an exception instead of silently failing // make it unmodifiable so that people who try to edit it will get an exception instead of silently failing
return Collections.unmodifiableMap(new HashMap<>(_stats)); return Collections.unmodifiableMap(new HashMap<>(_stats));

View File

@ -180,13 +180,10 @@ public class StatsManager extends MiniClientPlugin<PlayerStats>//MiniDbClientPlu
} }
long oldCompositeValue = snapshot.getStat(statName); long oldCompositeValue = snapshot.getStat(statName);
long oldValue = snapshot.getJustCurrentStat(statName); long newValue = snapshot.addStat(statName, value);
long oldOldValue = snapshot.getStatOld(statName);
final long incrementBy = value + (oldOldValue > oldValue ? (oldOldValue - oldValue) : 0L);
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, value));
} }
@ -220,27 +217,12 @@ public class StatsManager extends MiniClientPlugin<PlayerStats>//MiniDbClientPlu
public void incrementStat(final int accountId, final String statName, final long value) public void incrementStat(final int accountId, final String statName, final long value)
{ {
registerNewStat(statName, () -> registerNewStat(statName, () ->
{
_repository.loadStatsFromOld(accountId, oldMap ->
{
runAsync(() ->
{ {
Map<Integer, Map<Integer, Long>> uploadQueue = new HashMap<>(); Map<Integer, Map<Integer, Long>> uploadQueue = new HashMap<>();
uploadQueue.computeIfAbsent(accountId, key -> new HashMap<>()).put(_stats.get(statName), value); 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); _repository.saveStats(uploadQueue, false);
}); });
});
});
} }
/** /**
@ -391,32 +373,16 @@ public class StatsManager extends MiniClientPlugin<PlayerStats>//MiniDbClientPlu
_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.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()); 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!")); UtilPlayer.message(event.getPlayer(), F.main(getName(), "Your stats have been loaded!"));
} }
}); });