Use login query to handle logins

This commit is contained in:
Shaun Bennett 2015-08-13 06:12:11 -05:00
parent e1d04354fb
commit af15476f19
5 changed files with 150 additions and 68 deletions

View File

@ -8,107 +8,111 @@ import mineplex.database.tables.records.BonusRecord;
public class BonusClientData
{
private BonusRecord _record;
private Hologram _hologram;
public BonusClientData(BonusRecord record)
{
_record = record;
}
private int _accountId;
private Timestamp _dailyTime;
private Date _rankTime;
private Date _voteTime;
private int _dailyStreak;
private int _maxDailyStreak;
private int _voteStreak;
private int _maxVoteStreak;
private int _tickets;
public BonusRecord getRecord()
public BonusClientData()
{
return _record;
_accountId = -1;
}
public void setAccountId(Integer value)
{
_record.setAccountId(value);
_accountId = value;
}
public Integer getAccountId()
{
return _record.getAccountId();
return _accountId;
}
public void setDailyTime(Timestamp value)
{
_record.setDailytime(value);
_dailyTime = value;
}
public Timestamp getDailyTime()
{
return _record.getDailytime();
return _dailyTime;
}
public void setRankTime(Date value)
{
_record.setRanktime(value);
_rankTime = value;
}
public Date getRankTime()
{
return _record.getRanktime();
return _rankTime;
}
public void setVoteTime(Date value)
{
_record.setVotetime(value);
_voteTime = value;
}
public Date getVoteTime()
{
return _record.getVotetime();
return _voteTime;
}
public void setDailyStreak(Integer value)
{
_record.setDailyStreak(value);
_dailyStreak = value;
}
public Integer getDailyStreak()
{
return _record.getDailyStreak();
return _dailyStreak;
}
public void setMaxDailyStreak(Integer value)
{
_record.setMaxDailyStreak(value);
_maxDailyStreak = value;
}
public Integer getMaxDailyStreak()
{
return _record.getMaxDailyStreak();
return _maxDailyStreak;
}
public void setVoteStreak(Integer value)
{
_record.setVoteStreak(value);
_voteStreak = value;
}
public Integer getVoteStreak()
{
return _record.getVoteStreak();
return _voteStreak;
}
public void setMaxVoteStreak(Integer value)
{
_record.setMaxVoteStreak(value);
_maxVoteStreak = value;
}
public Integer getMaxVoteStreak()
{
return _record.getMaxVoteStreak();
return _maxVoteStreak;
}
public void setTickets(Integer value)
{
_record.setTickets(value);
_tickets = value;
}
public Integer getTickets()
{
return _record.getTickets();
return _tickets;
}
public Hologram getHologram()

View File

@ -1,6 +1,8 @@
package mineplex.core.bonuses;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
@ -14,6 +16,7 @@ import java.util.TimeZone;
import mineplex.core.MiniClientPlugin;
import mineplex.core.account.CoreClient;
import mineplex.core.account.CoreClientManager;
import mineplex.core.account.ILoginProcessor;
import mineplex.core.account.IQuerylessLoginProcessor;
import mineplex.core.account.event.ClientUnloadEvent;
import mineplex.core.bonuses.redis.VoteHandler;
@ -52,6 +55,7 @@ import mineplex.core.bonuses.gui.SpinGui;
import mineplex.core.poll.PollManager;
import mineplex.serverdata.commands.ServerCommandManager;
import org.jooq.DSLContext;
import org.jooq.SQLDialect;
import org.jooq.impl.DSL;
import org.bukkit.Bukkit;
@ -69,7 +73,7 @@ import org.bukkit.plugin.java.JavaPlugin;
import net.minecraft.server.v1_7_R4.DataWatcher;
import net.minecraft.server.v1_7_R4.PacketPlayOutEntityMetadata;
public class BonusManager extends MiniClientPlugin<BonusClientData> implements IQuerylessLoginProcessor
public class BonusManager extends MiniClientPlugin<BonusClientData> implements ILoginProcessor
{
public static final TimeZone TIMEZONE = TimeZone.getTimeZone("UTC");
@ -276,21 +280,30 @@ public class BonusManager extends MiniClientPlugin<BonusClientData> implements I
public void handleVote(final Player player, final int gemsRecieved)
{
final int accountId = _clientManager.getAccountId(player);
runAsync(new Runnable()
{
@Override
public void run()
{
Get(player).getRecord().refresh();
runSync(new Runnable()
_repository.getClientData(accountId, new Callback<BonusClientData>()
{
@Override
public void run()
public void run(final BonusClientData data)
{
_statsManager.incrementStat(player, "Global.DailyVote", 1);
addPendingExplosion(player, player.getName());
UtilPlayer.message(player, F.main("Carl", "Thanks for voting for Mineplex!"));
UtilPlayer.message(player, F.main("Carl", "You received " + F.elem(gemsRecieved + " Gems") + " and " + F.elem("1 Carl Spinner Ticket") + "!"));
runSync(new Runnable()
{
@Override
public void run()
{
Set(player, data);
_statsManager.incrementStat(player, "Global.DailyVote", 1);
addPendingExplosion(player, player.getName());
UtilPlayer.message(player, F.main("Carl", "Thanks for voting for Mineplex!"));
UtilPlayer.message(player, F.main("Carl", "You received " + F.elem(gemsRecieved + " Gems") + " and " + F.elem("1 Carl Spinner Ticket") + "!"));
}
});
}
});
}
@ -434,7 +447,7 @@ public class BonusManager extends MiniClientPlugin<BonusClientData> implements I
// This calculates the the next daily bonus, IT HAS TO MATCH THE MYSQL STORED FUNCTION.
public long nextDailyBonus(Player player)
{
Timestamp timestamp = Get(player).getRecord().getDailytime();
Timestamp timestamp = Get(player).getDailyTime();
if (timestamp == null)
return 0;
@ -506,7 +519,7 @@ public class BonusManager extends MiniClientPlugin<BonusClientData> implements I
// This calculates the the next rank bonus, IT HAS TO MATCH THE MYSQL STORED FUNCTION.
public long nextRankBonus(Player player)
{
Date date = Get(player).getRecord().getRanktime();
Date date = Get(player).getRankTime();
if (date == null)
return 0;
@ -532,16 +545,11 @@ public class BonusManager extends MiniClientPlugin<BonusClientData> implements I
}
}
public void updateVoteStreak(Player player)
public void updateVoteStreak(BonusRecord client)
{
updateVoteStreak(Get(player));
}
public void updateVoteStreak(BonusClientData client)
{
if (client.getVoteStreak() > 0 && client.getVoteTime() != null)
if (client.getVoteStreak() > 0 && client.getVotetime() != null)
{
long lastBonus = getLocalTime(client.getVoteTime().getTime());
long lastBonus = getLocalTime(client.getVotetime().getTime());
long timeLeft = getStreakTimeRemaining(lastBonus, BonusManager.VOTE_STREAK_RESET_TIME);
if (timeLeft < 0)
@ -561,7 +569,7 @@ public class BonusManager extends MiniClientPlugin<BonusClientData> implements I
data.setMaxDailyStreak(data.getDailyStreak());
}
public void incrementVoteStreak(BonusClientData client)
public void incrementVoteStreak(BonusRecord client)
{
client.setVoteStreak(client.getVoteStreak() + 1);
@ -609,10 +617,8 @@ public class BonusManager extends MiniClientPlugin<BonusClientData> implements I
return multiplyer;
}
public int getVoteMultiplyer(BonusClientData client)
public int getVoteMultiplyer(int streak)
{
int streak = client.getVoteStreak();
int multiplyer = Math.min(100, 5 * streak);
if (streak >= 20) multiplyer += (1 * (streak - 40));
return multiplyer;
@ -639,12 +645,12 @@ public class BonusManager extends MiniClientPlugin<BonusClientData> implements I
public BonusAmount getVoteBonusAmount(Player player)
{
return getVoteBonusAmount(Get(player));
return getVoteBonusAmount(Get(player).getVoteStreak());
}
public BonusAmount getVoteBonusAmount(BonusClientData client)
public BonusAmount getVoteBonusAmount(int voteStreak)
{
double mult = getVoteMultiplyer(client) / 100.0;
double mult = getVoteMultiplyer(voteStreak) / 100.0;
BonusAmount amount = new BonusAmount();
amount.setTickets(1);
@ -949,9 +955,7 @@ public class BonusManager extends MiniClientPlugin<BonusClientData> implements I
@Override
protected BonusClientData AddPlayer(String player)
{
// Should never be called!
BonusRecord record = new BonusRecord();
return new BonusClientData(record);
return new BonusClientData();
}
public BonusRepository getRepository()
@ -978,6 +982,16 @@ public class BonusManager extends MiniClientPlugin<BonusClientData> implements I
if (clientData.getHologram() != null)
clientData.getHologram().stop();
// Save streaks
runAsync(new Runnable()
{
@Override
public void run()
{
_repository.saveStreak(clientData.getAccountId(), clientData);
}
});
// This shouldnt be necessary anymore
// runAsync(new Runnable()
// {
@ -991,14 +1005,6 @@ public class BonusManager extends MiniClientPlugin<BonusClientData> implements I
super.UnloadPlayer(event);
}
@Override
public void processLogin(String playerName, int accountId)
{
BonusRecord record = _repository.loadRecord(playerName, accountId);
BonusClientData clientData = new BonusClientData(record);
Set(playerName, clientData);
}
public void addPendingExplosion(Player player, Object obj)
{
_pendingExplosions.add(obj);
@ -1131,4 +1137,16 @@ public class BonusManager extends MiniClientPlugin<BonusClientData> implements I
return true;
}
@Override
public void processLoginResultSet(String playerName, int accountId, ResultSet resultSet) throws SQLException
{
Set(playerName, _repository.loadData(accountId, resultSet));
}
@Override
public String getQuery(int accountId, String uuid, String name)
{
return "SELECT * FROM bonus WHERE accountId = '" + accountId + "';";
}
}

View File

@ -18,15 +18,18 @@ import mineplex.core.common.util.UtilPlayer;
import mineplex.core.database.DBPool;
import mineplex.core.database.RepositoryBase;
import mineplex.core.database.ResultSetCallable;
import mineplex.core.database.column.ColumnInt;
import mineplex.core.donation.DonationManager;
import mineplex.core.recharge.Recharge;
import mineplex.database.Tables;
import mineplex.database.tables.records.BonusRecord;
import org.jooq.Configuration;
import org.jooq.DSLContext;
import org.jooq.Record2;
import org.jooq.SQLDialect;
import org.jooq.TableField;
import org.jooq.impl.DSL;
import org.jooq.impl.DefaultConfiguration;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
@ -68,6 +71,40 @@ public class BonusRepository extends RepositoryBase
return record;
}
public BonusClientData loadData(final int accountId, ResultSet resultSet) throws SQLException
{
BonusClientData clientData = new BonusClientData();
clientData.setAccountId(accountId);
boolean foundClient = false;
while (resultSet.next())
{
foundClient = true;
clientData.setDailyTime(resultSet.getTimestamp(2));
clientData.setRankTime(resultSet.getDate(3));
clientData.setVoteTime(resultSet.getDate(4));
clientData.setDailyStreak(resultSet.getInt(5));
clientData.setMaxDailyStreak(resultSet.getInt(6));
clientData.setVoteStreak(resultSet.getInt(7));
clientData.setMaxVoteStreak(resultSet.getInt(8));
clientData.setTickets(resultSet.getInt(9));
}
if (!foundClient)
{
_manager.runAsync(new Runnable()
{
@Override
public void run()
{
executeInsert("INSERT IGNORE INTO bonus (accountId) VALUES (" + accountId + ")", null);
}
});
}
return clientData;
}
public void getDailyStreakRecord(Callback<StreakRecord> callback)
{
getStreakRecord(Tables.bonus.maxDailyStreak, callback);
@ -170,8 +207,6 @@ public class BonusRepository extends RepositoryBase
final Timestamp timeStamp = callableStatement.getTimestamp(5);
_manager.Get(player).getRecord().update();
Bukkit.getScheduler().runTask(plug, new Runnable()
{
@Override
@ -373,6 +408,30 @@ public class BonusRepository extends RepositoryBase
});
}
public void getClientData(final int accountId, final Callback<BonusClientData> callback)
{
String query = "SELECT * FROM bonus WHERE accountId = '" + accountId + "';";
executeQuery(query, new ResultSetCallable()
{
@Override
public void processResultSet(ResultSet resultSet) throws SQLException
{
BonusClientData clientData = loadData(accountId, resultSet);
callback.run(clientData);
}
});
}
public void saveStreak(int accountId, BonusClientData clientData)
{
jooq().update(Tables.bonus).set(Tables.bonus.dailyStreak, clientData.getDailyStreak())
.set(Tables.bonus.maxDailyStreak, clientData.getMaxDailyStreak())
.set(Tables.bonus.voteStreak, clientData.getVoteStreak())
.set(Tables.bonus.maxVoteStreak, clientData.getMaxVoteStreak())
.where(Tables.bonus.accountId.eq(accountId)).execute();
}
@Override
protected void initialize()
{

View File

@ -130,7 +130,7 @@ public class VoteButton implements GuiItem, Listener {
lore.add(" ");
lore.add(C.cYellow + "Current Streak: " + C.cWhite + client.getVoteStreak());
lore.add(C.cYellow + "Streak Bonus: " + C.cWhite + "+" + _bonusManager.getVoteMultiplyer(client) + "%");
lore.add(C.cYellow + "Streak Bonus: " + C.cWhite + "+" + _bonusManager.getVoteMultiplyer(client.getVoteStreak()) + "%");
if (client.getVoteTime() != null)
{
long lastBonus = _bonusManager.getLocalTime(client.getVoteTime().getTime());

View File

@ -20,6 +20,7 @@ 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;
@ -184,9 +185,9 @@ public class VotifierManager extends MiniPlugin
if (idRecord != null)
{
final int accountId = idRecord.value1();
final BonusClientData client = new BonusClientData(_bonusManager.getRepository().loadRecord(playerName, accountId));
final BonusRecord client = _bonusManager.getRepository().loadRecord(playerName, accountId);
final BonusAmount amount = _bonusManager.getVoteBonusAmount(client);
final BonusAmount amount = _bonusManager.getVoteBonusAmount(client.getVoteStreak());
_bonusManager.getRepository().attemptVoteBonus(accountId, new Callback<Pair<Boolean, Date>>()
{
@ -231,12 +232,12 @@ public class VotifierManager extends MiniPlugin
// Check if we need to reset vote streak
_bonusManager.updateVoteStreak(client);
client.getRecord().setVotetime(pair.getRight());
client.setVotetime(pair.getRight());
// Update Streak
_bonusManager.incrementVoteStreak(client);
client.getRecord().store();
client.store();
System.out.println("Awarded carl ticket to " + playerName);
onComplete.run(amount.getTotalGems());
}