Thread: Lotto amount off

Results 1 to 4 of 4
  1. #1 Lotto amount off 
    Jake from State Farm

    FKN Jake's Avatar
    Join Date
    Nov 2013
    Posts
    675
    Thanks given
    45
    Thanks received
    58
    Rep Power
    153
    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.

    Attached image

    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
    Reply With Quote  
     

  2. #2  
    Theory Wins?
    Greyfield's Avatar
    Join Date
    Nov 2008
    Age
    32
    Posts
    1,585
    Thanks given
    61
    Thanks received
    265
    Rep Power
    310
    It's because your system can't have duplicate entires added to the list. So even though your file may contain all those names, the size is realistically only going to be 3 because there's only three unique names. 3*7,500,000 = 22,500,000 which is the problem you're having.

    The code that's causing this is -
    Code:
    if(!CONTESTERS.contains(line))



    Reply With Quote  
     

  3. #3  
    Jake from State Farm

    FKN Jake's Avatar
    Join Date
    Nov 2013
    Posts
    675
    Thanks given
    45
    Thanks received
    58
    Rep Power
    153
    Quote Originally Posted by Greyfield View Post
    It's because your system can't have duplicate entires added to the list. So even though your file may contain all those names, the size is realistically only going to be 3 because there's only three unique names. 3*7,500,000 = 22,500,000 which is the problem you're having.

    The code that's causing this is -
    Code:
    if(!CONTESTERS.contains(line))
    Alright how to fix? Thanks for explaining but I'm still a tad lost
    Reply With Quote  
     

  4. #4  
    Theory Wins?
    Greyfield's Avatar
    Join Date
    Nov 2008
    Age
    32
    Posts
    1,585
    Thanks given
    61
    Thanks received
    265
    Rep Power
    310
    Quote Originally Posted by Yalu View Post
    Alright how to fix? Thanks for explaining but I'm still a tad lost
    I just told you how, remove the code I posted if you want duplicate entries.



    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Characters/Stats Show-Off
    By Karlis in forum Media
    Replies: 346
    Last Post: 08-11-2013, 02:57 PM
  2. Replies: 2
    Last Post: 06-25-2013, 04:21 AM
  3. AuraScape Icons - On and Off!
    By Zachera in forum Downloads
    Replies: 34
    Last Post: 07-16-2007, 11:27 AM
  4. To make use off the quest tab
    By bluurr in forum Tutorials
    Replies: 4
    Last Post: 06-07-2007, 05:22 PM
  5. Replies: 4
    Last Post: 05-22-2007, 07:28 PM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •