Make mcRank Async in SQL

This commit is contained in:
T00thpick1 2013-01-16 02:50:18 -05:00
parent 7fa1a8c6c7
commit c073c7ed3a
4 changed files with 79 additions and 43 deletions

View File

@ -1,43 +1,49 @@
package com.gmail.nossr50.commands.mc; package com.gmail.nossr50.commands.mc;
import java.util.Map; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.config.Config; import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.datatypes.SkillType; import com.gmail.nossr50.datatypes.SkillType;
import com.gmail.nossr50.util.Database; import com.gmail.nossr50.runnables.mcRankAsync;
import com.gmail.nossr50.util.Leaderboard; import com.gmail.nossr50.util.Leaderboard;
import com.gmail.nossr50.util.Misc; import com.gmail.nossr50.util.Misc;
public class McrankCommand implements CommandExecutor { public class McrankCommand implements CommandExecutor {
@Override @Override
public boolean onCommand(CommandSender arg0, Command arg1, String arg2, String[] arg3) { public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
//I'm being lazy and making this only work on yourself, I or someone else will make this work on other players in the future :D
if(!Config.getInstance().getUseMySQL()) if(!Config.getInstance().getUseMySQL())
Leaderboard.updateLeaderboards(); //Make sure the information is up to date Leaderboard.updateLeaderboards(); //Make sure the information is up to date
if(arg0 instanceof Player) { if(sender instanceof Player) {
Player player = (Player) arg0; Player player = (Player) sender;
String playerName = player.getName(); String playerName;
switch(args.length){
arg0.sendMessage(ChatColor.GOLD + "-=PERSONAL RANKINGS=-"); case 0:
arg0.sendMessage(ChatColor.RED+"TARGET: "+ChatColor.WHITE+playerName); playerName = player.getName();
break;
case 1:
playerName = args[0];
break;
default:
return false;
}
sender.sendMessage(ChatColor.GOLD + "-=PERSONAL RANKINGS=-");
sender.sendMessage(ChatColor.RED+"TARGET: "+ChatColor.WHITE+playerName);
if(Config.getInstance().getUseMySQL()) { if(Config.getInstance().getUseMySQL()) {
sqlDisplay(arg0, playerName); sqlDisplay(sender, playerName);
} else { } else {
flatfileDisplay(arg0, playerName); flatfileDisplay(sender, playerName);
} }
} else { } else {
arg0.sendMessage("Command currently not supported for console."); sender.sendMessage("Command currently not supported for console.");
} }
return true; return true;
@ -54,13 +60,6 @@ public class McrankCommand implements CommandExecutor {
private void sqlDisplay(CommandSender sender, String playerName) { private void sqlDisplay(CommandSender sender, String playerName) {
Database database = mcMMO.getPlayerDatabase(); Bukkit.getScheduler().runTaskAsynchronously(Bukkit.getPluginManager().getPlugin("mcMMO"), new mcRankAsync(playerName, sender));
Map<String, Integer> skills = database.readSQLRank(playerName);
for (SkillType skillType : SkillType.values()) {
if (skillType.equals(SkillType.ALL))
continue; // We want the overall ranking to be at the bottom
sender.sendMessage(ChatColor.YELLOW + Misc.getCapitalized(skillType.name()) + ChatColor.GREEN + " - " + ChatColor.GOLD + "Rank " + ChatColor.WHITE + "#" + ChatColor.GREEN + skills.get(skillType.name()));
}
sender.sendMessage(ChatColor.YELLOW + "Overall" + ChatColor.GREEN + " - " + ChatColor.GOLD + "Rank " + ChatColor.WHITE + "#" + ChatColor.GREEN + skills.get("ALL"));
} }
} }

View File

@ -153,7 +153,7 @@ public class MctopCommand implements CommandExecutor {
String tablePrefix = Config.getInstance().getMySQLTablePrefix(); String tablePrefix = Config.getInstance().getMySQLTablePrefix();
Database database = mcMMO.getPlayerDatabase(); Database database = mcMMO.getPlayerDatabase();
HashMap<Integer, ArrayList<String>> userslist = database.read("SELECT s." + query + ", u.user FROM " + tablePrefix + "skills AS s, " + tablePrefix + "users AS u WHERE (s.user_id = u.id) AND s." + query + " > 0 ORDER BY s." + query + " DESC LIMIT "+((page * 10) - 10)+",10"); HashMap<Integer, ArrayList<String>> userslist = database.read("SELECT s." + query + ", u.user, NOW() FROM " + tablePrefix + "skills AS s, " + tablePrefix + "users AS u WHERE (s.user_id = u.id) AND s." + query + " > 0 ORDER BY s." + query + " DESC LIMIT "+((page * 10) - 10)+",10");
if (query.equals("taming+s.mining+s.woodcutting+s.repair+s.unarmed+s.herbalism+s.excavation+s.archery+s.swords+s.axes+s.acrobatics+s.fishing")) { if (query.equals("taming+s.mining+s.woodcutting+s.repair+s.unarmed+s.herbalism+s.excavation+s.archery+s.swords+s.axes+s.acrobatics+s.fishing")) {
sender.sendMessage(LocaleLoader.getString("Commands.PowerLevel.Leaderboard")); sender.sendMessage(LocaleLoader.getString("Commands.PowerLevel.Leaderboard"));

View File

@ -0,0 +1,43 @@
package com.gmail.nossr50.runnables;
import java.util.Map;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.datatypes.SkillType;
import com.gmail.nossr50.util.Database;
import com.gmail.nossr50.util.Misc;
public class mcRankAsync implements Runnable {
private Database database = mcMMO.getPlayerDatabase();
private final String playerName;
private final CommandSender sender;
public mcRankAsync(String playerName, CommandSender sender) {
this.playerName = playerName;
this.sender = sender;
}
@Override
public void run() {
final Map<String, Integer> skills = database.readSQLRank(playerName);
Bukkit.getScheduler().scheduleSyncDelayedTask(Bukkit.getPluginManager().getPlugin("mcMMO"), new Runnable() {
@Override
public void run() {
for (SkillType skillType : SkillType.values()) {
if (skillType.equals(SkillType.ALL))
continue; // We want the overall ranking to be at the bottom
sender.sendMessage(ChatColor.YELLOW + Misc.getCapitalized(skillType.name()) + ChatColor.GREEN + " - " + ChatColor.GOLD + "Rank " + ChatColor.WHITE + "#" + ChatColor.GREEN + skills.get(skillType.name()));
}
sender.sendMessage(ChatColor.YELLOW + "Overall" + ChatColor.GREEN + " - " + ChatColor.GOLD + "Rank " + ChatColor.WHITE + "#" + ChatColor.GREEN + skills.get("ALL"));
}
}, 1L);
}
}

View File

@ -458,29 +458,23 @@ public class Database {
Map<String, Integer> skills = new HashMap<String, Integer>(); Map<String, Integer> skills = new HashMap<String, Integer>();
if (checkConnected()) { if (checkConnected()) {
try { try {
String sql = "SELECT " for (SkillType skillType: SkillType.values()) {
+ "(SELECT rank FROM (SELECT @rownum:=@rownum+1 rank, p.user AS user FROM (SELECT @rownum:=0) AS rank, ((SELECT u.user AS user FROM " + tablePrefix + "users u, " + tablePrefix + "skills s WHERE u.id = s.user_id ORDER BY s.taming+s.mining+s.woodcutting+s.repair+s.unarmed+s.herbalism+s.excavation+s.archery+s.swords+s.axes+s.acrobatics+s.fishing desc) AS p)) AS d WHERE user = '" + playerName + "') AS 'ALL'" String sql;
+ ", (SELECT rank FROM (SELECT @rownum:=@rownum+1 rank, p.user AS user FROM (SELECT @rownum:=0) AS rank, ((SELECT u.user AS user FROM " + tablePrefix + "users u, " + tablePrefix + "skills s WHERE u.id = s.user_id ORDER BY s.fishing desc) AS p)) AS d WHERE user = '" + playerName + "') AS 'FISHING'" if(skillType != SkillType.ALL) {
+ ", (SELECT rank FROM (SELECT @rownum:=@rownum+1 rank, p.user AS user FROM (SELECT @rownum:=0) AS rank, ((SELECT u.user AS user FROM " + tablePrefix + "users u, " + tablePrefix + "skills s WHERE u.id = s.user_id ORDER BY s.taming desc) AS p)) AS d WHERE user = '" + playerName + "') AS 'TAMING'" sql = "SELECT rank AS '" + skillType.name() + "' FROM (SELECT @rownum:=@rownum+1 rank, user, NOW() FROM (SELECT @rownum:=0) AS r, ((SELECT user FROM " + tablePrefix + "users, " + tablePrefix + "skills WHERE id = user_id ORDER BY " + skillType.name().toLowerCase() + " desc) AS p)) AS d WHERE user = '" + playerName + "'";
+ ", (SELECT rank FROM (SELECT @rownum:=@rownum+1 rank, p.user AS user FROM (SELECT @rownum:=0) AS rank, ((SELECT u.user AS user FROM " + tablePrefix + "users u, " + tablePrefix + "skills s WHERE u.id = s.user_id ORDER BY s.woodcutting desc) AS p)) AS d WHERE user = '" + playerName + "') AS 'WOODCUTTING'" } else {
+ ", (SELECT rank FROM (SELECT @rownum:=@rownum+1 rank, p.user AS user FROM (SELECT @rownum:=0) AS rank, ((SELECT u.user AS user FROM " + tablePrefix + "users u, " + tablePrefix + "skills s WHERE u.id = s.user_id ORDER BY s.repair desc) AS p)) AS d WHERE user = '" + playerName + "') AS 'REPAIR'" sql = "SELECT rank AS 'ALL' FROM (SELECT @rownum:=@rownum+1 rank, user, NOW() FROM (SELECT @rownum:=0) AS r, ((SELECT user FROM " + tablePrefix + "users, " + tablePrefix + "skills WHERE id = user_id ORDER BY taming+mining+woodcutting+repair+unarmed+herbalism+excavation+archery+swords+axes+acrobatics+fishing desc) AS p)) AS d WHERE user = '" + playerName + "'";
+ ", (SELECT rank FROM (SELECT @rownum:=@rownum+1 rank, p.user AS user FROM (SELECT @rownum:=0) AS rank, ((SELECT u.user AS user FROM " + tablePrefix + "users u, " + tablePrefix + "skills s WHERE u.id = s.user_id ORDER BY s.unarmed desc) AS p)) AS d WHERE user = '" + playerName + "') AS 'UNARMED'" }
+ ", (SELECT rank FROM (SELECT @rownum:=@rownum+1 rank, p.user AS user FROM (SELECT @rownum:=0) AS rank, ((SELECT u.user AS user FROM " + tablePrefix + "users u, " + tablePrefix + "skills s WHERE u.id = s.user_id ORDER BY s.herbalism desc) AS p)) AS d WHERE user = '" + playerName + "') AS 'HERBALISM'"
+ ", (SELECT rank FROM (SELECT @rownum:=@rownum+1 rank, p.user AS user FROM (SELECT @rownum:=0) AS rank, ((SELECT u.user AS user FROM " + tablePrefix + "users u, " + tablePrefix + "skills s WHERE u.id = s.user_id ORDER BY s.excavation desc) AS p)) AS d WHERE user = '" + playerName + "') AS 'EXCAVATION'"
+ ", (SELECT rank FROM (SELECT @rownum:=@rownum+1 rank, p.user AS user FROM (SELECT @rownum:=0) AS rank, ((SELECT u.user AS user FROM " + tablePrefix + "users u, " + tablePrefix + "skills s WHERE u.id = s.user_id ORDER BY s.archery desc) AS p)) AS d WHERE user = '" + playerName + "') AS 'ARCHERY'"
+ ", (SELECT rank FROM (SELECT @rownum:=@rownum+1 rank, p.user AS user FROM (SELECT @rownum:=0) AS rank, ((SELECT u.user AS user FROM " + tablePrefix + "users u, " + tablePrefix + "skills s WHERE u.id = s.user_id ORDER BY s.swords desc) AS p)) AS d WHERE user = '" + playerName + "') AS 'SWORDS'"
+ ", (SELECT rank FROM (SELECT @rownum:=@rownum+1 rank, p.user AS user FROM (SELECT @rownum:=0) AS rank, ((SELECT u.user AS user FROM " + tablePrefix + "users u, " + tablePrefix + "skills s WHERE u.id = s.user_id ORDER BY s.axes desc) AS p)) AS d WHERE user = '" + playerName + "') AS 'AXES'"
+ ", (SELECT rank FROM (SELECT @rownum:=@rownum+1 rank, p.user AS user FROM (SELECT @rownum:=0) AS rank, ((SELECT u.user AS user FROM " + tablePrefix + "users u, " + tablePrefix + "skills s WHERE u.id = s.user_id ORDER BY s.acrobatics desc) AS p)) AS d WHERE user = '" + playerName + "') AS 'ACROBATICS'"
+ ", (SELECT rank FROM (SELECT @rownum:=@rownum+1 rank, p.user AS user FROM (SELECT @rownum:=0) AS rank, ((SELECT u.user AS user FROM " + tablePrefix + "users u, " + tablePrefix + "skills s WHERE u.id = s.user_id ORDER BY s.mining desc) AS p)) AS d WHERE user = '" + playerName + "') AS 'MINING'";
PreparedStatement statement = connection.prepareStatement(sql); PreparedStatement statement = connection.prepareStatement(sql);
resultSet = statement.executeQuery(); resultSet = statement.executeQuery();
while (resultSet.next()) { while (resultSet.next()) {
for (SkillType skillType: SkillType.values()) {
skills.put(skillType.name(), resultSet.getInt(skillType.name())); skills.put(skillType.name(), resultSet.getInt(skillType.name()));
} }
statement.close();
} }
statement.close();
} }
catch (SQLException ex) { catch (SQLException ex) {
printErrors(ex); printErrors(ex);