package mineplex.votifier; import java.sql.Date; 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.bonuses.BonusAmount; import mineplex.core.bonuses.BonusClientData; import mineplex.core.bonuses.BonusManager; import mineplex.core.common.util.Callback; 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.Record1; 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 BonusManager _bonusManager; private RedisConfig _usConfig; private RedisConfig _euConfig; private RedisDataRepository _usPlayerRepo; private RedisDataRepository _euPlayerRepo; private JedisPool _usWritePool; private JedisPool _euWritePool; public VotifierManager(JavaPlugin plugin, CoreClientManager clientManager, DonationManager donationManager, BonusManager bonusManager) { super("Votifier", plugin); _clientManager = clientManager; _donationManager = donationManager; _bonusManager = bonusManager; _usConfig = ServerManager.getConfig("us-redis.dat"); _euConfig = ServerManager.getConfig("eu-redis.dat"); _usPlayerRepo = new RedisDataRepository(_usConfig.getConnection(true, "DefaultConnection"), _usConfig.getConnection(false, "DefaultConnection"), Region.US, PlayerStatus.class, "playerStatus"); _euPlayerRepo = new RedisDataRepository(_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); if (uuid == null) { System.out.println("Failed to load UUID of " + playerName + " from UUIDFetcher. Trying with database"); uuid = _clientManager.loadUUIDFromDB(playerName); if (uuid == null) { System.out.println("Failed to load UUID from database. Giving up on " + playerName); } } System.out.println("Loaded " + playerName + " with uuid " + uuid); System.out.println("Attempting to award bonus"); awardBonus(playerName, uuid); System.out.println(); System.out.println(); // 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(final String playerName, UUID uuid) { DSLContext create = DSL.using(DBPool.ACCOUNT, SQLDialect.MYSQL); Record1 idRecord = create.select(Tables.accounts.id).from(Tables.accounts).where(Tables.accounts.uuid.eq(uuid.toString())).fetchOne(); if (idRecord != null) { final int accountId = idRecord.value1(); final BonusClientData client = new BonusClientData(_bonusManager.getRepository().loadRecord(playerName, accountId)); final BonusAmount amount = _bonusManager.getVoteBonusAmount(client); // Reward Amount if (amount.getTickets() > 0) client.setTickets(client.getTickets() + amount.getTickets()); if (amount.getGems() > 0) { _donationManager.RewardGems(new Callback() { @Override public void run(Boolean data) { if (data) System.out.println("Gave " + amount.getGems() + " gems to " + playerName); else System.out.println("Failed to give " + amount.getGems() + " gems to " + playerName); } }, "Votifier", playerName, uuid, amount.getGems()); } if (amount.getCoins() > 0) { _donationManager.RewardCoins(new Callback() { @Override public void run(Boolean data) { if (data) System.out.println("Gave " + amount.getGems() + " coins to " + playerName); else System.out.println("Failed to give " + amount.getGems() + " coins to " + playerName); } }, "Votifier", playerName, accountId, amount.getCoins()); } // Update time client.getRecord().setVotetime(new Date(_bonusManager.getSqlTime())); // Update Streak _bonusManager.incrementVoteStreak(client); client.getRecord().store(); System.out.println("Awarded carl ticket to " + playerName); } } 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(); } }