forked from frogamic/sansan-messaging-grid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnrdb-local.js
266 lines (259 loc) · 11 KB
/
nrdb-local.js
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/* nrdb-local.js
* Written by Dominic Shelton.
* Provides functions for searching and storing a local copy of the netrunnedb card database.
*/
'use strict';
var request = require('request');
var schedule = require('node-schedule');
var FuzzySearch = require('fuzzysearch-js');
var LevenshteinFS = require('fuzzysearch-js/js/modules/LevenshteinFS');
var IndexOfFS = require('fuzzysearch-js/js/modules/IndexOfFS');
// var SiftFS = require('fuzzysearch-js/js/modules/Sift3FS');
var nrdbCardURL = 'https://netrunnerdb.com/api/2.0/public/cards';
var nrdbMWLURL = 'https://netrunnerdb.com/api/2.0/public/mwl';
var nrdbDeckURL = 'https://netrunnerdb.com/api/2.0/public/decklist/';
var nrdbDeckDisplayURL = 'https://netrunnerdb.com/en/decklist/';
var nrdbCardDisplayURL = 'https://netrunnerdb.com/en/card/';
var updateTime = process.env.UPDATE_TIME;
var cards = {};
var fuzzySearch;
var scheduledUpdate;
var loadPromise;
var indexPromise;
module.exports = {
init: init,
getCardByTitle: getCardByTitle,
getCardByCode: getCardByCode,
getDecklist: getDecklist
};
// Cards in a decklist need a type and quantity, return them via promise.
function createDecklistCard (code, quantity) {
return new Promise (function (resolve, reject) {
getCardByCode(code).then(function (card) {
var newCard = {};
newCard.type = card.type;
newCard.quantity = quantity;
newCard.card = card;
resolve(newCard);
}, reject);
});
}
function getDecklist (id) {
return new Promise (function (resolve, reject) {
request(nrdbDeckURL + id, function (error, response, body) {
if (error || response.statusCode !== 200) {
return reject(error);
}
var decklist = {cards: {}};
var deck = JSON.parse(body).data[0];
var promises = [];
decklist.name = deck.name;
decklist.url = nrdbDeckDisplayURL + id;
decklist.creator = deck.user_name;
Object.keys(deck.cards).forEach((code) => {
promises.push(createDecklistCard(code, deck.cards[code]));
});
Promise.all(promises).then(function (values) {
values.forEach((card, i) => {
var type = card.card.type;
// The ICE type must be further broken into subtypes.
if (type === 'ICE') {
var typematch = card.card.subtype.match(/(Barrier|Code\ Gate|Sentry)/g);
if (!typematch) {
// The ice matched no standard type
type = 'Other';
} else if (typematch.length > 1) {
// The ice matched multiple types
type = 'Multi';
} else {
// The ice matched only a single type
type = typematch[0];
}
// Likewise Icebreakers must be separated from programs.
} else if (type === 'Program' && card.card.subtype
&& card.card.subtype.match(/Icebreaker/)) {
type = 'Icebreaker';
}
// Add the card to the array of the correct type.
if (!decklist.cards[type]) {
decklist.cards[type] = [];
}
decklist.cards[type].push({
quantity: card.quantity, card: card.card
});
});
for (let type in decklist.cards) {
decklist.cards[type].sort(function (a, b) {
return a.card.title.localeCompare(b.card.title);
});
}
resolve(decklist);
}, function (err) {
console.log(err);
reject (err)
});
});
});
}
// Get a card by its nrdb code.
function getCardByCode (code) {
return new Promise (function (resolve, reject) {
loadPromise.then(function () {
if (cards[code]) {
resolve(cards[code]);
} else {
reject();
}
});
});
}
// Search for a card by title text (fuzzy)
function getCardByTitle (text) {
text = text.trim();
return new Promise (function (resolve, reject) {
if (text.length < 2) {
resolve(undefined);
} else {
indexPromise.then(function (cardArray) {
// Perform a fuzzy search for the card title.
var results = fuzzySearch.search(text);
if (results) {
// Output the fuzzysearch score for each card found on the console.
// console.info('\n\nSearching for ' + text);
// results.forEach((result, i) => {
// var details = '';
// result.details.forEach((detail) => {
// details += detail.name + ': ' + detail.score + ' ';
// });
// console.info(i +': ' + result.value.title + ' score: ' + result.score + '\t\t' + details);
// });
// Return the best hit.
getCardByCode(results[0].value.code).then(resolve, reject);
}else{
// Fall back to an acronym search.
var acronym = new RegExp(text.replace(/\W/g, '').replace(/(.)/g, '\\b$1.*?'), 'i');
var result = cardArray.find(function (e) {
return e.title.match(acronym);
});
if (results) {
getCardByCode(result.code).then(resolve, reject);
} else {
resolve(undefined);
}
}
});
}
});
}
// Initialise the local card array
function init (cardArray) {
// Initialised as a promise so requests can be queued while the db is downloading.
loadPromise = new Promise (function (resolve, reject) {
if (cardArray) {
console.log('Used passed card DB');
resolve(cardArray);
} else {
// Load the json data from netrunnerdb.
request(nrdbCardURL, function (error, response, body) {
if (error || response.statusCode !== 200) {
console.error('Failed to fetch card DB: status', response.statusCode);
reject (error);
} else {
cardArray = JSON.parse(body).data;
// Cancel any other updates as only the newest is necessary.
if (scheduledUpdate) {
scheduledUpdate.cancel();
}
console.log('Fetched card DB');
// Schedule the init method to be called again at the next midnight (NZ time)
var date = new Date();
if (date.getUTCHours() >= updateTime) {
date.setDate(date.getDate() + 1);
}
date.setUTCHours(updateTime);
date.setMinutes(0);
date.setSeconds(0);
date.setMilliseconds(0);
console.log('Scheduling DB update for ' + date.toUTCString());
scheduledUpdate = schedule.scheduleJob(date, init);
resolve(cardArray);
}
});
}
});
// Create a new promise for the most-wanted-list
var mwlPromise = new Promise ((resolve, reject) => {
loadPromise.then(() => {
// Fetch the Most Wanted List from the NetrunnerDB API
request(nrdbMWLURL, (error, response, body) => {
if (error || response.statusCode !== 200) {
console.error('Failed to fetch Most Wanted List: status', response.statusCode);
reject (error);
} else {
// The nrdb api provides several possible MWL rules, choose the latest official one.
var rulesets = JSON.parse(body).data;
var latest = {name: "Empty List", start: "1970-01-01", cards: {}};
rulesets.forEach((rule) => {
if (rule.active) {
latest = rule;
}
});
console.log('Using Most Wanted List: ', latest.name);
resolve (latest.cards);
}
});
});
});
// Create another promise for the search indexing of the db so that title searches can be queued.
indexPromise = new Promise ((resolve, reject) => {
Promise.all([loadPromise, mwlPromise]).then((results) => {
var cardArray = results[0];
var liteArray = [];
var mwl = results[1];
cardArray.forEach((card) => {
var liteCard = {
title: card.title,
text: card.text,
code: card.code,
uniqueness: card.uniqueness,
side: card.side_code,
faction: card.faction_code,
type: card.type_code.replace(/^(ice|[a-z])/, (c) => {return c.toUpperCase();}),
subtype: card.keywords,
factioncost: card.faction_cost,
baselink: card.base_link,
cost: card.cost,
memoryunits: card.memory_cost,
strength: card.strength,
trash: card.trash_cost,
advancementcost: card.advancement_cost,
minimumdecksize: card.minimum_deck_size,
influencelimit: card.influence_limit,
agendapoints: card.agenda_points,
url: nrdbCardDisplayURL + card.code
}
// Add a mwl flag to each card on the Most Wanted List.
if(mwl[card.code]) {
liteCard.mwl = mwl[card.code];
}
// Add the card to the card sparse array for quick lookup.
cards[card.code] = liteCard;
liteArray.push({title: card.title, code: card.code});
});
// Index the CardArray.
fuzzySearch = new FuzzySearch(liteArray, {
'caseSensitive': false,
'termPath': 'title',
'minimumScore': 200
});
fuzzySearch.addModule(IndexOfFS({'minTermLength': 3, 'maxIterations': 500, 'factor': 2}));
// fuzzySearch.addModule(SiftFS({'maxDistanceTolerance': 4, 'factor': 1}));
fuzzySearch.addModule(LevenshteinFS({'maxDistanceTolerance': 4, 'factor': 1}));
console.log('Cards indexed');
resolve(liteArray);
}, (error) => {
reject (error);
});
});
return indexPromise;
}