From 5512bd7df60122fc158a8d4af0dd398bbf31cb6b Mon Sep 17 00:00:00 2001 From: t00thpick1 Date: Tue, 15 Jan 2013 18:35:21 -0500 Subject: [PATCH 1/5] Part one of sql for mcRank --- .../nossr50/commands/mc/McrankCommand.java | 81 ++++++++++++------- 1 file changed, 51 insertions(+), 30 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/commands/mc/McrankCommand.java b/src/main/java/com/gmail/nossr50/commands/mc/McrankCommand.java index 85ef97749..67f3b3533 100644 --- a/src/main/java/com/gmail/nossr50/commands/mc/McrankCommand.java +++ b/src/main/java/com/gmail/nossr50/commands/mc/McrankCommand.java @@ -1,45 +1,66 @@ package com.gmail.nossr50.commands.mc; +import java.util.Map; + 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.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 - 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(Config.getInstance().getUseMySQL()) { - //MySQL Version - } else { - for(SkillType skillType : SkillType.values()) { - 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 { - arg0.sendMessage("Command currently not supported for console."); - } - - return true; - } -} \ No newline at end of file + @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 + 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(Config.getInstance().getUseMySQL()) { + sqlDisplay(arg0, playerName); + } else { + flatfileDisplayer(arg0, playerName); + } + + } else { + arg0.sendMessage("Command currently not supported for console."); + } + + return true; + } + + public void flatfileDisplayer(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) { + String tablePrefix = Config.getInstance().getMySQLTablePrefix(); + Database database = mcMMO.getPlayerDatabase(); + Map 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")); + } +} From 137c46b8ce4f86ae6cb91e7dc86d0ad23fa19ca4 Mon Sep 17 00:00:00 2001 From: t00thpick1 Date: Tue, 15 Jan 2013 19:03:13 -0500 Subject: [PATCH 2/5] Part two of sql mcRank --- .../java/com/gmail/nossr50/util/Database.java | 82 ++++++++++++++----- 1 file changed, 60 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/util/Database.java b/src/main/java/com/gmail/nossr50/util/Database.java index dad99e4ae..5648d2ec6 100644 --- a/src/main/java/com/gmail/nossr50/util/Database.java +++ b/src/main/java/com/gmail/nossr50/util/Database.java @@ -7,6 +7,7 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.HashMap; +import java.util.Map; import java.util.Properties; import org.bukkit.Bukkit; @@ -16,6 +17,7 @@ import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.config.Config; import com.gmail.nossr50.datatypes.DatabaseUpdate; import com.gmail.nossr50.datatypes.McMMOPlayer; +import com.gmail.nossr50.datatypes.SkillType; import com.gmail.nossr50.datatypes.SpoutHud; import com.gmail.nossr50.runnables.SQLReconnect; import com.gmail.nossr50.spout.SpoutStuff; @@ -56,27 +58,27 @@ public class Database { * Attempt to connect to the mySQL database. */ public static void connect() { - connectionString = "jdbc:mysql://" + configInstance.getMySQLServerName() + ":" + configInstance.getMySQLServerPort() + "/" + configInstance.getMySQLDatabaseName(); - try { - mcMMO.p.getLogger().info("Attempting connection to MySQL..."); + connectionString = "jdbc:mysql://" + configInstance.getMySQLServerName() + ":" + configInstance.getMySQLServerPort() + "/" + configInstance.getMySQLDatabaseName(); + try { + mcMMO.p.getLogger().info("Attempting connection to MySQL..."); - // Force driver to load if not yet loaded - Class.forName("com.mysql.jdbc.Driver"); - Properties connectionProperties = new Properties(); - connectionProperties.put("user", configInstance.getMySQLUserName()); - connectionProperties.put("password", configInstance.getMySQLUserPassword()); - connectionProperties.put("autoReconnect", "false"); - connectionProperties.put("maxReconnects", "0"); - connection = DriverManager.getConnection(connectionString, connectionProperties); + // Force driver to load if not yet loaded + Class.forName("com.mysql.jdbc.Driver"); + Properties connectionProperties = new Properties(); + connectionProperties.put("user", configInstance.getMySQLUserName()); + connectionProperties.put("password", configInstance.getMySQLUserPassword()); + connectionProperties.put("autoReconnect", "false"); + connectionProperties.put("maxReconnects", "0"); + connection = DriverManager.getConnection(connectionString, connectionProperties); - mcMMO.p.getLogger().info("Connection to MySQL was a success!"); - } catch (SQLException ex) { - connection = null; - if (reconnectAttempt == 0 || reconnectAttempt >= 11) mcMMO.p.getLogger().info("Connection to MySQL failed!"); - } catch (ClassNotFoundException ex) { - connection = null; - if (reconnectAttempt == 0 || reconnectAttempt >= 11) mcMMO.p.getLogger().info("MySQL database driver not found!"); - } + mcMMO.p.getLogger().info("Connection to MySQL was a success!"); + } catch (SQLException ex) { + connection = null; + if (reconnectAttempt == 0 || reconnectAttempt >= 11) mcMMO.p.getLogger().info("Connection to MySQL failed!"); + } catch (ClassNotFoundException ex) { + connection = null; + if (reconnectAttempt == 0 || reconnectAttempt >= 11) mcMMO.p.getLogger().info("MySQL database driver not found!"); + } } /** @@ -198,12 +200,12 @@ public class Database { catch (SQLException ex) { switch (update) { case BLAST_MINING: - mcMMO.p.getLogger().info("Updating mcMMO MySQL tables for Blast Mining..."); + mcMMO.p.getLogger().info("Updating mcMMO MySQL tables for Blast Mining..."); write("ALTER TABLE `"+tablePrefix + "cooldowns` ADD `blast_mining` int(32) NOT NULL DEFAULT '0' ;"); break; case FISHING: - mcMMO.p.getLogger().info("Updating mcMMO MySQL tables for Fishing..."); + mcMMO.p.getLogger().info("Updating mcMMO MySQL tables for Fishing..."); write("ALTER TABLE `"+tablePrefix + "skills` ADD `fishing` int(10) NOT NULL DEFAULT '0' ;"); write("ALTER TABLE `"+tablePrefix + "experience` ADD `fishing` int(10) NOT NULL DEFAULT '0' ;"); break; @@ -416,6 +418,42 @@ public class Database { return rows; } + + public Map readSQLRank(String playerName) { + ResultSet resultSet; + Map skills = new HashMap(); + 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() { plugin.getLogger().info("Purging powerless users..."); @@ -487,4 +525,4 @@ public class Database { } } } -} \ No newline at end of file +} From 89a36ae47e32e29cade2f6e79928943dd832ca5e Mon Sep 17 00:00:00 2001 From: t00thpick1 Date: Tue, 15 Jan 2013 19:04:30 -0500 Subject: [PATCH 3/5] Last change --- src/main/java/com/gmail/nossr50/commands/mc/McrankCommand.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/gmail/nossr50/commands/mc/McrankCommand.java b/src/main/java/com/gmail/nossr50/commands/mc/McrankCommand.java index 67f3b3533..5e11f38c4 100644 --- a/src/main/java/com/gmail/nossr50/commands/mc/McrankCommand.java +++ b/src/main/java/com/gmail/nossr50/commands/mc/McrankCommand.java @@ -53,7 +53,6 @@ public class McrankCommand implements CommandExecutor { private void sqlDisplay(CommandSender sender, String playerName) { - String tablePrefix = Config.getInstance().getMySQLTablePrefix(); Database database = mcMMO.getPlayerDatabase(); Map skills = database.readSQLRank(playerName); for (SkillType skillType : SkillType.values()) { From e6e8470851e504aefc5b1881819faa40047324e1 Mon Sep 17 00:00:00 2001 From: t00thpick1 Date: Tue, 15 Jan 2013 19:08:41 -0500 Subject: [PATCH 4/5] Should be capitalized --- src/main/java/com/gmail/nossr50/commands/mc/McrankCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/gmail/nossr50/commands/mc/McrankCommand.java b/src/main/java/com/gmail/nossr50/commands/mc/McrankCommand.java index 5e11f38c4..4a04acb59 100644 --- a/src/main/java/com/gmail/nossr50/commands/mc/McrankCommand.java +++ b/src/main/java/com/gmail/nossr50/commands/mc/McrankCommand.java @@ -60,6 +60,6 @@ public class McrankCommand implements CommandExecutor { 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")); + sender.sendMessage(ChatColor.YELLOW + "Overall" + ChatColor.GREEN + " - " + ChatColor.GOLD + "Rank " + ChatColor.WHITE + "#" + ChatColor.GREEN + skills.get("ALL")); } } From 7e297ac88445c952346751738695f02e0602636a Mon Sep 17 00:00:00 2001 From: t00thpick1 Date: Tue, 15 Jan 2013 19:09:48 -0500 Subject: [PATCH 5/5] Name methods properly --- .../java/com/gmail/nossr50/commands/mc/McrankCommand.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/commands/mc/McrankCommand.java b/src/main/java/com/gmail/nossr50/commands/mc/McrankCommand.java index 4a04acb59..56e5a0fc2 100644 --- a/src/main/java/com/gmail/nossr50/commands/mc/McrankCommand.java +++ b/src/main/java/com/gmail/nossr50/commands/mc/McrankCommand.java @@ -32,7 +32,7 @@ public class McrankCommand implements CommandExecutor { if(Config.getInstance().getUseMySQL()) { sqlDisplay(arg0, playerName); } else { - flatfileDisplayer(arg0, playerName); + flatfileDisplay(arg0, playerName); } } else { @@ -42,7 +42,7 @@ public class McrankCommand implements CommandExecutor { return true; } - public void flatfileDisplayer(CommandSender sender, String playerName) { + 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