Merge pull request #514 from t00thpick1/patch-2

SQL for mcRank command
This commit is contained in:
nossr50 2013-01-15 16:16:53 -08:00
commit ef69217b81
2 changed files with 110 additions and 52 deletions

View File

@ -1,13 +1,17 @@
package com.gmail.nossr50.commands.mc; package com.gmail.nossr50.commands.mc;
import java.util.Map;
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.util.Leaderboard; import com.gmail.nossr50.util.Leaderboard;
import com.gmail.nossr50.util.Misc; import com.gmail.nossr50.util.Misc;
@ -26,14 +30,9 @@ public class McrankCommand implements CommandExecutor {
arg0.sendMessage(ChatColor.RED+"TARGET: "+ChatColor.WHITE+playerName); arg0.sendMessage(ChatColor.RED+"TARGET: "+ChatColor.WHITE+playerName);
if(Config.getInstance().getUseMySQL()) { if(Config.getInstance().getUseMySQL()) {
//MySQL Version sqlDisplay(arg0, playerName);
} else { } else {
for(SkillType skillType : SkillType.values()) { flatfileDisplay(arg0, playerName);
if(skillType.equals(SkillType.ALL))
continue; //We want the overall ranking to be at the bottom
arg0.sendMessage(ChatColor.YELLOW+Misc.getCapitalized(skillType.name())+ChatColor.GREEN+" - "+ChatColor.GOLD+"Rank "+ChatColor.WHITE+"#"+ChatColor.GREEN+Leaderboard.getPlayerRank(playerName, skillType));
}
arg0.sendMessage(ChatColor.YELLOW+"Overall"+ChatColor.GREEN+" - "+ChatColor.GOLD+"Rank "+ChatColor.WHITE+"#"+ChatColor.GREEN+Leaderboard.getPlayerRank(playerName, SkillType.ALL));
} }
} else { } else {
@ -42,4 +41,25 @@ public class McrankCommand implements CommandExecutor {
return true; return true;
} }
public void flatfileDisplay(CommandSender sender, String 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 + Leaderboard.getPlayerRank(playerName, skillType));
}
sender.sendMessage(ChatColor.YELLOW + "Overall" + ChatColor.GREEN + " - " + ChatColor.GOLD + "Rank " + ChatColor.WHITE + "#" + ChatColor.GREEN + Leaderboard.getPlayerRank(playerName, SkillType.ALL));
}
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"));
}
} }

View File

