-
Notifications
You must be signed in to change notification settings - Fork 1
/
FixCreatures.java
183 lines (167 loc) · 7.82 KB
/
FixCreatures.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package com.wikia.tibia.usecases;
import com.wikia.tibia.repositories.WikiArticleRepository;
import net.sourceforge.jwbf.core.contentRep.Article;
import net.sourceforge.jwbf.mediawiki.actions.queries.CategoryMembersSimple;
import net.sourceforge.jwbf.mediawiki.bots.MediaWikiBot;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/** */
public class FixCreatures {
private static final Logger log = LoggerFactory.getLogger(FixCreatures.class);
private static final List ITEMS_WITH_NO_DROPPEDBY_LIST = Arrays.asList("Gold Coin", "Platinum Coin");
private static final List EVENT_ITEMS = Arrays.asList(
"Bunch of Winterberries",
"Envelope from the Wizards",
"Fireworks Rocket",
"Party Trumpet",
"Party Hat",
"Silver Raid Token",
"Golden Raid Token"
);
private static final List NON_EVENT_CREATURES_DROPPING_SNOWBALLS = Arrays.asList("Yeti", "Grynch Clan Goblin");
private static final String REGEX_LOOT_ITEM = "\\{\\{Loot Item";
private static final String REGEX_LOOT_ITEM_NAME = "\\{\\{Loot Item\\|(.*?)([A-Z].*?)}}";
private static final String REGEX_DROPPED_BY = "\\{\\{Dropped By\\|(.*?)}}";
private static final String REGEX_DEPRECATED_OR_EVENT = "\\{\\{(Deprecated|Event)}}";
private static final boolean DEBUG = false;
private HashMap<String, String> pluralItems = new HashMap<>();
private MediaWikiBot mediaWikiBot;
private WikiArticleRepository repository;
public FixCreatures(MediaWikiBot mediaWikiBot) {
this.mediaWikiBot = mediaWikiBot;
this.repository = new WikiArticleRepository(mediaWikiBot);
pluralItems.put("Bowls of *", "Bowl of");
pluralItems.put("Bunches of", "Bunch of");
pluralItems.put("Cookies", "Cookie");
pluralItems.put("Flasks of *", "Flask of");
pluralItems.put("Gooey Masses", "Gooey Mass");
pluralItems.put("Haunches of *", "Haunch of");
pluralItems.put("Mushroom Pies", "Mushroom Pie");
pluralItems.put("Pieces of *", "Piece of");
pluralItems.put("Small Rubies", "Small Ruby");
pluralItems.put("Veins of *", "Vein of");
}
public void checkCreatures() {
CategoryMembersSimple categoryMembers = repository.getMembersFromCategory("Creatures");
for (String creaturePageName : categoryMembers) {
Article article = repository.getArticle(creaturePageName);
String articleText = article.getText();
if (!articleDeprecatedOrEvent(articleText) && articleTextContainsLootItems(articleText)) {
List<String> listOfLootItems = makeListOfLootItems(creaturePageName, articleText);
for (String lootItem : listOfLootItems) {
checkIfCreatureNameIsPresent(creaturePageName, lootItem);
}
}
}
}
private boolean articleDeprecatedOrEvent(String articleText) {
Pattern p = Pattern.compile(REGEX_DEPRECATED_OR_EVENT);
Matcher m = p.matcher(articleText);
return m.find();
}
private boolean articleTextContainsLootItems(String articleText) {
Pattern p = Pattern.compile(REGEX_LOOT_ITEM);
Matcher m = p.matcher(articleText);
return m.find();
}
private List<String> makeListOfLootItems(String creaturePageName, String articleText) {
List<String> lootItems = new ArrayList<>();
String lootItemNamePrecise;
Pattern patternRough = Pattern.compile(REGEX_LOOT_ITEM_NAME);
Matcher matcherRough = patternRough.matcher(articleText);
while (matcherRough.find()) {
String lootItemNameRough = matcherRough.group(2);
int pipePosition = lootItemNameRough.indexOf('|');
if (pipePosition > 0) {
lootItemNamePrecise = lootItemNameRough.substring(0, pipePosition);
} else {
lootItemNamePrecise = lootItemNameRough;
}
if (itemShouldBeAdded(creaturePageName, lootItemNamePrecise)) {
String lootItemNameSingular = changePluralToSingular(lootItemNamePrecise);
lootItems.add(lootItemNameSingular);
}
}
return lootItems;
}
// TODO implement something more general like:
// lootparser_p_ends = {'che': 'ch', 'she': 'sh', 'ie': 'y', 've': 'fe', 'oe': 'o', 'ze': 'z'}
// lootparser_to_singular = function (t) {
// var x, lastletter;
// for (x in lootparser_p_words) {
// if (lootparser_p_words.hasOwnProperty(x)) {
// if ((new RegExp('^' + x.replace(/\*/g, '.*?') + '$')).test(t)) {
// return t.replace(x.replace(/\*/g, ''), lootparser_p_words[x]);
// }
// }
// }
// lastletter = t.slice(t.length - 1);
// if (lastletter === 's') {
// t = t.slice(0, t.length - 1); /*remove the s*/
// lastletter = t.slice(t.length - 3); /*check last 3 letters*/
// if (lootparser_p_ends[lastletter] !== undefined) {
// t = t.slice(0, t.length - 3) + lootparser_p_ends[lastletter];
// }
// lastletter = t.slice(t.length - 2); /*check last 2 letters*/
// if (lootparser_p_ends[lastletter] !== undefined) {
// t = t.slice(0, t.length - 2) + lootparser_p_ends[lastletter];
// }
// }
// return t;
// }
private String changePluralToSingular(String lootItemNamePrecise) {
String lootItemNameSingular = lootItemNamePrecise;
if (pluralItems.containsKey(lootItemNamePrecise)) {
lootItemNameSingular = pluralItems.get(lootItemNamePrecise);
}
return lootItemNameSingular;
}
private boolean itemShouldBeAdded(String creaturePageName, String lootItemNamePrecise) {
if ("Snowball".equals(lootItemNamePrecise)) {
return NON_EVENT_CREATURES_DROPPING_SNOWBALLS.contains(creaturePageName);
}
if (ITEMS_WITH_NO_DROPPEDBY_LIST.contains(lootItemNamePrecise)) {
return false;
}
if (EVENT_ITEMS.contains(lootItemNamePrecise)) {
return false;
}
return true;
}
private void checkIfCreatureNameIsPresent(String creaturePageName, String lootItem) {
Article article = repository.getArticle(lootItem);
String articleText = article.getText();
Pattern p = Pattern.compile(REGEX_DROPPED_BY);
Matcher m = p.matcher(articleText);
if (m.find()) {
String creatureNames = m.group(1);
if (!creatureNames.contains(creaturePageName)) {
String newCreatureNames = "{{Dropped By|" + creatureNames + "|" + creaturePageName + "}}";
addMissingCreatureNameToDroppedByList(creaturePageName, article, newCreatureNames);
}
} else {
log.warn("No DroppedBy template encountered on item '{}' while trying to add creature '{}'.", lootItem, creaturePageName);
}
}
private void addMissingCreatureNameToDroppedByList(String creaturePageName, Article article, String textToInsert) {
String articleText = article.getText();
Pattern p = Pattern.compile(REGEX_DROPPED_BY);
Matcher m = p.matcher(articleText);
if (m.find()) {
String newArticleText = m.replaceAll(textToInsert);
article.setText(newArticleText);
article.setEditSummary(String.format("[bot] adding creature '%s' to item '%s'.", creaturePageName, article.getTitle()));
if (DEBUG) {
article.save();
} else {
log.info("[bot] adding creature '{}' to item '{}'.", creaturePageName, article.getTitle());
}
}
}
}