queue concept, nowhere near final

This commit is contained in:
T00thpick1 2013-03-08 17:09:47 -05:00 committed by NuclearW
parent 3a7d88db4f
commit a7f146a835
9 changed files with 217 additions and 4 deletions

View File

@ -8,11 +8,11 @@ import org.bukkit.entity.Player;
import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.config.Config; import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.database.LeaderboardManager; import com.gmail.nossr50.database.LeaderboardManager;
import com.gmail.nossr50.database.queuemanager.McRankAsync;
import com.gmail.nossr50.datatypes.player.McMMOPlayer; import com.gmail.nossr50.datatypes.player.McMMOPlayer;
import com.gmail.nossr50.datatypes.player.PlayerProfile; import com.gmail.nossr50.datatypes.player.PlayerProfile;
import com.gmail.nossr50.datatypes.skills.SkillType; import com.gmail.nossr50.datatypes.skills.SkillType;
import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.runnables.commands.McrankCommandAsyncTask;
import com.gmail.nossr50.util.Misc; import com.gmail.nossr50.util.Misc;
import com.gmail.nossr50.util.Permissions; import com.gmail.nossr50.util.Permissions;
import com.gmail.nossr50.util.player.UserManager; import com.gmail.nossr50.util.player.UserManager;
@ -122,6 +122,8 @@ public class McrankCommand implements CommandExecutor {
} }
private void sqlDisplay(CommandSender sender, String playerName) { private void sqlDisplay(CommandSender sender, String playerName) {
mcMMO.p.getServer().getScheduler().runTaskAsynchronously(mcMMO.p, new McrankCommandAsyncTask(playerName, sender)); if (mcMMO.queueManager.contains(sender.getName()) || !mcMMO.queueManager.queue(new McRankAsync(playerName, sender))) {
//Warn that queue is unable to accept
}
} }
} }

View File

@ -8,9 +8,9 @@ import org.bukkit.command.CommandSender;
import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.config.Config; import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.database.LeaderboardManager; import com.gmail.nossr50.database.LeaderboardManager;
import com.gmail.nossr50.database.queuemanager.McTopAsync;
import com.gmail.nossr50.datatypes.skills.SkillType; import com.gmail.nossr50.datatypes.skills.SkillType;
import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.runnables.commands.MctopCommandAsyncTask;
import com.gmail.nossr50.util.Permissions; import com.gmail.nossr50.util.Permissions;
import com.gmail.nossr50.util.StringUtils; import com.gmail.nossr50.util.StringUtils;
import com.gmail.nossr50.util.skills.SkillUtils; import com.gmail.nossr50.util.skills.SkillUtils;
@ -123,6 +123,8 @@ public class MctopCommand implements CommandExecutor {
} }
private void sqlDisplay(int page, String query, CommandSender sender, Command command) { private void sqlDisplay(int page, String query, CommandSender sender, Command command) {
mcMMO.p.getServer().getScheduler().runTaskAsynchronously(mcMMO.p, new MctopCommandAsyncTask(page, query, sender, command)); if (mcMMO.queueManager.contains(sender.getName()) || !mcMMO.queueManager.queue(new McTopAsync(page, query, sender, command))) {
//Warn that queue is unable to accept
}
} }
} }

View File

