AWork on FRiends.

This commit is contained in:
Jonathan Williams 2014-09-26 12:38:51 -07:00
parent 34baa8eafb
commit eaee6c2733
2 changed files with 30 additions and 10 deletions

View File

@ -105,7 +105,7 @@ public class FriendManager extends MiniClientPlugin<FriendData>
{ {
public void run() public void run()
{ {
_repository.addFriend(caller.getUniqueId().toString(), name); _repository.addFriend(caller, name);
Bukkit.getServer().getScheduler().runTask(_plugin, new Runnable() Bukkit.getServer().getScheduler().runTask(_plugin, new Runnable()
{ {

View File

@ -14,10 +14,12 @@ import mineplex.core.database.column.ColumnVarChar;
public class FriendRepository extends RepositoryBase public class FriendRepository extends RepositoryBase
{ {
private static String CREATE_FRIEND_TABLE = "CREATE TABLE IF NOT EXISTS accountFriend (id INT NOT NULL AUTO_INCREMENT, uuidSource VARCHAR(100), uuidTarget VARCHAR(100), mutual BOOL, PRIMARY KEY (id), UNIQUE INDEX uuidIndex (uuidSource, uuidTarget));"; private static String CREATE_FRIEND_TABLE = "CREATE TABLE IF NOT EXISTS accountFriend (id INT NOT NULL AUTO_INCREMENT, uuidSource VARCHAR(100), uuidTarget VARCHAR(100), status VARCHAR(100), PRIMARY KEY (id), UNIQUE INDEX uuidIndex (uuidSource, uuidTarget));";
private static String RETRIEVE_MULTIPLE_FRIEND_RECORDS = "SELECT uuidSource, tA.Name, mutual, serverName, tA.lastLogin FROM accountFriend INNER Join accounts AS fA ON fA.uuid = uuidSource INNER JOIN accounts AS tA ON tA.uuid = uuidTarget LEFT JOIN playerMap ON tA.name = playerName WHERE uuidSource IN "; private static String RETRIEVE_MULTIPLE_FRIEND_RECORDS = "SELECT uuidSource, tA.Name, status, serverName, tA.lastLogin FROM accountFriend INNER Join accounts AS fA ON fA.uuid = uuidSource INNER JOIN accounts AS tA ON tA.uuid = uuidTarget LEFT JOIN playerMap ON tA.name = playerName WHERE uuidSource IN ";
private static String RETRIEVE_FRIEND_RECORDS = "SELECT tA.Name, mutual, serverName, tA.lastLogin FROM accountFriend INNER Join accounts AS fA ON fA.uuid = uuidSource INNER JOIN accounts AS tA ON tA.uuid = uuidTarget LEFT JOIN playerMap ON tA.name = playerName WHERE uuidSource = ?;"; private static String RETRIEVE_FRIEND_RECORDS = "SELECT tA.Name, mutual, serverName, tA.lastLogin FROM accountFriend INNER Join accounts AS fA ON fA.uuid = uuidSource INNER JOIN accounts AS tA ON tA.uuid = uuidTarget LEFT JOIN playerMap ON tA.name = playerName WHERE uuidSource = ?;";
private static String ADD_FRIEND_RECORD = "INSERT INTO accountFriend (uuidSource, uuidTarget) SELECT fA.uuid AS uuidSource, tA.uuid AS uuidTarget FROM accounts as fA LEFT JOIN accounts AS tA ON tA.name = ? WHERE fA.uuid = ?;"; private static String RETRIEVE_OTHER_FRIEND_RECORDS_BY_NAME = "SELECT tA.Name, mutual, serverName, tA.lastLogin FROM accountFriend INNER Join accounts AS fA ON fA.uuid = uuidSource INNER JOIN accounts AS tA ON tA.uuid = uuidTarget LEFT JOIN playerMap ON tA.name = playerName WHERE name = ?;";
private static String ADD_FRIEND_RECORD = "INSERT INTO accountFriend (uuidSource, uuidTarget, status) SELECT fA.uuid AS uuidSource, tA.uuid AS uuidTarget, 'Pending' FROM accounts as fA LEFT JOIN accounts AS tA ON tA.name = ? WHERE fA.name = ?;";
private static String UPDATE_MUTUAL_RECORD = "UPDATE aF SET status = ? FROM accountFriend AS aF INNER JOIN accounts as fA LEFT JOIN accounts AS tA ON tA.name = ? WHERE fA.name = ?;";
private static String DELETE_FRIEND_RECORD = "DELETE aF FROM accountFriend AS aF INNER JOIN accounts ON accounts.name = ? WHERE uuidSource = ? AND uuidTarget = accounts.uuid;"; private static String DELETE_FRIEND_RECORD = "DELETE aF FROM accountFriend AS aF INNER JOIN accounts ON accounts.name = ? WHERE uuidSource = ? AND uuidTarget = accounts.uuid;";
@ -40,14 +42,34 @@ public class FriendRepository extends RepositoryBase
{ {
} }
public boolean addFriend(String uuid, String name) public boolean addFriend(final Player caller, String name)
{ {
return executeUpdate(ADD_FRIEND_RECORD, new ColumnVarChar("name", 40, name), new ColumnVarChar("uuid", 100, uuid)) > 0; int rowsAffected = executeUpdate(ADD_FRIEND_RECORD, new ColumnVarChar("name", 100, name), new ColumnVarChar("name", 100, caller.getName()));
if (rowsAffected > 0)
return executeUpdate(ADD_FRIEND_RECORD, new ColumnVarChar("name", 100, caller.getName()), new ColumnVarChar("uuid", 100, name)) > 0;
return false;
} }
public boolean removeFriend(String uuid, String name) public boolean updateFriend(Player caller, String name, String status)
{ {
return executeUpdate(DELETE_FRIEND_RECORD, new ColumnVarChar("name", 40, name), new ColumnVarChar("uuid", 100, uuid)) > 0; int rowsAffected = executeUpdate(UPDATE_MUTUAL_RECORD, new ColumnVarChar("status", 100, status), new ColumnVarChar("name", 100, name), new ColumnVarChar("name", 100, caller.getName()));
if (rowsAffected > 0)
return executeUpdate(UPDATE_MUTUAL_RECORD, new ColumnVarChar("status", 100, status), new ColumnVarChar("name", 100, caller.getName()), new ColumnVarChar("uuid", 100, name)) > 0;
return false;
}
public boolean removeFriend(Player caller, String name)
{
int rowsAffected = executeUpdate(DELETE_FRIEND_RECORD, new ColumnVarChar("name", 100, name), new ColumnVarChar("name", 100, caller.getName()));
if (rowsAffected > 0)
return executeUpdate(DELETE_FRIEND_RECORD, new ColumnVarChar("name", 100, caller.getName()), new ColumnVarChar("uuid", 100, name)) > 0;
return false;
} }
public NautHashMap<String, FriendData> getFriendsForAll(Player...players) public NautHashMap<String, FriendData> getFriendsForAll(Player...players)
@ -109,8 +131,6 @@ public class FriendRepository extends RepositoryBase
friend.ServerName = resultSet.getString(3); friend.ServerName = resultSet.getString(3);
friend.LastSeenOnline = resultSet.getLong(4); friend.LastSeenOnline = resultSet.getLong(4);
System.out.println("Adding friend " + friend.Name + " for UUID " + uniqueId.toString());
friendData.Friends.add(friend); friendData.Friends.add(friend);
} }
} }