2015-08-06 07:45:39 +02:00
|
|
|
package mineplex.votifier;
|
|
|
|
|
2015-08-10 07:33:54 +02:00
|
|
|
import java.util.UUID;
|
|
|
|
|
2015-08-06 07:45:39 +02:00
|
|
|
import org.bukkit.event.EventHandler;
|
|
|
|
import org.bukkit.plugin.java.JavaPlugin;
|
|
|
|
|
|
|
|
import com.vexsoftware.votifier.model.Vote;
|
|
|
|
import com.vexsoftware.votifier.model.VotifierEvent;
|
|
|
|
import mineplex.core.MiniPlugin;
|
|
|
|
import mineplex.core.account.CoreClientManager;
|
2015-08-10 07:33:54 +02:00
|
|
|
import mineplex.core.common.util.UUIDFetcher;
|
|
|
|
import mineplex.core.database.DBPool;
|
2015-08-06 07:45:39 +02:00
|
|
|
import mineplex.core.donation.DonationManager;
|
2015-08-08 13:35:25 +02:00
|
|
|
import mineplex.core.votifier.VotifierCommand;
|
2015-08-10 07:33:54 +02:00
|
|
|
import mineplex.database.Tables;
|
2015-08-08 13:35:25 +02:00
|
|
|
import mineplex.serverdata.Region;
|
2015-08-10 07:33:54 +02:00
|
|
|
import mineplex.serverdata.Utility;
|
|
|
|
import mineplex.serverdata.commands.ServerCommand;
|
2015-08-08 13:35:25 +02:00
|
|
|
import mineplex.serverdata.commands.ServerCommandManager;
|
|
|
|
import mineplex.serverdata.data.PlayerStatus;
|
2015-08-10 07:33:54 +02:00
|
|
|
import mineplex.serverdata.redis.RedisConfig;
|
2015-08-08 13:35:25 +02:00
|
|
|
import mineplex.serverdata.redis.RedisDataRepository;
|
|
|
|
import mineplex.serverdata.servers.ServerManager;
|
2015-08-10 07:33:54 +02:00
|
|
|
import org.jooq.DSLContext;
|
|
|
|
import org.jooq.SQLDialect;
|
|
|
|
import org.jooq.impl.DSL;
|
|
|
|
import redis.clients.jedis.Jedis;
|
|
|
|
import redis.clients.jedis.JedisPool;
|
|
|
|
import redis.clients.jedis.exceptions.JedisConnectionException;
|
2015-08-06 07:45:39 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by shaun on 15-08-05.
|
|
|
|
*/
|
|
|
|
public class VotifierManager extends MiniPlugin
|
|
|
|
{
|
2015-08-10 07:33:54 +02:00
|
|
|
private CoreClientManager _clientManager;
|
|
|
|
private DonationManager _donationManager;
|
|
|
|
|
|
|
|
private RedisConfig _usConfig;
|
|
|
|
private RedisConfig _euConfig;
|
2015-08-08 13:35:25 +02:00
|
|
|
private RedisDataRepository<PlayerStatus> _usPlayerRepo;
|
|
|
|
private RedisDataRepository<PlayerStatus> _euPlayerRepo;
|
2015-08-10 07:33:54 +02:00
|
|
|
private JedisPool _usWritePool;
|
|
|
|
private JedisPool _euWritePool;
|
2015-08-08 13:35:25 +02:00
|
|
|
|
2015-08-06 07:45:39 +02:00
|
|
|
public VotifierManager(JavaPlugin plugin, CoreClientManager clientManager, DonationManager donationManager)
|
|
|
|
{
|
|
|
|
super("Votifier", plugin);
|
|
|
|
|
2015-08-10 07:33:54 +02:00
|
|
|
_clientManager = clientManager;
|
|
|
|
_donationManager = donationManager;
|
|
|
|
|
|
|
|
_usConfig = ServerManager.getConfig("us-redis.dat");
|
|
|
|
_euConfig = ServerManager.getConfig("eu-redis.dat");
|
|
|
|
|
|
|
|
_usPlayerRepo = new RedisDataRepository<PlayerStatus>(_usConfig.getConnection(true, "DefaultConnection"),
|
|
|
|
_usConfig.getConnection(false, "DefaultConnection"), Region.US, PlayerStatus.class, "playerStatus");
|
|
|
|
_euPlayerRepo = new RedisDataRepository<PlayerStatus>(_euConfig.getConnection(true, "DefaultConnection"),
|
|
|
|
_euConfig.getConnection(false, "DefaultConnection"), Region.EU, PlayerStatus.class, "playerStatus");
|
2015-08-06 07:45:39 +02:00
|
|
|
|
2015-08-10 07:33:54 +02:00
|
|
|
_usWritePool = Utility.generatePool(_usConfig.getConnection(true, "DefaultConnection"));
|
|
|
|
_euWritePool = Utility.generatePool(_euConfig.getConnection(true, "DefaultConnection"));
|
2015-08-08 13:35:25 +02:00
|
|
|
}
|
2015-08-06 07:45:39 +02:00
|
|
|
|
|
|
|
@EventHandler
|
|
|
|
public void handleVote(VotifierEvent event)
|
|
|
|
{
|
|
|
|
Vote vote = event.getVote();
|
2015-08-10 07:47:36 +02:00
|
|
|
String playerName = vote.getUsername();
|
2015-08-06 07:45:39 +02:00
|
|
|
|
2015-08-10 07:33:54 +02:00
|
|
|
System.out.println("New Vote: " + playerName);
|
2015-08-08 13:35:25 +02:00
|
|
|
|
2015-08-10 07:33:54 +02:00
|
|
|
// UUID uuid = UUIDFetcher.getUUIDOf(playerName);
|
2015-08-10 07:47:36 +02:00
|
|
|
// UUID uuid = _clientManager.loadUUIDFromDB(playerName);
|
|
|
|
// if (uuid != null)
|
|
|
|
// {
|
|
|
|
// System.out.println("Found UUID:" + uuid.toString());
|
2015-08-10 07:33:54 +02:00
|
|
|
// if (playerName.equalsIgnoreCase("Phinary"))
|
|
|
|
// {
|
|
|
|
// System.out.println("award bonus");
|
|
|
|
// awardBonus(uuid);
|
|
|
|
// }
|
2015-08-10 07:47:36 +02:00
|
|
|
// }
|
|
|
|
// else
|
|
|
|
// {
|
|
|
|
// System.out.println("Failed to load UUID for player: " + playerName);
|
|
|
|
// }
|
|
|
|
|
|
|
|
// PlayerStatus usStatus = _usPlayerRepo.getElement(playerName);
|
|
|
|
// if (usStatus != null)
|
|
|
|
// {
|
|
|
|
// System.out.println("Found on US Server: " + usStatus.getServer());
|
|
|
|
// writePool = _usWritePool;
|
|
|
|
// serverName = usStatus.getServer();
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// PlayerStatus euStatus = _euPlayerRepo.getElement(playerName);
|
|
|
|
// if (euStatus != null)
|
|
|
|
// {
|
|
|
|
// System.out.println("Found on EU Server: " + euStatus.getServer());
|
|
|
|
// writePool = _euWritePool;
|
|
|
|
// serverName = euStatus.getServer();
|
|
|
|
// }
|
|
|
|
|
|
|
|
// Currently we just notify all servers, and the server with the player on it can deal with it
|
|
|
|
notifyServer(playerName, false);
|
|
|
|
notifyServer(playerName, true);
|
2015-08-10 07:33:54 +02:00
|
|
|
}
|
|
|
|
|
2015-08-10 07:47:36 +02:00
|
|
|
private void notifyServer(String playerName, boolean eu)
|
2015-08-10 07:33:54 +02:00
|
|
|
{
|
2015-08-10 07:47:36 +02:00
|
|
|
JedisPool writePool = eu ? _euWritePool : _usWritePool;
|
|
|
|
|
|
|
|
VotifierCommand command = new VotifierCommand(playerName);
|
|
|
|
publishCommand(command, writePool);
|
2015-08-10 07:33:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void awardBonus(UUID uuid)
|
|
|
|
{
|
2015-08-10 07:47:36 +02:00
|
|
|
// Don't use this right now!
|
2015-08-10 07:33:54 +02:00
|
|
|
DSLContext create = DSL.using(DBPool.ACCOUNT, SQLDialect.MYSQL);
|
|
|
|
int updated = create.update(Tables.bonus).set(Tables.bonus.tickets, Tables.bonus.tickets.add(1))
|
|
|
|
.where(Tables.bonus.accountId.eq(DSL.select(Tables.accounts.id).where(Tables.accounts.uuid.eq(uuid.toString())))).execute();
|
|
|
|
System.out.println("Ran query with response: " + updated);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void publishCommand(final ServerCommand serverCommand, final JedisPool writePool)
|
|
|
|
{
|
|
|
|
new Thread(new Runnable()
|
|
|
|
{
|
|
|
|
public void run()
|
|
|
|
{
|
|
|
|
Jedis jedis = writePool.getResource();
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
String commandType = serverCommand.getClass().getSimpleName();
|
|
|
|
String serializedCommand = Utility.serialize(serverCommand);
|
|
|
|
jedis.publish("commands.server" + ":" + commandType, serializedCommand);
|
|
|
|
}
|
|
|
|
catch (JedisConnectionException exception)
|
|
|
|
{
|
|
|
|
exception.printStackTrace();
|
|
|
|
writePool.returnBrokenResource(jedis);
|
|
|
|
jedis = null;
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
if (writePool != null)
|
|
|
|
{
|
|
|
|
writePool.returnResource(jedis);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).start();
|
2015-08-06 07:45:39 +02:00
|
|
|
}
|
|
|
|
}
|