RuneHive-Game
Loading...
Searching...
No Matches
NpcDropsParser.java
Go to the documentation of this file.
1package com.runehive.util.tools.wiki.impl;
2
3import com.google.common.collect.LinkedListMultimap;
4import com.google.gson.JsonArray;
5import com.google.gson.JsonObject;
6import com.runehive.game.world.entity.mob.npc.definition.NpcDefinition;
7import com.runehive.game.world.entity.mob.npc.drop.NpcDropManager;
8import com.runehive.game.world.items.ItemDefinition;
9import com.runehive.util.parser.impl.NpcDropParser;
10import com.runehive.util.tools.wiki.parser.WikiTable;
11import com.runehive.util.tools.wiki.parser.WikiTableParser;
12import org.jsoup.nodes.Document;
13import org.jsoup.nodes.Element;
14import org.jsoup.select.Elements;
15
16import java.util.*;
17
18public class NpcDropsParser extends WikiTableParser {
19
20 private static final Map<String, ItemDefinition> itemByName = new HashMap<>();
21
22 private NpcDropsParser() {
23 super(generateTables());
24 }
25
26 private static LinkedList<WikiTable> generateTables() {
28
29 for (ItemDefinition definition : ItemDefinition.DEFINITIONS) {
30 if (definition == null || definition.getName() == null || definition.getName().equals("null") || itemByName.containsKey(definition.getName())) {
31 continue;
32 }
33 itemByName.put(definition.getName(), definition);
34 }
35
36 LinkedListMultimap<String, NpcDefinition> byName = LinkedListMultimap.create();
38 new NpcDropParser().run();
39
40 for (NpcDefinition definition : NpcDefinition.DEFINITIONS) {
41 if (definition == null || definition.getName() == null || definition.getName().equals("null")) {
42 continue;
43 }
44
45 if (!definition.isAttackable()) {
46 continue;
47 }
48
49 if (NpcDropManager.NPC_DROPS.containsKey(definition.getId())) {
50 continue;
51 }
52
53 byName.put(definition.getName(), definition);
54 }
55
56 LinkedList<WikiTable> tables = new LinkedList<>();
57// tables.add(new NpcDropTable(WIKI_LINK.concat("King Black Dragon".replaceAll(" ", "_")), ImmutableList.of(NpcDefinition.DEFINITIONS[239])));
58
59 for (Map.Entry<String, Collection<NpcDefinition>> entry : byName.asMap().entrySet()) {
60 String name = entry.getKey();
61 name = name.replaceAll(" ", "_");
62 int idx = name.indexOf("(");
63 if (idx > -1)
64 name = name.substring(0, idx);
65 tables.add(new NpcDropTable(WIKI_LINK.concat(name), entry.getValue()));
66 }
67 return tables;
68 }
69
70 @Override
71 protected void finish() {
72 JsonArray array = new JsonArray();
73 for (WikiTable table : tables) {
74 array.addAll(table.getTable());
75 }
76 writeToJson("wiki_drop_dump", array);
77 }
78
79 public static void main(String[] args) throws InterruptedException {
81 parser.begin();
82 }
83
84 private static class NpcDropTable extends WikiTable {
85 private final Collection<NpcDefinition> definitions;
86
87 NpcDropTable(String link, Collection<NpcDefinition> definitions) {
88 super(link);
89 this.definitions = definitions;
90 }
91
92 @Override
93 protected void parseDocument(Document document) {
94 Elements dropTable = document.select(".wikitable.sortable.dropstable");
95 Elements td = dropTable.select("td");
96 Iterator<Element> iterator = td.iterator();
97
98 List<DropItem> drops = new LinkedList<>();
99 boolean rareDropTable = false;
100
101 while (iterator.hasNext()) {
102 iterator.next();
103 String name = iterator.next().text();
104 String amount = iterator.next().text().replaceAll(",", "");
105 String rarity = iterator.next().text().toLowerCase();
106 iterator.next();
107
108 if (!itemByName.containsKey(name)) {
109 log("missing item: " + name);
110 if (name.contains("Rare drop table")) {
111 rareDropTable = true;
112 }
113 continue;
114 }
115
116 if (amount.contains("Unknown")) {
117 log("unknown drop amount: " + name);
118 continue;
119 }
120
121 try {
122 int id, min, max;
123
124 int notedIndex = amount.indexOf("(noted)");
125 if (notedIndex > -1) {
126 amount = amount.substring(0, notedIndex - 1).trim();
127 id = itemByName.get(name).getNotedId();
128 } else {
129 id = itemByName.get(name).getId();
130 }
131
132 DropRarity rate;
133 if (rarity.startsWith("very rare")) {
134 rate = DropRarity.VERY_RARE;
135 } else if (rarity.startsWith("rare")) {
136 rate = DropRarity.RARE;
137 } else if (rarity.startsWith("uncommon")) {
138 rate = DropRarity.UNCOMMON;
139 } else if (rarity.startsWith("common")) {
140 rate = DropRarity.COMMON;
141 } else if (rarity.startsWith("always")) {
142 rate = DropRarity.ALWAYS;
143 } else {
144 log("unknown drop rarity: " + rarity);
145 continue;
146 }
147
148 if (amount.contains(";")) {
149 String[] amounts = amount.split("; ");
150 for (String s : amounts) {
151 int hyphen = s.indexOf("–");
152 if (hyphen > -1) {
153 min = Integer.valueOf(s.substring(0, hyphen));
154 max = Integer.valueOf(s.substring(hyphen + 1));
155 } else {
156 min = max = Integer.valueOf(s);
157 }
158 drops.add(new DropItem(id, min, max, rate));
159 }
160 } else {
161 int hyphen = amount.indexOf("–");
162 if (hyphen > -1) {
163 min = Integer.valueOf(amount.substring(0, hyphen));
164 max = Integer.valueOf(amount.substring(hyphen + 1));
165 } else {
166 min = max = Integer.valueOf(amount);
167 }
168 drops.add(new DropItem(id, min, max, rate));
169 }
170 } catch (Exception e) {
171 e.printStackTrace();
172 }
173 }
174
175 if (drops.isEmpty()) {
176 return;
177 }
178
179 JsonObject object = new JsonObject();
180 JsonArray array = new JsonArray();
181
182 for (NpcDefinition definition : definitions) {
183 array.add(definition.getId());
184 }
185
186 drops.sort(Comparator.comparing(drop -> drop.rarity));
187
188 object.add("ids", array);
189 object.addProperty("rare_table", rareDropTable);
190 object.add("drops", buildDropItemTables(drops));
191 table.add(object);
192 }
193
194 private static JsonArray buildDropItemTables(Collection<DropItem> drops) {
195 JsonArray items = new JsonArray();
196
197 for (DropItem item : drops) {
198 JsonObject dropItem = new JsonObject();
199
200 dropItem.addProperty("id", item.id);
201 dropItem.addProperty("minimum", item.min);
202 dropItem.addProperty("maximum", item.max);
203 dropItem.addProperty("type", item.rarity.name());
204
205 items.add(dropItem);
206 }
207 return items;
208 }
209
210 private void log(String message) {
211 String name = ((NpcDefinition) definitions.toArray()[0]).getName();
212 System.out.println("[name=" + name + "] -- " + message);
213 }
214
215 }
216
217 private static class DropItem {
218 private final int id;
219 private final int min;
220 private final int max;
221 private final DropRarity rarity;
222
223 private DropItem(int id, int min, int max, DropRarity rarity) {
224 this.id = id;
225 this.min = min;
226 this.max = max;
227 this.rarity = rarity;
228 }
229
230 @Override
231 public int hashCode() {
232 return Objects.hash(id, min, max, rarity);
233 }
234
235 @Override
236 public boolean equals(Object obj) {
237 if (obj == this) return true;
238 if (obj instanceof DropItem) {
239 DropItem other = (DropItem) obj;
240 return other.id == id && other.min == min && other.max == max && other.rarity.equals(rarity);
241 }
242 return false;
243 }
244 }
245
253
254}
static final NpcDefinition[] DEFINITIONS
The array of npc definitions.
The manager class which holds the static entries of drop tables and has a method to execute a drop ta...
static final Map< Integer, NpcDropTable > NPC_DROPS
The collection of npc ids by their representative drop tables.
Represents all of an in-game Item's attributes.
static ItemDefinition[] DEFINITIONS
An array of item definitions.
DropItem(int id, int min, int max, DropRarity rarity)
static JsonArray buildDropItemTables(Collection< DropItem > drops)
NpcDropTable(String link, Collection< NpcDefinition > definitions)
static final Map< String, ItemDefinition > itemByName
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.
static final String WIKI_LINK
The path to the wiki page address.
WikiTableParser(LinkedList< WikiTable > tables)
Constructs a new WikiTableParser object.
final LinkedList< WikiTable > tables
The WikiTable list to parse.