@ -7,6 +7,7 @@ import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map;
import java.util.Properties; import java.util.Properties;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -16,6 +17,7 @@ import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.config.Config; import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.datatypes.DatabaseUpdate; import com.gmail.nossr50.datatypes.DatabaseUpdate;
import com.gmail.nossr50.datatypes.McMMOPlayer; import com.gmail.nossr50.datatypes.McMMOPlayer;
import com.gmail.nossr50.datatypes.SkillType;
import com.gmail.nossr50.datatypes.SpoutHud; import com.gmail.nossr50.datatypes.SpoutHud;
import com.gmail.nossr50.runnables.SQLReconnect; import com.gmail.nossr50.runnables.SQLReconnect;
import com.gmail.nossr50.spout.SpoutStuff; import com.gmail.nossr50.spout.SpoutStuff;
@ -417,6 +419,42 @@ public class Database {
return rows; return rows;
} }
public Map<String, Integer> readSQLRank(String playerName) {
ResultSet resultSet;
Map<String, Integer> skills = new HashMap<String, Integer>();
if (checkConnected()) {
try {
String sql = "SELECT "
+ "(SELECT (COUNT(*)+1) AS rank FROM " + tablePrefix + "skills AS s, " + tablePrefix + "users AS u WHERE u.id = s.user_id AND s.taming+s.mining+s.woodcutting+s.repair+s.unarmed+s.herbalism+s.excavation+s.archery+s.swords+s.axes+s.acrobatics+s.fishing >= (SELECT s.taming+s.mining+s.woodcutting+s.repair+s.unarmed+s.herbalism+s.excavation+s.archery+s.swords+s.axes+s.acrobatics+s.fishing FROM " + tablePrefix + "skills as s, " + tablePrefix + "users as u WHERE u.id = s.user_id AND u.user = " + playerName + ")) AS 'ALL'"
+ ", (SELECT (COUNT(*)+1) AS rank FROM " + tablePrefix + "skills AS s, " + tablePrefix + "users AS u WHERE u.id = s.user_id AND s.fishing >= (SELECT s.fishing FROM " + tablePrefix + "skills as s, " + tablePrefix + "users as u WHERE u.id = s.user_id AND u.user = " + playerName + ")) AS 'FISHING'"
+ ", (SELECT (COUNT(*)+1) AS rank FROM " + tablePrefix + "skills AS s, " + tablePrefix + "users AS u WHERE u.id = s.user_id AND s.taming >= (SELECT s.taming FROM " + tablePrefix + "skills as s, " + tablePrefix + "users as u WHERE u.id = s.user_id AND u.user = " + playerName + ")) AS 'TAMING'"
+ ", (SELECT (COUNT(*)+1) AS rank FROM " + tablePrefix + "skills AS s, " + tablePrefix + "users AS u WHERE u.id = s.user_id AND s.woodcutting >= (SELECT s.woodcutting FROM " + tablePrefix + "skills as s, " + tablePrefix + "users as u WHERE u.id = s.user_id AND u.user = " + playerName + ")) AS 'WOODCUTTING'"
+ ", (SELECT (COUNT(*)+1) AS rank FROM " + tablePrefix + "skills AS s, " + tablePrefix + "users AS u WHERE u.id = s.user_id AND s.repair >= (SELECT s.repair FROM " + tablePrefix + "skills as s, " + tablePrefix + "users as u WHERE u.id = s.user_id AND u.user = " + playerName + ")) AS 'REPAIR'"
+ ", (SELECT (COUNT(*)+1) AS rank FROM " + tablePrefix + "skills AS s, " + tablePrefix + "users AS u WHERE u.id = s.user_id AND s.unarmed >= (SELECT s.unarmed FROM " + tablePrefix + "skills as s, " + tablePrefix + "users as u WHERE u.id = s.user_id AND u.user = " + playerName + ")) AS 'UNARMED'"
+ ", (SELECT (COUNT(*)+1) AS rank FROM " + tablePrefix + "skills AS s, " + tablePrefix + "users AS u WHERE u.id = s.user_id AND s.herbalism >= (SELECT s.herbalism FROM " + tablePrefix + "skills as s, " + tablePrefix + "users as u WHERE u.id = s.user_id AND u.user = " + playerName + ")) AS 'HERBALISM'"
+ ", (SELECT (COUNT(*)+1) AS rank FROM " + tablePrefix + "skills AS s, " + tablePrefix + "users AS u WHERE u.id = s.user_id AND s.excavation >= (SELECT s.excavation FROM " + tablePrefix + "skills as s, " + tablePrefix + "users as u WHERE u.id = s.user_id AND u.user = " + playerName + ")) AS 'EXCAVATION'"
+ ", (SELECT (COUNT(*)+1) AS rank FROM " + tablePrefix + "skills AS s, " + tablePrefix + "users AS u WHERE u.id = s.user_id AND s.archery >= (SELECT s.archery FROM " + tablePrefix + "skills as s, " + tablePrefix + "users as u WHERE u.id = s.user_id AND u.user = " + playerName + ")) AS 'ARCHERY'"
+ ", (SELECT (COUNT(*)+1) AS rank FROM " + tablePrefix + "skills AS s, " + tablePrefix + "users AS u WHERE u.id = s.user_id AND s.swords >= (SELECT s.swords FROM " + tablePrefix + "skills as s, " + tablePrefix + "users as u WHERE u.id = s.user_id AND u.user = " + playerName + ")) AS 'SWORDS'"
+ ", (SELECT (COUNT(*)+1) AS rank FROM " + tablePrefix + "skills AS s, " + tablePrefix + "users AS u WHERE u.id = s.user_id AND s.axes >= (SELECT s.axes FROM " + tablePrefix + "skills as s, " + tablePrefix + "users as u WHERE u.id = s.user_id AND u.user = " + playerName + ")) AS 'AXES'"
+ ", (SELECT (COUNT(*)+1) AS rank FROM " + tablePrefix + "skills AS s, " + tablePrefix + "users AS u WHERE u.id = s.user_id AND s.acrobatics >= (SELECT s.acrobatics FROM " + tablePrefix + "skills as s, " + tablePrefix + "users as u WHERE u.id = s.user_id AND u.user = " + playerName + ")) AS 'ACROBATICS'"
+ ", (SELECT (COUNT(*)+1) AS rank FROM " + tablePrefix + "skills AS s, " + tablePrefix + "users AS u WHERE u.id = s.user_id AND s.mining >= (SELECT s.mining FROM " + tablePrefix + "skills as s, " + tablePrefix + "users as u WHERE u.id = s.user_id AND u.user = " + playerName + ")) AS 'MINING'";
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();
}
catch (SQLException ex) {
printErrors(ex);
}
}
return skills;
}
public void purgePowerlessSQL() { public void purgePowerlessSQL() {
plugin.getLogger().info("Purging powerless users..."); plugin.getLogger().info("Purging powerless users...");
HashMap<Integer, ArrayList<String>> usernames = read("SELECT u.user FROM " + tablePrefix + "skills AS s, " + tablePrefix + "users AS u WHERE s.user_id = u.id AND (s.taming+s.mining+s.woodcutting+s.repair+s.unarmed+s.herbalism+s.excavation+s.archery+s.swords+s.axes+s.acrobatics+s.fishing) = 0"); HashMap<Integer, ArrayList<String>> usernames = read("SELECT u.user FROM " + tablePrefix + "skills AS s, " + tablePrefix + "users AS u WHERE s.user_id = u.id AND (s.taming+s.mining+s.woodcutting+s.repair+s.unarmed+s.herbalism+s.excavation+s.archery+s.swords+s.axes+s.acrobatics+s.fishing) = 0");