Converted TaskManager

This commit is contained in:
Jonathan Williams 2015-05-11 01:21:27 -07:00
parent b7df5c43db
commit b1a46d0f0d
11 changed files with 218 additions and 58 deletions

View File

@ -0,0 +1,13 @@
package mineplex.core.task;
public class Task
{
public int Id;
public String Name;
public Task(int id, String name)
{
Id = id;
Name = name;
}
}

View File

@ -5,17 +5,10 @@ import java.util.List;
public class TaskClient public class TaskClient
{ {
public String Name;
public List<String> TasksCompleted; public List<String> TasksCompleted;
public TaskClient(String name) public TaskClient()
{ {
Name = name;
TasksCompleted = new ArrayList<String>(); TasksCompleted = new ArrayList<String>();
} }
public String toString()
{
return Name + " Tasks: {" + TasksCompleted.toString() + "}";
}
} }

View File

@ -1,42 +1,88 @@
package mineplex.core.task; package mineplex.core.task;
import org.bukkit.craftbukkit.libs.com.google.gson.Gson; import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import mineplex.core.MiniClientPlugin; import mineplex.core.MiniDbClientPlugin;
import mineplex.core.account.event.ClientWebResponseEvent; import mineplex.core.account.CoreClientManager;
import mineplex.core.common.util.Callback;
import mineplex.core.common.util.NautHashMap;
import mineplex.core.task.repository.TaskRepository; import mineplex.core.task.repository.TaskRepository;
import mineplex.core.task.repository.TaskToken;
public class TaskManager extends MiniClientPlugin<TaskClient> public class TaskManager extends MiniDbClientPlugin<TaskClient>
{ {
private static Object _taskLock = new Object();
private TaskRepository _repository; private TaskRepository _repository;
public TaskManager(JavaPlugin plugin, String webServerAddress) private NautHashMap<String, Integer> _tasks = new NautHashMap<String, Integer>();
{
super("Task Manager", plugin);
_repository = new TaskRepository(webServerAddress); public TaskManager(JavaPlugin plugin, CoreClientManager clientManager, String webServerAddress)
{
super("Task Manager", plugin, clientManager);
_repository = new TaskRepository(plugin);
updateTasks();
}
private void updateTasks()
{
List<Task> tasks = _repository.retrieveTasks();
synchronized (_taskLock)
{
for (Task task : tasks)
{
_tasks.put(task.Name, task.Id);
}
}
} }
@Override @Override
protected TaskClient AddPlayer(String playerName) protected TaskClient AddPlayer(String playerName)
{ {
return new TaskClient(playerName); return new TaskClient();
} }
@EventHandler public void addTaskForOfflinePlayer(final Callback<Boolean> callback, final UUID uuid, final String task)
public void OnClientWebResponse(ClientWebResponseEvent event)
{ {
TaskToken token = new Gson().fromJson(event.GetResponse(), TaskToken.class); Bukkit.getServer().getScheduler().runTaskAsynchronously(getPlugin(), new Runnable()
TaskClient client = new TaskClient(token.Name); {
public void run()
{
synchronized (_taskLock)
{
if (!_tasks.containsKey(task))
{
_repository.addTask(task);
System.out.println("TaskManager Adding Task : " + task);
}
}
if (token.TasksCompleted != null) updateTasks();
client.TasksCompleted = token.TasksCompleted;
Set(token.Name, client); synchronized (_taskLock)
{
final boolean success = _repository.addAccountTask(ClientManager.getCachedClientAccountId(uuid), _tasks.get(task));
if (callback != null)
{
Bukkit.getServer().getScheduler().runTask(getPlugin(), new Runnable()
{
public void run()
{
callback.run(success);
}
});
}
}
}
});
} }
public boolean hasCompletedTask(Player player, String taskName) public boolean hasCompletedTask(Player player, String taskName)
@ -44,11 +90,41 @@ public class TaskManager extends MiniClientPlugin<TaskClient>
return Get(player.getName()).TasksCompleted.contains(taskName); return Get(player.getName()).TasksCompleted.contains(taskName);
} }
public void completedTask(Player player, String taskName) public void completedTask(final Callback<Boolean> callback, final Player player, final String taskName)
{ {
TaskClient client = Get(player.getName()); Get(player.getName()).TasksCompleted.add(taskName);
client.TasksCompleted.add(taskName);
_repository.AddTask(client.Name, taskName); addTaskForOfflinePlayer(new Callback<Boolean>()
{
public void run(Boolean success)
{
if (!success)
{
System.out.println("Add task FAILED for " + player.getName());
if (_tasks.containsKey(taskName))
{
Get(player.getName()).TasksCompleted.remove(taskName);
}
}
if (callback != null)
{
callback.run(success);
}
}
}, player.getUniqueId(), taskName);
}
@Override
public void processLoginResultSet(String playerName, ResultSet resultSet) throws SQLException
{
Set(playerName, _repository.loadClientInformation(resultSet));
}
@Override
public String getQuery(int accountId, String uuid, String name)
{
return "SELECT taskId FROM accountTasks WHERE accountId = '" + accountId + "';";
} }
} }

View File

@ -1,22 +1,79 @@
package mineplex.core.task.repository; package mineplex.core.task.repository;
import mineplex.core.server.remotecall.AsyncJsonWebCall; import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class TaskRepository import org.bukkit.plugin.java.JavaPlugin;
{
private String _webAddress;
public TaskRepository(String webServerAddress) import mineplex.core.database.DBPool;
import mineplex.core.database.RepositoryBase;
import mineplex.core.database.ResultSetCallable;
import mineplex.core.database.column.ColumnInt;
import mineplex.core.database.column.ColumnVarChar;
import mineplex.core.task.Task;
import mineplex.core.task.TaskClient;
public class TaskRepository extends RepositoryBase
{ {
_webAddress = webServerAddress; private static String ADD_ACCOUNT_TASK = "INSERT INTO accountTasks (accountId, taskId) VALUES (?, ?);";
private static String ADD_TASK = "INSERT INTO tasks (name) VALUES (?);";
private static String RETRIEVE_TASKS = "SELECT id, name FROM tasks;";
public TaskRepository(JavaPlugin plugin)
{
super(plugin, DBPool.ACCOUNT);
} }
public void AddTask(String name, String newTask) @Override
protected void initialize()
{ {
UpdateTaskToken token = new UpdateTaskToken(); }
token.Name = name;
token.NewTaskCompleted = newTask;
new AsyncJsonWebCall(_webAddress + "PlayerAccount/AddTask").Execute(token); @Override
protected void update()
{
}
public boolean addAccountTask(int accountId, int taskId)
{
return executeUpdate(ADD_ACCOUNT_TASK, new ColumnInt("accountId", accountId), new ColumnInt("taskId", taskId)) > 0;
}
public TaskClient loadClientInformation(ResultSet resultSet) throws SQLException
{
final TaskClient taskClient = new TaskClient();
while (resultSet.next())
{
taskClient.TasksCompleted.add(resultSet.getString(1));
}
return taskClient;
}
public List<Task> retrieveTasks()
{
final List<Task> tasks = new ArrayList<Task>();
executeQuery(RETRIEVE_TASKS, new ResultSetCallable()
{
public void processResultSet(ResultSet resultSet) throws SQLException
{
while (resultSet.next())
{
tasks.add(new Task(resultSet.getInt(1), resultSet.getString(2)));
}
}
});
return tasks;
}
public void addTask(String task)
{
executeUpdate(ADD_TASK, new ColumnVarChar("name", 100, task));
} }
} }

View File

@ -118,7 +118,7 @@ public class Hub extends JavaPlugin implements IRelation
PartyManager partyManager = new PartyManager(this, portal, clientManager, preferenceManager); PartyManager partyManager = new PartyManager(this, portal, clientManager, preferenceManager);
HubManager hubManager = new HubManager(this, blockRestore, clientManager, donationManager, new ConditionManager(this), disguiseManager, new TaskManager(this, webServerAddress), portal, partyManager, preferenceManager, petManager, pollManager, statsManager, achievementManager, new HologramManager(this)); HubManager hubManager = new HubManager(this, blockRestore, clientManager, donationManager, new ConditionManager(this), disguiseManager, new TaskManager(this, clientManager, webServerAddress), portal, partyManager, preferenceManager, petManager, pollManager, statsManager, achievementManager, new HologramManager(this));
QueueManager queueManager = new QueueManager(this, clientManager, donationManager, new EloManager(this, clientManager), partyManager); QueueManager queueManager = new QueueManager(this, clientManager, donationManager, new EloManager(this, clientManager), partyManager);

View File

@ -390,18 +390,25 @@ public class ParkourManager extends MiniPlugin
{ {
final ParkourData fData = data; final ParkourData fData = data;
_taskManager.completedTask(new Callback<Boolean>()
{
public void run(Boolean completed)
{
_donationManager.RewardGems(new Callback<Boolean>() _donationManager.RewardGems(new Callback<Boolean>()
{ {
public void run(Boolean completed) public void run(Boolean completed)
{ {
UtilPlayer.message(player, F.main("Parkour", "You received " + F.elem(C.cGreen + fData.Gems + " Gems") + ".")); UtilPlayer.message(player, F.main("Parkour", "You received " + F.elem(C.cGreen + fData.Gems + " Gems") + "."));
_taskManager.completedTask(player, fData.Name); //Sound
player.playSound(player.getLocation(), Sound.LEVEL_UP, 2f, 1.5f);
}
}, "Parkour " + fData.Name, player.getName(), player.getUniqueId(), fData.Gems);
//Sound //Sound
player.playSound(player.getLocation(), Sound.LEVEL_UP, 2f, 1.5f); player.playSound(player.getLocation(), Sound.LEVEL_UP, 2f, 1.5f);
} }
}, "Parkour " + data.Name, player.getName(), player.getUniqueId(), data.Gems); }, player, fData.Name);
} }
} }
} }

View File

@ -128,18 +128,24 @@ public class TutorialManager extends MiniPlugin
//Gems //Gems
if (!_taskManager.hasCompletedTask(player, tut.GetTask())) if (!_taskManager.hasCompletedTask(player, tut.GetTask()))
{
_taskManager.completedTask(new Callback<Boolean>()
{
public void run(Boolean completed)
{ {
_donationManager.RewardGems(new Callback<Boolean>() _donationManager.RewardGems(new Callback<Boolean>()
{ {
public void run(Boolean completed) public void run(Boolean completed)
{ {
UtilPlayer.message(player, F.main("Tutorial", "You received " + F.elem(C.cGreen + tut.GetGems() + " Gems") + ".")); UtilPlayer.message(player, F.main("Tutorial", "You received " + F.elem(C.cGreen + tut.GetGems() + " Gems") + "."));
_taskManager.completedTask(player, tut.GetTask());
//Sound //Sound
player.playSound(player.getLocation(), Sound.LEVEL_UP, 2f, 1.5f); player.playSound(player.getLocation(), Sound.LEVEL_UP, 2f, 1.5f);
} }
}, "Tutorial " + tut.GetTutName(), player.getName(), player.getUniqueId(), tut.GetGems()); }, "Tutorial " + tut.GetTutName(), player.getName(), player.getUniqueId(), tut.GetGems());
} }
}, player, tut.GetTask());
}
} }
} }
} }

View File

@ -112,7 +112,9 @@ public class Utility
*/ */
public static JedisPool generatePool(ConnectionData connData) public static JedisPool generatePool(ConnectionData connData)
{ {
return new JedisPool(new JedisPoolConfig(), connData.getHost(), connData.getPort()); JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxWaitMillis(10);
return new JedisPool(jedisPoolConfig, connData.getHost(), connData.getPort());
} }
/** /**

View File

@ -252,7 +252,7 @@ public class ArcadeManager extends MiniPlugin implements IRelation
_partyManager = new PartyManager(plugin, portal, _clientManager, preferences); _partyManager = new PartyManager(plugin, portal, _clientManager, preferences);
_statsManager = new StatsManager(plugin, clientManager); _statsManager = new StatsManager(plugin, clientManager);
_taskManager = new TaskManager(plugin, webAddress); _taskManager = new TaskManager(plugin, clientManager, webAddress);
_achievementManager = new AchievementManager(_statsManager, clientManager, donationManager); _achievementManager = new AchievementManager(_statsManager, clientManager, donationManager);
_inventoryManager = inventoryManager; _inventoryManager = inventoryManager;
_cosmeticManager = cosmeticManager; _cosmeticManager = cosmeticManager;

View File

@ -107,14 +107,20 @@ public class GameAchievementManager implements Listener
player.playSound(player.getLocation(), Sound.LEVEL_UP, 1f, 1f); player.playSound(player.getLocation(), Sound.LEVEL_UP, 1f, 1f);
Manager.GetTaskManager().completedTask(new Callback<Boolean>()
{
public void run(Boolean completed)
{
Manager.GetDonation().RewardGems(new Callback<Boolean>() Manager.GetDonation().RewardGems(new Callback<Boolean>()
{ {
public void run(Boolean completed) public void run(Boolean completed)
{ {
Manager.GetTaskManager().completedTask(player, type.getName());
} }
}, type.getName(), player.getName(), player.getUniqueId(), type.getGemReward()); }, type.getName(), player.getName(), player.getUniqueId(), type.getGemReward());
} }
}, player, type.getName());
}
else else
{ {
//Display nothing because already complete bro :O //Display nothing because already complete bro :O

Binary file not shown.