RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
NpcDropsParser.java
1package com.osroyale.util.tools.wiki.impl;
2
3import com.google.common.collect.LinkedListMultimap;
4import com.google.gson.JsonArray;
5import com.google.gson.JsonObject;
6import com.osroyale.game.world.entity.mob.npc.definition.NpcDefinition;
7import com.osroyale.game.world.entity.mob.npc.drop.NpcDropManager;
8import com.osroyale.game.world.items.ItemDefinition;
9import com.osroyale.util.parser.impl.NpcDropParser;
10import com.osroyale.util.tools.wiki.parser.WikiTable;
11import com.osroyale.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
56
57public class NpcDropsParser extends WikiTableParser {
58
59 private static final Map<String, ItemDefinition> itemByName = new HashMap<>();
60
61 private NpcDropsParser() {
62 super(generateTables());
63 }
64
65 private static LinkedList<WikiTable> generateTables() {
66 ItemDefinition.createParser().run();
67
68 for (ItemDefinition definition : ItemDefinition.DEFINITIONS) {
69 if (definition == null || definition.getName() == null || definition.getName().equals("null") || itemByName.containsKey(definition.getName())) {
70 continue;
71 }
72 itemByName.put(definition.getName(), definition);
73 }
74
75 LinkedListMultimap<String, NpcDefinition> byName = LinkedListMultimap.create();
76 NpcDefinition.createParser().run();
77 new NpcDropParser().run();
78
79 for (NpcDefinition definition : NpcDefinition.DEFINITIONS) {
80 if (definition == null || definition.getName() == null || definition.getName().equals("null")) {
81 continue;
82 }
83
84 if (!definition.isAttackable()) {
85 continue;
86 }
87
88 if (NpcDropManager.NPC_DROPS.containsKey(definition.getId())) {
89 continue;
90 }
91
92 byName.put(definition.getName(), definition);
93 }
94
95 LinkedList<WikiTable> tables = new LinkedList<>();
96// tables.add(new NpcDropTable(WIKI_LINK.concat("King Black Dragon".replaceAll(" ", "_")), ImmutableList.of(NpcDefinition.DEFINITIONS[239])));
97
98 for (Map.Entry<String, Collection<NpcDefinition>> entry : byName.asMap().entrySet()) {
99 String name = entry.getKey();
100 name = name.replaceAll(" ", "_");
101 int idx = name.indexOf("(");
102 if (idx > -1)
103 name = name.substring(0, idx);
104 tables.add(new NpcDropTable(WIKI_LINK.concat(name), entry.getValue()));
105 }
106 return tables;
107 }
108
109 @Override
110 protected void finish() {
111 JsonArray array = new JsonArray();
112 for (WikiTable table : tables) {
113 array.addAll(table.getTable());
114 }
115 writeToJson("wiki_drop_dump", array);
116 }
117
118 public static void main(String[] args) throws InterruptedException {
119 NpcDropsParser parser = new NpcDropsParser();
120 parser.begin();
121 }
122
123 private static class NpcDropTable extends WikiTable {
124 private final Collection<NpcDefinition> definitions;
125
126 NpcDropTable(String link, Collection<NpcDefinition> definitions) {
127 super(link);
128 this.definitions = definitions;
129 }
130
131 @Override
132 protected void parseDocument(Document document) {
133 Elements dropTable = document.select(".wikitable.sortable.dropstable");
134 Elements td = dropTable.select("td");
135 Iterator<Element> iterator = td.iterator();
136
137 List<DropItem> drops = new LinkedList<>();
138 boolean rareDropTable = false;
139
140 while (iterator.hasNext()) {
141 iterator.next();
142 String name = iterator.next().text();
143 String amount = iterator.next().text().replaceAll(",", "");
144 String rarity = iterator.next().text().toLowerCase();
145 iterator.next();
146
147 if (!itemByName.containsKey(name)) {
148 log("missing item: " + name);
149 if (name.contains("Rare drop table")) {
150 rareDropTable = true;
151 }
152 continue;
153 }
154
155 if (amount.contains("Unknown")) {
156 log("unknown drop amount: " + name);
157 continue;
158 }
159
160 try {
161 int id, min, max;
162
163 int notedIndex = amount.indexOf("(noted)");
164 if (notedIndex > -1) {
165 amount = amount.substring(0, notedIndex - 1).trim();
166 id = itemByName.get(name).getNotedId();
167 } else {
168 id = itemByName.get(name).getId();
169 }
170
171 DropRarity rate;
172 if (rarity.startsWith("very rare")) {
173 rate = DropRarity.VERY_RARE;
174 } else if (rarity.startsWith("rare")) {
175 rate = DropRarity.RARE;
176 } else if (rarity.startsWith("uncommon")) {
177 rate = DropRarity.UNCOMMON;
178 } else if (rarity.startsWith("common")) {
179 rate = DropRarity.COMMON;
180 } else if (rarity.startsWith("always")) {
181 rate = DropRarity.ALWAYS;
182 } else {
183 log("unknown drop rarity: " + rarity);
184 continue;
185 }
186
187 if (amount.contains(";")) {
188 String[] amounts = amount.split("; ");
189 for (String s : amounts) {
190 int hyphen = s.indexOf("–");
191 if (hyphen > -1) {
192 min = Integer.valueOf(s.substring(0, hyphen));
193 max = Integer.valueOf(s.substring(hyphen + 1));
194 } else {
195 min = max = Integer.valueOf(s);
196 }
197 drops.add(new DropItem(id, min, max, rate));
198 }
199 } else {
200 int hyphen = amount.indexOf("–");
201 if (hyphen > -1) {
202 min = Integer.valueOf(amount.substring(0, hyphen));
203 max = Integer.valueOf(amount.substring(hyphen + 1));
204 } else {
205 min = max = Integer.valueOf(amount);
206 }
207 drops.add(new DropItem(id, min, max, rate));
208 }
209 } catch (Exception e) {
210 e.printStackTrace();
211 }
212 }
213
214 if (drops.isEmpty()) {
215 return;
216 }
217
218 JsonObject object = new JsonObject();
219 JsonArray array = new JsonArray();
220
221 for (NpcDefinition definition : definitions) {
222 array.add(definition.getId());
223 }
224
225 drops.sort(Comparator.comparing(drop -> drop.rarity));
226
227 object.add("ids", array);
228 object.addProperty("rare_table", rareDropTable);
229 object.add("drops", buildDropItemTables(drops));
230 table.add(object);
231 }
232
233 private static JsonArray buildDropItemTables(Collection<DropItem> drops) {
234 JsonArray items = new JsonArray();
235
236 for (DropItem item : drops) {
237 JsonObject dropItem = new JsonObject();
238
239 dropItem.addProperty("id", item.id);
240 dropItem.addProperty("minimum", item.min);
241 dropItem.addProperty("maximum", item.max);
242 dropItem.addProperty("type", item.rarity.name());
243
244 items.add(dropItem);
245 }
246 return items;
247 }
248
249 private void log(String message) {
250 String name = ((NpcDefinition) definitions.toArray()[0]).getName();
251 System.out.println("[name=" + name + "] -- " + message);
252 }
253
254 }
255
256 private static class DropItem {
257 private final int id;
258 private final int min;
259 private final int max;
260 private final DropRarity rarity;
261
262 private DropItem(int id, int min, int max, DropRarity rarity) {
263 this.id = id;
264 this.min = min;
265 this.max = max;
266 this.rarity = rarity;
267 }
268
269 @Override
270 public int hashCode() {
271 return Objects.hash(id, min, max, rarity);
272 }
273
274 @Override
275 public boolean equals(Object obj) {
276 if (obj == this) return true;
277 if (obj instanceof DropItem) {
278 DropItem other = (DropItem) obj;
279 return other.id == id && other.min == min && other.max == max && other.rarity.equals(rarity);
280 }
281 return false;
282 }
283 }
284
285private enum DropRarity {
286 ALWAYS,
287 COMMON,
288 UNCOMMON,
289 RARE,
290 VERY_RARE
291 }
292
293}
static void writeToJson(String name, JsonElement array)