From 31711bd2f91f116c34dcc709ccce10bfdde95452 Mon Sep 17 00:00:00 2001 From: Alejandro <25-alejandro@users.noreply.git.krews.org> Date: Tue, 30 Jul 2019 13:45:39 +0300 Subject: [PATCH] Minor fixes --- sqlupdates/2_1_1_TO_2_2_0-RC-1.sql | 10 ++ .../com/eu/habbo/habbohotel/games/Game.java | 12 +- .../habbo/habbohotel/items/ItemManager.java | 9 + .../InteractionWiredHighscore.java | 73 +++----- .../com/eu/habbo/habbohotel/rooms/Room.java | 66 ------- .../habbohotel/wired/WiredHighscoreData.java | 16 -- .../WiredHighscoreClearType.java | 2 +- .../highscores/WiredHighscoreDataEntry.java | 51 ++++++ .../highscores/WiredHighscoreManager.java | 167 ++++++++++++++++++ .../WiredHighscoreMidnightUpdater.java | 33 ++++ .../wired/highscores/WiredHighscoreRow.java | 32 ++++ .../WiredHighscoreScoreType.java | 2 +- 12 files changed, 337 insertions(+), 136 deletions(-) delete mode 100644 src/main/java/com/eu/habbo/habbohotel/wired/WiredHighscoreData.java rename src/main/java/com/eu/habbo/habbohotel/wired/{ => highscores}/WiredHighscoreClearType.java (80%) create mode 100644 src/main/java/com/eu/habbo/habbohotel/wired/highscores/WiredHighscoreDataEntry.java create mode 100644 src/main/java/com/eu/habbo/habbohotel/wired/highscores/WiredHighscoreManager.java create mode 100644 src/main/java/com/eu/habbo/habbohotel/wired/highscores/WiredHighscoreMidnightUpdater.java create mode 100644 src/main/java/com/eu/habbo/habbohotel/wired/highscores/WiredHighscoreRow.java rename src/main/java/com/eu/habbo/habbohotel/wired/{ => highscores}/WiredHighscoreScoreType.java (79%) diff --git a/sqlupdates/2_1_1_TO_2_2_0-RC-1.sql b/sqlupdates/2_1_1_TO_2_2_0-RC-1.sql index 5ca7ca4a..7d4b4451 100644 --- a/sqlupdates/2_1_1_TO_2_2_0-RC-1.sql +++ b/sqlupdates/2_1_1_TO_2_2_0-RC-1.sql @@ -10,3 +10,13 @@ INSERT INTO `emulator_settings`(`key`, `value`) VALUES ('hotel.auto.gotwpoints.i INSERT INTO `emulator_settings`(`key`, `value`) VALUES ('hotel.auto.gotwpoints.ignore.hotelview', '1'); INSERT INTO `emulator_settings`(`key`, `value`) VALUES ('hotel.auto.gotwpoints.type', '4'); INSERT INTO `emulator_settings`(`key`, `value`) VALUES ('hotel.auto.gotwpoints.name', 'shell'); + +CREATE TABLE `items_highscore_data` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `item_id` int(11) NOT NULL, + `user_ids` varchar(500) NOT NULL, + `score` int(11) NOT NULL, + `is_win` tinyint(1) NULL DEFAULT 0, + `timestamp` int(11) NOT NULL, + PRIMARY KEY (`id`) +); \ No newline at end of file diff --git a/src/main/java/com/eu/habbo/habbohotel/games/Game.java b/src/main/java/com/eu/habbo/habbohotel/games/Game.java index c52d5115..e2b2a2ae 100644 --- a/src/main/java/com/eu/habbo/habbohotel/games/Game.java +++ b/src/main/java/com/eu/habbo/habbohotel/games/Game.java @@ -5,13 +5,13 @@ import com.eu.habbo.habbohotel.achievements.AchievementManager; import com.eu.habbo.habbohotel.items.interactions.InteractionWiredHighscore; import com.eu.habbo.habbohotel.items.interactions.games.InteractionGameTimer; import com.eu.habbo.habbohotel.items.interactions.wired.extra.WiredBlob; +import com.eu.habbo.habbohotel.wired.highscores.WiredHighscoreDataEntry; import com.eu.habbo.habbohotel.items.interactions.wired.triggers.WiredTriggerTeamLoses; import com.eu.habbo.habbohotel.items.interactions.wired.triggers.WiredTriggerTeamWins; import com.eu.habbo.habbohotel.rooms.Room; import com.eu.habbo.habbohotel.users.Habbo; import com.eu.habbo.habbohotel.users.HabboItem; import com.eu.habbo.habbohotel.wired.WiredHandler; -import com.eu.habbo.habbohotel.wired.WiredTriggerType; import com.eu.habbo.plugin.Event; import com.eu.habbo.plugin.events.games.GameHabboJoinEvent; import com.eu.habbo.plugin.events.games.GameHabboLeaveEvent; @@ -21,6 +21,7 @@ import com.eu.habbo.threading.runnables.SaveScoreForTeam; import gnu.trove.map.hash.THashMap; import java.util.Map; +import java.util.stream.Collectors; public abstract class Game implements Runnable { @@ -164,16 +165,25 @@ public abstract class Game implements Runnable { } } + for (HabboItem item : this.room.getRoomSpecialTypes().getItemsOfType(InteractionWiredHighscore.class)) { + Emulator.getGameEnvironment().getItemManager().getHighscoreManager().addHighscoreData(new WiredHighscoreDataEntry(item.getId(), winningTeam.getMembers().stream().map(m -> m.getHabbo().getHabboInfo().getId()).collect(Collectors.toList()), winningTeam.getTotalScore(), true, Emulator.getIntUnixTimestamp())); + } + for (GameTeam team : this.teams.values()) { if (team == winningTeam) continue; for (GamePlayer player : winningTeam.getMembers()) { WiredHandler.handleCustomTrigger(WiredTriggerTeamLoses.class, player.getHabbo().getRoomUnit(), this.room, new Object[]{this}); } + + for (HabboItem item : this.room.getRoomSpecialTypes().getItemsOfType(InteractionWiredHighscore.class)) { + Emulator.getGameEnvironment().getItemManager().getHighscoreManager().addHighscoreData(new WiredHighscoreDataEntry(item.getId(), winningTeam.getMembers().stream().map(m -> m.getHabbo().getHabboInfo().getId()).collect(Collectors.toList()), winningTeam.getTotalScore(), false, Emulator.getIntUnixTimestamp())); + } } } for (HabboItem item : this.room.getRoomSpecialTypes().getItemsOfType(InteractionWiredHighscore.class)) { + ((InteractionWiredHighscore) item).reloadData(); this.room.updateItem(item); } } diff --git a/src/main/java/com/eu/habbo/habbohotel/items/ItemManager.java b/src/main/java/com/eu/habbo/habbohotel/items/ItemManager.java index b475fb1e..ce313579 100644 --- a/src/main/java/com/eu/habbo/habbohotel/items/ItemManager.java +++ b/src/main/java/com/eu/habbo/habbohotel/items/ItemManager.java @@ -43,6 +43,7 @@ import com.eu.habbo.habbohotel.items.interactions.wired.effects.*; import com.eu.habbo.habbohotel.items.interactions.wired.extra.WiredBlob; import com.eu.habbo.habbohotel.items.interactions.wired.extra.WiredExtraRandom; import com.eu.habbo.habbohotel.items.interactions.wired.extra.WiredExtraUnseen; +import com.eu.habbo.habbohotel.wired.highscores.WiredHighscoreManager; import com.eu.habbo.habbohotel.items.interactions.wired.triggers.*; import com.eu.habbo.habbohotel.users.Habbo; import com.eu.habbo.habbohotel.users.HabboItem; @@ -69,6 +70,7 @@ public class ItemManager { private final THashSet interactionsList; private final THashMap soundTracks; private final YoutubeManager youtubeManager; + private final WiredHighscoreManager highscoreManager; private final TreeMap newuserGifts; public ItemManager() { @@ -77,6 +79,7 @@ public class ItemManager { this.interactionsList = new THashSet<>(); this.soundTracks = new THashMap<>(); this.youtubeManager = new YoutubeManager(); + this.highscoreManager = new WiredHighscoreManager(); this.newuserGifts = new TreeMap<>(); } @@ -90,6 +93,7 @@ public class ItemManager { this.loadCrackable(); this.loadSoundTracks(); this.youtubeManager.load(); + this.highscoreManager.load(); this.loadNewUserGifts(); Emulator.getLogging().logStart("Item Manager -> Loaded! (" + (System.currentTimeMillis() - millis) + " MS)"); @@ -784,8 +788,13 @@ public class ItemManager { return this.youtubeManager; } + public WiredHighscoreManager getHighscoreManager() { + return highscoreManager; + } + public void dispose() { this.items.clear(); + this.highscoreManager.dispose(); Emulator.getLogging().logShutdownLine("Item Manager -> Disposed!"); } diff --git a/src/main/java/com/eu/habbo/habbohotel/items/interactions/InteractionWiredHighscore.java b/src/main/java/com/eu/habbo/habbohotel/items/interactions/InteractionWiredHighscore.java index 9da022e9..054ce7d8 100644 --- a/src/main/java/com/eu/habbo/habbohotel/items/interactions/InteractionWiredHighscore.java +++ b/src/main/java/com/eu/habbo/habbohotel/items/interactions/InteractionWiredHighscore.java @@ -3,24 +3,23 @@ package com.eu.habbo.habbohotel.items.interactions; import com.eu.habbo.Emulator; import com.eu.habbo.habbohotel.gameclients.GameClient; import com.eu.habbo.habbohotel.items.Item; +import com.eu.habbo.habbohotel.wired.highscores.WiredHighscoreRow; import com.eu.habbo.habbohotel.rooms.Room; import com.eu.habbo.habbohotel.rooms.RoomUnit; import com.eu.habbo.habbohotel.users.HabboItem; -import com.eu.habbo.habbohotel.wired.WiredHighscoreClearType; -import com.eu.habbo.habbohotel.wired.WiredHighscoreData; -import com.eu.habbo.habbohotel.wired.WiredHighscoreScoreType; +import com.eu.habbo.habbohotel.wired.highscores.WiredHighscoreClearType; +import com.eu.habbo.habbohotel.wired.highscores.WiredHighscoreScoreType; import com.eu.habbo.messages.ServerMessage; -import gnu.trove.set.hash.THashSet; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.List; public class InteractionWiredHighscore extends HabboItem { public WiredHighscoreScoreType scoreType; public WiredHighscoreClearType clearType; - private THashSet data; - private int lastUpdate; + private List data; public InteractionWiredHighscore(ResultSet set, Item baseItem) throws SQLException { super(set, baseItem); @@ -37,13 +36,7 @@ public class InteractionWiredHighscore extends HabboItem { Emulator.getLogging().logErrorLine(e); } - if (this.getRoomId() > 0) { - Room room = Emulator.getGameEnvironment().getRoomManager().getRoom(this.getRoomId()); - - if (room != null) { - this.reloadData(room); - } - } + this.reloadData(); } public InteractionWiredHighscore(int id, int userId, Item item, String extradata, int limitedStack, int limitedSells) { @@ -60,6 +53,8 @@ public class InteractionWiredHighscore extends HabboItem { } catch (Exception e) { Emulator.getLogging().logErrorLine(e); } + + this.reloadData(); } @Override @@ -86,7 +81,6 @@ public class InteractionWiredHighscore extends HabboItem { try { int state = Integer.valueOf(this.getExtradata()); this.setExtradata(Math.abs(state - 1) + ""); - this.reloadData(room); room.updateItem(this); } catch (Exception e) { Emulator.getLogging().logErrorLine(e); @@ -98,43 +92,22 @@ public class InteractionWiredHighscore extends HabboItem { public void serializeExtradata(ServerMessage serverMessage) { serverMessage.appendInt(6); serverMessage.appendString(this.getExtradata()); - serverMessage.appendInt(this.scoreType.type); //score type - serverMessage.appendInt(this.clearType.type); //clear type + serverMessage.appendInt(this.scoreType.type); + serverMessage.appendInt(this.clearType.type); - if (this.getRoomId() == 0) { - serverMessage.appendInt(0); - } else { - Room room = Emulator.getGameEnvironment().getRoomManager().getRoom(this.getRoomId()); + if (this.data != null) { + serverMessage.appendInt(this.data.size()); - if (room == null) { - serverMessage.appendInt(0); - } else { - if (Emulator.getIntUnixTimestamp() - this.lastUpdate > 60 * 60) { - this.reloadData(room); - } + for (WiredHighscoreRow row : this.data) { + serverMessage.appendInt(row.getValue()); - if (this.data != null) { - serverMessage.appendInt(this.data.size()); //count - - for (WiredHighscoreData dataSet : this.data) { - if (this.scoreType == WiredHighscoreScoreType.PERTEAM) { - serverMessage.appendInt(dataSet.teamScore); //Team score - } else if (dataSet.usernames.length == 1) { - serverMessage.appendInt(dataSet.score); - } else { - serverMessage.appendInt(dataSet.totalScore); - } - - serverMessage.appendInt(dataSet.usernames.length); //Users count - - for (String codeDragon : dataSet.usernames) { - serverMessage.appendString(codeDragon); - } - } - } else { - serverMessage.appendInt(0); + serverMessage.appendInt(row.getUsers().size()); + for (String username : row.getUsers()) { + serverMessage.appendString(username); } } + } else { + serverMessage.appendInt(0); } super.serializeExtradata(serverMessage); @@ -142,7 +115,7 @@ public class InteractionWiredHighscore extends HabboItem { @Override public void onPlace(Room room) { - this.reloadData(room); + this.reloadData(); super.onPlace(room); } @@ -151,11 +124,9 @@ public class InteractionWiredHighscore extends HabboItem { if (this.data != null) { this.data.clear(); } - this.lastUpdate = 0; } - private void reloadData(Room room) { - this.data = room.getWiredHighscoreData(this.scoreType, this.clearType); - this.lastUpdate = Emulator.getIntUnixTimestamp(); + public void reloadData() { + this.data = Emulator.getGameEnvironment().getItemManager().getHighscoreManager().getHighscoreRowsForItem(this.getId(), this.clearType, this.scoreType); } } diff --git a/src/main/java/com/eu/habbo/habbohotel/rooms/Room.java b/src/main/java/com/eu/habbo/habbohotel/rooms/Room.java index ba34a047..1dd10859 100644 --- a/src/main/java/com/eu/habbo/habbohotel/rooms/Room.java +++ b/src/main/java/com/eu/habbo/habbohotel/rooms/Room.java @@ -135,7 +135,6 @@ public class Room implements Comparable, ISerialize, Runnable { private final TIntObjectMap moodlightData; private final THashSet wordFilterWords; private final TIntObjectMap roomItems; - private final THashMap>> wiredHighscoreData; private final Object loadLock = new Object(); //Use appropriately. Could potentially cause memory leaks when used incorrectly. public volatile boolean preventUnloading = false; @@ -289,7 +288,6 @@ public class Room implements Comparable, ISerialize, Runnable { this.activeTrades = new THashSet<>(0); this.rights = new TIntArrayList(); - this.wiredHighscoreData = new THashMap<>(); this.userVotes = new ArrayList<>(); } @@ -4211,70 +4209,6 @@ public class Room implements Comparable, ISerialize, Runnable { } } - public THashSet getWiredHighscoreData(WiredHighscoreScoreType scoreType, WiredHighscoreClearType clearType) { - if (!this.wiredHighscoreData.containsKey(scoreType)) { - this.loadWiredHighscoreData(scoreType, clearType); - } - - return this.wiredHighscoreData.get(scoreType).get(clearType); - } - - public void loadWiredHighscoreData(WiredHighscoreScoreType scoreType, WiredHighscoreClearType clearType) { - this.wiredHighscoreData.clear(); - THashSet wiredData = new THashSet<>(); - - try { - String query = "SELECT " + - "SUM(score + team_score) as total_score, " + - "COUNT(*) as wins, " + - "users.username, " + - "room_game_scores.*, " + - "GROUP_CONCAT(users.username) as usernames " + - "FROM room_game_scores " + - "INNER JOIN users ON room_game_scores.user_id = users.id " + - "WHERE room_id = ? AND game_start_timestamp >= ? "; - - int timestamp = 0; - if (clearType != WiredHighscoreClearType.ALLTIME) { - if (clearType == WiredHighscoreClearType.MONTHLY) { - timestamp = Emulator.getIntUnixTimestamp() - (31 * 24 * 60 * 60); - } else if (clearType == WiredHighscoreClearType.WEEKLY) { - timestamp = Emulator.getIntUnixTimestamp() - (7 * 24 * 60 * 60); - } else if (clearType == WiredHighscoreClearType.DAILY) { - timestamp = Emulator.getIntUnixTimestamp() - (24 * 60 * 60); - } - } - - - if (scoreType == WiredHighscoreScoreType.CLASSIC) { - query += "GROUP BY game_start_timestamp, user_id, team_id ORDER BY total_score DESC"; - } else if (scoreType == WiredHighscoreScoreType.MOSTWIN) { - query += "GROUP BY game_start_timestamp, game_name, team_id ORDER BY wins DESC, total_score ASC"; - } else if (scoreType == WiredHighscoreScoreType.PERTEAM) { - query += "GROUP BY game_start_timestamp, team_id ORDER BY team_score DESC"; - } - - query += " LIMIT " + Emulator.getConfig().getValue("wired.highscores.displaycount"); - - try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement(query)) { - statement.setInt(1, this.id); - statement.setInt(2, timestamp); - - try (ResultSet set = statement.executeQuery()) { - while (set.next()) { - wiredData.add(new WiredHighscoreData(set.getString("usernames").split(","), set.getInt("score"), set.getInt("team_score"), set.getInt("total_score"))); - } - } - } - } catch (SQLException e) { - Emulator.getLogging().logSQLException(e); - } - - THashMap> dataMap = new THashMap<>(); - dataMap.put(clearType, wiredData); - this.wiredHighscoreData.put(scoreType, dataMap); - } - public void handleWordQuiz(Habbo habbo, String answer) { synchronized (this.userVotes) { if (!this.wordQuiz.isEmpty() && !this.hasVotedInWordQuiz(habbo)) { diff --git a/src/main/java/com/eu/habbo/habbohotel/wired/WiredHighscoreData.java b/src/main/java/com/eu/habbo/habbohotel/wired/WiredHighscoreData.java deleted file mode 100644 index 07221e5b..00000000 --- a/src/main/java/com/eu/habbo/habbohotel/wired/WiredHighscoreData.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.eu.habbo.habbohotel.wired; - -public class WiredHighscoreData { - public String[] usernames; - public int score; - public int teamScore; - public int totalScore; - - public WiredHighscoreData(String[] usernames, int score, int teamScore, int totalScore) { - this.usernames = usernames; - - this.score = score; - this.teamScore = teamScore; - this.totalScore = totalScore; - } -} diff --git a/src/main/java/com/eu/habbo/habbohotel/wired/WiredHighscoreClearType.java b/src/main/java/com/eu/habbo/habbohotel/wired/highscores/WiredHighscoreClearType.java similarity index 80% rename from src/main/java/com/eu/habbo/habbohotel/wired/WiredHighscoreClearType.java rename to src/main/java/com/eu/habbo/habbohotel/wired/highscores/WiredHighscoreClearType.java index aead67bd..602daa09 100644 --- a/src/main/java/com/eu/habbo/habbohotel/wired/WiredHighscoreClearType.java +++ b/src/main/java/com/eu/habbo/habbohotel/wired/highscores/WiredHighscoreClearType.java @@ -1,4 +1,4 @@ -package com.eu.habbo.habbohotel.wired; +package com.eu.habbo.habbohotel.wired.highscores; public enum WiredHighscoreClearType { ALLTIME(0), diff --git a/src/main/java/com/eu/habbo/habbohotel/wired/highscores/WiredHighscoreDataEntry.java b/src/main/java/com/eu/habbo/habbohotel/wired/highscores/WiredHighscoreDataEntry.java new file mode 100644 index 00000000..e063d05e --- /dev/null +++ b/src/main/java/com/eu/habbo/habbohotel/wired/highscores/WiredHighscoreDataEntry.java @@ -0,0 +1,51 @@ +package com.eu.habbo.habbohotel.wired.highscores; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class WiredHighscoreDataEntry { + private final int itemId; + private final List userIds; + private final int score; + private final boolean isWin; + private final int timestamp; + + public WiredHighscoreDataEntry(int itemId, List userIds, int score, boolean isWin, int timestamp) { + this.itemId = itemId; + this.userIds = userIds; + this.score = score; + this.isWin = isWin; + this.timestamp = timestamp; + } + + public WiredHighscoreDataEntry(ResultSet set) throws SQLException { + this.itemId = set.getInt("item_id"); + this.userIds = Arrays.stream(set.getString("user_ids").split(",")).map(Integer::valueOf).collect(Collectors.toList()); + this.score = set.getInt("score"); + this.isWin = set.getInt("is_win") == 1; + this.timestamp = set.getInt("timestamp"); + } + + public int getItemId() { + return itemId; + } + + public List getUserIds() { + return userIds; + } + + public int getScore() { + return score; + } + + public boolean isWin() { + return isWin; + } + + public int getTimestamp() { + return timestamp; + } +} diff --git a/src/main/java/com/eu/habbo/habbohotel/wired/highscores/WiredHighscoreManager.java b/src/main/java/com/eu/habbo/habbohotel/wired/highscores/WiredHighscoreManager.java new file mode 100644 index 00000000..aa5935ef --- /dev/null +++ b/src/main/java/com/eu/habbo/habbohotel/wired/highscores/WiredHighscoreManager.java @@ -0,0 +1,167 @@ +package com.eu.habbo.habbohotel.wired.highscores; + +import com.eu.habbo.Emulator; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.time.*; +import java.time.temporal.TemporalAdjusters; +import java.time.temporal.WeekFields; +import java.util.*; +import java.util.concurrent.ScheduledFuture; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class WiredHighscoreManager { + private final HashMap> data = new HashMap<>(); + + private final static DayOfWeek firstDayOfWeek = WeekFields.of(new Locale(System.getProperty("user.language"), System.getProperty("user.country"))).getFirstDayOfWeek(); + private final static DayOfWeek lastDayOfWeek = DayOfWeek.of(((firstDayOfWeek.getValue() + 5) % DayOfWeek.values().length) + 1); + private final static ZoneId zoneId = ZoneId.systemDefault(); + + public static ScheduledFuture midnightUpdater = null; + + public void load() { + long millis = System.currentTimeMillis(); + + this.data.clear(); + this.loadHighscoreData(); + + if (midnightUpdater != null) { + midnightUpdater.cancel(true); + } + midnightUpdater = Emulator.getThreading().run(new WiredHighscoreMidnightUpdater(), WiredHighscoreMidnightUpdater.getNextUpdaterRun()); + + Emulator.getLogging().logStart("Highscore Manager -> Loaded! (" + (System.currentTimeMillis() - millis) + " MS, " + this.data.size() + " items)"); + } + + public void dispose() { + if (midnightUpdater != null) { + midnightUpdater.cancel(true); + } + + this.data.clear(); + } + + private void loadHighscoreData() { + try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("SELECT * FROM items_highscore_data")) { + try (ResultSet set = statement.executeQuery()) { + while (set.next()) { + WiredHighscoreDataEntry entry = new WiredHighscoreDataEntry(set); + + if (!this.data.containsKey(entry.getItemId())) { + this.data.put(entry.getItemId(), new ArrayList<>()); + } + + this.data.get(entry.getItemId()).add(entry); + } + } + } catch (SQLException e) { + Emulator.getLogging().logSQLException(e); + } + } + + public void addHighscoreData(WiredHighscoreDataEntry entry) { + if (!this.data.containsKey(entry.getItemId())) { + this.data.put(entry.getItemId(), new ArrayList<>()); + } + + this.data.get(entry.getItemId()).add(entry); + + try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("INSERT INTO `items_highscore_data` (`item_id`, `user_ids`, `score`, `is_win`, `timestamp`) VALUES (?, ?, ?, ?, ?)")) { + statement.setInt(1, entry.getItemId()); + statement.setString(2, String.join(",", entry.getUserIds().stream().map(Object::toString).collect(Collectors.toList()))); + statement.setInt(3, entry.getScore()); + statement.setInt(4, entry.isWin() ? 1 : 0); + statement.setInt(5, entry.getTimestamp()); + + statement.execute(); + } catch (SQLException e) { + Emulator.getLogging().logSQLException(e); + } + } + + public List getHighscoreRowsForItem(int itemId, WiredHighscoreClearType clearType, WiredHighscoreScoreType scoreType) { + if (!this.data.containsKey(itemId)) return null; + + Stream highscores = this.data.get(itemId).stream() + .filter(entry -> this.timeMatchesEntry(entry, clearType) && (scoreType != WiredHighscoreScoreType.MOSTWIN || entry.isWin())) + .map(entry -> new WiredHighscoreRow( + entry.getUserIds().stream() + .map(id -> Emulator.getGameEnvironment().getHabboManager().getHabboInfo(id).getUsername()) + .collect(Collectors.toList()), + entry.getScore() + )); + + if (scoreType == WiredHighscoreScoreType.CLASSIC) { + return highscores.sorted(WiredHighscoreRow::compareTo).collect(Collectors.toList()); + } + + if (scoreType == WiredHighscoreScoreType.PERTEAM) { + return highscores + .collect(Collectors.groupingBy(h -> h.getUsers().hashCode())) + .entrySet() + .stream() + .map(e -> e.getValue().stream() + .sorted(WiredHighscoreRow::compareTo) + .collect(Collectors.toList()) + .get(0) + ) + .sorted(WiredHighscoreRow::compareTo) + .collect(Collectors.toList()); + } + + if (scoreType == WiredHighscoreScoreType.MOSTWIN) { + return highscores + .collect(Collectors.groupingBy(h -> h.getUsers().hashCode())) + .entrySet() + .stream() + .map(e -> new WiredHighscoreRow(e.getValue().get(0).getUsers(), e.getValue().size())) + .sorted(WiredHighscoreRow::compareTo) + .collect(Collectors.toList()); + } + + return null; + } + + private boolean timeMatchesEntry(WiredHighscoreDataEntry entry, WiredHighscoreClearType timeType) { + switch (timeType) { + case DAILY: + return entry.getTimestamp() > this.getTodayStartTimestamp() && entry.getTimestamp() < this.getTodayEndTimestamp(); + case WEEKLY: + return entry.getTimestamp() > this.getWeekStartTimestamp() && entry.getTimestamp() < this.getWeekEndTimestamp(); + case MONTHLY: + return entry.getTimestamp() > this.getMonthStartTimestamp() && entry.getTimestamp() < this.getMonthEndTimestamp(); + case ALLTIME: + return true; + } + + return false; + } + + private long getTodayStartTimestamp() { + return LocalDateTime.now().with(LocalTime.MIDNIGHT).atZone(zoneId).toEpochSecond(); + } + + private long getTodayEndTimestamp() { + return LocalDateTime.now().with(LocalTime.MIDNIGHT).plusDays(1).plusSeconds(-1).atZone(zoneId).toEpochSecond(); + } + + private long getWeekStartTimestamp() { + return LocalDateTime.now().with(LocalTime.MIDNIGHT).with(TemporalAdjusters.previousOrSame(firstDayOfWeek)).atZone(zoneId).toEpochSecond(); + } + + private long getWeekEndTimestamp() { + return LocalDateTime.now().with(LocalTime.MIDNIGHT).plusDays(1).plusSeconds(-1).with(TemporalAdjusters.nextOrSame(lastDayOfWeek)).atZone(zoneId).toEpochSecond(); + } + + private long getMonthStartTimestamp() { + return LocalDateTime.now().with(LocalTime.MIDNIGHT).with(TemporalAdjusters.firstDayOfMonth()).atZone(zoneId).toEpochSecond(); + } + + private long getMonthEndTimestamp() { + return LocalDateTime.now().with(LocalTime.MIDNIGHT).plusDays(1).plusSeconds(-1).with(TemporalAdjusters.lastDayOfMonth()).atZone(zoneId).toEpochSecond(); + } +} diff --git a/src/main/java/com/eu/habbo/habbohotel/wired/highscores/WiredHighscoreMidnightUpdater.java b/src/main/java/com/eu/habbo/habbohotel/wired/highscores/WiredHighscoreMidnightUpdater.java new file mode 100644 index 00000000..4a24f569 --- /dev/null +++ b/src/main/java/com/eu/habbo/habbohotel/wired/highscores/WiredHighscoreMidnightUpdater.java @@ -0,0 +1,33 @@ +package com.eu.habbo.habbohotel.wired.highscores; + +import com.eu.habbo.Emulator; +import com.eu.habbo.habbohotel.items.interactions.InteractionWiredHighscore; +import com.eu.habbo.habbohotel.rooms.Room; +import com.eu.habbo.habbohotel.users.HabboItem; +import gnu.trove.set.hash.THashSet; + +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.ZoneId; +import java.util.List; + +public class WiredHighscoreMidnightUpdater implements Runnable { + @Override + public void run() { + List rooms = Emulator.getGameEnvironment().getRoomManager().getActiveRooms(); + + for (Room room : rooms) { + THashSet items = room.getRoomSpecialTypes().getItemsOfType(InteractionWiredHighscore.class); + for (HabboItem item : items) { + ((InteractionWiredHighscore) item).reloadData(); + room.updateItem(item); + } + } + + WiredHighscoreManager.midnightUpdater = Emulator.getThreading().run(new WiredHighscoreMidnightUpdater(), getNextUpdaterRun()); + } + + public static int getNextUpdaterRun() { + return Math.toIntExact(LocalDateTime.now().with(LocalTime.MIDNIGHT).plusDays(1).plusSeconds(-1).atZone(ZoneId.systemDefault()).toEpochSecond() - Emulator.getIntUnixTimestamp()) + 5; + } +} diff --git a/src/main/java/com/eu/habbo/habbohotel/wired/highscores/WiredHighscoreRow.java b/src/main/java/com/eu/habbo/habbohotel/wired/highscores/WiredHighscoreRow.java new file mode 100644 index 00000000..1282a1bc --- /dev/null +++ b/src/main/java/com/eu/habbo/habbohotel/wired/highscores/WiredHighscoreRow.java @@ -0,0 +1,32 @@ +package com.eu.habbo.habbohotel.wired.highscores; + +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +public class WiredHighscoreRow implements Comparable { + public static final Comparator COMPARATOR = Comparator.comparing(WiredHighscoreRow::getValue).reversed(); + + private final List users; + private final int value; + + public WiredHighscoreRow(List users, int value) { + Collections.sort(users); + + this.users = users; + this.value = value; + } + + public List getUsers() { + return users; + } + + public int getValue() { + return value; + } + + @Override + public int compareTo(WiredHighscoreRow otherRow) { + return COMPARATOR.compare(this, otherRow); + } +} diff --git a/src/main/java/com/eu/habbo/habbohotel/wired/WiredHighscoreScoreType.java b/src/main/java/com/eu/habbo/habbohotel/wired/highscores/WiredHighscoreScoreType.java similarity index 79% rename from src/main/java/com/eu/habbo/habbohotel/wired/WiredHighscoreScoreType.java rename to src/main/java/com/eu/habbo/habbohotel/wired/highscores/WiredHighscoreScoreType.java index cc2e50a0..d70e07ed 100644 --- a/src/main/java/com/eu/habbo/habbohotel/wired/WiredHighscoreScoreType.java +++ b/src/main/java/com/eu/habbo/habbohotel/wired/highscores/WiredHighscoreScoreType.java @@ -1,4 +1,4 @@ -package com.eu.habbo.habbohotel.wired; +package com.eu.habbo.habbohotel.wired.highscores; public enum WiredHighscoreScoreType { PERTEAM(0),