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;
import java.util.Map;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.config.Config;
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.Misc;
public class McrankCommand implements CommandExecutor {
@Override
public boolean onCommand(CommandSender arg0, Command arg1, String arg2, String[] arg3) {
//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
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if(!Config.getInstance().getUseMySQL())
Leaderboard.updateLeaderboards(); //Make sure the information is up to date
if(arg0 instanceof Player) {
Player player = (Player) arg0;
String playerName = player.getName();
arg0.sendMessage(ChatColor.GOLD + "-=PERSONAL RANKINGS=-");
arg0.sendMessage(ChatColor.RED+"TARGET: "+ChatColor.WHITE+playerName);
if(sender instanceof Player) {
Player player = (Player) sender;
String playerName;
switch(args.length){
case 0:
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()) {
sqlDisplay(arg0, playerName);
sqlDisplay(sender, playerName);
} else {
flatfileDisplay(arg0, playerName);
flatfileDisplay(sender, playerName);
}
} else {
arg0.sendMessage("Command currently not supported for console.");
sender.sendMessage("Command currently not supported for console.");
}
return true;
@ -54,13 +60,6 @@ public class McrankCommand implements CommandExecutor {
private void sqlDisplay(CommandSender sender, String playerName) {
Database database = mcMMO.getPlayerDatabase();
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"));
Bukkit.getScheduler().runTaskAsynchronously(Bukkit.getPluginManager().getPlugin("mcMMO"), new mcRankAsync(playerName, sender));
}
}

View File

@ -153,7 +153,7 @@ public class MctopCommand implements CommandExecutor {
String tablePrefix = Config.getInstance().getMySQLTablePrefix();
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")) {
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>();
if (checkConnected()) {
try {
String sql = "SELECT "
+ "(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'"
+ ", (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'"
+ ", (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'"
+ ", (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'"
+ ", (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'"
+ ", (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'";
for (SkillType skillType: SkillType.values()) {
String sql;
if(skillType != SkillType.ALL) {
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 + "'";
} else {
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 + "'";
}
PreparedStatement statement = connection.prepareStatement(sql);
resultSet = statement.executeQuery();
while (resultSet.next()) {
for (SkillType skillType: SkillType.values()) {
skills.put(skillType.name(), resultSet.getInt(skillType.name()));
}
statement.close();
}
statement.close();
}
catch (SQLException ex) {
printErrors(ex);