-
-
Save aikar/7097b6c1c14e2b44b7a7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.empireminecraft.systems; | |
import com.empireminecraft.config.EmpireConfig; | |
import com.empireminecraft.config.meta.EmpireMetaKey; | |
import com.empireminecraft.data.EmpireUser; | |
import com.empireminecraft.data.items.PromoItems; | |
import com.empireminecraft.data.items.VotingItems; | |
import com.empireminecraft.features.EmpireRupees; | |
import com.empireminecraft.features.items.CustomItem; | |
import com.empireminecraft.systems.db.DbRow; | |
import com.empireminecraft.systems.db.DbStatement; | |
import com.empireminecraft.util.TaskChain; | |
import com.empireminecraft.util.Util; | |
import org.bukkit.Material; | |
import java.util.Calendar; | |
import java.util.GregorianCalendar; | |
import static com.empireminecraft.features.Mail.sendSystemMail; | |
public class Voting { | |
public static void creditVote(final String player, final String service) { | |
if (!EmpireUser.validateUsername(player)) { | |
Util.log("Could not validate user name " + player); | |
return; | |
} | |
if (service == null) { | |
Util.logWarning("Player " + player + " voted with unknown service"); | |
return; | |
} | |
TaskChain.newChain().add(new TaskChain.GenericTask() { | |
@Override | |
protected void run() { | |
new VotingRewards(player, service); | |
} | |
}).execute(); | |
} | |
static class VotingRewards { | |
Long userId; | |
Long voteBonus; | |
Long lastVote; | |
Long voteMax; | |
Long voteMissed; | |
Long yesterday; | |
Long midnight; | |
Long timestamp; | |
int voteRupees; | |
String player; | |
String service; | |
VotingRewards(String name, String service) { | |
player = name; | |
this.service = service; | |
try (DbStatement stm = new DbStatement()) { | |
stm.startTransaction(); | |
stm.query("SELECT " + | |
"lastvote, votebonus,voteitembonus, " + | |
"user_id, votemax,votelastmiss " + | |
"FROM user WHERE name = ? FOR UPDATE"); | |
stm.execute(player); | |
DbRow rs = stm.getNextRow(); | |
if (rs == null) { | |
Util.log("[vote] Could not find user '" + player + "' for '" + service + "'"); | |
return; | |
} | |
userId = rs.get("user_id"); | |
voteBonus = rs.get("votebonus"); | |
lastVote = rs.get("lastvote"); | |
voteMax = rs.get("votemax"); | |
voteMissed = rs.get("votelastmiss"); | |
voteRupees = EmpireConfig.get().getInt("vote-rewards." + Util.simplifyString(service), 100); | |
timestamp = System.currentTimeMillis() / 1000; | |
if (timestamp - lastVote < (60*60*36)) { | |
final EmpireUser user = EmpireUser.getUser(player); | |
player = user.getName(); | |
final Long lastBonus = user.getMeta(EmpireMetaKey.VOTE_LAST_BONUS).getLong(); | |
if (lastBonus == null || timestamp - lastBonus > (60*60*16)) { | |
voteRupees += 100; | |
voteRupees += Math.min(voteBonus++ * 10, 500); | |
if (voteBonus > voteMax) { | |
processRupeeBonus(); | |
processItemBonus(); | |
} | |
user.setMeta(EmpireMetaKey.VOTE_LAST_BONUS, timestamp); | |
} | |
} else if (lastVote < timestamp - (60 * 60 * 24 * 3)) { // 3 day window before losing streak | |
long daysMissed = (long) Math.floor((timestamp - lastVote) / (60 * 60 * 24)); | |
voteBonus = Math.max(0, voteBonus - daysMissed); | |
voteMissed = timestamp; | |
} | |
lastVote = timestamp; | |
stm.query("UPDATE user SET votes = votes + 1, " | |
+ " lastvote = ?, votebonus = ?, votemax = ?, votelastmiss = ? WHERE user_id = ?"); | |
stm.executeUpdate( | |
lastVote, | |
voteBonus, | |
Math.max(voteBonus, voteMax), | |
voteMissed, | |
userId); | |
stm.commit(); | |
EmpireRupees.credit(userId, voteRupees, 31, service + " - day bonus: " + voteBonus); | |
Util.notifyPlayer(player, "&aYou successfully voted on&f " + service + " &aand earned &f" + voteRupees + "&a rupees!"); | |
if (voteBonus > voteMax) { | |
Util.notifyPlayer(player, "&aYour vote bonus is now &f" + voteBonus + "&a! Keep up the good work!"); | |
} | |
Util.log("[vote] '" + player + "' voted on '" + service + "' and earned: " + voteRupees + | |
" rupees with mod:" + voteBonus); | |
} catch (Exception e) { | |
Util.printException("[vote] FAILED for '" + player + "' on '" + service + "'", e); | |
} | |
} | |
private void processRupeeBonus() { | |
/* | |
DO NOT add any other logic after shouldGet* - do it BEFORE. | |
If one of the shouldGetItem returns true, it must deliver item. | |
*/ | |
if (shouldGetEvery(5, "rupee5")) { | |
voteRupees += 300; | |
} | |
if (shouldGetEvery(15, "rupee15")) { | |
voteRupees += 1000; | |
} | |
if (shouldGetEvery(30, "rupee30")) { | |
voteRupees += 4000; | |
} | |
if (shouldGetEvery(45, "rupee45")) { | |
voteRupees += 7500; | |
} | |
if (shouldGetEvery(50, "rupee50")) { | |
voteRupees += 25000; | |
} | |
if (shouldGetEvery(100, "rupee100")) { | |
voteRupees += 40000; | |
} | |
} | |
private void processItemBonus() { | |
String str = "Vote Bonus " + voteBonus + " Reward"; | |
/* | |
DO NOT add any other logic after shouldGet* - do it BEFORE. | |
If one of the shouldGetItem returns true, it must deliver item. | |
*/ | |
if (shouldGetEvery(2, "diamond")) { | |
sendSystemMail(player, str, CustomItem.newItem(Material.DIAMOND) | |
.withRange(1, Math.max(1, (int) Math.min(10, voteBonus / 5))).getDrop(0)); | |
} | |
if (shouldGetEvery(3, "emerald")) { | |
sendSystemMail(player, str, CustomItem.newItem(Material.EMERALD) | |
.withRange(1, Math.max(1, (int) Math.min(10, voteBonus / 5))).getDrop(0)); | |
} | |
if (shouldGetEvery(20, "vaultVoucher")) { | |
sendSystemMail(player, str, PromoItems.VAULT_VOUCHER); | |
} | |
if (shouldGetEvery(50, "stableVoucher")) { | |
sendSystemMail(player, str, PromoItems.STABLE_VOUCHER); | |
} | |
if (shouldGetAt(20, "votersBoots")) { | |
sendSystemMail(player, str, VotingItems.makeVotingItem(player, VotingItems.VOTERS_BOOTS)); | |
} | |
if (shouldGetAt(30, "votersHead")) { | |
sendSystemMail(player, str, VotingItems.makeVotingItem(player, VotingItems.VOTERS_HEAD)); | |
} | |
if (shouldGetAt(40, "votersLegs")) { | |
sendSystemMail(player, str, VotingItems.makeVotingItem(player, VotingItems.VOTERS_LEGS)); | |
} | |
if (shouldGetAt(60, "votersChest")) { | |
sendSystemMail(player, str, VotingItems.makeVotingItem(player, VotingItems.VOTERS_CHEST)); | |
} | |
if (shouldGetAt(100, "tyCertificate")) { | |
sendSystemMail(player, str, VotingItems.makeVotingThankYou(player)); | |
} | |
} | |
private boolean shouldGetEvery(int i, String key) { | |
if (shouldGetAt(i, key)) { | |
return true; | |
} else if ((voteBonus % i == 0) && !hasMetaKey(key + voteBonus)) { | |
setMetaKey(key + voteBonus); | |
return true; | |
} | |
return false; | |
} | |
private boolean shouldGetAt(int i, String key) { | |
if (voteBonus >= i && !hasMetaKey(key + i)) { | |
setMetaKey(key + i); | |
return true; | |
} | |
return false; | |
} | |
private void setMetaKey(String key) { | |
EmpireMetaKey metaKey = new EmpireMetaKey("votereward." + key, 0); | |
EmpireUser.getUser(player).setMeta(metaKey, "1"); | |
} | |
private boolean hasMetaKey(String key) { | |
EmpireMetaKey metaKey = new EmpireMetaKey("votereward." + key, 0); | |
return EmpireUser.getUser(player).getMeta(metaKey).getString() != null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Aikar: Here are some suggestions for improving the code if you are interested. The changes I made are tagged with comments that start with "**"
https://gist.github.com/mangstadt/88398851bcd3f0e88c4d
Thanks,
shavingfoam