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 String Name;
public List<String> TasksCompleted;
public TaskClient(String name)
public TaskClient()
{
Name = name;
TasksCompleted = new ArrayList<String>();
}
public String toString()
{
return Name + " Tasks: {" + TasksCompleted.toString() + "}";
}
}

View File

@ -1,42 +1,88 @@
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.event.EventHandler;
import org.bukkit.plugin.java.JavaPlugin;
import mineplex.core.MiniClientPlugin;
import mineplex.core.account.event.ClientWebResponseEvent;
import mineplex.core.MiniDbClientPlugin;
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.TaskToken;
public class TaskManager extends MiniClientPlugin<TaskClient>
public class TaskManager extends MiniDbClientPlugin<TaskClient>
{
private static Object _taskLock = new Object();
private TaskRepository _repository;
public TaskManager(JavaPlugin plugin, String webServerAddress)
private NautHashMap<String, Integer> _tasks = new NautHashMap<String, Integer>();
public TaskManager(JavaPlugin plugin, CoreClientManager clientManager, String webServerAddress)
{
super("Task Manager", plugin);
super("Task Manager", plugin, clientManager);
_repository = new TaskRepository(webServerAddress);
_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
protected TaskClient AddPlayer(String playerName)
{
return new TaskClient(playerName);
return new TaskClient();
}
@EventHandler
public void OnClientWebResponse(ClientWebResponseEvent event)
public void addTaskForOfflinePlayer(final Callback<Boolean> callback, final UUID uuid, final String task)
{
TaskToken token = new Gson().fromJson(event.GetResponse(), TaskToken.class);
TaskClient client = new TaskClient(token.Name);
if (token.TasksCompleted != null)
client.TasksCompleted = token.TasksCompleted;
Set(token.Name, client);
Bukkit.getServer().getScheduler().runTaskAsynchronously(getPlugin(), new Runnable()
{
public void run()
{
synchronized (_taskLock)
{
if (!_tasks.containsKey(task))
{
_repository.addTask(task);
System.out.println("TaskManager Adding Task : " + task);
}
}
updateTasks();
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)
@ -44,11 +90,41 @@ public class TaskManager extends MiniClientPlugin<TaskClient>
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());
client.TasksCompleted.add(taskName);
Get(player.getName()).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;
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;
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
{
private String _webAddress;
private static String ADD_ACCOUNT_TASK = "INSERT INTO accountTasks (accountId, taskId) VALUES (?, ?);";
public TaskRepository(String webServerAddress)
private static String ADD_TASK = "INSERT INTO tasks (name) VALUES (?);";
private static String RETRIEVE_TASKS = "SELECT id, name FROM tasks;";
public TaskRepository(JavaPlugin plugin)
{
_webAddress = webServerAddress;
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;
}
@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));
}
new AsyncJsonWebCall(_webAddress + "PlayerAccount/AddTask").Execute(token);
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);
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);

View File

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

View File

@ -129,16 +129,22 @@ public class TutorialManager extends MiniPlugin
//Gems
if (!_taskManager.hasCompletedTask(player, tut.GetTask()))
{
_donationManager.RewardGems(new Callback<Boolean>()
_taskManager.completedTask(new Callback<Boolean>()
{
public void run(Boolean completed)
{
UtilPlayer.message(player, F.main("Tutorial", "You received " + F.elem(C.cGreen + tut.GetGems() + " Gems") + "."));
_taskManager.completedTask(player, tut.GetTask());
//Sound
player.playSound(player.getLocation(), Sound.LEVEL_UP, 2f, 1.5f);
_donationManager.RewardGems(new Callback<Boolean>()
{
public void run(Boolean completed)
{
UtilPlayer.message(player, F.main("Tutorial", "You received " + F.elem(C.cGreen + tut.GetGems() + " Gems") + "."));
//Sound
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)
{
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);
_statsManager = new StatsManager(plugin, clientManager);
_taskManager = new TaskManager(plugin, webAddress);
_taskManager = new TaskManager(plugin, clientManager, webAddress);
_achievementManager = new AchievementManager(_statsManager, clientManager, donationManager);
_inventoryManager = inventoryManager;
_cosmeticManager = cosmeticManager;

View File

@ -106,14 +106,20 @@ public class GameAchievementManager implements Listener
" " + F.elem(C.cGreen + C.Bold + "+" + type.getGemReward() + " Gems"));
player.playSound(player.getLocation(), Sound.LEVEL_UP, 1f, 1f);
Manager.GetDonation().RewardGems(new Callback<Boolean>()
Manager.GetTaskManager().completedTask(new Callback<Boolean>()
{
public void run(Boolean completed)
{
Manager.GetTaskManager().completedTask(player, type.getName());
Manager.GetDonation().RewardGems(new Callback<Boolean>()
{
public void run(Boolean completed)
{
}
}, type.getName(), player.getName(), player.getUniqueId(), type.getGemReward());
}
}, type.getName(), player.getName(), player.getUniqueId(), type.getGemReward());
}, player, type.getName());
}
else
{

Binary file not shown.