"cache" to quick count of individual stats by player uuid

Took 16 seconds
This commit is contained in:
Kiran Hart 2022-09-22 21:40:43 -04:00
parent 3c0ea24ac6
commit 0756da3039
No known key found for this signature in database
GPG Key ID: 5F36C7BC79D3EBC3

View File

@ -24,20 +24,48 @@ import ca.tweetzy.auctionhouse.auction.enums.AuctionStatisticType;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
public final class AuctionStatisticManager {
private final List<AuctionStatistic> statistics = Collections.synchronizedList(new ArrayList<>());
// 'cheat' way of doing this leaderboard thing, I will fix this eventually...
private final Map<UUID, Double> createdAuctionCount = new ConcurrentHashMap<>();
private final Map<UUID, Double> createdBinCount = new ConcurrentHashMap<>();
private final Map<UUID, Double> soldAuctionCount = new ConcurrentHashMap<>();
private final Map<UUID, Double> soldBinCount = new ConcurrentHashMap<>();
private final Map<UUID, Double> moneySpentCount = new ConcurrentHashMap<>();
private final Map<UUID, Double> moneyEarnedCount = new ConcurrentHashMap<>();
public void addStatistic(AuctionStatistic statistic) {
synchronized (this.statistics) {
if (this.statistics.contains(statistic)) return;
this.statistics.add(statistic);
switch (statistic.getStatisticType()) {
case CREATED_AUCTION:
this.createdAuctionCount.put(statistic.getStatOwner(), this.createdAuctionCount.getOrDefault(statistic.getStatOwner(), 0D) + statistic.getValue());
break;
case CREATED_BIN:
this.createdBinCount.put(statistic.getStatOwner(), this.createdBinCount.getOrDefault(statistic.getStatOwner(), 0D) + statistic.getValue());
break;
case SOLD_AUCTION:
this.soldAuctionCount.put(statistic.getStatOwner(), this.soldAuctionCount.getOrDefault(statistic.getStatOwner(), 0D) + statistic.getValue());
break;
case SOLD_BIN:
this.soldBinCount.put(statistic.getStatOwner(), this.soldBinCount.getOrDefault(statistic.getStatOwner(), 0D) + statistic.getValue());
break;
case MONEY_SPENT:
this.moneySpentCount.put(statistic.getStatOwner(), this.moneySpentCount.getOrDefault(statistic.getStatOwner(), 0D) + statistic.getValue());
break;
case MONEY_EARNED:
this.moneyEarnedCount.put(statistic.getStatOwner(), this.moneyEarnedCount.getOrDefault(statistic.getStatOwner(), 0D) + statistic.getValue());
break;
}
}
}
@ -53,6 +81,31 @@ public final class AuctionStatisticManager {
}
}
public Map<UUID, Double> getStatisticMap(AuctionStatisticType statisticType) {
switch (statisticType) {
case CREATED_AUCTION:
return this.createdAuctionCount;
case CREATED_BIN:
return this.createdBinCount;
case SOLD_AUCTION:
return this.soldAuctionCount;
case SOLD_BIN:
return this.soldBinCount;
case MONEY_SPENT:
return this.moneySpentCount;
case MONEY_EARNED:
return this.moneyEarnedCount;
}
return new HashMap<>();
}
public List<AuctionStatistic> getStatistics(AuctionStatisticType statisticType) {
synchronized (this.statistics) {
return this.statistics.stream().filter(stat -> stat.getStatisticType() == statisticType).collect(Collectors.toList());
}
}
public double getStatistic(AuctionStatisticType statisticType) {
synchronized (this.statistics) {
return this.statistics.stream().filter(stat -> stat.getStatisticType() == statisticType).mapToDouble(AuctionStatistic::getValue).sum();