Thread: [OSRS]Calculate total amount of gold in economy

Results 1 to 1 of 1
  1. #1 [OSRS]Calculate total amount of gold in economy 
    Extreme Donator

    mrmanager's Avatar
    Join Date
    Feb 2017
    Posts
    48
    Thanks given
    20
    Thanks received
    42
    Rep Power
    236
    I just wrote this program for calculating the total amount of gold in the eco. Sorry if I'm missing anything, I'm new.

    This was written for vencillo/divine reality base, but with very slight modification, this will work with PI as well.

    Accounts for both coins and platinum tokens, and checks the players' inventory, bank, and looting bag.

    Everything you need to know is described in my comments. Enjoy!

    Code:
    /**
     * Program for finding the amount of gold in the eco
     *
     * @author https://www.rune-server.ee/members/mrmanager/
     */
    public class CommandUtils {
    	
    	/**The total amount of gold*/
    	private static long goldInEco;
    	/**The gold which we have recently calculated*/
    	private static long recentGold;
    	/**The time of our last calculation*/
    	private static long lastFileUpdate;
    	/**A list of character files which have been updated since our last calculation*/
    	private static List<File> recentChars = new ArrayList<>();
    	
    	/**The path of the file we will be saving our info in*/
    	private static final String SAVE_PATH = "your_path_here";
    	/**The path of our character file directory*/
    	private static final String CHAR_PATH = "your_path_here";
    	
    	/**Fields representing the item ID of coins and platinum tokens*/
    	private static final int COINS = 995;
    	private static final int TOKENS = 13204;
    	
    	/**How much 1 platinum token is worth*/
    	private static final int TOKEN_MODIFIER = 1000;
    
    	/**
    	 * Our method for loading the info in from the file
    	 */
    	private static void load() {
    		try {
    			BufferedReader in = new BufferedReader(new FileReader(SAVE_PATH));
    			String line;
    			while ((line = in.readLine()) != null) {
    				if (line.startsWith("gold: ")) {
    					goldInEco = Long.parseLong(line.split(": ")[1]);
    				} else if (line.startsWith("recent gold: ")) {
    					recentGold = Long.parseLong(line.split(": ")[1]);
    				} else if (line.startsWith("time: ")) {
    					lastFileUpdate = Long.parseLong(line.split(": ")[1]);
    				}
    			}
    			in.close();
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    
    	/**
    	 * Our method for writing the info to the file
    	 */
    	private static void save() {
    		try {
    			BufferedWriter out = new BufferedWriter(new FileWriter(SAVE_PATH));
    			out.write("gold: " + goldInEco);
    			out.newLine();
    			out.write("recently added: " + recentGold);
    			out.newLine();
    			out.write("time: " + lastFileUpdate);
    			out.close();
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    	
    	/**
    	 * Finds the total amount of gold in the eco. Only searches character files which have been updated since the last search.
    	 * Checks the players' inventory, bank, and looting bag.
    	 * Call this method in a command, and in the servers main method
    	 * @return the amount of gold in the economy, formatted nicely with commas
    	 */
    	public static String moneyInEco() {
    		/**
    		 * We first load the latest data from the file, and then reset/remove the results of our latest calculation,
    		 * as we will be calculating it again now, and would otherwise keep adding the same results over and over
    		 */
    		load();
    		goldInEco -= recentGold;
    		recentGold = 0;
    		
    		/**
    		 * We create an array of every character file, and then filter it into our recently updated list
    		 * by checking if the time of last modification is greater than the time of our last calculation
    		 */
    		File[] characters = new File(CHAR_PATH).listFiles();
    		for (File file: characters) {
    			if (file.lastModified() > lastFileUpdate)
    				recentChars.add(file);
    		}
    		
    		System.out.println("total size: " + characters.length);
    		System.out.println("recent size: " + recentChars.size());
    		
    		/**Update the last calculation time*/
    		lastFileUpdate = System.currentTimeMillis();
    		
    		/**Initialize the array of players we will be using from the list of files we filtered*/
    		Player[] players = new Player[recentChars.size()];
    		for (int i = 0; i < players.length; i++) {
    			String path = recentChars.get(i).toString();
    			players[i] = new Player(path.substring(path.lastIndexOf("/")+1).replace(".txt", ""));
    			PlayerSerialization.loadGame(players[i], players[i].getUsername(), "", true);
    		}
    		
    		/**Clear the list of recent files to prepare for the next method call*/
    		recentChars.clear();
    		
    		/**Booleans we will use to know when we are done calculating*/
    		boolean coins = false, tokens = false;
    		
    		/**Looping through the array of players*/
    		for (Player p : players) {
    			
    			/**If the player doesn't have spawn*/
    			if (!p.isSuperAdministrator()) {
    				
    				/**Calculate the players inventory worth*/
    				for (int i = 0; i < p.playerItems.length; i++) {
    					/**If we have found coins, it to our recently calculated sum.
    					 * If we have already found coins, no need to check the next item because coins stack*/
    					if (!coins && p.playerItems[i] == COINS + 1) { 
    						recentGold += p.playerItemsN[i]; coins = true;
    					}
    					/**If we have found platinum tokens, add it to our recently calculated sum
    					 * 1 platinum token is worth 1000 coins.
    					 * If we have already found tokens, no need to check the next item because tokens stack*/
    					if (!tokens && p.playerItems[i] == TOKENS + 1) {
    						recentGold += (Long.parseLong(""+p.playerItemsN[i])*TOKEN_MODIFIER); tokens = true;
    					}
    					/**If we have found both coins and tokens, leave the players inventory*/
    					if (coins && tokens)  break;
    				}
    				
    				/**Reset our booleans to prep for checking bank*/
    				coins = tokens = false;
    				
    				/**Calculate the players bank worth*/
    				for (int i = 0; i < p.getBank().getBankTab().length; i++) {
    					/**If the current bank tab doesn't have any coins or platinum tokens, we can skip it*/
    					if (!(p.getBank().getBankTab(i).contains(new BankItem(COINS)) && p.getBank().getBankTab(i).contains(new BankItem(TOKENS))))
    						continue;
    					/**If the current tab has coins, add the amount to our recently calculated sum
    					 * If we have already found coins, no need to check the next tab because coins stack*/
    					if (!coins && p.getBank().getBankTab(i).contains(new BankItem(COINS))){
    						recentGold += p.getBank().getBankTab(i).getItemAmount(new BankItem(COINS)); coins = true;
    					}
    					/**If the current tab has tokens, add the amount*TOKEN_MODIFIER to our recently calculated sum
    					 * If we have already found tokens, no need to check the next tab because tokens stack*/
    					if (!tokens && p.getBank().getBankTab(i).contains(new BankItem(TOKENS))){
    						recentGold += Long.parseLong(""+(p.getBank().getBankTab(i).getItemAmount(new BankItem(TOKENS)))*TOKEN_MODIFIER); tokens = true;
    					}
    					/**If we have found both coins and tokens, leave the players bank*/
    					if (coins && tokens) break;
    				}
    				
    				/**Reset our booleans to prep for checking looting bag*/
    				coins = tokens = false;
    				
    				/**Calculate the players looting bag worth*/
    				for (int i = 0; i < p.resourceItemId.length; i++) {
    					/**If we have found coins, add the amount to our recently calculated sum*/
    					if (!coins && p.resourceItemId[i] == COINS) {
    						recentGold += p.resourceItemAmount[i]; coins = true;
    					}
    					/**If we have found tokens, add the worth to our recently calculated sum*/
    					if (!tokens && p.resourceItemId[i] == TOKENS) {
    						recentGold += (Long.parseLong(""+p.resourceItemAmount[i])*TOKEN_MODIFIER); tokens = true;
    					}
    					/**If we have found both coins and tokens, leave the looting bag*/
    					if (coins && tokens) break;
    				}
    			}
    		}
    		
    		/**Add the recent calculation to our total gold and save the values*/
    		goldInEco += recentGold;
    		save();
    		
    		/**Format our string with commas, and return it for printing*/
    		DecimalFormat df = new DecimalFormat("#,###");
    		return df.format(goldInEco);
    	}
    }
    Reply With Quote  
     

  2. Thankful user:



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. Replies: 3
    Last Post: 03-12-2013, 09:25 PM
  2. Total amount of objects in 377 revision?
    By Lil Str Kid in forum Requests
    Replies: 1
    Last Post: 12-10-2010, 07:14 PM
  3. Amount of players in a certain area
    By Ayton in forum Help
    Replies: 3
    Last Post: 11-23-2009, 07:07 PM
  4. method for amount of item in inv
    By dont frunt in forum Help
    Replies: 3
    Last Post: 05-10-2009, 12:06 PM
  5. Changing Max Amount of gold
    By XxOwNxX in forum Tutorials
    Replies: 14
    Last Post: 11-03-2007, 11:52 AM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •