Thread: Collection Log Base

Results 1 to 10 of 10
  1. #1 Collection Log Base 
    Community Veteran

    Dexter Morgan's Avatar
    Join Date
    Nov 2008
    Age
    15
    Posts
    4,364
    Thanks given
    1,027
    Thanks received
    703
    Rep Power
    2991
    There are things that can be done better. I track all npcs and their drops that's why you see a separate statement for them.


    Code:
    package com.elvarg.game.content.collectionlog;
    
    import java.lang.reflect.Type;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Map.Entry;
    import java.util.Optional;
    import java.util.Set;
    
    import com.elvarg.game.World;
    import com.elvarg.game.content.KillTrackManager;
    import com.elvarg.game.content.NpcDropTableViewer;
    import com.elvarg.game.definition.NpcDefinition;
    import com.elvarg.game.definition.drop.NpcDropDefinition;
    import com.elvarg.game.definition.drop.NpcDropLoader;
    import com.elvarg.game.entity.impl.player.Player;
    import com.elvarg.game.model.Item;
    import com.elvarg.game.model.JsonIO;
    import com.elvarg.net.packet.interaction.PacketInteraction;
    import com.elvarg.util.Misc;
    import com.google.gson.Gson;
    import com.google.gson.JsonElement;
    import com.google.gson.JsonObject;
    import com.google.gson.reflect.TypeToken;
    
    public class CollectionLogManager extends PacketInteraction {
    
    	public static final Type REFERENCE = new TypeToken<HashMap<CollectionLogType, HashMap<String, HashMap<Integer, Integer>>>>() {
    	}.getType();
    
    	public HashMap<CollectionLogType, HashMap<String, HashMap<Integer, Integer>>> log = new HashMap<>();
    
    	public static final JsonIO IO = new JsonIO("./data/saves/collectionlog/") {
    
    		@Override
    		public void init(String name, Gson builder, JsonObject reader) {
    			JsonElement element = reader.get("log");
    
    			HashMap<CollectionLogType, HashMap<String, HashMap<Integer, Integer>>> log = GSON
    					.fromJson(element.toString(), REFERENCE);
    
    			Optional<Player> p = World.getPlayerByName(name);
    
    			if (!p.isPresent()) {
    				return;
    			}
    
    			p.get().getCollectionLog().log = log;
    		}
    
    		@Override
    		public JsonObject save(String name, Gson builder, JsonObject object) {
    
    			Optional<Player> p = World.getPlayerByName(name);
    
    			if (!p.isPresent()) {
    				return object;
    			}
    
    			object.add("log", builder.toJsonTree(p.get().getCollectionLog().log, REFERENCE));
    			return object;
    		}
    	};
    
    	private CollectionLogType viewingType;
    
    	public static void open(Player player, CollectionLogType type) {
    
    		int line = 34_371;
    
    		if (type.equals(CollectionLogType.BOSSES)) {
    			int slot = 1;
    
    			for (String s : NpcDropTableViewer.SORTED_NAME_LIST) {
    				player.getPacketSender().sendString(line, (slot++) + ". " + s);
    				line += 4;
    			}
    		} else {
    			if (type.log == null) {
    				return;
    			}
    			for (CollectionLog log : type.log) {
    				player.getPacketSender().sendString(line, log.name);
    				line += 4;
    			}
    		}
    
    		player.getPacketSender().sendConfig(833, type.ordinal());
    
    		int size = (line - 34_371) / 4;
    
    		player.getPacketSender().sendScrollbarHeight(34367, size * 15);
    
    		player.getPacketSender().clearInterfaceText(line, 35_371);
    
    		player.getCollectionLog().viewingType = type;
    
    		player.getPacketSender().sendInterface(34_343);
    	}
    
    	private static void viewType(Player player, int index) {
    		if (player.getCollectionLog().viewingType == null) {
    			return;
    		}
    
    		CollectionLogType type = player.getCollectionLog().viewingType;
    
    		if (type.equals(CollectionLogType.BOSSES)) {
    			String name = NpcDropTableViewer.SORTED_NAME_LIST.get(index);
    
    			NpcDropDefinition drop = NpcDropLoader.FOR_NAME.get(name);
    
    			if (drop == null) {
    				return;
    			}
    
    			ArrayList<Item> droptable = NpcDropLoader.FOR_NAME_COLLECTION_LOG.get(name);
    
    			Set<Entry<Integer, Integer>> log = getLog(player, type, name);
    
    			int obtained = 0;
    
    			if (log != null) {
    				for (Entry<Integer, Integer> s : log) {
    					droptable.add(new Item(s.getKey(), s.getValue()));
    				}
    				obtained = log.size();
    			}
    
    			player.getPacketSender().sendString(34_348, name);
    
    			player.getPacketSender().sendString(34_349, "Obtained: @[email protected]" + obtained + "/" + droptable.size());
    
    			int kills = KillTrackManager.getKills(player, name);
    
    			player.getPacketSender().sendString(34_350, "Kills: @[email protected]" + kills);
    
    			player.getPacketSender().sendCollectionLog(droptable, 34366);
    		} else {
    			player.getPacketSender().sendString(34_348, Misc.ucFirst(type.log[index].name));
    
    			Set<Entry<Integer, Integer>> log = getLog(player, type, type.log[index].name);
    
    			ArrayList<Item> droptable = new ArrayList<Item>();
    
    			for (Item item : type.log[index].items) {
    				droptable.add(new Item(item.getId(), 0));
    			}
    
    			int obtained = 0;
    
    			if (log != null) {
    				for (Entry<Integer, Integer> s : log) {
    					droptable.add(new Item(s.getKey(), s.getValue()));
    				}
    				obtained = log.size();
    			}
    
    			player.getPacketSender().sendString(34_349,
    					"Obtained: @[email protected]" + obtained + "/" + type.log[index].items.size());
    
    			player.getPacketSender().sendString(34_350, "");
    
    			player.getPacketSender().sendCollectionLog(droptable, 34366);
    		}
    	}
    
    	public static void track(Player player, CollectionLogType type, String s, Item item) {
    		if (player.getCollectionLog().log.get(type) == null) {
    			player.getCollectionLog().log.put(type, new HashMap<String, HashMap<Integer, Integer>>());
    		}
    
    		if (player.getCollectionLog().log.get(type).get(s) == null) {
    			player.getCollectionLog().log.get(type).put(s, new HashMap<Integer, Integer>());
    		}
    
    		player.getCollectionLog().log.get(type).get(s).merge(item.getId(), item.getAmount(), Integer::sum);
    
    		IO.save(player.getUsername());
    	}
    
    	public static void trackBoss(Player player, int id, Item item) {
    		NpcDefinition def = NpcDefinition.forId(id);
    
    		if (def == null) {
    			return;
    		}
    
    		track(player, CollectionLogType.BOSSES, def.getName(), item);
    	}
    
    	private static Set<Entry<Integer, Integer>> getLog(Player player, CollectionLogType type, String s) {
    		if (player.getCollectionLog().log.get(type) == null) {
    			return null;
    		}
    
    		if (player.getCollectionLog().log.get(type).get(s) == null) {
    			return null;
    		}
    
    		return player.getCollectionLog().log.get(type).get(s).entrySet();
    	}
    
    	@Override
    	public boolean handleButtonInteraction(Player player, int button) {
    		if (button >= 34_368 && button <= 35_612) {
    			int index = button - 34_368;
    
    			if (index > 0) {
    				index /= 4;
    			}
    
    			viewType(player, index);
    			return true;
    		}
    
    		switch (button) {
    		case 33_689:
    		case 34352:
    			open(player, CollectionLogType.BOSSES);
    			return true;
    		case 34353:
    			open(player, CollectionLogType.RAIDS);
    			return true;
    		case 34354:
    			open(player, CollectionLogType.CLUES);
    			return true;
    		case 34355:
    			open(player, CollectionLogType.MINIGAMES);
    			return true;
    		case 34356:
    			open(player, CollectionLogType.OTHER);
    			return true;
    		}
    		return false;
    	}
    }
    Code:
    package com.elvarg.game.content.collectionlog;
    
    import java.util.ArrayList;
    
    import com.elvarg.game.model.Item;
    
    public class CollectionLog {
    
    	public String name;
    
    	public ArrayList<Item> items;
    
    	public CollectionLog(String name) {
    		this.name = name;
    		this.items = new ArrayList<Item>();
    	}
    	
    	public CollectionLog add(Item item) {
    		this.items.add(item);
    		return this;
    	}
    }
    Code:
    package com.elvarg.game.content.collectionlog;
    
    import com.elvarg.game.model.Item;
    
    public enum CollectionLogType {
    
    	BOSSES(),
    
    	RAIDS(),
    
    	CLUES(),
    
    	MINIGAMES(new CollectionLog[] {
    
    	}),
    
    	OTHER(new CollectionLog[] { new CollectionLog("Hello").add(new Item(4151)).add(new Item(4153)), }),
    
    	;
    
    	public CollectionLog[] log;
    
    	CollectionLogType(CollectionLog[] log) {
    		this.log = log;
    	}
    
    	CollectionLogType() {
    
    	}
    }
    Additional classes may be required:
    Code:
    package com.elvarg.game.model;
    
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.lang.reflect.Type;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    import java.util.HashMap;
    import java.util.logging.Level;
    
    import com.elvarg.Server;
    import com.google.gson.Gson;
    import com.google.gson.GsonBuilder;
    import com.google.gson.JsonObject;
    import com.google.gson.JsonParser;
    import com.google.gson.reflect.TypeToken;
    
    public abstract class JsonIO {
    
    	public static final Type HASH_STRING_INTEGER = new TypeToken<HashMap<String, Integer>>() {
    	}.getType();
    
    	public static final Gson GSON = new Gson();
    
    	private String location;
    
    	public String name;
    
    	public JsonIO(String location, String name) {
    		this.location = location;
    		this.name = name;
    	}
    
    	public JsonIO(String location) {
    		this(location, "");
    	}
    
    	public abstract void init(String name, Gson builder, JsonObject reader);
    
    	public abstract JsonObject save(String name, Gson builder, JsonObject object);
    
    	public void initAll() {
    		File[] files = new File(location).listFiles();
    
    		if (files.length == 0) {
    			return;
    		}
    
    		for (File f : files) {
    			if (f == null) {
    				continue;
    			}
    			init(f.getName().replaceAll(".json", ""));
    		}
    
    		System.out.println("Init directory: " + location + ". Found: " + files.length + " files.");
    	}
    
    	public void init() {
    		init(name);
    	}
    
    	public void init(String name) {
    
    		final Path path = Paths.get(location + "" + name + ".json");
    		final File file = path.toFile();
    
    		if (!file.exists()) {
    			return;
    		}
    
    		try (FileReader fileReader = new FileReader(file)) {
    			final JsonParser fileParser = new JsonParser();
    			final Gson builder = new GsonBuilder().create();
    			final JsonObject reader = (JsonObject) fileParser.parse(fileReader);
    
    			init(name, builder, reader);
    
    		} catch (final Exception e) {
    			e.printStackTrace();
    		}
    	}
    
    	public void saveFile(String file) {
    		save(name + "/" + file);
    	}
    
    	public void save() {
    		save(name);
    	}
    
    	public void save(String name) {
    		final Path path = Paths.get(location + "" + name + ".json");
    		final File file = path.toFile();
    		
    
    		if (!file.getParentFile().exists()) {
    			try {
    				file.getParentFile().mkdirs();
    			} catch (final SecurityException e) {
    			}
    		}file.getParentFile().setWritable(true);
    		try (FileWriter writer = new FileWriter(file)) {
    
    			final Gson builder = new GsonBuilder().setPrettyPrinting().create();
    			final JsonObject object = new JsonObject();
    
    			writer.write(builder.toJson(save(name, builder, object)));
    			writer.close();
    
    		} catch (final Exception e) {
    			Server.getLogger().log(Level.WARNING,
    					"An error has occured while saving " + location + " " + name + " file!", e);
    		}
    	}
    
    	public String getFile() {
    		return location + "" + name + ".json";
    	}
    }
    I have a custom system for interfaces

    Code:
    package com.runescape.cache.graphics.widget.impl;
    
    import com.runescape.cache.graphics.widget.custom.CustomWidget;
    
    public class CollectionLogWidget extends CustomWidget {
    
    	private static final String[] TYPES = { "Bosses", "Raids", "Clues", "Minigames", "Other", };
    
    	public CollectionLogWidget() {
    		super(34_343);
    	}
    
    	@Override
    	public String getName() {
    		return "Collection Log";
    	}
    
    	@Override
    	public void init() {
    		addBackground(882);
    		add(addText("#", 2), 210, 68);
    		add(addText("#", 0, 0xFFFFFF), 210, 88);
    		add(addText("#", 0, 0xFFFFFF), 350, 88);
    		add(addButtonConfigList(TYPES, 883, 884, 0, false, 833), 15, 45);
    		add(addTextList(TYPES, 2, OR1, true, 96, false), -6, 48);
    		add(addScrollbarWithItem(7, 10, 13, 0, new String[] {"Check KC"}, 211, 267), 212, 103);
    		add(addScrollbarWithClickTextBackground("#", "Select", 0, OR1, 1390, 1391, 1392, 247, 174, 350), 15, 67);
    	}
    
    }
    Sprites:
    [Only registered and activated users can see links. ]

    [Only registered and activated users can see links. ]
    Quote Originally Posted by UberNation View Post
    Did you update the quantum network sockets to match the polarity of the wilderness counter levels in the Boolean that divides the wilderness level by zero?
    Reply With Quote  
     

  2. Thankful user:


  3. #2  
    Donator

    Kid Buu's Avatar
    Join Date
    Aug 2017
    Posts
    350
    Thanks given
    115
    Thanks received
    180
    Rep Power
    320
    Very nice contribution
    Reply With Quote  
     

  4. #3  
    Registered Member
    Bartvh's Avatar
    Join Date
    May 2017
    Posts
    356
    Thanks given
    79
    Thanks received
    153
    Rep Power
    314
    To anyone reading this article, please don't use this. Saving player data as jsons on disks is a very bad practice.
    [Only registered and activated users can see links. ] Open Source Emulation Framework
    Reply With Quote  
     

  5. #4  
    Endeavor

    Mikey`'s Avatar
    Join Date
    Dec 2007
    Posts
    4,418
    Thanks given
    682
    Thanks received
    1,405
    Rep Power
    1113
    Quote Originally Posted by Bartvh View Post
    To anyone reading this article, please don't use this. Saving player data as jsons on disks is a very bad practice.
    How would you do it?
    Reply With Quote  
     

  6. #5  
    Registered Member
    Bartvh's Avatar
    Join Date
    May 2017
    Posts
    356
    Thanks given
    79
    Thanks received
    153
    Rep Power
    314
    Quote Originally Posted by Mikey` View Post
    How would you do it?
    Store it in a database. That's the whole purpose of a database.
    [Only registered and activated users can see links. ] Open Source Emulation Framework
    Reply With Quote  
     

  7. Thankful users:


  8. #6  
    Endeavor

    Mikey`'s Avatar
    Join Date
    Dec 2007
    Posts
    4,418
    Thanks given
    682
    Thanks received
    1,405
    Rep Power
    1113
    Quote Originally Posted by Bartvh View Post
    Store it in a database. That's the whole purpose of a database.
    I bet 99% of servers still use txt files, but something like MongoDB would be ideal for storing player data so you don't have to deal with sql schema.
    Reply With Quote  
     

  9. #7  
    primal.ps
    Join Date
    Jan 2011
    Posts
    6
    Thanks given
    0
    Thanks received
    0
    Rep Power
    0
    Good to see the trash we paid you for you released for free.
    [Only registered and activated users can see links. ]
    [Only registered and activated users can see links. ] [Only registered and activated users can see links. ]
    Reply With Quote  
     

  10. #8  



    Omar's Avatar
    Join Date
    Dec 2007
    Posts
    166
    Thanks given
    284
    Thanks received
    409
    Rep Power
    4248
    Quote Originally Posted by Mikey` View Post
    I bet 99% of servers still use txt files, but something like MongoDB would be ideal for storing player data so you don't have to deal with sql schema.
    Ideal and MongoDB should probably never be used in the same sentence.
    [Only registered and activated users can see links. ]

    Spoiler for aaaa:




    [Only registered and activated users can see links. ]
    Reply With Quote  
     

  11. Thankful users:


  12. #9  
    Endeavor

    Mikey`'s Avatar
    Join Date
    Dec 2007
    Posts
    4,418
    Thanks given
    682
    Thanks received
    1,405
    Rep Power
    1113
    Quote Originally Posted by Omar View Post
    Ideal and MongoDB should probably never be used in the same sentence.
    Sure, but you want to use some type of nosql database unless you want to manage a strict schema for all the data you want to persist. There are probably other database technologies that wil work for this, what database system would you use?
    Reply With Quote  
     

  13. #10  
    Super Donator

    Tyluur's Avatar
    Join Date
    Jun 2010
    Age
    23
    Posts
    4,521
    Thanks given
    1,570
    Thanks received
    1,468
    Rep Power
    1179
    Quote Originally Posted by Bartvh View Post
    To anyone reading this article, please don't use this. Saving player data as jsons on disks is a very bad practice.
    That’s rather dismissive lol there’s tons of reason to use flat files. Everything doesn’t need a database.

    [Only registered and activated users can see links. ] | [Only registered and activated users can see links. ] | [Only registered and activated users can see links. ] (official dog of rune-server)
    -------------------
    [Only registered and activated users can see links. ]
    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. Replies: 15
    Last Post: 09-30-2020, 07:31 PM
  2. Collection Log
    By Simplex in forum Show-off
    Replies: 17
    Last Post: 03-31-2020, 05:33 PM
  3. [317] osrs collection log
    By mikan in forum Snippets
    Replies: 13
    Last Post: 03-23-2020, 01:51 AM
  4. Replies: 11
    Last Post: 06-28-2019, 07:27 AM
  5. [317] Extremity - Collection Log
    By nuka in forum Show-off
    Replies: 3
    Last Post: 08-30-2018, 08:13 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
  •