forked from quisquous/cactbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
seabase.js
375 lines (316 loc) · 9.74 KB
/
seabase.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import gFisherData from './static-data';
export default class SeaBase {
constructor(options) {
this._dbName = 'seabase';
this._dbVersion = 1;
this._storeName = 'catches';
this.db = null;
this.options = options;
this.parserLang = this.options.ParserLanguage;
}
findKey(obj, val) {
// This is a little inefficient to lowercase everything here.
// However, we want the real capitalization to use for UI so can't
// lowercase it in the data unless we made another copy.
const lcVal = val.toLowerCase();
return Object.keys(obj).find((key) => {
if (Array.isArray(obj[key]))
return obj[key].some((subVal) => subVal.toLowerCase() === lcVal);
return obj[key].toLowerCase() === lcVal;
});
}
firstIfArray(obj) {
if (Array.isArray(obj))
return obj[0];
return obj;
}
getConnection() {
return new Promise((resolve, reject) => {
const req = window.indexedDB.open(this._dbName, this._dbVersion);
req.onsuccess = (event) => {
resolve(req.result);
};
req.onerror = (event) => {
reject(req.error);
};
req.onupgradeneeded = (event) => {
const db = event.target.result;
const tx = event.target.transaction;
let objectStore;
if (!db.objectStoreNames.contains(this._storeName))
objectStore = db.createObjectStore(this._storeName, { autoIncrement: true });
else
objectStore = tx.objectStore(this._storeName);
if (!objectStore.indexNames.contains('fish'))
objectStore.createIndex('fish', 'fish', { unique: false });
if (!objectStore.indexNames.contains('fishbaitchum'))
objectStore.createIndex('fishbaitchum', ['fish', 'bait', 'chum'], { unique: false });
tx.oncomplete = (event) => {
resolve(db);
};
};
});
}
getTransaction(db, mode) {
return db.transaction(this._storeName, mode);
}
getIQRThresholds(times) {
// first, calculate IQR to get a threshold for outliers
let q1;
let q3;
times.sort((a, b) => {
return a - b;
});
// if there's less than 5 items, just assume it's legit
if (times.length < 5) {
return {
low: times[0],
high: times[times.length - 1],
};
}
// find q2 (median)
// we only need the index for the median
const q2Index = Math.floor(times.length / 2);
// find q1 (median of first half)
const q1Index = Math.floor(q2Index / 2);
if (q2Index % 2 || q2Index === 0)
q1 = times[q1Index];
else
q1 = (times[q1Index] + times[q1Index - 1]) / 2;
// find q2 (median of second half)
const q3Index = q1Index + q2Index;
if (q3Index % 2 || q2Index === 0)
q3 = times[q3Index];
else
q3 = (times[q3Index] + times[q3Index - 1]) / 2;
const iqr = q3 - q1;
// use these to calculate thresholds for outliers
return {
low: q1 - iqr * 1.5,
high: q3 + iqr * 1.5,
};
}
normalizeHooks(times) {
const thresholds = this.getIQRThresholds(times);
let min;
let max;
let i;
// Iterate forward until a suitable minimum
for (i = 0; i < times.length; i++) {
if (times[i] >= thresholds.low) {
min = times[i];
break;
}
}
// Iterate backward until a suitable maximum
for (i = times.length - 1; i >= 0; i--) {
if (times[i] <= thresholds.high) {
max = times[i];
break;
}
}
return {
min: min,
max: max,
};
}
addCatch(data) {
// Add a catch to the database
let commit = true;
// Make sure we have complete data before recording
const keys = [
'fish',
'bait',
'place',
'castTimestamp',
'hookTime',
'reelTime',
'chum',
'snagging',
];
for (const index in keys) {
if (
!Object.prototype.hasOwnProperty.call(data, keys[index]) ||
data[keys[index]] === null
) {
commit = false;
console.log(keys[index] + 'missing in catch');
}
}
if (!commit)
return false;
this.getConnection().then((db) => {
const tx = db.transaction(this._storeName, 'readwrite');
const store = tx.objectStore(this._storeName);
store.add(data);
});
}
getInfo(lookup, value) {
// Note: the name entry may be a single string, or it may
// be an array with multiple values in it. The first name
// in the array is the canonical value and should always be
// returned, even if looking up by another name in its list.
// This lets getPlace("german grammar used only when casting")
// return the correct place name to display in the ui.
let info;
// Value can be one of three things
if (typeof value === 'object' && value !== null) {
// 1. Object with id and/or name
// If we have one and not the other, fill in the other
if (value.id && !value.name) {
info = {
id: value.id,
name: this.firstIfArray(gFisherData[lookup][this.parserLang][value.id]),
};
} else if (!value.id && value.name) {
// Return the first / primary name regardless of what is passed in
// when doing a reverse lookup by name.
const key = this.findKey(gFisherData[lookup][this.parserLang], value.name);
info = {
id: key,
name: this.firstIfArray(gFisherData[lookup][this.parserLang][key]),
};
} else {
info = value;
}
} else if (isNaN(value)) {
// 2. String with the name
// See note above about reverse lookups.
const key = this.findKey(gFisherData[lookup][this.parserLang], value);
info = {
id: key,
name: this.firstIfArray(gFisherData[lookup][this.parserLang][key]),
};
} else {
// 3. Number with the ID
info = {
id: value,
name: this.firstIfArray(gFisherData[lookup][this.parserLang][value]),
};
}
return info;
}
getFish(fish) {
const result = this.getInfo('fish', fish);
if (!result.id || !result.name)
console.log('failed to look up fish: ' + fish);
return result;
}
getBait(bait) {
const result = this.getInfo('tackle', bait);
if (!result.id || !result.name)
console.log('failed to look up bait: ' + bait);
return result;
}
getPlace(place) {
const result = this.getInfo('places', place);
if (!result.id || !result.name)
console.log('failed to look up place: ' + place);
return result;
}
getFishForPlace(place) {
// Get place object
const placeObject = this.getPlace(place);
// Get fish IDs for place ID
const fishList = gFisherData['placefish'][placeObject.id];
// Get fish names for IDs
const placeFish = [];
for (const fishID in fishList)
placeFish.push(this.getFish(fishList[fishID]));
return placeFish;
}
queryHookTimes(index, fish, bait, chum) {
const times = [];
return new Promise((resolve, reject) => {
index.openCursor(IDBKeyRange.only([fish.id.toString(), bait.id, chum ? 1 : 0]))
.onsuccess = (event) => {
const cursor = event.target.result;
if (cursor) {
times.push(cursor.value.hookTime);
if (times.length < this.options.IQRHookQuantity)
cursor.continue();
else
resolve(times);
} else {
resolve(times);
}
};
});
}
getHookTimes(fish, bait, chum) {
if (!fish || !bait) {
return new Promise((resolve, reject) => {
resolve();
});
}
return new Promise((resolve, reject) => {
this.getConnection().then((db) => {
const tx = db.transaction(this._storeName, 'readwrite');
const store = tx.objectStore(this._storeName);
const index = store.index('fishbaitchum');
this.queryHookTimes(index, fish, bait, chum).then((times) => {
if (!times.length)
resolve({ min: undefined, max: undefined });
resolve(this.normalizeHooks(times));
});
});
});
}
queryTug(index, fish) {
const reelTimes = [];
return new Promise((resolve, reject) => {
index.openCursor(IDBKeyRange.only(fish.id.toString())).onsuccess = (event) => {
const cursor = event.target.result;
if (cursor) {
reelTimes.push(cursor.value.reelTime);
if (reelTimes.length < this.options.IQRTugQuantity)
cursor.continue();
else
resolve(reelTimes);
} else {
resolve(reelTimes);
}
};
});
}
getTug(fish) {
return new Promise((resolve, reject) => {
this.getConnection().then((db) => {
const tx = db.transaction(this._storeName, 'readwrite');
const store = tx.objectStore(this._storeName);
const index = store.index('fish');
const tug = gFisherData['tugs'][fish.id];
if (tug) {
resolve(tug);
} else {
this.queryTug(index, fish).then((reelTimes) => {
if (!reelTimes.length)
resolve(0);
const thresholds = this.getIQRThresholds(reelTimes);
let sum = 0;
let validValues = 0;
reelTimes.forEach((time) => {
if (time >= thresholds.low && time <= thresholds.high) {
sum += time;
validValues++;
}
});
const average = sum / validValues;
let tug;
// Small: <8000
// Medium: >8000, <10700
// Large: >10700
// 1 small, 2 medium, 3 large
if (average < 8000)
tug = 1;
else if (average > 10700)
tug = 3;
else
tug = 2;
resolve(tug);
});
}
});
});
}
}