@ -59,6 +59,7 @@ public class Config extends AutoUpdateConfigLoader {
public String getMySQLUserName() { return getStringIncludingInts(config, "MySQL.Database.User_Name"); } public String getMySQLUserName() { return getStringIncludingInts(config, "MySQL.Database.User_Name"); }
public int getMySQLServerPort() { return config.getInt("MySQL.Server.Port", 3306); } public int getMySQLServerPort() { return config.getInt("MySQL.Server.Port", 3306); }
public String getMySQLServerName() { return config.getString("MySQL.Server.Address", "localhost"); } public String getMySQLServerName() { return config.getString("MySQL.Server.Address", "localhost"); }
public long getQueueThrottle() { return config.getLong("MySQL.QueueThrottle", 100L); }
public String getMySQLUserPassword() { public String getMySQLUserPassword() {
if (getStringIncludingInts(config, "MySQL.Database.User_Password") != null) { if (getStringIncludingInts(config, "MySQL.Database.User_Password") != null) {
@ -253,4 +254,5 @@ public class Config extends AutoUpdateConfigLoader {
/* XP Formula Multiplier */ /* XP Formula Multiplier */
public int getFormulaMultiplierCurve() { return config.getInt("Experience.Formula.Curve_Modifier", 20); } public int getFormulaMultiplierCurve() { return config.getInt("Experience.Formula.Curve_Modifier", 20); }
public double getForumulaMultiplier(SkillType skill) { return config.getDouble("Experience.Formula.Multiplier." + StringUtils.getCapitalized(skill.toString())); } public double getForumulaMultiplier(SkillType skill) { return config.getDouble("Experience.Formula.Multiplier." + StringUtils.getCapitalized(skill.toString())); }
} }

View File

@ -0,0 +1,56 @@
package com.gmail.nossr50.database.queuemanager;
import java.util.concurrent.LinkedBlockingQueue;
public class AsyncQueueManager implements Runnable {
private LinkedBlockingQueue<Queueable> queue;
private boolean running;
private long throttle;
public AsyncQueueManager(long throttle) {
this.queue = new LinkedBlockingQueue<Queueable>();
this.running = true;
this.throttle = throttle;
}
@Override
public void run() {
while(running) {
try {
queue.take().run();
Thread.sleep(throttle);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void disable() {
running = false;
}
public boolean queue(Queueable task) {
return queue.offer(task);
}
public boolean contains(String player) {
return queue.contains(new EqualString(player));
}
private class EqualString {
private String player;
public EqualString(String player) {
this.player = player;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Queueable) {
return ((Queueable)obj).getPlayer().equalsIgnoreCase(player);
}
return false;
}
}
}

View File

@ -0,0 +1,62 @@
package com.gmail.nossr50.database.queuemanager;
import java.util.Map;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.database.DatabaseManager;
import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.util.skills.SkillUtils;
import com.gmail.nossr50.datatypes.skills.SkillType;
public class McRankAsync implements Queueable {
private final String playerName;
private final CommandSender sender;
private String player;
public McRankAsync(String playerName, CommandSender sender) {
this.playerName = playerName;
this.sender = sender;
this.player = sender.getName();
}
public void run() {
final Map<String, Integer> skills = DatabaseManager.readSQLRank(playerName);
Bukkit.getScheduler().scheduleSyncDelayedTask(mcMMO.p, new Runnable() {
@Override
public void run() {
sender.sendMessage(LocaleLoader.getString("Commands.mcrank.Heading"));
sender.sendMessage(LocaleLoader.getString("Commands.mcrank.Player", playerName));
for (SkillType skillType : SkillType.values()) {
if (skillType.isChildSkill()) {
continue;
}
if (skills.get(skillType.name()) == null) {
sender.sendMessage(LocaleLoader.getString("Commands.mcrank.Skill", SkillUtils.getSkillName(skillType), LocaleLoader.getString("Commands.mcrank.Unranked")));
}
else {
sender.sendMessage(LocaleLoader.getString("Commands.mcrank.Skill", SkillUtils.getSkillName(skillType), skills.get(skillType.name())));
}
}
if (skills.get("ALL") == null) {
sender.sendMessage(LocaleLoader.getString("Commands.mcrank.Overall", LocaleLoader.getString("Commands.mcrank.Unranked")));
}
else {
sender.sendMessage(LocaleLoader.getString("Commands.mcrank.Overall", skills.get("ALL")));
}
}
}, 1L);
}
@Override
public String getPlayer() {
return player;
}
}

View File

@ -0,0 +1,74 @@
package com.gmail.nossr50.database.queuemanager;
import java.util.ArrayList;
import java.util.HashMap;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.database.DatabaseManager;
import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.util.Permissions;
import com.gmail.nossr50.util.StringUtils;
import com.gmail.nossr50.datatypes.skills.SkillType;
public class McTopAsync implements Queueable {
private CommandSender sender;
private String query;
private int page;
private Command command;
private String player;
public McTopAsync(int page, String query, CommandSender sender, Command command) {
this.page = page;
this.query = query;
this.sender = sender;
this.command = command;
this.player = sender.getName();
}
public void run() {
if (!query.equalsIgnoreCase("taming+mining+woodcutting+repair+unarmed+herbalism+excavation+archery+swords+axes+acrobatics+fishing")) {
if (!Permissions.mctop(sender, SkillType.getSkill(query))) {
sender.sendMessage(command.getPermissionMessage());
return;
}
}
String tablePrefix = Config.getInstance().getMySQLTablePrefix();
final HashMap<Integer, ArrayList<String>> userslist = DatabaseManager.read("SELECT " + query + ", user, NOW() FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON (user_id = id) WHERE " + query + " > 0 ORDER BY " + query + " DESC, user LIMIT " + ((page * 10) - 10) + ",10");
Bukkit.getScheduler().scheduleSyncDelayedTask(mcMMO.p, new Runnable() {
@Override
public void run() {
if (query.equals("taming+mining+woodcutting+repair+unarmed+herbalism+excavation+archery+swords+axes+acrobatics+fishing")) {
sender.sendMessage(LocaleLoader.getString("Commands.PowerLevel.Leaderboard"));
} else {
sender.sendMessage(LocaleLoader.getString("Commands.Skill.Leaderboard", StringUtils.getCapitalized(query)));
}
int place = (page * 10) - 9;
for (int i = 1; i <= 10; i++) {
if (userslist.get(i) == null) {
break;
}
// Format: 1. Playername - skill value
sender.sendMessage(place + ". " + ChatColor.GREEN + userslist.get(i).get(1) + " - " + ChatColor.WHITE + userslist.get(i).get(0));
place++;
}
sender.sendMessage(LocaleLoader.getString("Commands.mctop.Tip"));
}
}, 1L);
}
@Override
public String getPlayer() {
return player;
}
}

View File

@ -0,0 +1,6 @@
package com.gmail.nossr50.database.queuemanager;
public interface Queueable {
public void run();
public String getPlayer();
}

View File

@ -27,6 +27,7 @@ import com.gmail.nossr50.config.spout.SpoutConfig;
import com.gmail.nossr50.config.treasure.TreasureConfig; import com.gmail.nossr50.config.treasure.TreasureConfig;
import com.gmail.nossr50.database.DatabaseManager; import com.gmail.nossr50.database.DatabaseManager;
import com.gmail.nossr50.database.LeaderboardManager; import com.gmail.nossr50.database.LeaderboardManager;
import com.gmail.nossr50.database.queuemanager.AsyncQueueManager;
import com.gmail.nossr50.datatypes.player.PlayerProfile; import com.gmail.nossr50.datatypes.player.PlayerProfile;
import com.gmail.nossr50.listeners.BlockListener; import com.gmail.nossr50.listeners.BlockListener;
import com.gmail.nossr50.listeners.EntityListener; import com.gmail.nossr50.listeners.EntityListener;
@ -70,6 +71,7 @@ public class mcMMO extends JavaPlugin {
public static ChunkManager placeStore; public static ChunkManager placeStore;
public static RepairableManager repairableManager; public static RepairableManager repairableManager;
public static AsyncQueueManager queueManager;
// Jar Stuff // Jar Stuff
public static File mcmmo; public static File mcmmo;
@ -89,6 +91,7 @@ public class mcMMO extends JavaPlugin {
// XP Event Check // XP Event Check
private boolean xpEventEnabled = false; private boolean xpEventEnabled = false;
// Metadata Values // Metadata Values
public static FixedMetadataValue metadataValue; public static FixedMetadataValue metadataValue;
public final static String entityMetadataKey = "mcMMO: Spawned Entity"; public final static String entityMetadataKey = "mcMMO: Spawned Entity";
@ -455,6 +458,11 @@ public class mcMMO extends JavaPlugin {
// Bleed timer (Runs every two seconds) // Bleed timer (Runs every two seconds)
scheduler.scheduleSyncRepeatingTask(this, new BleedTimerTask(), 40, 40); scheduler.scheduleSyncRepeatingTask(this, new BleedTimerTask(), 40, 40);
if (Config.getInstance().getUseMySQL()) {
queueManager = new AsyncQueueManager(Config.getInstance().getQueueThrottle());
scheduler.runTaskAsynchronously(this, queueManager);
}
// Old & Powerless User remover // Old & Powerless User remover
int purgeInterval = Config.getInstance().getPurgeInterval(); int purgeInterval = Config.getInstance().getPurgeInterval();

View File

@ -48,6 +48,7 @@ MySQL:
Server: Server:
Port: 3306 Port: 3306
Address: localhost Address: localhost
QueueThrottle: 100L
# #
# Settings for Hardcore mode # Settings for Hardcore mode
### ###