Implement a method for administrators to remove the link between an in-game minecraft account and a forum account, and rewrite synchronization of tags to more effectively handle the intricacies of the API being utilized

This commit is contained in:
AlexTheCoder 2017-02-25 12:52:34 -05:00 committed by cnr
parent 3ebfb453c5
commit f6fdc1f817
2 changed files with 411 additions and 157 deletions

View File

@ -0,0 +1,30 @@
package mineplex.core.website;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import mineplex.core.command.CommandBase;
import mineplex.core.common.Rank;
import mineplex.core.common.util.F;
import mineplex.core.common.util.UtilPlayer;
public class UnlinkCommand extends CommandBase<WebsiteLinkManager>
{
public UnlinkCommand(WebsiteLinkManager plugin)
{
super(plugin, Rank.ADMIN, "unlink");
}
@Override
public void Execute(final Player caller, String[] args)
{
if (args.length < 1)
{
UtilPlayer.message(caller, F.help("/" + _aliasUsed + " <Player>", "Removes a link to a forum and in-game account.", Rank.ADMIN, ChatColor.RED));
}
else
{
Plugin.unlink(caller, args[0]);
}
}
}

View File

@ -55,10 +55,120 @@ public class WebsiteLinkManager extends MiniDbClientPlugin<ForumUserData>
super("Website Link", plugin, clientManager); super("Website Link", plugin, clientManager);
addCommand(new LinkCommand(this)); addCommand(new LinkCommand(this));
addCommand(new UnlinkCommand(this));
Authenticator.setDefault(new MineplexAuthenticator("minexen", "c4cADuj&ChaQ")); Authenticator.setDefault(new MineplexAuthenticator("minexen", "c4cADuj&ChaQ"));
} }
public void unlink(Player sender, String target)
{
getClientManager().getOrLoadClient(target, client ->
{
if (client != null)
{
Callback<ForumUserData> dataCallback = data ->
{
if (data == null)
{
UtilPlayer.message(sender, F.main(getName(), "Could not find " + F.name(target) + "!"));
}
else
{
if (!data.Linked)
{
UtilPlayer.message(sender, F.main(getName(), F.name(target) + " is not linked to a forum account!"));
}
else
{
runAsync(() ->
{
loadXenforoAccount(data.LinkedForumId, user ->
{
List<Integer> remove = new ArrayList<>();
remove.add(17);
remove.add(91);
for (Rank rank : Rank.values())
{
if (rank.getForumId() != -1)
{
remove.add(rank.getForumId());
}
}
String call = "action=editUser&user=" + user.username + "&custom_fields=mcAcctIdPC=";
doAPICall(call, err ->
{
runSync(() -> UtilPlayer.message(sender, F.main(getName(), F.name(target) + " was not able to be unlinked at this time!")));
}, () ->
{
runSync(() -> UtilPlayer.message(sender, F.main(getName(), F.name(target) + " was not able to be unlinked at this time!")));
}, () ->
{
refreshSiteTags(data.LinkedForumId, remove, new ArrayList<>(), false, () ->
{
try (Connection c = DBPool.getAccount().getConnection())
{
c.prepareStatement("DELETE FROM forumLink WHERE accountId=" + client.getAccountId()).execute();
runSync(() ->
{
UtilPlayer.message(sender, F.main(getName(), F.name(target) + " was successfully unlinked!"));
data.Linked = false;
data.LinkedForumId = -1;
data.LastSyncedPowerPlayStatus = false;
});
}
catch (SQLException e)
{
e.printStackTrace();
runSync(() -> UtilPlayer.message(sender, F.main(getName(), F.name(target) + " was not able to be unlinked at this time!")));
}
}, false, () -> UtilPlayer.message(sender, F.main(getName(), F.name(target) + " was not able to be unlinked at this time!")), true);
});
});
});
}
}
};
if (Bukkit.getPlayer(client.getUniqueId()) != null)
{
dataCallback.run(Get(client.getUniqueId()));
}
else
{
runAsync(() ->
{
try (Connection c = DBPool.getAccount().getConnection())
{
ResultSet rs = c.prepareStatement("SELECT userId, powerPlayStatus FROM forumLink WHERE accountId=" + client.getAccountId() + ";").executeQuery();
if (rs.next())
{
Integer userId = rs.getInt(1);
Boolean powerPlay = rs.getBoolean(2);
final ForumUserData data = new ForumUserData();
data.Linked = true;
data.LinkedForumId = userId;
data.LastSyncedPowerPlayStatus = powerPlay;
runSync(() -> dataCallback.run(data));
}
else
{
runSync(() -> dataCallback.run(new ForumUserData()));
}
}
catch (SQLException e)
{
e.printStackTrace();
runSync(() -> dataCallback.run(new ForumUserData()));
}
});
}
}
else
{
UtilPlayer.message(sender, F.main(getName(), "Could not find " + F.name(target) + "!"));
}
});
}
public void startLink(Player player, String code) public void startLink(Player player, String code)
{ {
final int forumId = getForumId(code); final int forumId = getForumId(code);
@ -85,66 +195,248 @@ public class WebsiteLinkManager extends MiniDbClientPlugin<ForumUserData>
UtilPlayer.message(player, F.main(getName(), "That link code is invalid!")); UtilPlayer.message(player, F.main(getName(), "That link code is invalid!"));
return; return;
} }
completeLink(player, data.username, data.user_id, accountId, rank, powerPlay); completeLink(player, data, accountId, rank, powerPlay);
}); });
}); });
} }
private void completeLink(Player player, String forumUser, int userId, int accountId, Rank rank, boolean powerPlay) private void completeLink(Player player, XenForoData data, int accountId, Rank rank, boolean powerPlay)
{ {
try (Connection c = DBPool.getAccount().getConnection()) try (Connection c = DBPool.getAccount().getConnection())
{ {
boolean success = c.prepareStatement("INSERT INTO forumLink (accountId, userId, powerPlayStatus) VALUES (" + accountId + ", " + userId + ", " + powerPlay + ");").executeUpdate() > 0; boolean success = c.prepareStatement("INSERT INTO forumLink (accountId, userId, powerPlayStatus) VALUES (" + accountId + ", " + data.user_id + ", " + powerPlay + ");").executeUpdate() > 0;
if (success) if (success)
{ {
String call = "action=editUser&user=" + forumUser + "&custom_fields=mcAcctIdPC=" + accountId; String call = "action=editUser&user=" + data.username + "&custom_fields=mcAcctIdPC=" + accountId;
List<Integer> adding = new ArrayList<>();
adding.add(91);
if (rank.getForumId() != -1) if (rank.getForumId() != -1)
{ {
call += ("&add_groups=" + rank.getForumId()); adding.add(rank.getForumId());
} }
if (powerPlay) if (powerPlay)
{ {
if (call.contains("&add_groups=")) adding.add(17);
{
call += (",17");
}
else
{
call += ("&add_groups=17");
}
} }
doAPICall(call, err -> doAPICall(call, err ->
{ {
UtilPlayer.message(player, F.main(getName(), "The link failed! Please try again!")); runSync(() -> UtilPlayer.message(player, F.main(getName(), "The link failed! Please try again!")));
}, () -> }, () ->
{ {
UtilPlayer.message(player, F.main(getName(), "The link failed! Please try again!")); runSync(() -> UtilPlayer.message(player, F.main(getName(), "The link failed! Please try again!")));
}, () -> }, () ->
{ {
runSync(() -> refreshSiteTags(data, new ArrayList<>(), adding, false, () ->
{ {
UtilPlayer.message(player, F.main(getName(), "You have successfully linked your account!")); UtilPlayer.message(player, F.main(getName(), "You have successfully linked your account!"));
if (player.isOnline()) if (player.isOnline())
{ {
Get(player).LinkedForumId = userId; Get(player).LinkedForumId = data.user_id;
Get(player).LastSyncedPowerPlayStatus = powerPlay; Get(player).LastSyncedPowerPlayStatus = powerPlay;
Get(player).Linked = true; Get(player).Linked = true;
} }
}); }, true, () -> UtilPlayer.message(player, F.main(getName(), "The link failed! Please try again!")), true);
}); });
} }
else else
{ {
UtilPlayer.message(player, F.main(getName(), "The link failed! Please try again!")); runSync(() -> UtilPlayer.message(player, F.main(getName(), "The link failed! Please try again!")));
} }
} }
catch (SQLException e) catch (SQLException e)
{ {
UtilPlayer.message(player, F.main(getName(), "The link failed! Please try again!")); runSync(() -> UtilPlayer.message(player, F.main(getName(), "The link failed! Please try again!")));
e.printStackTrace(); e.printStackTrace();
} }
} }
private void refreshSiteTags(int userId, List<Integer> removing, List<Integer> adding, boolean runAsync, Runnable after, boolean runAfterSync, Runnable onErr, boolean runErrSync)
{
Runnable r = () ->
{
loadXenforoAccount(userId, data ->
{
refreshSiteTags(data, removing, adding, false, after, runAfterSync, onErr, runErrSync);
});
};
if (runAsync)
{
runAsync(r);
}
else
{
r.run();
}
}
private void refreshSiteTags(XenForoData data, List<Integer> removing, List<Integer> adding, boolean runAsync, Runnable after, boolean runAfterSync, Runnable onErr, boolean runErrSync)
{
Runnable r = () ->
{
if (data == null)
{
return;
}
String callBase = "action=editUser&user=" + data.username;
String groups = "";
for (int groupId : data.secondary_group_ids)
{
if (!removing.contains(groupId) && !adding.contains(groupId))
{
groups += ("," + groupId);
}
}
for (Integer groupId : adding)
{
groups += ("," + groupId);
}
if (!groups.isEmpty())
{
groups = groups.substring(1);
}
final String addGroups = groups;
groups = "";
for (int groupId : data.secondary_group_ids)
{
groups += ("," + groupId);
}
if (!groups.isEmpty())
{
groups = groups.substring(1);
}
final String remGroups = groups;
if (!remGroups.isEmpty())
{
doAPICall(callBase + "&remove_groups=" + remGroups, err ->
{
if (runErrSync)
{
runSync(onErr);
}
else
{
onErr.run();
}
}, () ->
{
if (runErrSync)
{
runSync(onErr);
}
else
{
onErr.run();
}
}, () ->
{
if (!addGroups.isEmpty())
{
doAPICall(callBase + "&add_groups=" + addGroups, err ->
{
if (runErrSync)
{
runSync(onErr);
}
else
{
onErr.run();
}
}, () ->
{
if (runErrSync)
{
runSync(onErr);
}
else
{
onErr.run();
}
}, () ->
{
if (runAfterSync)
{
runSync(after);
}
else
{
after.run();
}
});
}
else
{
if (runAfterSync)
{
runSync(after);
}
else
{
after.run();
}
}
});
}
else
{
if (!addGroups.isEmpty())
{
doAPICall(callBase + "&add_groups=" + addGroups, err ->
{
if (runErrSync)
{
runSync(onErr);
}
else
{
onErr.run();
}
}, () ->
{
if (runErrSync)
{
runSync(onErr);
}
else
{
onErr.run();
}
}, () ->
{
if (runAfterSync)
{
runSync(after);
}
else
{
after.run();
}
});
}
else
{
if (runAfterSync)
{
runSync(after);
}
else
{
after.run();
}
}
}
};
if (runAsync)
{
runAsync(r);
}
else
{
r.run();
}
}
private void loadXenforoAccount(int userId, Callback<XenForoData> callback) private void loadXenforoAccount(int userId, Callback<XenForoData> callback)
{ {
try try
@ -288,7 +580,7 @@ public class WebsiteLinkManager extends MiniDbClientPlugin<ForumUserData>
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC")); Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
int offset = 100000000; int offset = 100000000;
StringBuilder sb = new StringBuilder((cal.get(Calendar.DAY_OF_YEAR) - 1) + ""); StringBuilder sb = new StringBuilder(cal.get(Calendar.DAY_OF_YEAR) + "");
while (sb.length() < 3) while (sb.length() < 3)
{ {
sb.insert(0, "0"); sb.insert(0, "0");
@ -299,7 +591,20 @@ public class WebsiteLinkManager extends MiniDbClientPlugin<ForumUserData>
Integer input = Integer.parseInt(given); Integer input = Integer.parseInt(given);
return Math.abs(test - input); Integer abs = Math.abs(test - input);
// Bukkit.broadcastMessage("REVERSE: " + reverse);
// Bukkit.broadcastMessage("TEST: " + test);
// Bukkit.broadcastMessage("INPUT: " + input);
// Bukkit.broadcastMessage("ABS: " + abs);
// Bukkit.broadcastMessage("MODULUS: " + abs % 11);
if (abs % 11 != 0)
{
return -1;
}
return abs / 11;
} }
private Pair<Boolean, UUID> checkAccountOnline(int accountId) private Pair<Boolean, UUID> checkAccountOnline(int accountId)
@ -318,57 +623,22 @@ public class WebsiteLinkManager extends MiniDbClientPlugin<ForumUserData>
@EventHandler @EventHandler
public void handleRankSave(RankSaveEvent event) public void handleRankSave(RankSaveEvent event)
{ {
Callback<XenForoData> dataCallback = new Callback<XenForoData>() Callback<Integer> dataCallback = id ->
{ {
public void run(XenForoData data) List<Integer> remove = new ArrayList<>();
List<Integer> add = new ArrayList<>();
for (Rank rank : Rank.values())
{ {
List<Integer> remove = new ArrayList<>(); if (rank.getForumId() != -1 && rank != event.getRank())
for (Rank rank : Rank.values())
{ {
if (rank.getForumId() != -1 && rank != event.getRank()) remove.add(rank.getForumId());
{
boolean found = false;
for (int groupId : data.secondary_group_ids)
{
if (groupId == rank.getForumId())
{
found = true;
}
}
if (found)
{
remove.add(rank.getForumId());
}
}
}
String callBase = "action=editUser&user=" + data.username;
if (!remove.isEmpty())
{
String removal = "&remove_groups=" + remove.get(0).intValue();
for (int i = 1; i < remove.size(); i++)
{
removal += ("," + remove.get(i));
}
String call = callBase + removal;
doAPICall(call, error -> {}, () -> {}, () -> {});
}
if (event.getRank().getForumId() != -1)
{
boolean found = false;
for (int groupId : data.secondary_group_ids)
{
if (groupId == event.getRank().getForumId())
{
found = true;
}
}
if (!found)
{
String call = callBase + "&add_groups=" + event.getRank().getForumId();
doAPICall(call, error -> {}, () -> {}, () -> {});
}
} }
} }
if (event.getRank().getForumId() != -1)
{
add.add(event.getRank().getForumId());
}
refreshSiteTags(id, remove, add, false, () -> {}, false, () -> {}, false);
}; };
if (Bukkit.getPlayer(event.getUUID()) != null) if (Bukkit.getPlayer(event.getUUID()) != null)
{ {
@ -378,7 +648,7 @@ public class WebsiteLinkManager extends MiniDbClientPlugin<ForumUserData>
final int userId = fd.LinkedForumId; final int userId = fd.LinkedForumId;
runAsync(() -> runAsync(() ->
{ {
loadXenforoAccount(userId, dataCallback); dataCallback.run(userId);
}); });
} }
} }
@ -396,7 +666,7 @@ public class WebsiteLinkManager extends MiniDbClientPlugin<ForumUserData>
if (rs.next()) if (rs.next())
{ {
Integer userId = rs.getInt(1); Integer userId = rs.getInt(1);
loadXenforoAccount(userId, dataCallback); dataCallback.run(userId);
} }
} }
catch (SQLException e) catch (SQLException e)
@ -421,35 +691,23 @@ public class WebsiteLinkManager extends MiniDbClientPlugin<ForumUserData>
final int userId = fd.LinkedForumId; final int userId = fd.LinkedForumId;
runAsync(() -> runAsync(() ->
{ {
loadXenforoAccount(userId, data -> List<Integer> add = new ArrayList<>();
add.add(17);
refreshSiteTags(userId, new ArrayList<>(), add, false, () ->
{ {
boolean has = false; runSync(() ->
for (int groupId : data.secondary_group_ids)
{ {
if (groupId == 17) fd.LastSyncedPowerPlayStatus = true;
{ });
has = true; try (Connection c = DBPool.getAccount().getConnection())
}
}
if (!has)
{ {
doAPICall("action=editUser&user=" + data.username + "&add_groups=17", error -> {}, () -> {}, () -> c.prepareStatement("UPDATE forumLink SET powerPlayStatus=true WHERE accountId=" + event.getAccountId() + ";").execute();
{
runSync(() ->
{
fd.LastSyncedPowerPlayStatus = true;
});
try (Connection c = DBPool.getAccount().getConnection())
{
c.prepareStatement("UPDATE forumLink SET powerPlayStatus=true WHERE accountId=" + event.getAccountId() + ";").execute();
}
catch (SQLException e)
{
e.printStackTrace();
}
});
} }
}); catch (SQLException e)
{
e.printStackTrace();
}
}, false, () -> {}, false);
}); });
} }
} }
@ -463,31 +721,19 @@ public class WebsiteLinkManager extends MiniDbClientPlugin<ForumUserData>
if (rs.next()) if (rs.next())
{ {
Integer userId = rs.getInt(1); Integer userId = rs.getInt(1);
loadXenforoAccount(userId, data -> List<Integer> add = new ArrayList<>();
add.add(17);
refreshSiteTags(userId, new ArrayList<>(), add, false, () ->
{ {
boolean has = false; try
for (int groupId : data.secondary_group_ids)
{ {
if (groupId == 17) c.prepareStatement("UPDATE forumLink SET powerPlayStatus=true WHERE accountId=" + event.getAccountId() + ";").execute();
{
has = true;
}
} }
if (!has) catch (SQLException e)
{ {
doAPICall("action=editUser&user=" + data.username + "&add_groups=17", error -> {}, () -> {}, () -> e.printStackTrace();
{
try
{
c.prepareStatement("UPDATE forumLink SET powerPlayStatus=true WHERE accountId=" + event.getAccountId() + ";").execute();
}
catch (SQLException e)
{
e.printStackTrace();
}
});
} }
}); }, false, () -> {}, false);
} }
} }
catch (SQLException e) catch (SQLException e)
@ -517,56 +763,34 @@ public class WebsiteLinkManager extends MiniDbClientPlugin<ForumUserData>
final int accountId = getClientManager().getAccountId(event.getPlayer()); final int accountId = getClientManager().getAccountId(event.getPlayer());
runAsync(() -> runAsync(() ->
{ {
loadXenforoAccount(userId, data -> List<Integer> remove = new ArrayList<>();
List<Integer> add = new ArrayList<>();
if (powerPlay)
{ {
String call = ""; add.add(17);
boolean change = false; }
boolean has = false; else
for (int groupId : data.secondary_group_ids) {
remove.add(17);
}
refreshSiteTags(userId, remove, add, false, () ->
{
runSync(() ->
{ {
if (groupId == 17) if (event.getPlayer().isOnline())
{ {
has = true; Get(event.getPlayer()).LastSyncedPowerPlayStatus = powerPlay;
} }
} });
if (powerPlay) try (Connection c = DBPool.getAccount().getConnection())
{ {
if (!has) c.prepareStatement("UPDATE forumLink SET powerPlayStatus=" + powerPlay + " WHERE accountId=" + accountId + ";").execute();
{
call = "action=editUser&user=" + data.username + "&add_groups=17";
change = true;
}
} }
else catch (SQLException e)
{ {
if (has) e.printStackTrace();
{
call = "action=editUser&user=" + data.username + "&remove_groups=17";
change = true;
}
} }
if (change) }, false, () -> {}, false);
{
doAPICall(call, error -> {}, () -> {}, () ->
{
runSync(() ->
{
if (event.getPlayer().isOnline())
{
Get(event.getPlayer()).LastSyncedPowerPlayStatus = powerPlay;
}
});
try (Connection c = DBPool.getAccount().getConnection())
{
c.prepareStatement("UPDATE forumLink SET powerPlayStatus=" + powerPlay + " WHERE accountId=" + accountId + ";").execute();
}
catch (SQLException e)
{
e.printStackTrace();
}
});
}
});
}); });
} }
} }