-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathscript.js
More file actions
370 lines (307 loc) · 11.7 KB
/
script.js
File metadata and controls
370 lines (307 loc) · 11.7 KB
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
/*jshint esversion: 6 */
// Array to store all the entered crops
let crops = {};
// Current crop id
let cid = -1;
// Current work settings
let search_settings = {
settings_type : 0 // 0 => 'desired-gene' or 1 => 'priority-sliders'
};
// Web worker (genetics-worker.js)
let worker;
// Onload
$(function(){
// Initialize values
search_settings.y_priority = parseFloat(document.getElementById('y-priority').value);
search_settings.g_priority = parseFloat(document.getElementById('g-priority').value);
search_settings.h_priority = parseFloat(document.getElementById('h-priority').value);
search_settings.y_count = parseFloat(document.getElementById('y-count').value);
search_settings.g_count = parseFloat(document.getElementById('g-count').value);
search_settings.h_count = parseFloat(document.getElementById('h-count').value);
// Make sure browser has support for web workers
if(!window.Worker){
alert("This browser doesn't support this website");
return;
}
worker = new Worker('genetics-worker.js');
worker.onmessage = processWorkerMessage;
loadLocalCrops();
// Initialize all tooltips
// $('[data-toggle="tooltip"]').tooltip()
});
// Helper function to dynamicaly create elements
function createElement(parent, type, text, classList){
let el = document.createElement(type);
el.innerHTML = text;
el.classList = classList;
parent.appendChild(el);
return el;
}
// Feedback form callback
function feedback() {
let text = document.getElementById('text').value;
let contact = document.getElementById('contact').value;
let http = new XMLHttpRequest();
let url = 'feedback.php?text=' + encodeURIComponent(text) + '&contact=' + encodeURIComponent(contact);
http.open('GET', url);
http.send();
document.getElementById('feedback-form').reset();
$('#feedbackCollapse').collapse('hide');
let btn = document.getElementById('btn-feedback');
btn.textContent = 'Thank you';
btn.classList = 'btn btn-success';
}
// Form callback function which handles adding of a new crop
function addCrop(updateCalc = true){
let add_crop = document.getElementById('add-crop');
// Validate input
let crop = add_crop.value.toUpperCase();
if(crop.search(/^[YGHWXyghwx]{6}$/) > -1){
// Prevent duplicates
if (Object.values(crops).indexOf(crop) != -1) {
alert(crop + " is already added to your crops!");
return;
}
// Store and display crop
crops[++cid] = crop;
add_crop.value = '';
displayCrop(crop);
saveLocalCrops();
// Then update calculation
if(updateCalc)
settingsChanged();
// I am interested if this is even being used and what is the statistics of plants being calculated, hope
// you don't mind if I store these crops in a database. DM me if you are interested getting the data.
// I am doing this for free so I would ask you kindly not to abuse this endpoint by sending bullshit data. Thanks
let http = new XMLHttpRequest();
http.open('GET', 'statistics.php?genes=' + crop);
http.send();
}
else{
return false;
}
}
// Function to import list of crops from a file
function importCrops(input){
let file = input.files[0];
let reader = new FileReader();
reader.readAsText(file);
reader.onload = function() {
let inp = reader.result.split('\n').map(s => s.trim());
if(inp[0] == 'genes'){
clearCrops();
for(let i = 1; i < inp.length; i++){
if(i < inp.length - 1){
addCropByGene(inp[i]);
}
else{
addCropByGene(inp[i], true);
}
}
}
else{
alert("File has wrong format:\nFirst line must be \"genes\" then list of genes row by row");
}
};
}
// Load crops saved in local storage
function loadLocalCrops() {
if (! window.localStorage) return;
if (! window.localStorage.getItem('crops')) {
clearCrops();
return;
}
let inp = window.localStorage.getItem('crops').split('\n').map(s => s.trim());
clearCrops();
for(let i = 0; i < inp.length; i++){
if(i < inp.length - 1){
addCropByGene(inp[i]);
}
else{
addCropByGene(inp[i], true);
}
}
}
// Save current crops to local storage
function saveLocalCrops() {
if (! window.localStorage) return;
window.localStorage.setItem('crops', Object.values(crops).join('\n'));
}
// Function to export crops to a file
function exportCrops(){
let file = new Blob(["genes\n" + Object.values(crops).join('\n')], {type: 'text/csv'});
let filename = 'genes.txt';
// IE10+
if (window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveOrOpenBlob(file, filename);
}
// Others
else {
let a = document.createElement("a");
let url = URL.createObjectURL(file);
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
setTimeout(function() {
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
}, 0);
}
}
// Function to rename the crop list
function renameTitle() {
const newTitle = prompt("Enter new name for these crops");
if (!newTitle) return;
document.getElementById('crop-title').innerText = newTitle;
document.title = `${newTitle} - Rust Genetics Calculator`;
}
// Button callback to delete added crop
function deleteCrop(crop_id){
if(Object.values(crops).length == 1)
clearCrops();
else {
delete crops[crop_id];
saveLocalCrops();
settingsChanged();
}
}
// Helper function for debugging
function addCropByGene(gene, updateCalc = false) {
let add_crop = document.getElementById('add-crop');
add_crop.value = gene;
addCrop(updateCalc);
}
// Function to display added crop in 'my crops'
function displayCrop(crop){
document.getElementById('my-crops').hidden = false;
let list = document.getElementById('crop-list');
let li = createElement(list, 'li', '', 'mt-1');
let del_btn = createElement(li, 'button', 'X', 'btn btn-del');
let del_id = cid;
del_btn.addEventListener('click', function (){ deleteCrop(del_id); li.remove(); });
for(let i = 0; i < 6; i++){
let c = crop.charAt(i);
createElement(li, 'span', c, (c == 'X' || c == 'W' ? 'bad' : 'good') + ' gene');
}
}
// Function to reset the calculator
function clearCrops(){
crops = {};
saveLocalCrops();
worker.postMessage('reject');
document.getElementById('crop-list').innerHTML = '';
document.getElementById('my-crops').hidden = true;
document.getElementById('calculation').innerHTML = '';
}
// Gene value enum
const U = 0 << 0;
const W = 1 << 0;
const X = 1 << 1;
const Y = 1 << 2;
const G = 1 << 3;
const H = 1 << 4;
// Good an bad gene bitmasks
// HGYXW
const GOOD = 0b11100;
const BAD = 0b00011;
function processWorkerMessage(e){
// Stop loading animation
document.getElementById('calc-loading').hidden = true;
document.getElementById('calculation').innerHTML = '';
// If any good crossbreeding is found
let calculation_div = document.getElementById('calculation');
if(e.data.max_crop_parents.length > 0){
// Display parents
createElement(calculation_div, 'h3', 'Crossbreed these', '');
e.data.max_crop_parents.forEach(function (crop) {
for(let i = 0; i < 6; i++){
let c = crop.charAt(i);
createElement(calculation_div, 'span', c, (c == 'X' || c == 'W' ? 'bad' : 'good') + ' gene mt-1');
}
createElement(calculation_div, 'br', '', '');
});
// And the result
createElement(calculation_div, 'br', '', '');
createElement(calculation_div, 'h3', 'to get', '');
for(let i = 0; i < 6; i++){
let g = e.data.max_crop[i];
let bad = [];
let good = [];
if((g & W) > 0) bad.push('W');
if((g & X) > 0) bad.push('X');
if((g & Y) > 0) good.push('Y');
if((g & G) > 0) good.push('G');
if((g & H) > 0) good.push('H');
if(g == U) good.push('?');
let gene_div = createElement(calculation_div, 'div', '', 'multi-gene mt-1');
if(bad.length > 0){
createElement(gene_div, 'span', bad.join('<br>'), 'bad gene' + (good.length > 0 ? ' gene-border-nb' : ''));
}
if(good.length > 0){
createElement(gene_div, 'span', good.join('<br>'), 'good gene' + (bad.length > 0 ? ' gene-border-nt' : ''));
}
}
createElement(calculation_div, 'br', '', '');
}
else if(e.data.max_crop != undefined){
createElement(calculation_div, 'h3', 'Best crop is', '');
for(let i = 0; i < 6; i++){
let g = e.data.max_crop[i];
let bad = [];
let good = [];
if((g & W) > 0) bad.push('W');
if((g & X) > 0) bad.push('X');
if((g & Y) > 0) good.push('Y');
if((g & G) > 0) good.push('G');
if((g & H) > 0) good.push('H');
if(g == U) good.push('?');
let gene_div = createElement(calculation_div, 'div', '', 'multi-gene mt-1');
if(bad.length > 0){
createElement(gene_div, 'span', bad.join('<br>'), 'bad gene' + (good.length > 0 ? ' gene-border-nb' : ''));
}
if(good.length > 0){
createElement(gene_div, 'span', good.join('<br>'), 'good gene' + (bad.length > 0 ? ' gene-border-nt' : ''));
}
}
}
}
// Function callback when calculation parameters are changed
function settingsChanged(force = false) {
search_settings.y_priority = parseFloat(document.getElementById('y-priority').value);
search_settings.g_priority = parseFloat(document.getElementById('g-priority').value);
search_settings.h_priority = parseFloat(document.getElementById('h-priority').value);
search_settings.y_count = parseFloat(document.getElementById('y-count').value);
search_settings.g_count = parseFloat(document.getElementById('g-count').value);
search_settings.h_count = parseFloat(document.getElementById('h-count').value);
if(Object.values(crops).length == 0) return;
if(search_settings.settings_type == 0 && (search_settings.y_count + search_settings.g_count + search_settings.h_count) != 6) {
let calculation_div = document.getElementById('calculation');
calculation_div.innerHTML = '';
createElement(calculation_div, 'span', 'Count needs to add to 6!', 'badge rounded-pill bg-danger bad mx-auto mt-5 d-block');
return;
}
if(Object.values(crops).length > 9 && !force) {
let calculation_div = document.getElementById('calculation');
calculation_div.innerHTML = '';
let btn = createElement(calculation_div, 'button', 'Start calculation', 'btn btn-success good mx-auto mt-5 d-block');
btn.addEventListener('click', function (){ calculateBest(true); });
return;
}
calculateBest();
}
// Function to calculate and display the best crop combination
function calculateBest(){
// Stop any running calculations
worker.postMessage('reject');
// Show calculation loading animation loading and delete old result
document.getElementById('calc-loading').hidden = false;
document.getElementById('calculation').innerHTML = '';
// Pack data for web worker
let work_data = {
genes : Object.values(crops),
search_settings : search_settings,
};
// Start calculation
worker.postMessage(work_data);
}