RuneHive-Game
Loading...
Searching...
No Matches
BonusParser.java
Go to the documentation of this file.
1package com.runehive.util.tools.wiki.impl;
2
3import com.google.gson.JsonArray;
4import com.google.gson.JsonElement;
5import com.google.gson.JsonObject;
6import com.runehive.game.world.items.ItemDefinition;
7import com.runehive.game.world.items.containers.equipment.Equipment;
8import com.runehive.util.tools.wiki.parser.WikiTable;
9import com.runehive.util.tools.wiki.parser.WikiTableParser;
10import org.jsoup.nodes.Document;
11import org.jsoup.nodes.Element;
12import org.jsoup.select.Elements;
13
14import java.util.*;
15
16public class BonusParser extends WikiTableParser {
17
18 /** The table links. */
19 private static final String[] TABLE_LINKS = {
20 "http://oldschoolrunescape.wikia.com/wiki/Ammunition_slot_table",
21 "http://oldschoolrunescape.wikia.com/wiki/Body_slot_table",
22 "http://oldschoolrunescape.wikia.com/wiki/Cape_slot_table",
23 "http://oldschoolrunescape.wikia.com/wiki/Feet_slot_table",
24 "http://oldschoolrunescape.wikia.com/wiki/Hand_slot_table",
25 "http://oldschoolrunescape.wikia.com/wiki/Head_slot_table",
26 "http://oldschoolrunescape.wikia.com/wiki/Legwear_slot_table",
27 "http://oldschoolrunescape.wikia.com/wiki/Neck_slot_table",
28 "http://oldschoolrunescape.wikia.com/wiki/Ring_slot_table",
29 "http://oldschoolrunescape.wikia.com/wiki/Shield_slot_table",
30 "http://oldschoolrunescape.wikia.com/wiki/Two-handed_slot_table",
31 "http://oldschoolrunescape.wikia.com/wiki/Weapons_table"
32 };
33
34 private BonusParser() {
35 super(generateTables());
36 }
37
38 private static LinkedList<WikiTable> generateTables() {
39 LinkedList<WikiTable> tables = new LinkedList<>();
40
41 for (String link : TABLE_LINKS) {
42 tables.add(new WikiTable(link) {
43 @Override
44 public void parseDocument(Document document) {
45 for (Element wikiTable : document.select(".wikitable")) {
46 for (Element children : wikiTable.children()) {
47 Iterator<Element> iterator = children.children().iterator();
48 iterator.next();
49 while (iterator.hasNext()) {
50 Element child = iterator.next();
51 Elements bonuses = child.children();
52 String name = bonuses.remove(0).text();
53 try {
54 JsonObject object = new JsonObject();
55 object.addProperty("name", name);
56
57 int idx = 0;
58 int[] bonusArray = new int[Equipment.SIZE];
59 for (Element data : bonuses) {
60
61 if (data.text().isEmpty()) {
62 idx++;
63 continue;
64 }
65
66 if (data.text().contains("trimmed"))
67 continue;
68
69 if (idx >= bonusArray.length)
70 break;
71
72 if ((link.contains("Neck") || link.contains("Body") || link.contains("Leg") || link.contains("Feet") || link.contains("Head") || link.contains("Cape") || link.contains("Ammu") || link.contains("Hand")) && idx == Equipment.RANGED_STRENGTH) {
73 bonusArray[Equipment.PRAYER_BONUS] = Integer.parseInt(data.text());
74 } else {
75 bonusArray[idx++] = Integer.parseInt(data.text());
76 }
77 }
78 object.add("bonuses", GSON.toJsonTree(bonusArray));
79 table.add(object);
80 } catch (Exception e) {
81 System.err.println("Failed to parse " + name + " --- " + e.toString());
82 }
83 }
84 }
85 }
86 }
87 });
88 }
89
90 return tables;
91 }
92
93 @Override
94 protected void finish() {
95 Map<String, Integer[]> bonusMap = new HashMap<>(tables.size());
96 JsonArray array = new JsonArray();
97
98 for (WikiTable table : tables) {
99 JsonArray tableData = table.getTable();
100 array.addAll(tableData);
101
102 for (JsonElement data : tableData) {
103 JsonObject next = (JsonObject) data;
104 String name = next.get("name").getAsString();
105 Integer[] bonuses = GSON.fromJson(next.get("bonuses"), Integer[].class);
106 if (bonusMap.containsKey(name)) {
107 System.err.println("Conflicting item: " + name);
108 System.err.println(Arrays.toString(bonuses));
109 System.err.println(Arrays.toString(bonusMap.get(name)));
110 System.err.println();
111 } else {
112 bonusMap.put(name, bonuses);
113 }
114 }
115 }
116
117 writeToJson("wiki_dump", array);
118
120 for (ItemDefinition definition : ItemDefinition.DEFINITIONS) {
121 if (definition == null) continue;
122 Integer[] bonuses = bonusMap.get(definition.getName());
123 if (bonuses != null) {
124 Arrays.setAll(definition.getBonuses(), index -> bonuses[index]);
125 } else {
126 Arrays.setAll(definition.getBonuses(), index -> 0);
127 }
128 }
129 ItemDefinition.dump("wiki/bonus_dump");
130 }
131
132 public static void main(String[] args) throws InterruptedException {
134 parser.begin();
135 }
136
137}
Represents all of an in-game Item's attributes.
static void dump(ItemDefinition[] definitions, String path)
static ItemDefinition[] DEFINITIONS
An array of item definitions.
The container that manages the equipment for a player.
static final int SIZE
The size of all equipment instances.
static final String[] TABLE_LINKS
The table links.
static LinkedList< WikiTable > generateTables()
static void writeToJson(String name, JsonElement array)
Writes the parsed wiki dump to a json file in ./data/wiki named as the name parameter.
WikiTableParser(LinkedList< WikiTable > tables)
Constructs a new WikiTableParser object.
final LinkedList< WikiTable > tables
The WikiTable list to parse.