Implement most viewed guild forums

This commit is contained in:
Alejandro 2020-01-28 01:06:53 +02:00
parent be6b206211
commit 4f351b04ee
6 changed files with 110 additions and 10 deletions

View File

@ -10,3 +10,11 @@ ADD COLUMN `category` int(11) NOT NULL DEFAULT 0 AFTER `start_timestamp`;
INSERT INTO `emulator_settings`(`key`, `value`) VALUES ('navigator.eventcategories', '1,Hottest Events,false;2,Parties & Music,true;3,Role Play,true;4,Help Desk,true;5,Trading,true;6,Games,true;7,Debates & Discussions,true;8,Grand Openings,true;9,Friending,true;10,Jobs,true;11,Group Events,true');
INSERT INTO `emulator_settings`(`key`, `value`) VALUES ('room.promotion.badge', 'RADZZ');
CREATE TABLE `guild_forum_views` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`guild_id` int(11) NOT NULL,
`timestamp` int(11) NOT NULL,
PRIMARY KEY (`id`)
);

View File

@ -2,6 +2,7 @@ package com.eu.habbo.habbohotel.guilds;
import com.eu.habbo.Emulator;
import com.eu.habbo.habbohotel.gameclients.GameClient;
import com.eu.habbo.habbohotel.guilds.forums.ForumView;
import com.eu.habbo.habbohotel.items.interactions.InteractionGuildFurni;
import com.eu.habbo.habbohotel.rooms.Room;
import com.eu.habbo.habbohotel.users.Habbo;
@ -14,10 +15,8 @@ import gnu.trove.map.hash.TIntObjectHashMap;
import gnu.trove.set.hash.THashSet;
import java.sql.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.*;
import java.util.stream.Collectors;
public class GuildManager {
@ -25,12 +24,16 @@ public class GuildManager {
private final TIntObjectMap<Guild> guilds;
private final THashSet<ForumView> views = new THashSet<>();
public GuildManager() {
long millis = System.currentTimeMillis();
this.guildParts = new THashMap<GuildPartType, THashMap<Integer, GuildPart>>();
this.guilds = TCollections.synchronizedMap(new TIntObjectHashMap<Guild>());
this.loadGuildParts();
this.loadGuildViews();
Emulator.getLogging().logStart("Guild Manager -> Loaded! (" + (System.currentTimeMillis() - millis) + " MS)");
}
@ -53,6 +56,19 @@ public class GuildManager {
}
}
public void loadGuildViews() {
this.views.clear();
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection();
Statement statement = connection.createStatement();
ResultSet set = statement.executeQuery("SELECT * FROM guild_forum_views")) {
while (set.next()) {
this.views.add(new ForumView(set));
}
} catch (SQLException e) {
Emulator.getLogging().logSQLException(e);
}
}
public Guild createGuild(Habbo habbo, int roomId, String roomName, String name, String description, String badge, int colorOne, int colorTwo) {
Guild guild = new Guild(habbo.getHabboInfo().getId(), habbo.getHabboInfo().getUsername(), roomId, roomName, name, description, colorOne, colorTwo, badge);
@ -594,4 +610,38 @@ public class GuildManager {
}
Emulator.getLogging().logShutdownLine("Guild Manager -> Disposed!");
}
public boolean hasViewedForum(int userId, int guildId) {
return this.views.stream()
.anyMatch(v -> v.getUserId() == userId && v.getGuildId() == guildId && v.getTimestamp() > (Emulator.getIntUnixTimestamp() - 7 * 24 * 60 * 60));
}
public void addView(int userId, int guildId) {
ForumView view = new ForumView(userId, guildId, Emulator.getIntUnixTimestamp());
this.views.add(view);
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("INSERT INTO `guild_forum_views`(`user_id`, `guild_id`, `timestamp`) VALUES (?, ?, ?)")) {
statement.setInt(1, view.getUserId());
statement.setInt(2, view.getGuildId());
statement.setInt(3, view.getTimestamp());
statement.execute();
} catch (SQLException e) {
Emulator.getLogging().logSQLException(e);
}
}
public Set<Guild> getMostViewed() {
return this.views.stream()
.filter(v -> v.getTimestamp() > (Emulator.getIntUnixTimestamp() - 7 * 24 * 60 * 60))
.collect(Collectors.groupingBy(ForumView::getGuildId))
.entrySet()
.stream()
.sorted(Comparator.comparingInt((Map.Entry<Integer, List<ForumView>> a) -> a.getValue().size()))
.map(k -> this.getGuild(k.getKey()))
.filter(g -> g != null && g.canReadForum() == SettingsState.EVERYONE)
.limit(100)
.collect(Collectors.toSet());
}
}

View File

@ -0,0 +1,34 @@
package com.eu.habbo.habbohotel.guilds.forums;
import java.sql.ResultSet;
import java.sql.SQLException;
public class ForumView {
private final int userId;
private final int guildId;
private final int timestamp;
public ForumView(int userId, int guildId, int timestamp) {
this.userId = userId;
this.guildId = guildId;
this.timestamp = timestamp;
}
public ForumView(ResultSet set) throws SQLException {
this.userId = set.getInt("user_id");
this.guildId = set.getInt("guild_id");
this.timestamp = set.getInt("timestamp");
}
public int getUserId() {
return userId;
}
public int getGuildId() {
return guildId;
}
public int getTimestamp() {
return timestamp;
}
}

View File

@ -16,5 +16,9 @@ public class GuildForumDataEvent extends MessageHandler {
return;
this.client.sendResponse(new GuildForumDataComposer(guild, this.client.getHabbo()));
if (!Emulator.getGameEnvironment().getGuildManager().hasViewedForum(this.client.getHabbo().getHabboInfo().getId(), guildId)) {
Emulator.getGameEnvironment().getGuildManager().addView(this.client.getHabbo().getHabboInfo().getId(), guildId);
}
}
}

View File

@ -11,6 +11,7 @@ import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Set;
public class GuildForumListEvent extends MessageHandler {
@Override
@ -19,14 +20,14 @@ public class GuildForumListEvent extends MessageHandler {
int offset = this.packet.readInt();
int amount = this.packet.readInt();
THashSet<Guild> guilds = null;
Set<Guild> guilds = null;
switch (mode) {
case 0: // most active
guilds = getPopularForums();
guilds = getActiveForums();
break;
case 1: // most viewed
guilds = getPopularForums();
guilds = Emulator.getGameEnvironment().getGuildManager().getMostViewed();
break;
case 2: // my groups
@ -39,14 +40,16 @@ public class GuildForumListEvent extends MessageHandler {
}
}
private THashSet<Guild> getPopularForums() {
private THashSet<Guild> getActiveForums() {
THashSet<Guild> guilds = new THashSet<Guild>();
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("SELECT `guilds`.`id`, SUM(`guilds_forums_threads`.`posts_count`) AS `post_count` " +
"FROM `guilds_forums_threads` " +
"LEFT JOIN `guilds` ON `guilds`.`id` = `guilds_forums_threads`.`guild_id` " +
"WHERE `guilds`.`read_forum` = 'EVERYONE' AND `guilds_forums_threads`.`created_at` > ? " +
"GROUP BY `guilds`.`id` " +
"ORDER BY `post_count` DESC LIMIT 100")) {
statement.setInt(1, Emulator.getIntUnixTimestamp() - 7 * 24 * 60 * 60);
ResultSet set = statement.executeQuery();
while (set.next()) {

View File

@ -8,14 +8,15 @@ import com.eu.habbo.messages.outgoing.Outgoing;
import gnu.trove.set.hash.THashSet;
import java.util.Iterator;
import java.util.Set;
public class GuildForumListComposer extends MessageComposer {
private final THashSet<Guild> guilds;
private final Set<Guild> guilds;
private final Habbo habbo;
private final int mode;
private final int index;
public GuildForumListComposer(THashSet<Guild> guilds, Habbo habbo, int mode, int index) {
public GuildForumListComposer(Set<Guild> guilds, Habbo habbo, int mode, int index) {
this.guilds = guilds;
this.habbo = habbo;
this.mode = mode;