Mineplex2018-withcommit/Plugins/Mineplex.Votifier/src/mineplex/votifier/VotifierManager.java

158 lines
5.0 KiB
Java
Raw Normal View History

package mineplex.votifier;
import java.util.UUID;
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;
import mineplex.core.common.util.UUIDFetcher;
import mineplex.core.database.DBPool;
import mineplex.core.donation.DonationManager;
import mineplex.core.votifier.VotifierCommand;
import mineplex.database.Tables;
import mineplex.serverdata.Region;
import mineplex.serverdata.Utility;
import mineplex.serverdata.commands.ServerCommand;
import mineplex.serverdata.commands.ServerCommandManager;
import mineplex.serverdata.data.PlayerStatus;
import mineplex.serverdata.redis.RedisConfig;
import mineplex.serverdata.redis.RedisDataRepository;
import mineplex.serverdata.servers.ServerManager;
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;
/**
* Created by shaun on 15-08-05.
*/
public class VotifierManager extends MiniPlugin
{
private CoreClientManager _clientManager;
private DonationManager _donationManager;
private RedisConfig _usConfig;
private RedisConfig _euConfig;
private RedisDataRepository<PlayerStatus> _usPlayerRepo;
private RedisDataRepository<PlayerStatus> _euPlayerRepo;
private JedisPool _usWritePool;
private JedisPool _euWritePool;
public VotifierManager(JavaPlugin plugin, CoreClientManager clientManager, DonationManager donationManager)
{
super("Votifier", plugin);
_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");
_usWritePool = Utility.generatePool(_usConfig.getConnection(true, "DefaultConnection"));
_euWritePool = Utility.generatePool(_euConfig.getConnection(true, "DefaultConnection"));
}
@EventHandler
public void handleVote(VotifierEvent event)
{
Vote vote = event.getVote();
String playerName = vote.getUsername();
System.out.println("New Vote: " + playerName);
// UUID uuid = UUIDFetcher.getUUIDOf(playerName);
// UUID uuid = _clientManager.loadUUIDFromDB(playerName);
// if (uuid != null)
// {
// System.out.println("Found UUID:" + uuid.toString());
// if (playerName.equalsIgnoreCase("Phinary"))
// {
// System.out.println("award bonus");
// awardBonus(uuid);
// }
// }
// 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);
}
private void notifyServer(String playerName, boolean eu)
{
JedisPool writePool = eu ? _euWritePool : _usWritePool;
VotifierCommand command = new VotifierCommand(playerName);
publishCommand(command, writePool);
}
private void awardBonus(UUID uuid)
{
// Don't use this right now!
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();
}
}