-
-
Save aikar/a71df45f47fc2c88ec2c 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.config.typemap.RupeeTransType; | |
import com.empireminecraft.config.typemap.TokenTransType; | |
import com.empireminecraft.data.EmpireUser; | |
import com.empireminecraft.data.items.PromoItems; | |
import com.empireminecraft.data.items.VotingItems; | |
import com.empireminecraft.systems.currency.Rupees; | |
import com.empireminecraft.features.items.CustomItem; | |
import com.empireminecraft.systems.currency.Tokens; | |
import com.empireminecraft.systems.db.DbRow; | |
import com.empireminecraft.systems.db.DbStatement; | |
import com.empireminecraft.util.TaskChain; | |
import com.empireminecraft.util.TimeUtil; | |
import com.empireminecraft.util.Util; | |
import org.bukkit.Material; | |
import org.bukkit.inventory.ItemStack; | |
import java.util.ArrayList; | |
import java.util.List; | |
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("[vote] Could not validate user name " + player); | |
return; | |
} | |
if (service == null) { | |
Util.logWarning("[vote] 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; | |
int voteTokens; | |
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) { | |
stm.rollback(); | |
Util.log("[vote] Could not find user '" + player + "' for '" + service + "'"); | |
return; | |
} | |
String simpleService = Util.simplifyString(service); | |
userId = rs.get("user_id"); | |
voteBonus = rs.get("votebonus"); | |
lastVote = rs.get("lastvote"); | |
voteMax = rs.get("votemax"); | |
voteMissed = rs.get("votelastmiss"); | |
voteRupees = EmpireConfig.getInt("vote-rewards.rupees." + simpleService, 100); | |
voteTokens = EmpireConfig.getInt("vote-rewards.tokens." + simpleService, 5); | |
timestamp = Util.timestamp(); | |
final EmpireUser user = EmpireUser.getUser(player); | |
if (user == null) { | |
stm.rollback(); | |
return; | |
} | |
player = user.getName(); | |
EmpireMetaKey lastServiceVote = new EmpireMetaKey("vote-last." + simpleService, -1); | |
final Long lastServiceVoteTime = user.getMeta(lastServiceVote).getLong(0L); | |
user.setMeta(lastServiceVote, timestamp); | |
int hoursBetweenVotes = EmpireConfig.getInt("vote-rewards.hours-between-vote", 11); | |
long timeSinceLastServiceVote = timestamp - lastServiceVoteTime; | |
boolean newVoteBonus = false; | |
long timeSinceLastVote = timestamp - lastVote; | |
if (timeSinceLastServiceVote < TimeUtil.HOUR.inSeconds(hoursBetweenVotes)) { | |
voteTokens = 0; | |
if (timeSinceLastServiceVote > TimeUtil.HOUR.inSeconds(1)) { | |
voteRupees = EmpireConfig.getInt("vote-rewards.quickvote-rupees", 50); | |
} else { | |
voteRupees = 0; | |
} | |
} else if (timeSinceLastVote < TimeUtil.HOUR.inSeconds(36)) { | |
Long lastBonus = user.getMeta(EmpireMetaKey.VOTE_LAST_BONUS).getLong(); | |
if (lastBonus == null || timestamp - lastBonus > TimeUtil.HOUR.inSeconds(16)) { | |
voteRupees += 100; | |
voteRupees += Math.min(voteBonus++ * 10, 200); | |
newVoteBonus = true; | |
processRupeeBonus(); | |
processItemBonus(); | |
processTokenBonus(); | |
user.setMeta(EmpireMetaKey.VOTE_LAST_BONUS, timestamp); | |
} else { | |
int minutesPerVote = EmpireConfig.getInt("vote-rewards.minutes-boost-per-vote", 90); | |
user.setMeta(EmpireMetaKey.VOTE_LAST_BONUS, lastBonus - TimeUtil.MINUTE.inSeconds(minutesPerVote)); | |
} | |
} else if (timeSinceLastVote > TimeUtil.DAY.inSeconds(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; | |
user.setMeta(EmpireMetaKey.VOTE_LAST_BONUS, timestamp); | |
} else { | |
// User missed vote-bonus window increase but didn't lose bonus | |
user.setMeta(EmpireMetaKey.VOTE_LAST_BONUS, timestamp); | |
} | |
// Max a user can lose is 10 days worth of bonus | |
voteBonus = Math.max(voteBonus, voteMax - 10); | |
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(); | |
Rupees.credit(userId, voteRupees, RupeeTransType.VOTED.id(), service + " - day bonus: " + voteBonus); | |
Tokens.credit(userId, voteTokens, TokenTransType.VOTED.id(), service + " - day bonus: " + voteBonus); | |
String notify = "&aYou successfully voted on&f " + service + " &a!\n&aEarned &f" + voteRupees + "&a Rupees - &f" + voteTokens + "&a Tokens!"; | |
if (newVoteBonus) { | |
notify += "\n&aYour vote bonus is now &f" + voteBonus + "&a! Keep up the good work!"; | |
} | |
if (user.currentServer != null) { | |
CommandQueue.queueServerCommand(user.currentServer, "updatevote " + user.name + " " + lastVote + " " + voteBonus); | |
} | |
Util.notifyPlayer(player, notify); | |
Util.log("[vote] '" + player + "' voted on '" + service + "' and earned: " + voteRupees + | |
" rupees and " + voteTokens + " tokens with mod:" + voteBonus); | |
} catch (Exception e) { | |
Util.printException("[vote] FAILED for '" + player + "' on '" + service + "'", e); | |
} | |
} | |
private void processTokenBonus() { | |
/* | |
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, "token2")) { | |
voteTokens += 60; | |
} | |
if (shouldGetEvery(5, "token5")) { | |
voteTokens += 150; | |
} | |
if (shouldGetEvery(10, "token10")) { | |
voteTokens += 300; | |
} | |
if (shouldGetEvery(30, "token30")) { | |
voteTokens += 900; | |
} | |
if (shouldGetEvery(50, "token50")) { | |
voteTokens += 1500; | |
} | |
if (shouldGetEvery(100, "token100")) { | |
voteTokens += 3000; | |
} | |
if (shouldGetEvery(150, "token150")) { | |
voteTokens += 4500; | |
} | |
if (shouldGetEvery(200, "token200")) { | |
voteTokens += 6000; | |
} | |
if (shouldGetEvery(365, "token365")) { | |
voteTokens += 10000; | |
} | |
} | |
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() { | |
List<ItemStack> rewards = new ArrayList<>(); | |
/* | |
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")) { | |
rewards.add(CustomItem.newItem(Material.DIAMOND) | |
.qtyRange(1, Math.max(1, (int) Math.min(10, voteBonus / 5))).getDrop()); | |
} | |
if (shouldGetEvery(3, "emerald")) { | |
rewards.add(CustomItem.newItem(Material.EMERALD) | |
.qtyRange(1, Math.max(1, (int) Math.min(10, voteBonus / 5))).getDrop()); | |
} | |
if (shouldGetEvery(20, "vaultVoucher")) { | |
rewards.add(PromoItems.VAULT_VOUCHER); | |
} | |
if (shouldGetEvery(50, "stableVoucher")) { | |
rewards.add(PromoItems.STABLE_VOUCHER); | |
} | |
if (shouldGetAt(20, "votersBoots")) { | |
rewards.add(VotingItems.makeVotingItem(player, VotingItems.VOTERS_BOOTS)); | |
} | |
if (shouldGetAt(30, "votersHead")) { | |
rewards.add(VotingItems.makeVotingItem(player, VotingItems.VOTERS_HEAD)); | |
} | |
if (shouldGetAt(40, "votersLegs")) { | |
rewards.add(VotingItems.makeVotingItem(player, VotingItems.VOTERS_LEGS)); | |
} | |
if (shouldGetAt(60, "votersChest")) { | |
rewards.add(VotingItems.makeVotingItem(player, VotingItems.VOTERS_CHEST)); | |
} | |
if (shouldGetAt(75, "votersIronSword")) { | |
rewards.add(VotingItems.makeVotingItem(player, VotingItems.VOTERS_IRON_SWORD)); | |
} | |
if (shouldGetAt(90, "votersHoe")) { // originally was only 1 so the key didn't have variety | |
rewards.add(VotingItems.makeVotingItem(player, VotingItems.VOTERS_IRON_HOE)); | |
} | |
if (shouldGetAt(100, "tyCertificate")) { | |
rewards.add(VotingItems.makeVotingThankYou(player)); | |
} | |
if (shouldGetAt(115, "votersIronShovel")) { | |
rewards.add(VotingItems.makeVotingItem(player, VotingItems.VOTERS_IRON_SHOVEL)); | |
} | |
if (shouldGetAt(135, "votersIronAxe")) { | |
rewards.add(VotingItems.makeVotingItem(player, VotingItems.VOTERS_IRON_AXE)); | |
} | |
if (shouldGetAt(150, "votersShears")) { | |
rewards.add(VotingItems.makeVotingItem(player, VotingItems.VOTERS_SHEARS)); | |
} | |
if (shouldGetAt(175, "votersIronPickaxe")) { | |
rewards.add(VotingItems.makeVotingItem(player, VotingItems.VOTERS_IRON_PICKAXE)); | |
} | |
if (shouldGetAt(200, "votersBow")) { | |
rewards.add(VotingItems.makeVotingItem(player, VotingItems.VOTERS_BOW)); | |
} | |
if (shouldGetAt(215, "votersDiamondShovel")) { | |
rewards.add(VotingItems.makeVotingItem(player, VotingItems.VOTERS_DIAMOND_SHOVEL)); | |
} | |
if (shouldGetAt(235, "votersDiamondAxe")) { | |
rewards.add(VotingItems.makeVotingItem(player, VotingItems.VOTERS_DIAMOND_AXE)); | |
} | |
if (shouldGetAt(255, "votersDiamondPickaxe")) { | |
rewards.add(VotingItems.makeVotingItem(player, VotingItems.VOTERS_DIAMOND_PICKAXE)); | |
} | |
if (shouldGetAt(275, "votersDiamondSword")) { | |
rewards.add(VotingItems.makeVotingItem(player, VotingItems.VOTERS_DIAMOND_SWORD)); | |
} | |
if (shouldGetAt(290, "votersDiamondHoe")) { | |
rewards.add(VotingItems.makeVotingItem(player, VotingItems.VOTERS_DIAMOND_HOE)); | |
} | |
if (shouldGetAt(300, "votersFishingRod")) { | |
rewards.add(VotingItems.makeVotingItem(player, VotingItems.VOTERS_FISHING_ROD)); | |
} | |
if (!rewards.isEmpty()) { | |
sendSystemMail(player, "Vote Bonus " + voteBonus + " Reward", | |
rewards.toArray(new ItemStack[rewards.size()])); | |
} | |
} | |
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, -1); | |
EmpireUser.getUser(player).setMeta(metaKey, "1"); | |
} | |
private boolean hasMetaKey(String key) { | |
EmpireMetaKey metaKey = new EmpireMetaKey("votereward." + key, -1); | |
return EmpireUser.getUser(player).getMeta(metaKey).getString() != null; | |
} | |
} | |
} |
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.data.items; | |
import com.empireminecraft.features.items.CustomItem; | |
import com.empireminecraft.features.items.CustomItems; | |
import com.empireminecraft.features.items.UsableItem; | |
import com.empireminecraft.util.ItemUtil; | |
import org.bukkit.Material; | |
import org.bukkit.enchantments.Enchantment; | |
import org.bukkit.entity.Player; | |
import org.bukkit.inventory.ItemStack; | |
public class VotingItems { | |
private static final UsableItem VOTING_THANK_YOU = new UsableItem("VotingThankYou", Material.PAPER) { | |
@Override | |
public ItemStack onUse(Player player, ItemStack item) { | |
return null; | |
} | |
}; | |
public static final CustomItem VOTERS_CHEST = new CustomItem(Material.LEATHER_CHESTPLATE) {{ | |
ItemUtil.setName(this, "&fVoters Chest"); | |
addUnsafeEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 3); | |
addUnsafeEnchantment(Enchantment.THORNS, 1); | |
CustomItems.makeWhiteLeather(this); | |
}}; | |
public static final CustomItem VOTERS_LEGS = new CustomItem(Material.LEATHER_LEGGINGS) {{ | |
ItemUtil.setName(this, "&fVoters Legs"); | |
addUnsafeEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 2); | |
CustomItems.makeWhiteLeather(this); | |
}}; | |
public static final CustomItem VOTERS_HEAD = new CustomItem(Material.LEATHER_HELMET) {{ | |
ItemUtil.setName(this, "&fVoters Helmet"); | |
addUnsafeEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1); | |
addUnsafeEnchantment(Enchantment.WATER_WORKER, 5); | |
addUnsafeEnchantment(Enchantment.OXYGEN, 3); | |
CustomItems.makeWhiteLeather(this); | |
}}; | |
public static final CustomItem VOTERS_BOOTS = new CustomItem(Material.LEATHER_BOOTS) {{ | |
ItemUtil.setName(this, "&fVoters Boots"); | |
addUnsafeEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1); | |
addUnsafeEnchantment(Enchantment.PROTECTION_FALL, 5); | |
CustomItems.makeWhiteLeather(this); | |
}}; | |
public static final CustomItem VOTERS_BOW = new CustomItem(Material.BOW) {{ | |
ItemUtil.setName(this, "&fVoters Bow"); | |
addUnsafeEnchantment(Enchantment.ARROW_DAMAGE, 1); | |
addUnsafeEnchantment(Enchantment.ARROW_INFINITE, 1); | |
addUnsafeEnchantment(Enchantment.LOOT_BONUS_MOBS, 3); | |
addUnsafeEnchantment(Enchantment.ARROW_KNOCKBACK, 1); | |
addUnsafeEnchantment(Enchantment.ARROW_FIRE, 1); | |
}}; | |
public static final CustomItem VOTERS_SHEARS = new CustomItem(Material.SHEARS) {{ | |
ItemUtil.setName(this, "&fVoters Shears"); | |
addUnsafeEnchantment(Enchantment.DIG_SPEED, 1); | |
addUnsafeEnchantment(Enchantment.LOOT_BONUS_BLOCKS, 3); | |
}}; | |
public static final CustomItem VOTERS_IRON_SWORD = new CustomItem(Material.IRON_SWORD) {{ | |
ItemUtil.setName(this, "&fVoters Sword"); | |
addUnsafeEnchantment(Enchantment.DAMAGE_ALL, 1); | |
addUnsafeEnchantment(Enchantment.LOOT_BONUS_MOBS, 3); | |
addUnsafeEnchantment(Enchantment.FIRE_ASPECT, 1); | |
}}; | |
public static final CustomItem VOTERS_DIAMOND_SWORD = new CustomItem(Material.DIAMOND_SWORD) {{ | |
ItemUtil.setName(this, "&fVoters Sword"); | |
addUnsafeEnchantment(Enchantment.DAMAGE_ALL, 1); | |
addUnsafeEnchantment(Enchantment.LOOT_BONUS_MOBS, 3); | |
addUnsafeEnchantment(Enchantment.FIRE_ASPECT, 1); | |
}}; | |
public static final CustomItem VOTERS_IRON_HOE = new CustomItem(Material.IRON_HOE) {{ | |
ItemUtil.setName(this, "&fVoters Hoe"); | |
addUnsafeEnchantment(Enchantment.DIG_SPEED, 1); | |
addUnsafeEnchantment(Enchantment.LOOT_BONUS_BLOCKS, 1); | |
}}; | |
public static final CustomItem VOTERS_DIAMOND_HOE = new CustomItem(Material.DIAMOND_HOE) {{ | |
ItemUtil.setName(this, "&fVoters Hoe"); | |
addUnsafeEnchantment(Enchantment.DIG_SPEED, 1); | |
addUnsafeEnchantment(Enchantment.LOOT_BONUS_BLOCKS, 3); | |
}}; | |
public static final CustomItem VOTERS_IRON_PICKAXE = new CustomItem(Material.IRON_PICKAXE) {{ | |
ItemUtil.setName(this, "&fVoters Pickaxe"); | |
addUnsafeEnchantment(Enchantment.DIG_SPEED, 1); | |
addUnsafeEnchantment(Enchantment.LOOT_BONUS_BLOCKS, 3); | |
}}; | |
public static final CustomItem VOTERS_DIAMOND_PICKAXE = new CustomItem(Material.DIAMOND_PICKAXE) {{ | |
ItemUtil.setName(this, "&fVoters Pickaxe"); | |
addUnsafeEnchantment(Enchantment.DIG_SPEED, 1); | |
addUnsafeEnchantment(Enchantment.LOOT_BONUS_BLOCKS, 3); | |
}}; | |
public static final CustomItem VOTERS_IRON_SHOVEL = new CustomItem(Material.IRON_SPADE) {{ | |
ItemUtil.setName(this, "&fVoters Shovel"); | |
addUnsafeEnchantment(Enchantment.DIG_SPEED, 1); | |
addUnsafeEnchantment(Enchantment.LOOT_BONUS_BLOCKS, 3); | |
}}; | |
public static final CustomItem VOTERS_DIAMOND_SHOVEL = new CustomItem(Material.DIAMOND_SPADE) {{ | |
ItemUtil.setName(this, "&fVoters Shovel"); | |
addUnsafeEnchantment(Enchantment.DIG_SPEED, 1); | |
addUnsafeEnchantment(Enchantment.LOOT_BONUS_BLOCKS, 3); | |
}}; | |
public static final CustomItem VOTERS_IRON_AXE = new CustomItem(Material.IRON_AXE) {{ | |
ItemUtil.setName(this, "&fVoters Axe"); | |
addUnsafeEnchantment(Enchantment.DIG_SPEED, 1); | |
addUnsafeEnchantment(Enchantment.LOOT_BONUS_BLOCKS, 3); | |
}}; | |
public static final CustomItem VOTERS_DIAMOND_AXE = new CustomItem(Material.DIAMOND_AXE) {{ | |
ItemUtil.setName(this, "&fVoters Axe"); | |
addUnsafeEnchantment(Enchantment.DIG_SPEED, 1); | |
addUnsafeEnchantment(Enchantment.LOOT_BONUS_BLOCKS, 3); | |
}}; | |
public static final CustomItem VOTERS_FISHING_ROD = new CustomItem(Material.FISHING_ROD) {{ | |
ItemUtil.setName(this, "&fVoters Fishing Rod"); | |
addUnsafeEnchantment(Enchantment.LUCK, 3); | |
addUnsafeEnchantment(Enchantment.LURE, 3); | |
}}; | |
public static ItemStack makeVotingItem(String name, ItemStack item) { | |
item = item.clone(); | |
ItemUtil.buildLore(item) | |
.setName("&r&f" + name + "'s " + ItemUtil.getName(item)) | |
.add(0, "&6&l&nVoting Reward") | |
.makeFinal().makeShiny().makeSoulbound().makeUnbreakable().save(); | |
return item; | |
} | |
public static ItemStack makeVotingThankYou(String name) { | |
ItemStack item = VOTING_THANK_YOU.clone(); | |
ItemUtil.buildLore(item) | |
.setName("&5Thank You to " + name + " from &6Empire Minecraft") | |
.empty() | |
.add("&eThis is a certificate of thanks from the") | |
.add("&6Empire Minecraft &eStaff team for your continued voting") | |
.add("&eefforts on the Empire. Your vote helps us tremendously.") | |
.empty() | |
.add("&eShow this certificate in an item frame with pride!") | |
.empty() | |
.add("&eThank You, &6Empire Minecraft") | |
.makeSoulbound().makeFinal().makeShiny().save(); | |
return item; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment