|
|
|
@@ -21,6 +21,7 @@ import java.util.ArrayList;
|
|
|
|
|
import java.util.Collection;
|
|
|
|
|
import java.util.Collections;
|
|
|
|
|
import java.util.Comparator;
|
|
|
|
|
import java.util.LinkedHashMap;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.Map.Entry;
|
|
|
|
@@ -46,12 +47,11 @@ import utils.PuSelector;
|
|
|
|
|
|
|
|
|
|
public class Facade {
|
|
|
|
|
|
|
|
|
|
private ConcurrentMap<String, Player> playerCache = new MapMaker().concurrencyLevel(4).makeMap();
|
|
|
|
|
private ConcurrentMap<String, MapBoard> mapBoardCache = new MapMaker().concurrencyLevel(4).makeMap();
|
|
|
|
|
private ConcurrentMap<String, ConcurrentMap<String, MapValues>> playerRelatedMapValuesCache = new MapMaker().concurrencyLevel(6).makeMap();
|
|
|
|
|
private final long EXPIRE_TIME_IN_SECONDS = TimeUnit.SECONDS.convert(30, TimeUnit.MINUTES);
|
|
|
|
|
private Instant elapsedTime;
|
|
|
|
|
private int initSetup = 0;
|
|
|
|
|
private static Map<String, Player> playerCache = new LinkedHashMap();
|
|
|
|
|
private static ConcurrentMap<String, MapBoard> mapBoardCache = new MapMaker().concurrencyLevel(4).makeMap();
|
|
|
|
|
private static ConcurrentMap<String, ConcurrentMap<String, MapValues>> playerRelatedMapValuesCache = new MapMaker().concurrencyLevel(6).makeMap();
|
|
|
|
|
private static final long EXPIRE_TIME_IN_SECONDS = TimeUnit.SECONDS.convert(30, TimeUnit.MINUTES);
|
|
|
|
|
private static Instant elapsedTime = Instant.now();
|
|
|
|
|
private static final ForkJoinPool executor = instantiateExecutor();
|
|
|
|
|
|
|
|
|
|
private static ForkJoinPool instantiateExecutor() {
|
|
|
|
@@ -60,11 +60,6 @@ public class Facade {
|
|
|
|
|
null, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Facade() {
|
|
|
|
|
elapsedTime = Instant.now();
|
|
|
|
|
initSetup = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean getMap(String map, String Identifier) {
|
|
|
|
|
boolean identified = Pattern.compile(Pattern.quote(Identifier), Pattern.CASE_INSENSITIVE).matcher(map).find();
|
|
|
|
|
return identified;
|
|
|
|
@@ -236,24 +231,24 @@ public class Facade {
|
|
|
|
|
public Collection<PlayerDTO> getleaderBoard(int iterator) {
|
|
|
|
|
int playerAddiditionCap = 99;
|
|
|
|
|
Collection<Player> playerAvatarsCaching = new ArrayList();
|
|
|
|
|
List<Player> players = new ArrayList(playerCache.values());
|
|
|
|
|
Collection<PlayerDTO> playersDTO = new ArrayList();
|
|
|
|
|
players.sort(Comparator.comparing(Player::getRank));
|
|
|
|
|
int counter = 0;
|
|
|
|
|
for (Player player : players) {
|
|
|
|
|
if (counter >= iterator && counter <= iterator + playerAddiditionCap && player.getAvatar() == null) {
|
|
|
|
|
playerAvatarsCaching.add(player);
|
|
|
|
|
for (Player player : playerCache.values()) {
|
|
|
|
|
if (counter >= iterator && counter <= iterator + playerAddiditionCap) {
|
|
|
|
|
if (player.getAvatar() == null || player.getAvatar().isEmpty()) {
|
|
|
|
|
playerAvatarsCaching.add(player);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (counter > iterator + playerAddiditionCap) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
counter++;
|
|
|
|
|
}
|
|
|
|
|
updateAvatarsToCache(playerAvatarsCaching);
|
|
|
|
|
players = new ArrayList(playerCache.values());
|
|
|
|
|
players.sort(Comparator.comparing(Player::getRank));
|
|
|
|
|
if (!playerAvatarsCaching.isEmpty()) {
|
|
|
|
|
updateAvatarsToCache(playerAvatarsCaching);
|
|
|
|
|
}
|
|
|
|
|
counter = 0;
|
|
|
|
|
for (Player player : players) {
|
|
|
|
|
for (Player player : playerCache.values()) {
|
|
|
|
|
if (counter >= iterator && counter <= iterator + playerAddiditionCap) {
|
|
|
|
|
playersDTO.add(new PlayerDTO(player));
|
|
|
|
|
}
|
|
|
|
@@ -265,9 +260,10 @@ public class Facade {
|
|
|
|
|
return playersDTO;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void addNewPlayerValuesToCache(Collection<Player> keyCollection, ConcurrentMap<String, Collection<Integer>> playerUrlBanners) {
|
|
|
|
|
private ConcurrentMap<String, Player> addNewPlayerValuesToCache(Collection<Player> keyCollection, ConcurrentMap<String, Collection<Integer>> playerUrlBanners) {
|
|
|
|
|
//2 = user, 6 = mapper, 7 = admin, 8 = technical staff, 10 = leader, 12 = vip, 13 = trial admin, 15 = event winner, 19 = discord manager
|
|
|
|
|
// 20 NSFW, 21 = Retired admin, 25 = event manager, 11 = GA
|
|
|
|
|
ConcurrentMap<String, Player> playersConcurrentMap = new MapMaker().concurrencyLevel(6).makeMap();
|
|
|
|
|
for (Player player : keyCollection) {
|
|
|
|
|
Collection<Integer> banners = playerUrlBanners.getOrDefault(player.getSteamID(), null);
|
|
|
|
|
if (banners != null) {
|
|
|
|
@@ -319,8 +315,9 @@ public class Facade {
|
|
|
|
|
}
|
|
|
|
|
player.setUrlBanners(urlBanners);
|
|
|
|
|
}
|
|
|
|
|
playerCache.put(player.getSteamID(), player);
|
|
|
|
|
playersConcurrentMap.put(player.getSteamID(), player);
|
|
|
|
|
}
|
|
|
|
|
return playersConcurrentMap;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void retrieveAvatarFull(String avatarurl) {
|
|
|
|
@@ -333,12 +330,17 @@ public class Facade {
|
|
|
|
|
JSONObject jsonObject = root.getJSONObject("response");
|
|
|
|
|
JSONArray jsonArray = jsonObject.getJSONArray("players");
|
|
|
|
|
for (int incrementer = 0; incrementer < jsonArray.length(); incrementer++) {
|
|
|
|
|
worker = new CallableJsonObject(jsonArray.getJSONObject(incrementer));
|
|
|
|
|
futures.put(incrementer, executor.submit(worker));
|
|
|
|
|
}
|
|
|
|
|
futures.values().parallelStream().forEach(future -> {
|
|
|
|
|
JSONObject jsonObjectPlayer = jsonArray.getJSONObject(incrementer);
|
|
|
|
|
try {
|
|
|
|
|
Entry<String, String> getEntry = future.get(3, TimeUnit.SECONDS);
|
|
|
|
|
worker = new CallableJsonObject(jsonObjectPlayer);
|
|
|
|
|
futures.put(incrementer, executor.submit(worker));
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
//some are simply nosteamers with no steam data
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
futures.values().forEach(future -> {
|
|
|
|
|
try {
|
|
|
|
|
Entry<String, String> getEntry = future.get(5, TimeUnit.SECONDS);
|
|
|
|
|
playerCache.get(getEntry.getKey()).setAvatar(getEntry.getValue());
|
|
|
|
|
} catch (InterruptedException | ExecutionException | TimeoutException ex) {
|
|
|
|
|
Logger.getLogger(Facade.class.getName()).log(Level.SEVERE, null, ex);
|
|
|
|
@@ -355,37 +357,15 @@ public class Facade {
|
|
|
|
|
//http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=262F3F0C6B83E0F263C272C3762002F1&steamids=76561198029832363,76561198003907342
|
|
|
|
|
StringBuilder avatarURL = new StringBuilder();
|
|
|
|
|
avatarURL.append("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=262F3F0C6B83E0F263C272C3762002F1&steamids=");
|
|
|
|
|
int incrementer = 0;
|
|
|
|
|
int overallCounter = 0;
|
|
|
|
|
int fetchCapacity = players.size() > 5 ? players.size() / 5 : 6;
|
|
|
|
|
CountDownLatch cdl = new CountDownLatch(1);
|
|
|
|
|
boolean init = true;
|
|
|
|
|
for (Player player : players) {
|
|
|
|
|
if (incrementer > 0) {
|
|
|
|
|
if (!init) {
|
|
|
|
|
avatarURL.append(",");
|
|
|
|
|
}
|
|
|
|
|
incrementer++;
|
|
|
|
|
avatarURL.append(Long.toString(convertSteamIdToCommunityId(player.getSteamID())));
|
|
|
|
|
if (incrementer >= fetchCapacity || incrementer + overallCounter >= players.size()) {
|
|
|
|
|
final String fullUrl = avatarURL.toString();
|
|
|
|
|
final int overall = incrementer + overallCounter;
|
|
|
|
|
new Thread(() -> {
|
|
|
|
|
retrieveAvatarFull(fullUrl);
|
|
|
|
|
if (overall >= players.size()) {
|
|
|
|
|
cdl.countDown();
|
|
|
|
|
}
|
|
|
|
|
}).start();
|
|
|
|
|
overallCounter += incrementer;
|
|
|
|
|
incrementer = 0;
|
|
|
|
|
avatarURL = new StringBuilder();
|
|
|
|
|
avatarURL.append("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=262F3F0C6B83E0F263C272C3762002F1&steamids=");
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
cdl.await();
|
|
|
|
|
} catch (InterruptedException ex) {
|
|
|
|
|
System.out.println("cdl await interrupted: " + ex.getLocalizedMessage() + "\n");
|
|
|
|
|
init = false;
|
|
|
|
|
}
|
|
|
|
|
retrieveAvatarFull(avatarURL.toString());
|
|
|
|
|
setNonExistingAvatarOnPlayers(players);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -439,23 +419,24 @@ public class Facade {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void setPlayerPoints() {
|
|
|
|
|
private List<Player> setPlayerPoints(ConcurrentMap<String, Player> playersMap) {
|
|
|
|
|
for (MapBoard mapboard : mapBoardCache.values()) {
|
|
|
|
|
List<MapValues> mapvalues = mapboard.getMapvalues();
|
|
|
|
|
for (MapValues mapvalue : mapvalues) {
|
|
|
|
|
String steamID = mapvalue.getPlayerSteamID();
|
|
|
|
|
int mapPoints = mapvalue.getMapPoints();
|
|
|
|
|
playerCache.get(steamID).addPlayerPoints(mapPoints);
|
|
|
|
|
playersMap.get(steamID).addPlayerPoints(mapPoints);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return new ArrayList(playersMap.values());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void updatePlayerRanks() {
|
|
|
|
|
List<Player> playerList = new ArrayList(playerCache.values());
|
|
|
|
|
playerList.sort(Comparator.comparing(Player::getPlayerPoints).reversed());
|
|
|
|
|
private void updatePlayerRanks(List<Player> playersConcurrentmap) {
|
|
|
|
|
int rank = 1;
|
|
|
|
|
for (Player player : playerList) {
|
|
|
|
|
playerCache.get(player.getSteamID()).setRank(rank);
|
|
|
|
|
playersConcurrentmap.sort(Comparator.comparing(Player::getPlayerPoints).reversed());
|
|
|
|
|
for (Player player : playersConcurrentmap) {
|
|
|
|
|
player.setRank(rank);
|
|
|
|
|
playerCache.put(player.getSteamID(), player);
|
|
|
|
|
rank++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@@ -478,13 +459,12 @@ public class Facade {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void checkTimeElapse() {
|
|
|
|
|
public String checkTimeElapse() {
|
|
|
|
|
Instant current = Instant.now();
|
|
|
|
|
long timeElapsed = Duration.between(elapsedTime, current).toMillis();
|
|
|
|
|
//perharps 3 mins instead of 30?
|
|
|
|
|
if (timeElapsed >= EXPIRE_TIME_IN_SECONDS * 1000 || initSetup > 0) {
|
|
|
|
|
//System.out.println("went through \ntimeElapsed: " + timeElapsed + "\nEXPIRE_TIME_IN_SECONDS: " + EXPIRE_TIME_IN_SECONDS * 100);
|
|
|
|
|
initSetup = 0;
|
|
|
|
|
int cachesize = playerCache.size();
|
|
|
|
|
if (timeElapsed >= EXPIRE_TIME_IN_SECONDS * 1000 || playerCache.isEmpty()) {
|
|
|
|
|
String elapsed = String.valueOf(timeElapsed);
|
|
|
|
|
elapsedTime = Instant.now();
|
|
|
|
|
Entry<Collection<Player>, Collection<MapBoard>> playersMapBoards = DataMapperCalls.getAllValues();
|
|
|
|
|
Collection<MapBoard> mapBoardCollection = playersMapBoards.getValue();
|
|
|
|
@@ -493,14 +473,16 @@ public class Facade {
|
|
|
|
|
mapBoardCache.clear();
|
|
|
|
|
playerRelatedMapValuesCache.clear();
|
|
|
|
|
addNewMapBoardValuesToCache(mapBoardCollection);
|
|
|
|
|
addNewPlayerValuesToCache(playersMapBoards.getKey(), playerRelatedUrlBanners);
|
|
|
|
|
ConcurrentMap<String, Player> playersConcurrentmap = addNewPlayerValuesToCache(playersMapBoards.getKey(), playerRelatedUrlBanners);
|
|
|
|
|
orderingMapBoardValuesByTime();
|
|
|
|
|
setPositionAndMapPoints();
|
|
|
|
|
setPlayerPoints();
|
|
|
|
|
updatePlayerRanks();
|
|
|
|
|
updatePlayerRanks(setPlayerPoints(playersConcurrentmap));
|
|
|
|
|
setTimesAsociatedToPeople();
|
|
|
|
|
//updateEntitiesPersistence();
|
|
|
|
|
//System.out.println("timeElapsed: " + elapsed + "\nEXPIRE_TIME_IN_SECONDS: " + EXPIRE_TIME_IN_SECONDS * 1000 + "cachesize: " + cachesize);
|
|
|
|
|
//return "timeElapsed: " + elapsed + "\nEXPIRE_TIME_IN_SECONDS: " + EXPIRE_TIME_IN_SECONDS * 1000 + "cachesize: " + cachesize;
|
|
|
|
|
}
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void updateEntitiesPersistence() {
|
|
|
|
|