Thread: [634] Optimal varp handling/saving

Page 2 of 2 FirstFirst 12
Results 11 to 14 of 14
  1. #11  
    Registered Member
    Tyluur's Avatar
    Join Date
    Jun 2010
    Age
    26
    Posts
    5,103
    Thanks given
    1,819
    Thanks received
    1,767
    Rep Power
    2438
    Quote Originally Posted by clem585 View Post
    Yea that's what I ended up going for. Here's the updated code:

    Code:
    package com.rs.game.player.scripts;
    
    import com.common.cache.reader.loaders.ConfigDefinitions;
    import com.common.json.JsonFileLoader;
    import com.rs.json.VarpsDb;
    import com.rs.net.game.out.game.GlobalConfigPacket;
    import com.rs.net.game.out.game.GlobalStringPacket;
    import com.rs.net.game.out.game.VarpPacket;
    
    import java.util.HashMap;
    import java.util.HashSet;
    import java.util.Map;
    import java.util.Set;
    
    /**
     * Created by clem585 on 2021-02-14.
     */
    public class VarpManager extends PlayerScript {
    
    	private transient Map<Integer, Integer> varps;
    	private Map<Integer, Integer> persistentVarps;
    	private transient Map<Integer, Integer> globalInts;
    	private transient Map<Integer, String> globalStrings;
    
    	private Set<Integer> dirtyVarps;
    	private Set<Integer> dirtyGlobalInts;
    	private Set<Integer> dirtyGlobalStrings;
    
    	public VarpManager() {
    		varps = new HashMap<>();
    		persistentVarps = new HashMap<>();
    		globalInts = new HashMap<>();
    		globalStrings = new HashMap<>();
    
    		dirtyVarps = new HashSet<>();
    		dirtyGlobalInts = new HashSet<>();
    		dirtyGlobalStrings = new HashSet<>();
    	}
    
    	@Override
    	public void init() {
    		for (int id : JsonFileLoader.get(VarpsDb.class).fetchLoginVarps()) {
    			if (getVarp(id) != 0) {
    				player.getEncoder().write(new VarpPacket(id, getVarp(id)));
    			}
    		}
    	}
    
    	@Override
    	public void tick() {
    		for (int varp : dirtyVarps) {
    			player.getEncoder().write(new VarpPacket(varp, getVarp(varp)));
    		}
    		dirtyVarps.clear();
    
    		for (int globalInt : dirtyGlobalInts) {
    			player.getEncoder().write(new GlobalConfigPacket(globalInt, getGlobalInt(globalInt)));
    		}
    		dirtyGlobalInts.clear();
    
    		for (int globalString : dirtyGlobalStrings) {
    			player.getEncoder().write(new GlobalStringPacket(globalString, getGlobalString(globalString)));
    		}
    		dirtyGlobalStrings.clear();
    	}
    
    	public int getVarp(int id) {
    		Map<Integer, Integer> configs = JsonFileLoader.get(VarpsDb.class).isPersistent(id) ? persistentVarps : varps;
    		return configs.getOrDefault(id, 0);
    
    	}
    
    	public int getVarb(int id) {
    		ConfigDefinitions configDefinitions = ConfigDefinitions.getConfigDefinitions(id);
    		Map<Integer, Integer> configs = JsonFileLoader.get(VarpsDb.class).isPersistent(configDefinitions.getConfigId())
    				? persistentVarps : varps;
    		return (configs.getOrDefault(configDefinitions.getConfigId(), 0) & getMask(id))
    				>> configDefinitions.getStartBit();
    	}
    
    	public int getGlobalInt(int id) {
    		return globalInts.getOrDefault(id, 0);
    	}
    
    	public String getGlobalString(int id) {
    		return globalStrings.getOrDefault(id, "");
    	}
    
    	public void setVarp(int id, int value) {
    		Map<Integer, Integer> configs = JsonFileLoader.get(VarpsDb.class).isPersistent(id) ? persistentVarps : varps;
    		configs.put(id, value);
    		dirtyVarps.add(id);
    	}
    
    	public void setVarb(int id, int value) {
    		ConfigDefinitions configDefinitions = ConfigDefinitions.getConfigDefinitions(id);
    
    		int max = (int) Math.pow(2, configDefinitions.getEndBit() - configDefinitions.getStartBit());
    		if (value >= max) {
    			throw new RuntimeException(String.format("Attempted to overflow varbit, max: %d, value: %d", max, value));
    		}
    
    		value <<= configDefinitions.getStartBit();
    		Map<Integer, Integer> configs = JsonFileLoader.get(VarpsDb.class).isPersistent(configDefinitions.getConfigId())
    				? persistentVarps : varps;
    		configs.put(configDefinitions.getConfigId(), varps.getOrDefault(configDefinitions.getConfigId(), 0)
    				& ~getMask(id) | value);
    		dirtyVarps.add(configDefinitions.getConfigId());
    	}
    
    	public void setGlobalInt(int id, int value) {
    		globalInts.put(id, value);
    		dirtyGlobalInts.add(id);
    	}
    
    	public void setGlobalString(int id, String value) {
    		globalStrings.put(id, value);
    		dirtyGlobalStrings.add(id);
    	}
    
    	private int getMask(int id) {
    		ConfigDefinitions configDefinitions = ConfigDefinitions.getConfigDefinitions(id);
    
    		int mask = 0;
    		for (int i = configDefinitions.getStartBit(); i <= configDefinitions.getEndBit(); ++i) {
    			mask |= (int)Math.pow(2, i);
    		}
    
    		return mask;
    	}
    
    }
    y u no @author @Since????

    wat r these strings that are called dirty btw?

    excuse me my contacts r not in my eyes and this is a painful thing to be analysing
    Quote Originally Posted by blakeman8192 View Post
    Keep trying. Quitting is the only true failure.
    Spoiler for skrrrrr:

    Attached image
    Reply With Quote  
     

  2. Thankful user:


  3. #12  
    Contributor

    clem585's Avatar
    Join Date
    Sep 2013
    Posts
    3,788
    Thanks given
    706
    Thanks received
    702
    Rep Power
    570
    Quote Originally Posted by Tyluur View Post
    y u no @author @Since????
    I set that up ages ago like in 2015 or something. Just changed it to follow convention with @Created and @author

    Quote Originally Posted by Tyluur View Post
    wat r these strings that are called dirty btw?

    excuse me my contacts r not in my eyes and this is a painful thing to be analysing
    Read Dread's previous comment to understand.
    Project thread
    Reply With Quote  
     

  4. Thankful user:


  5. #13  
    Registered Member

    Join Date
    Dec 2009
    Posts
    774
    Thanks given
    367
    Thanks received
    455
    Rep Power
    927
    Quote Originally Posted by Dread View Post
    I do think it's a better idea to store a set of dirty varps that have changed this tick, and then flush all changed varps (I believe this is what Clem was trying to ask), for cases where a varp might have its value mutated multiple times from different varbits.
    If you just queue the varp packet on player level then something like that shouldn't be needed.
    link removed
    Reply With Quote  
     

  6. #14  
    Respected Member


    Polar's Avatar
    Join Date
    Sep 2015
    Age
    28
    Posts
    420
    Thanks given
    0
    Thanks received
    418
    Rep Power
    3098
    Quote Originally Posted by Displee View Post
    If you just queue the varp packet on player level then something like that shouldn't be needed.
    He's saying modifying multiple varbits can be changed in a single tick that modify a single varp, which in your case would queue up the varp packet for each state instead of just the once that tick.
    Reply With Quote  
     

Page 2 of 2 FirstFirst 12

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. Varp for lumby bank chest 634 obects
    By DragonSmith in forum Requests
    Replies: 0
    Last Post: 11-08-2016, 03:48 AM
  2. Better way to handle my Json Load/Save?
    By Kangaroo in forum Help
    Replies: 1
    Last Post: 09-24-2016, 04:37 AM
  3. 614/634 Appearance saving after logout?
    By zzzfishstick in forum Help
    Replies: 0
    Last Post: 01-27-2012, 10:09 PM
  4. make split private chat save [anti leech]
    By pkin3 in forum Tutorials
    Replies: 13
    Last Post: 05-20-2007, 12:36 AM
  5. Better format for saving 3D/21-Century sigs...
    By Killa Man in forum Tutorials
    Replies: 7
    Last Post: 04-05-2007, 09:35 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
  •