RuneHive-Game
Loading...
Searching...
No Matches
ItemDefParser.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.JsonObject;
5import com.runehive.game.world.items.containers.equipment.Equipment;
6import com.runehive.game.world.items.containers.equipment.EquipmentType;
7import com.runehive.util.Utility;
8import com.runehive.util.parser.GsonParser;
9import com.runehive.util.tools.wiki.parser.WikiTable;
10import com.runehive.util.tools.wiki.parser.WikiTableParser;
11import org.apache.logging.log4j.LogManager;
12import org.apache.logging.log4j.Logger;
13import org.jsoup.nodes.Document;
14import org.jsoup.nodes.Element;
15import org.jsoup.select.Elements;
16
17import java.util.*;
18
19import static com.runehive.game.world.entity.combat.CombatConstants.BONUS_CONFIG_FIELD_NAMES;
20
21public class ItemDefParser extends WikiTableParser {
22
23 private static final Logger logger = LogManager.getLogger(ItemDefParser.class);
24
25 private ItemDefParser() {
26 super(generateTables());
27 }
28
29 public static void main(String[] args) throws InterruptedException {
31 parser.begin();
32 }
33
34 private static LinkedList<WikiTable> generateTables() {
35 List<Definition> definitions = parseDefinitions();
36 LinkedList<WikiTable> tables = new LinkedList<>();
37 String link = "https://oldschoolrunescape.fandom.com/wiki/Category:Items";
38 Iterator<Definition> iterator = definitions.iterator();
39
40 while (iterator.hasNext()) {
41 Definition definition = iterator.next();
42 iterator.remove();
43 tables.add(new WikiTable(link + format(definition.name)) {
44 @Override
45 protected void parseDocument(Document document) {
46 Elements infobox = document.select(".wikitable.infobox");
47 Elements equipment = document.select(".wikitable.smallpadding");
48 int idx = 0;
49
50 EquipmentType type = EquipmentType.NOT_WIELDABLE;
51 boolean twoHanded = false;
52 int[] bonuses = null;
53
54 for (Element info : infobox) {
55 JsonObject object = new JsonObject();
56 Elements attributes = infobox.select("a");
57
58 String destroyMessage = "Drop";
59 double weight = 0;
60 boolean tradeable = false;
61
62 for (Element attribute : attributes) {
63 String attr = attribute.attr("title");
64 try {
65 switch (attr) {
66 case "Tradeable": {
67 tradeable = attribute.parent().nextElementSibling().text().equalsIgnoreCase("Yes");
68 break;
69 }
70 case "Destroy": {
71 destroyMessage = attribute.parent().nextElementSibling().text();
72 break;
73 }
74 case "Weight": {
75 Element element = attribute.parent().nextElementSibling();
76 String alchemy = element.text().replaceAll("[^-\\d.]", "");
77 if (!alchemy.matches("-?[0-9]*\\.?[0-9]*")) {
78 alchemy = element.children().last().nextSibling().toString().replaceAll("[^-\\d.]", "");
79 }
80 if (!alchemy.isEmpty())
81 weight = Double.parseDouble(alchemy);
82 break;
83 }
84 }
85 } catch (Exception ignored) {
86 logger.warn("Exception while parsing item [id=" + definition.id + ", name=" + definition.name + "]", ignored);
87 }
88 }
89
90 try {
91 if (!equipment.isEmpty()) {
92 bonuses = new int[Equipment.SIZE];
93
94 Elements equipmentInfo = equipment.select("td");
95 int ind = 0;
96 if (equipmentInfo.size() > idx) {
97 do {
98 Element attribute = equipmentInfo.get(idx++);
99 if (attribute.attr("style").equals("text-align: center; width: 35px;")
100 || attribute.attr("style").equals("text-align: center; width: 30px;")) {
101 bonuses[ind++ % Equipment.SIZE] = Integer.parseInt(attribute.text().replace("%", ""));
102 }
103 } while (ind < Equipment.SIZE);
104 }
105
106 for (Element attribute : equipment.select("img")) {
107 switch (attribute.attr("data-image-key")) {
108 case "Head_slot.png":
109 type = EquipmentType.HELM; // TODO
110 break;
111 case "Cape_slot.png":
112 type = EquipmentType.CAPE;
113 break;
114 case "Weapon_slot.png":
115 type = EquipmentType.WEAPON;
116 break;
117 case "Neck_slot.png":
118 type = EquipmentType.AMULET;
119 break;
120 case "Ammo_slot.png":
121 type = EquipmentType.ARROWS;
122 break;
123 case "Shield_slot.png":
124 type = EquipmentType.SHIELD;
125 break;
126 case "Torso_slot.png":
127 type = EquipmentType.BODY; // TODO
128 break;
129 case "Legs_slot.png":
130 type = EquipmentType.LEGS;
131 break;
132 case "Gloves_slot.png":
133 type = EquipmentType.GLOVES;
134 break;
135 case "Boots_slot.png":
136 type = EquipmentType.BOOTS;
137 break;
138 case "Ring_slot.png":
139 type = EquipmentType.RING;
140 break;
141 case "2h_slot.png":
142 type = EquipmentType.WEAPON;
143 twoHanded = true;
144 break;
145 }
146 }
147 }
148 } catch (Exception ignored) {
149 bonuses = null;
150 type = EquipmentType.NOT_WIELDABLE;
151 logger.warn("Exception while parsing item equipment data [id=" + definition.id + ", name=" + definition.name + "]", ignored);
152 }
153
154 object.addProperty("name", info.select("caption").text());
155
156 if (!destroyMessage.equals("Drop"))
157 object.addProperty("destroy-message", destroyMessage);
158
159 if (tradeable)
160 object.addProperty("tradable", true);
161
162 if (twoHanded)
163 object.addProperty("two-handed", true);
164
165 if (weight != 0)
166 object.addProperty("weight", weight);
167
168 if (type != EquipmentType.NOT_WIELDABLE)
169 object.addProperty("equipment-type", type.name());
170
171 if (bonuses != null) {
172 for (int index = 0; index < BONUS_CONFIG_FIELD_NAMES.length; index++) {
173 String bonusName = BONUS_CONFIG_FIELD_NAMES[index];
174 if (bonuses[index] != 0) {
175 object.addProperty(bonusName, bonuses[index]);
176 }
177 }
178 }
179
180 if (!table.contains(object)) {
181 table.add(object);
182 }
183 }
184 }
185 });
186 }
187 return tables;
188 }
189
190 @Override
191 protected void finish() {
192 JsonArray array = new JsonArray();
193 for (WikiTable table : tables) {
194 array.addAll(table.getTable());
195 }
196 writeToJson("wiki_item_dump", array);
197 }
198
199 private static String format(String name) {
200 name = Utility.capitalizeSentence(name);
201 name = name.replace(" ", "_");
202 return name;
203 }
204
205 private static List<Definition> parseDefinitions() {
206 Set<String> added = new HashSet<>();
207 List<Definition> definitions = new LinkedList<>();
208 new GsonParser("wiki/wiki_item_dump") {
209 @Override
210 protected void parse(JsonObject data) {
211 Definition definition = new Definition();
212
213 definition.id = data.get("id").getAsInt();
214 definition.name = data.get("name").getAsString();
215
216 if (added.contains(definition.name))
217 return;
218
219 added.add(definition.name);
220 definitions.add(definition);
221 }
222 }.run();
223 added.clear();
224 return definitions;
225 }
226
227 public static final class Definition {
228 public int id;
229 public String name;
230 }
231
232}
Handles miscellaneous methods.
Definition Utility.java:27
static String capitalizeSentence(final String string)
Capitalize each letter after .
Definition Utility.java:99
This class provides an easy to use google gson parser specifically designed for parsing JSON files.
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.