Further set up communities database class

This commit is contained in:
AlexTheCoder 2016-12-14 22:08:56 -05:00 committed by cnr
parent 9ba38ffd3b
commit 216b4fe947
1 changed files with 335 additions and 42 deletions

View File

@ -1,84 +1,377 @@
package mineplex.core.communities.storage;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.Map;
import java.util.UUID;
import org.bukkit.plugin.java.JavaPlugin;
import com.google.common.collect.Lists;
import mineplex.core.common.util.Callback;
import mineplex.core.communities.Community;
import mineplex.core.communities.CommunityJoinRequestInfo;
import mineplex.core.communities.CommunityMemberInfo;
import mineplex.core.communities.CommunityRole;
import mineplex.core.communities.CommunitySetting;
import mineplex.core.database.MinecraftRepository;
import mineplex.serverdata.data.DataRepository;
import mineplex.serverdata.data.PlayerStatus;
import mineplex.serverdata.database.DBPool;
import mineplex.serverdata.database.column.ColumnInt;
import mineplex.serverdata.database.column.ColumnVarChar;
public class CommunityRepository extends MinecraftRepository
{
private static final String GET_ALL_COMMUNITIES_FOR_BROWSER = "SELECT * FROM communities WHERE (SELECT COUNT(id) AS idTotal FROM communityMembers WHERE communityId=communities.id) > 5;";
//private static final String GET_TWO = "SELECT c.id, cm.clanRole FROM clans c WHERE (SELECT COUNT(id) AS idTotal FROM accountClan WHERE clanId=clans.id) > 5 INNER JOIN accountClan cm ON c.id=cm.clanId;";
private static final String GET_ALL_COMMUNITIES = /*Revise Query*/"SELECT * FROM communities;";//"SELECT * FROM communities WHERE (SELECT COUNT(id) AS idTotal FROM communityMembers WHERE communityId=communities.id) > 5;";
private static final String GET_COMMUNITY_BY_ID = "SELECT * FROM communities WHERE id=?;";
private static final String GET_COMMUNITY_BY_NAME = "SELECT * FROM communities WHERE name=?;";
private static final String GET_COMMUNITY_MEMBERS = "SELECT accountId, communityRole FROM communityMembers WHERE communityId=?;";
private static final String GET_COMMUNITY_MEMBERS = "SELECT cm.accountId, cm.communityRole, ac.name, ac.uuid, ac.lastLogin, now() FROM communityMembers cm INNER JOIN accounts ac ON ac.id=cm.accountId WHERE communityId=?;";
private static final String GET_COMMUNITY_JOIN_REQUESTS = "SELECT cjr.accountId, ac.name, ac.uuid FROM communityJoinRequests cjr INNER JOIN accounts ac ON ac.id=cjr.accountId WHERE communityId=?;";
private static final String GET_COMMUNITY_SETTINGS = "SELECT settingId, settingValue FROM communitySettings WHERE communityId=?;";
public CommunityRepository(JavaPlugin plugin)
private static final String REMOVE_FROM_COMMUNITY = "DELETE * FROM communityMembers WHERE accountId=? AND communityId=?;";
private static final String UPDATE_COMMUNITY_ROLE = "UPDATE communityMembers SET communityRole=? WHERE accountId=? AND communityId=?;";
private static final String ADD_TO_COMMUNITY = "INSERT INTO communityMembers (accountId, communityId, communityRole) VALUES (?, ?, ?);";
private static final String UPDATE_COMMUNITY_SETTING = "INSERT INTO communitySettings (settingId, communityId, settingValue) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE settingValue=VALUES(settingValue);";
private static final String UPDATE_COMMUNITY_NAME = "UPDATE communities SET name=? WHERE id=?;";
private static final String INVITE_TO_COMMUNITY = "INSERT INTO communityInvites (accountId, communityId) SELECT a.id AS accountId, ? FROM accounts as a WHERE a.name = ? ORDER BY a.lastLogin DESC LIMIT 1;";
private static final String DELETE_INVITE_TO_COMMUNITY = "DELETE i FROM communityInvites AS i INNER JOIN accounts as a ON i.accountId = a.id WHERE a.name = ? AND i.communityId=?;";
private static final String ADD_JOIN_REQUEST = "INSERT INTO communityJoinRequests (accountId, communityId) VALUES (?, ?);";
private static final String REMOVE_JOIN_REQUEST = "DELETE FROM communityJoinRequests WHERE accountId=? AND communityId=?;";
private static final String CREATE_COMMUNITY = "INSERT INTO communities (name) VALUES (?);";
private DataRepository<PlayerStatus> _repo;
public CommunityRepository(JavaPlugin plugin, DataRepository<PlayerStatus> statusRepo)
{
super(plugin, DBPool.getAccount());
_repo = statusRepo;
}
public Community getCommunityById(Integer id)
public void loadCommunity(int communityId, final Map<Integer, Community> communityMap)
{
Community community = null;
executeQuery(GET_COMMUNITY_BY_ID, resultSet ->
{
if (resultSet.next())
{
final int id = resultSet.getInt("id");
final String cName = resultSet.getString("name");
final Community community = new Community(id, cName);
executeQuery(GET_COMMUNITY_MEMBERS, memberSet ->
{
while (memberSet.next())
{
final int accountId = memberSet.getInt("accountId");
final String name = memberSet.getString("name");
final UUID uuid = UUID.fromString(memberSet.getString("uuid"));
final CommunityRole role = CommunityRole.parseRole(memberSet.getString("communityRole"));
final long timeSinceOnline = memberSet.getTimestamp(5).getTime() - memberSet.getTimestamp(4).getTime();
CommunityMemberInfo info = new CommunityMemberInfo(name, uuid, accountId, role, timeSinceOnline);
PlayerStatus status = _repo.getElement(name);
if (status != null)
{
info.update(name, role, timeSinceOnline, true, status.getServer());
}
community.getMembers().put(info.UUID, info);
}
}, new ColumnInt("communityId", community.getId()));
executeQuery(GET_COMMUNITY_JOIN_REQUESTS, requestSet ->
{
while (requestSet.next())
{
final int accountId = requestSet.getInt("accountId");
final UUID uuid = UUID.fromString(requestSet.getString("uuid"));
final String name = requestSet.getString("name");
community.getJoinRequests().put(uuid, new CommunityJoinRequestInfo(name, uuid, accountId));
}
}, new ColumnInt("communityId", community.getId()));
executeQuery(GET_COMMUNITY_SETTINGS, settingSet ->
{
while (settingSet.next())
{
final int settingId = settingSet.getInt("settingId");
final String value = settingSet.getString("settingValue");
CommunitySetting setting = CommunitySetting.getSetting(settingId);
if (setting != null)
{
setting.parseValueInto(value, community);
}
}
}, new ColumnInt("communityId", community.getId()));
communityMap.put(community.getId(), community);
}
}, new ColumnInt("id", id));
return community;
}, new ColumnInt("id", communityId));
}
public Community getCommunityByName(String name)
public void loadCommunities(final Map<Integer, Community> communityMap)
{
Community community = null;
executeQuery(GET_ALL_COMMUNITIES, resultSet ->
{
Map<Integer, Community> resultant = new HashMap<>();
while (resultSet.next())
{
final int id = resultSet.getInt("id");
final String cName = resultSet.getString("name");
final Community community = new Community(id, cName);
executeQuery(GET_COMMUNITY_MEMBERS, memberSet ->
{
while (memberSet.next())
{
final int accountId = memberSet.getInt("accountId");
final String name = memberSet.getString("name");
final UUID uuid = UUID.fromString(memberSet.getString("uuid"));
final CommunityRole role = CommunityRole.parseRole(memberSet.getString("communityRole"));
final long timeSinceOnline = memberSet.getTimestamp(5).getTime() - memberSet.getTimestamp(4).getTime();
executeQuery(GET_COMMUNITY_BY_NAME, resultSet ->
CommunityMemberInfo info = new CommunityMemberInfo(name, uuid, accountId, role, timeSinceOnline);
PlayerStatus status = _repo.getElement(name);
if (status != null)
{
info.update(name, role, timeSinceOnline, true, status.getServer());
}
community.getMembers().put(info.UUID, info);
}
}, new ColumnInt("communityId", community.getId()));
executeQuery(GET_COMMUNITY_JOIN_REQUESTS, requestSet ->
{
while (requestSet.next())
{
final int accountId = requestSet.getInt("accountId");
final UUID uuid = UUID.fromString(requestSet.getString("uuid"));
final String name = requestSet.getString("name");
community.getJoinRequests().put(uuid, new CommunityJoinRequestInfo(name, uuid, accountId));
}
}, new ColumnInt("communityId", community.getId()));
executeQuery(GET_COMMUNITY_SETTINGS, settingSet ->
{
while (settingSet.next())
{
final int settingId = settingSet.getInt("settingId");
final String value = settingSet.getString("settingValue");
CommunitySetting setting = CommunitySetting.getSetting(settingId);
if (setting != null)
{
setting.parseValueInto(value, community);
}
}
}, new ColumnInt("communityId", community.getId()));
resultant.put(community.getId(), community);
}
communityMap.clear();
communityMap.putAll(resultant);
});
}
public void updateMembersAndJoinRequests(LinkedList<Community> communities)
{
if (communities.isEmpty())
{
return;
}
String query = "";
for (Community c : communities)
{
query = query + GET_COMMUNITY_MEMBERS.replace("?", c.getId().toString()) + GET_COMMUNITY_JOIN_REQUESTS.replace("?", c.getId().toString());
}
if (query.isEmpty())
{
return;
}
try (Connection connection = getConnection(); Statement statement = connection.createStatement())
{
statement.executeQuery(query);
for (Community c : communities)
{
ResultSet memberSet = statement.getResultSet();
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();
if (c.getMembers().containsKey(uuid))
{
PlayerStatus status = _repo.getElement(name);
boolean online = false;
String server = "";
if (status != null)
{
online = true;
server = status.getServer();
}
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();
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).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)
{
executeQuery("SELECT communityId FROM communityInvites WHERE accountId=?;", resultSet ->
{
while (resultSet.next())
{
invites.add(resultSet.getInt("communityId"));
}
});
}
public void removeFromCommunity(int accountId, int communityId)
{
executeUpdate(REMOVE_FROM_COMMUNITY, new ColumnInt("accountId", accountId), new ColumnInt("communityId", communityId));
}
public void updateCommunityRole(int accountId, int communityId, CommunityRole role)
{
executeUpdate(UPDATE_COMMUNITY_ROLE, new ColumnVarChar("communityRole", 20, role.toString()), new ColumnInt("accountId", accountId), new ColumnInt("communityId", communityId));
}
public void addToCommunity(int accountId, int communityId)
{
executeUpdate(ADD_TO_COMMUNITY, new ColumnInt("accountId", accountId), new ColumnInt("communityId", communityId), new ColumnVarChar("communityRole", 20, CommunityRole.MEMBER.toString()));
}
public void updateCommunitySetting(CommunitySetting setting, int communityId, String value)
{
executeUpdate(UPDATE_COMMUNITY_SETTING, new ColumnInt("settingId", setting.getId()), new ColumnInt("communityId", communityId), new ColumnVarChar("settingValue", 100, value));
}
public void updateCommunityName(int communityId, String name)
{
executeUpdate(UPDATE_COMMUNITY_NAME, new ColumnVarChar("name", 15, name), new ColumnInt("id", communityId));
}
public boolean inviteToCommunity(int communityId, String name)
{
return executeUpdate(INVITE_TO_COMMUNITY, new ColumnInt("communityId"), new ColumnVarChar("name", 32, name)) > 0;
}
public boolean deleteInviteToCommunity(int communityId, String name)
{
return executeUpdate(DELETE_INVITE_TO_COMMUNITY, new ColumnInt("communityId", communityId), new ColumnVarChar("name", 32, name)) > 0;
}
public void addJoinRequest(int communityId, int accountId)
{
executeUpdate(ADD_JOIN_REQUEST, new ColumnInt("accountId", accountId), new ColumnInt("communityId", communityId));
}
public void removeJoinRequest(int communityId, int accountId)
{
executeUpdate(REMOVE_JOIN_REQUEST, new ColumnInt("accountId", accountId), new ColumnInt("communityId", communityId));
}
public void createCommunity(String name, int leaderAccount, Callback<Integer> idCallback)
{
executeInsert(CREATE_COMMUNITY, resultSet ->
{
if (resultSet.next())
{
int id = resultSet.getInt(1);
executeUpdate(ADD_TO_COMMUNITY, new ColumnInt("accountId", leaderAccount), new ColumnInt("communityId", id), new ColumnVarChar("communityRole", 20, CommunityRole.LEADER.toString()));
idCallback.run(id);
}
}, new ColumnVarChar("name", name.length(), name));
return community;
else
{
idCallback.run(-1);
}
}, new ColumnVarChar("name", 15, name));
}
public void loadCommunities(final List<Community> communityList)
public void deleteCommunity(int communityId)
{
executeQuery(GET_ALL_COMMUNITIES_FOR_BROWSER, resultSet ->
{
List<Community> resultant = Lists.newArrayList();
while (resultSet.next())
{
Community c = new Community(resultSet.getInt("id"), "Name");
resultant.add(c);
}
Random rand = new Random();
List<Community> sortedResultant = Lists.newLinkedList();
while (!resultant.isEmpty())
{
if (rand.nextBoolean())
{
sortedResultant.add(resultant.remove(0));
}
}
communityList.clear();
communityList.addAll(sortedResultant);
});
executeUpdate("DELETE FROM communities WHERE id=?;", new ColumnInt("id", communityId));
executeUpdate("DELETE FROM communitySettings WHERE communityId=?;", new ColumnInt("communityId", communityId));
executeUpdate("DELETE FROM communityMembers WHERE communityId=?;", new ColumnInt("communityId", communityId));
}
protected void initialize() {}