283 lines
8.7 KiB
Java
283 lines
8.7 KiB
Java
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.Pair;
|
|
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.bonuses.redis.VotifierCommand;
|
|
import mineplex.database.Tables;
|
|
import mineplex.database.tables.records.BonusRecord;
|
|
import mineplex.serverdata.Region;
|
|
import mineplex.serverdata.Utility;
|
|
import mineplex.serverdata.commands.ServerCommand;
|
|
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<PlayerStatus> _usPlayerRepo;
|
|
private RedisDataRepository<PlayerStatus> _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.loadConfig("us-redis.dat");
|
|
_euConfig = ServerManager.loadConfig("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)
|
|
{
|
|
final Vote vote = event.getVote();
|
|
final String playerName = vote.getUsername();
|
|
|
|
System.out.println("New Vote: " + playerName);
|
|
|
|
runAsync(new Runnable()
|
|
{
|
|
@Override
|
|
public void run()
|
|
{
|
|
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);
|
|
}
|
|
}
|
|
|
|
String lowerPlayerName = playerName.toLowerCase();
|
|
final PlayerStatus usStatus = _usPlayerRepo.getElement(lowerPlayerName);
|
|
final PlayerStatus euStatus = _euPlayerRepo.getElement(lowerPlayerName);
|
|
|
|
System.out.println("Loaded " + playerName + " with uuid " + uuid);
|
|
System.out.println("Attempting to award bonus");
|
|
final UUID finalUuid = uuid;
|
|
awardBonus(playerName, finalUuid, new Callback<Integer>()
|
|
{
|
|
@Override
|
|
public void run(final Integer gems)
|
|
{
|
|
runSync(new Runnable()
|
|
{
|
|
@Override
|
|
public void run()
|
|
{
|
|
if (usStatus != null)
|
|
{
|
|
System.out.println("Found " + playerName + " on US " + usStatus.getServer());
|
|
notifyServer(playerName, gems, Region.US, usStatus.getServer());
|
|
}
|
|
|
|
if (euStatus != null)
|
|
{
|
|
System.out.println("Found " + playerName + " on EU " + euStatus.getServer());
|
|
notifyServer(playerName, gems, Region.EU, euStatus.getServer());
|
|
}
|
|
}
|
|
});
|
|
}
|
|
});
|
|
}
|
|
});
|
|
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, true);
|
|
}
|
|
|
|
private void notifyServer(String playerName, int gems, Region region, String targetServer)
|
|
{
|
|
JedisPool writePool = region == Region.EU ? _euWritePool : _usWritePool;
|
|
|
|
VotifierCommand command = new VotifierCommand(playerName, gems, targetServer);
|
|
publishCommand(command, writePool);
|
|
}
|
|
|
|
private void awardBonus(final String playerName, final UUID uuid, final Callback<Integer> onComplete)
|
|
{
|
|
DSLContext create = DSL.using(DBPool.getAccount(), SQLDialect.MYSQL);
|
|
|
|
Record1<Integer> 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 BonusRecord client = _bonusManager.getRepository().loadRecord(playerName, accountId);
|
|
|
|
final BonusAmount amount = _bonusManager.getVoteBonusAmount(client.getVoteStreak());
|
|
|
|
_bonusManager.getRepository().attemptVoteBonus(accountId, new Callback<Pair<Boolean, Date>>()
|
|
{
|
|
@Override
|
|
public void run(Pair<Boolean, Date> pair)
|
|
{
|
|
if (pair.getLeft())
|
|
{
|
|
// Reward Amount
|
|
if (amount.getTickets() > 0)
|
|
client.setTickets(client.getTickets() + amount.getTickets());
|
|
|
|
if (amount.getTotalGems() > 0)
|
|
{
|
|
_donationManager.RewardGems(new Callback<Boolean>()
|
|
{
|
|
@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.getTotalGems());
|
|
}
|
|
|
|
if (amount.getTotalCoins() > 0)
|
|
{
|
|
_donationManager.RewardCoins(new Callback<Boolean>()
|
|
{
|
|
@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.getTotalCoins());
|
|
}
|
|
|
|
// Check if we need to reset vote streak
|
|
_bonusManager.updateVoteStreak(client);
|
|
client.setVotetime(pair.getRight());
|
|
|
|
// Update Streak
|
|
_bonusManager.incrementVoteStreak(client);
|
|
|
|
client.store();
|
|
System.out.println("Awarded carl ticket to " + playerName);
|
|
onComplete.run(amount.getTotalGems());
|
|
}
|
|
else
|
|
{
|
|
System.out.println(playerName + " attempted to vote, vote bonus returned false!");
|
|
}
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
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();
|
|
}
|
|
} |