Alright so the lottery is off by a bit..
There is currently at 105m, there are 14 contesters in Lottery.txt they enter 10m and server takes 2.5m, 14 * 7,500,000 = 105m. For some reason something is off, It's been initiated in GameLoader, I'm a bit confused.
GameLoader:
Code:
serviceLoader.execute(() -> Lottery.init());
Lottery Class:
Code:
public class Lottery {
/**
* The list holding all users who have entered the lottery.
*/
private static final List<String> CONTESTERS = new ArrayList<String>();
/*
* The time before lottery in periodically announced globally.
*/
/*
* The timer.
*/
private static final int TIME = 6000000; //60 minutes
public static Stopwatch timer = new Stopwatch().reset();
/*
* The location to the Lottery file where users are saved.
*/
private static final File CONTESTERS_FILE_LOCATION = new File("./data/saves/lottery/lottery.txt");
/*
* The location to the Lottery file where the winners are saved.
*/
private static final File LAST_WINNER_FILE_LOCATION = new File("./data/saves/lottery/lotterywin.txt");
/*
* Can players enter the lottery right now?
*/
private static boolean LOTTERY_ENABLED = true;
/*
* The amount of coins required to enter the lottery.
*/
private static final int PRICE_TO_ENTER = 10000000;
/*
* Get's the amount of gold people have put in the pot.
*/
public static void sequence() {
if(timer.elapsed(TIME)) {
timer.reset();
{
World.sendMessage("[@red@Lottery@bla@]@blu@ The Lottery is currently at "+Misc.insertCommasToNumber(""+getPot()+"")+" coins.");
World.savePlayers();
//Lottery.restartLottery();
}
}}
public static final int getPot() {
if(CONTESTERS.size() == 0) {
return 0;
}
return (CONTESTERS.size() * (PRICE_TO_ENTER - 2500000));
}
/*
* The user who won the Lottery last
*/
private static String LAST_WINNER = "Jake";
public static String getLastWinner() {
return LAST_WINNER;
}
/*
* Has the last week's winner been rewarded?
*/
private static boolean LAST_WINNER_REWARDED = true;
/**
* Gets a random winner for the lottery.
* @return A random user who has won the lottery.
*/
public static String getRandomWinner() {
String winner = null;
int listSize = CONTESTERS.size();
if(listSize >= 4)
winner = CONTESTERS.get(Misc.getRandom(listSize - 1));
return winner;
}
/**
* Handles a player who wishes to enter the lottery.
* @param p The player who wants to enter the lottery.
*/
public static void enterLottery(Player p) {
if(!LOTTERY_ENABLED) {
p.getPacketSender().sendInterfaceRemoval().sendMessage("The lottery is currently not active. Try again soon!");
return;
}
boolean usePouch = p.getMoneyInPouch() >= PRICE_TO_ENTER;
if(p.getInventory().getAmount(995) < PRICE_TO_ENTER) {
p.getPacketSender().sendInterfaceRemoval().sendMessage("The lottery for this week costs "+Misc.insertCommasToNumber(""+PRICE_TO_ENTER+"")+" coins to enter.");
return;
}
if(usePouch) {
p.setMoneyInPouch(p.getMoneyInPouch() - PRICE_TO_ENTER);
p.getPacketSender().sendString(8135, ""+p.getMoneyInPouch());
} else
p.getInventory().delete(995, PRICE_TO_ENTER);
p.getAchievementAttributes().setCoinsGambled(p.getAchievementAttributes().getCoinsGambled() + PRICE_TO_ENTER);
addToLottery(p.getUsername());
p.getPacketSender().sendMessage("You have entered the lottery!").sendMessage("A winner is announced when the lottery hits 250m.");
DialogueManager.start(p, 18);
Achievements.finishAchievement(p, AchievementData.ENTER_THE_LOTTERY);
Achievements.doProgress(p, AchievementData.ENTER_THE_LOTTERY_THREE_TIMES);
Achievements.doProgress(p, AchievementData.ENTER_THE_LOTTERY_50_TIMES);
}
/**
* Adds a user to the lottery by writing their username to the file aswell as adding them to the list of users
* who have entered already.
* @param user The username to add to the lists.
*/
public static void addToLottery(String user) {
CONTESTERS.add(user);
GameServer.getLoader().getEngine().submit(() -> {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(CONTESTERS_FILE_LOCATION, true));
writer.write(""+user+"");
writer.newLine();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
});
}
/**
* Reads the lottery list and adds every user from the .txt files to the lists.
*/
public static void init() {
try {
BufferedReader r = new BufferedReader(new FileReader(CONTESTERS_FILE_LOCATION));
while(true) {
String line = r.readLine();
if(line == null) {
break;
} else {
line = line.trim();
}
if(line.length() > 0) {
if(!CONTESTERS.contains(line))
CONTESTERS.add(line);
}
}
r.close();
BufferedReader r2 = new BufferedReader(new FileReader(LAST_WINNER_FILE_LOCATION));
while(true) {
String line = r2.readLine();
if(line == null) {
break;
} else {
line = line.trim();
}
if(line.length() > 0) {
if(!line.contains("NOT REWARDED. NEEDS REWARD!"))
LAST_WINNER = line;
else
LAST_WINNER_REWARDED = false;
}
}
r2.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Restarts the lottery and rewards this week's winner.
*/
public static void restartLottery() {
if(!LOTTERY_ENABLED)
return;
try {
String winner = getRandomWinner();
if(winner != null) {
LAST_WINNER = winner;
Player player = World.getPlayerByName(winner);
BufferedWriter writer = new BufferedWriter(new FileWriter(LAST_WINNER_FILE_LOCATION));
writer.write(winner);
writer.newLine();
if(player != null) {
rewardPlayer(player, true);
} else {
LAST_WINNER_REWARDED = false;
writer.write("NOT REWARDED. NEEDS REWARD!");
System.out.println("Player "+winner+" won the lottery but wasn't online.");
}
CONTESTERS.clear();
writer.close();
writer = new BufferedWriter(new FileWriter(CONTESTERS_FILE_LOCATION));
writer.write("");
writer.close();
} else
System.out.println("");
} catch(Exception e) {
e.printStackTrace();
}
}
/**
* Rewards a player with items for winning the lottery.
* @param player The player to reward
* @param ignore Should a check be ignored?
* @throws IOException Throws exceptions
*/
public static void rewardPlayer(Player player, boolean ignore) throws IOException {
if((!LAST_WINNER_REWARDED || ignore) && LAST_WINNER.equalsIgnoreCase(player.getUsername())) {
LAST_WINNER_REWARDED = true;
player.setMoneyInPouch(player.getMoneyInPouch() + getPot());
player.getPacketSender().sendString(8135, ""+player.getMoneyInPouch());
player.getPacketSender().sendMessage("You've won the lottery for this week! Congratulations!");
player.getPacketSender().sendMessage("The reward has been added to your money pouch.");
BufferedWriter writer = new BufferedWriter(new FileWriter(LAST_WINNER_FILE_LOCATION));
writer.write(player.getUsername());
writer.close();
World.sendMessage("<col=FFFF00>"+player.getUsername()+" got "+getPot()+" from winning the lottery!");
}
}
/**
* Handles the lottery for a player on login
* Checks if a user won the lottery without being rewarded.
* @param p The player to handle login for.
*/
public static void onLogin(Player p) {
try {
rewardPlayer(p, false);
} catch(Exception e) {
e.printStackTrace();
}
}
Lottery.txt:
Code:
Pwnedef
Pwnedef
Dark
Dark
Dark
Dark
Dark
Lightmyblunt
Lightmyblunt
Lightmyblunt
Lightmyblunt
Lightmyblunt
Lightmyblunt
Lightmyblunt