Make community membership updating entirely redis-based

This commit is contained in:
AlexTheCoder 2016-12-22 20:27:33 -05:00 committed by cnr
parent a0716ae774
commit 56ecab1985
7 changed files with 89 additions and 112 deletions

View File

@ -82,7 +82,7 @@ public class PlayerTracker implements Listener
public void playerConnect(final PostLoginEvent event) public void playerConnect(final PostLoginEvent event)
{ {
_ignoreKick.add(event.getPlayer().getUniqueId()); _ignoreKick.add(event.getPlayer().getUniqueId());
PlayerJoinCommand command = new PlayerJoinCommand(event.getPlayer().getUniqueId()); PlayerJoinCommand command = new PlayerJoinCommand(event.getPlayer().getUniqueId(), event.getPlayer().getName());
command.publish(); command.publish();
} }

View File

@ -7,7 +7,6 @@ import java.util.UUID;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import mineplex.core.common.util.C;
import mineplex.core.common.util.UtilPlayer; import mineplex.core.common.util.UtilPlayer;
import mineplex.core.game.GameDisplay; import mineplex.core.game.GameDisplay;

View File

@ -2,6 +2,7 @@ package mineplex.core.communities;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -56,11 +57,13 @@ import mineplex.core.communities.redis.CommunityUpdateName;
import mineplex.core.communities.redis.CommunityUpdateNameHandler; import mineplex.core.communities.redis.CommunityUpdateNameHandler;
import mineplex.core.communities.redis.CommunityUpdateSetting; import mineplex.core.communities.redis.CommunityUpdateSetting;
import mineplex.core.communities.redis.CommunityUpdateSettingHandler; import mineplex.core.communities.redis.CommunityUpdateSettingHandler;
import mineplex.core.communities.redis.PlayerJoinHandler;
import mineplex.core.communities.storage.CommunityRepository; import mineplex.core.communities.storage.CommunityRepository;
import mineplex.core.preferences.Preference; import mineplex.core.preferences.Preference;
import mineplex.core.preferences.PreferencesManager; import mineplex.core.preferences.PreferencesManager;
import mineplex.core.recharge.Recharge; import mineplex.core.recharge.Recharge;
import mineplex.serverdata.Region; import mineplex.serverdata.Region;
import mineplex.serverdata.commands.PlayerJoinCommand;
import mineplex.serverdata.commands.ServerCommandManager; import mineplex.serverdata.commands.ServerCommandManager;
import mineplex.serverdata.data.DataRepository; import mineplex.serverdata.data.DataRepository;
import mineplex.serverdata.data.PlayerStatus; import mineplex.serverdata.data.PlayerStatus;
@ -76,6 +79,7 @@ public class CommunityManager extends MiniDbClientPlugin<CommunityMemberData>
private final Map<Integer, Community> _loadedCommunities; private final Map<Integer, Community> _loadedCommunities;
public final List<Integer> BrowserIds = new LinkedList<>(); public final List<Integer> BrowserIds = new LinkedList<>();
private final List<UUID> _creating = new ArrayList<>();
public final DataRepository<PlayerStatus> StatusRepository; public final DataRepository<PlayerStatus> StatusRepository;
@ -110,7 +114,7 @@ public class CommunityManager extends MiniDbClientPlugin<CommunityMemberData>
LinkedList<Community> communities = new LinkedList<>(); LinkedList<Community> communities = new LinkedList<>();
_loadedCommunities.values().forEach(community -> communities.add(community)); _loadedCommunities.values().forEach(community -> communities.add(community));
_repo.updateMembersAndJoinRequests(communities); _repo.updateMembers(communities);
}, 0L, 20 * 5); }, 0L, 20 * 5);
Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, () -> Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, () ->
@ -134,6 +138,7 @@ public class CommunityManager extends MiniDbClientPlugin<CommunityMemberData>
ServerCommandManager.getInstance().registerCommandType(CommunityUpdateMembership.class, new CommunityUpdateMembershipHandler(this)); ServerCommandManager.getInstance().registerCommandType(CommunityUpdateMembership.class, new CommunityUpdateMembershipHandler(this));
ServerCommandManager.getInstance().registerCommandType(CommunityUpdateName.class, new CommunityUpdateNameHandler(this)); ServerCommandManager.getInstance().registerCommandType(CommunityUpdateName.class, new CommunityUpdateNameHandler(this));
ServerCommandManager.getInstance().registerCommandType(CommunityUpdateSetting.class, new CommunityUpdateSettingHandler(this)); ServerCommandManager.getInstance().registerCommandType(CommunityUpdateSetting.class, new CommunityUpdateSettingHandler(this));
ServerCommandManager.getInstance().registerCommandType(PlayerJoinCommand.class, new PlayerJoinHandler(this));
} }
public boolean ownsCommunity(UUID uuid) public boolean ownsCommunity(UUID uuid)
@ -143,6 +148,15 @@ public class CommunityManager extends MiniDbClientPlugin<CommunityMemberData>
.anyMatch(entry -> entry.getKey().equals(uuid) && entry.getValue().Role == CommunityRole.LEADER); .anyMatch(entry -> entry.getKey().equals(uuid) && entry.getValue().Role == CommunityRole.LEADER);
} }
public void updateAllMemberData(UUID uuid, String name)
{
_loadedCommunities.values().stream().filter(community -> community.getMembers().containsKey(uuid) && !community.getMembers().get(uuid).Name.equalsIgnoreCase(name)).forEach(community ->
{
community.getMembers().get(uuid).updateName(name);
UtilServer.CallEvent(new CommunityMembershipUpdateEvent(community));
});
}
private void cycleBrowser() private void cycleBrowser()
{ {
if (_cycling) if (_cycling)
@ -595,6 +609,12 @@ public class CommunityManager extends MiniDbClientPlugin<CommunityMemberData>
public void handleCreate(Player sender, int accountId, String name) public void handleCreate(Player sender, int accountId, String name)
{ {
if (_creating.contains(sender.getUniqueId()))
{
sender.sendMessage(F.main(getName(), "You are already creating a Community!"));
return;
}
_creating.add(sender.getUniqueId());
runAsync(() -> runAsync(() ->
{ {
_repo.createCommunity(name, accountId, id -> _repo.createCommunity(name, accountId, id ->
@ -606,6 +626,7 @@ public class CommunityManager extends MiniDbClientPlugin<CommunityMemberData>
else else
{ {
new CommunityCreate(sender.getUniqueId().toString(), id).publish(); new CommunityCreate(sender.getUniqueId().toString(), id).publish();
runSync(() -> _creating.remove(sender.getUniqueId()));
} }
}); });
}); });

View File

@ -15,7 +15,6 @@ public class CommunityMemberInfo
public final UUID UUID; public final UUID UUID;
public final int AccountId; public final int AccountId;
public CommunityRole Role; public CommunityRole Role;
public boolean OwnsCommunity = false;
public boolean ReadingChat = true; public boolean ReadingChat = true;
private ItemStack _memberIcon, _outsiderIcon; private ItemStack _memberIcon, _outsiderIcon;
private long _sinceLastLogin; private long _sinceLastLogin;
@ -54,10 +53,21 @@ public class CommunityMemberInfo
_memberIcon = builder.build(); _memberIcon = builder.build();
} }
public void update(String name, CommunityRole role, long timeSinceLastLogin, boolean online, String currentServer) public void setOffline()
{
_online = false;
_currentServer = "";
buildIcons();
}
public void updateName(String name)
{ {
Name = name; Name = name;
Role = role; buildIcons();
}
public void update(long timeSinceLastLogin, boolean online, String currentServer)
{
_sinceLastLogin = timeSinceLastLogin; _sinceLastLogin = timeSinceLastLogin;
_online = online; _online = online;
_currentServer = currentServer; _currentServer = currentServer;

View File

@ -0,0 +1,28 @@
package mineplex.core.communities.redis;
import java.util.UUID;
import mineplex.core.communities.CommunityManager;
import mineplex.serverdata.commands.CommandCallback;
import mineplex.serverdata.commands.PlayerJoinCommand;
import mineplex.serverdata.commands.ServerCommand;
public class PlayerJoinHandler implements CommandCallback
{
private CommunityManager _communityManager;
public PlayerJoinHandler(CommunityManager communityManager)
{
_communityManager = communityManager;
}
@Override
public void run(ServerCommand command)
{
if (command instanceof PlayerJoinCommand)
{
PlayerJoinCommand joinCommand = (PlayerJoinCommand)command;
_communityManager.updateAllMemberData(UUID.fromString(joinCommand.getUuid()), joinCommand.getName());
}
}
}

View File

@ -1,9 +1,7 @@
package mineplex.core.communities.storage; package mineplex.core.communities.storage;
import java.sql.Connection; import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
@ -86,7 +84,7 @@ public class CommunityRepository extends MinecraftRepository
PlayerStatus status = _repo.getElement(name); PlayerStatus status = _repo.getElement(name);
if (status != null) if (status != null)
{ {
info.update(name, role, timeSinceOnline, true, status.getServer()); info.update(timeSinceOnline, true, status.getServer());
} }
info.ReadingChat = readingChat; info.ReadingChat = readingChat;
community.getMembers().put(info.UUID, info); community.getMembers().put(info.UUID, info);
@ -157,7 +155,7 @@ public class CommunityRepository extends MinecraftRepository
PlayerStatus status = _repo.getElement(name); PlayerStatus status = _repo.getElement(name);
if (status != null) if (status != null)
{ {
info.update(name, role, timeSinceOnline, true, status.getServer()); info.update(timeSinceOnline, true, status.getServer());
} }
info.ReadingChat = readingChat; info.ReadingChat = readingChat;
community.getMembers().put(info.UUID, info); community.getMembers().put(info.UUID, info);
@ -204,120 +202,34 @@ public class CommunityRepository extends MinecraftRepository
} }
} }
public void updateMembersAndJoinRequests(LinkedList<Community> communities) public void updateMembers(LinkedList<Community> communities)
{ {
if (communities.isEmpty()) if (communities.isEmpty())
{ {
return; return;
} }
String query = "";
for (Community c : communities) for (Community c : communities)
{ {
query = query + GET_COMMUNITY_MEMBERS.replace("?", c.getId().toString()) + GET_COMMUNITY_JOIN_REQUESTS.replace("?", c.getId().toString()); for (CommunityMemberInfo info : c.getMembers().values())
}
if (query.isEmpty())
{
return;
}
try (Connection connection = getConnection(); Statement statement = connection.createStatement())
{
statement.executeQuery(query);
for (Community c : communities)
{ {
ResultSet memberSet = statement.getResultSet(); PlayerStatus status = _repo.getElement(info.Name);
while (memberSet.next()) boolean online = false;
String server = "";
if (status != null)
{ {
final UUID uuid = UUID.fromString(memberSet.getString("uuid")); online = true;
final String name = memberSet.getString("name"); server = status.getServer();
final CommunityRole role = CommunityRole.parseRole(memberSet.getString("communityRole")); info.update(System.currentTimeMillis(), online, server);
final long timeSinceOnline = memberSet.getTimestamp(6).getTime() - memberSet.getTimestamp(5).getTime(); }
//boolean readingChat = memberSet.getBoolean("readingChat"); else
{
if (c.getMembers().containsKey(uuid)) if (info.isOnline())
{ {
PlayerStatus status = _repo.getElement(name); info.setOffline();
boolean online = false;
String server = "";
if (status != null)
{
online = true;
server = status.getServer();
}
//c.getMembers().get(uuid).ReadingChat = readingChat;
c.getMembers().get(uuid).update(name, role, timeSinceOnline, online, server);
} }
} }
statement.getMoreResults();
ResultSet requestSet = statement.getResultSet();
while (requestSet.next())
{
final UUID uuid = UUID.fromString(requestSet.getString("uuid"));
final String name = requestSet.getString("name");
if (c.getJoinRequests().containsKey(uuid))
{
c.getJoinRequests().get(uuid).update(name);
}
}
statement.getMoreResults();
} }
} }
catch (SQLException e)
{
e.printStackTrace();
}
}
public void updateMembers(Map<UUID, CommunityMemberInfo> members, int communityId)
{
executeQuery(GET_COMMUNITY_MEMBERS, memberSet ->
{
while (memberSet.next())
{
final UUID uuid = UUID.fromString(memberSet.getString("uuid"));
final String name = memberSet.getString("name");
final CommunityRole role = CommunityRole.parseRole(memberSet.getString("communityRole"));
final long timeSinceOnline = memberSet.getTimestamp(5).getTime() - memberSet.getTimestamp(4).getTime();
//boolean readingChat = memberSet.getBoolean("readingChat");
if (members.containsKey(uuid))
{
PlayerStatus status = _repo.getElement(name);
boolean online = false;
String server = "";
if (status != null)
{
online = true;
server = status.getServer();
}
//members.get(uuid).ReadingChat = readingChat;
members.get(uuid).update(name, role, timeSinceOnline, online, server);
}
}
}, new ColumnInt("communityId", communityId));
}
public void updateJoinRequests(Map<UUID, CommunityJoinRequestInfo> requests, int communityId)
{
executeQuery(GET_COMMUNITY_JOIN_REQUESTS, requestSet ->
{
while (requestSet.next())
{
final UUID uuid = UUID.fromString(requestSet.getString("uuid"));
final String name = requestSet.getString("name");
if (requests.containsKey(uuid))
{
requests.get(uuid).update(name);
}
}
}, new ColumnInt("communityId", communityId));
} }
public void loadInvites(int accountId, List<Integer> invites) public void loadInvites(int accountId, List<Integer> invites)

View File

@ -5,10 +5,12 @@ import java.util.UUID;
public class PlayerJoinCommand extends ServerCommand public class PlayerJoinCommand extends ServerCommand
{ {
private String _uuid; private String _uuid;
private String _name;
public PlayerJoinCommand(UUID uuid) public PlayerJoinCommand(UUID uuid, String name)
{ {
_uuid = uuid.toString(); _uuid = uuid.toString();
_name = name;
} }
@Override @Override
@ -21,4 +23,9 @@ public class PlayerJoinCommand extends ServerCommand
{ {
return _uuid; return _uuid;
} }
}
public String getName()
{
return _name;
}
}