-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathumbra.user.js
458 lines (371 loc) · 15.8 KB
/
umbra.user.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
// ==UserScript==
// @id iitc-plugin-umbra
// @name IITC plugin: Umbra Deploy Challenge
// @category Misc
// @version 0.1.1.20191224.211839
// @description [2019-12-24-211839] Allow manual entry of portals deployed during Umbra Deploy Challenge. Use the 'highlighter-umbra' plugin to show the portals on the map, and 'sync' to share between multiple browsers or desktop/mobile.
// @updateURL https://raw.githubusercontent.com/mrDinckleman/iitc-plugins/master/umbra.user.js
// @downloadURL https://raw.githubusercontent.com/mrDinckleman/iitc-plugins/master/umbra.user.js
// @namespace https://github.com/mrDinckleman/iitc-plugins
// @include https://intel.ingress.com/*
// @match https://intel.ingress.com/*
// @grant none
// ==/UserScript==
/* globals $ */
function wrapper(plugin_info) {
// Ensure plugin framework is there, even if iitc is not yet loaded
if (typeof window.plugin !== 'function') window.plugin = function () {};
// PLUGIN START //////////////////////////////////////////////////////////
var pluginName = 'umbra';
// Use own namespace for plugin
window.plugin[pluginName] = function () {};
// Delay in ms
window.plugin[pluginName].SYNC_DELAY = 5000;
// Maps the JS property names to localStorage keys
window.plugin[pluginName].FIELDS = {
'deployed': 'plugin-' + pluginName + '-data',
'updateQueue': 'plugin-' + pluginName + '-data-queue',
'updatingQueue': 'plugin-' + pluginName + '-data-updating-queue'
};
window.plugin[pluginName].deployed = {};
window.plugin[pluginName].updateQueue = {};
window.plugin[pluginName].updatingQueue = {};
window.plugin[pluginName].enableSync = false;
window.plugin[pluginName].disabledMessage = null;
window.plugin[pluginName].contentHTML = null;
window.plugin[pluginName].isHighlightActive = false;
window.plugin[pluginName].onPortalDetailsUpdated = function () {
var $preview = $('#portaldetails > .imgpreview');
if (typeof(Storage) === 'undefined') {
$preview.after(window.plugin[pluginName].disabledMessage);
return;
}
var guid = window.selectedPortal;
$preview.after(window.plugin[pluginName].contentHTML);
window.plugin[pluginName].updateCheckedAndHighlight(guid);
};
window.plugin[pluginName].updateCheckedAndHighlight = function (guid) {
if (guid === window.selectedPortal) {
var deployCount = window.plugin[pluginName].deployed[guid] || 0;
$('#umbra-deploy').val(deployCount);
}
if (window.plugin[pluginName].isHighlightActive) {
if (window.portals[guid]) {
window.setMarkerStyle(window.portals[guid], guid === window.selectedPortal);
}
}
};
window.plugin[pluginName].updateDeployed = function (count, guid) {
if (typeof guid === 'undefined') guid = window.selectedPortal;
count = parseInt(count, 10);
var deployCount = window.plugin[pluginName].deployed[guid] || 0;
if (!deployCount) {
window.plugin[pluginName].deployed[guid] = deployCount = 0;
}
// Nothing changed
if (count === deployCount) return;
window.plugin[pluginName].deployed[guid] = count;
window.plugin[pluginName].updateCheckedAndHighlight(guid);
window.plugin[pluginName].sync(guid);
$('select.umbra-deploy[data-guid="' + guid + '"]').val(count);
$('#dialog-' + pluginName + '-dialog').prev().find('.ui-dialog-title').text(window.plugin[pluginName].getModalTitle());
};
// Stores the given GUID for sync
window.plugin[pluginName].sync = function (guid) {
window.plugin[pluginName].updateQueue[guid] = true;
window.plugin[pluginName].storeLocal('deployed');
window.plugin[pluginName].storeLocal('updateQueue');
window.plugin[pluginName].syncQueue();
};
// Sync the queue, but delay the actual sync to group a few updates in a single request
window.plugin[pluginName].syncQueue = function () {
if (!window.plugin[pluginName].enableSync) return;
clearTimeout(window.plugin[pluginName].syncTimer);
window.plugin[pluginName].syncTimer = setTimeout(function () {
window.plugin[pluginName].syncTimer = null;
$.extend(window.plugin[pluginName].updatingQueue, window.plugin[pluginName].updateQueue);
window.plugin[pluginName].updateQueue = {};
window.plugin[pluginName].storeLocal('updatingQueue');
window.plugin[pluginName].storeLocal('updateQueue');
window.plugin.sync.updateMap('umbra', 'deployed', Object.keys(window.plugin[pluginName].updatingQueue));
}, window.plugin[pluginName].SYNC_DELAY);
};
// Call after IITC and all plugin loaded
window.plugin[pluginName].registerFieldForSyncing = function () {
if (!window.plugin.sync) return;
window.plugin.sync.registerMapForSync(
'umbra',
'deployed',
window.plugin[pluginName].syncCallback,
window.plugin[pluginName].syncInitialed
);
};
// Call after local or remote change uploaded
window.plugin[pluginName].syncCallback = function (inPluginName, fieldName, e, fullUpdated) {
if (fieldName === 'deployed') {
window.plugin[pluginName].storeLocal('deployed');
// All data is replaced if other client update the data during this client offline
if (fullUpdated) {
// A full update - update the selected portal sidebar
if (window.selectedPortal) {
window.plugin[pluginName].updateCheckedAndHighlight(window.selectedPortal);
}
// And also update all highlights, if needed
if (window.plugin[pluginName].isHighlightActive) {
window.resetHighlightedPortals();
}
return;
}
if (!e) return;
if (e.isLocal) {
// Update pushed successfully, remove it from updatingQueue
delete window.plugin[pluginName].updatingQueue[e.property];
} else {
// Remote update
delete window.plugin[pluginName].updateQueue[e.property];
window.plugin[pluginName].storeLocal('updateQueue');
window.plugin[pluginName].updateCheckedAndHighlight(e.property);
}
}
};
// Syncing of the field is initialed, upload all queued update
window.plugin[pluginName].syncInitialed = function (inPluginName, fieldName) {
if (fieldName === 'deployed') {
window.plugin[pluginName].enableSync = true;
if (Object.keys(window.plugin[pluginName].updateQueue).length > 0) {
window.plugin[pluginName].syncQueue();
}
}
};
window.plugin[pluginName].storeLocal = function (name) {
var key = window.plugin[pluginName].FIELDS[name];
if (key === undefined) return;
var value = window.plugin[pluginName][name];
if (typeof value !== 'undefined' && value !== null) {
localStorage[key] = JSON.stringify(window.plugin[pluginName][name]);
} else {
localStorage.removeItem(key);
}
};
window.plugin[pluginName].loadLocal = function (name) {
var key = window.plugin[pluginName].FIELDS[name];
if (key === undefined) return;
if (localStorage[key] !== undefined) {
window.plugin[pluginName][name] = JSON.parse(localStorage[key]);
}
};
/**
* HIGHLIGHTER
*/
window.plugin[pluginName].highlighter = {
highlight: function (data) {
var guid = data.portal.options.ent[0];
var deployCount = window.plugin[pluginName].deployed[guid] || 0;
var style = {};
switch (deployCount) {
case 0:
style.fillColor = hsl(0); // hsl(0,100%,50%)
style.fillOpacity = 0.7;
break;
case 1:
style.fillColor = hsl(1);
style.fillOpacity = 0.7;
break;
case 2:
style.fillColor = hsl(1.5);
style.fillOpacity = 0.7;
break;
case 3:
style.fillColor = hsl(2);
style.fillOpacity = 0.7;
break;
case 4:
style.fillColor = hsl(2.5);
style.fillOpacity = 0.6;
break;
case 5:
style.fillColor = hsl(3);
style.fillOpacity = 0.6;
break;
case 6:
style.fillColor = hsl(4);
style.fillOpacity = 0.6;
break;
case 7:
style.fillColor = hsl(6);
style.fillOpacity = 0.8;
break;
case 8:
// fully deployed - no highlights
break;
}
data.portal.setStyle(style);
function hsl(index) { // from 0 to 8
return 'hsl(' + (index / 8 * 120) + ', 100%, ' + (50 - index / 8 * 25) + '%)';
}
},
setSelected: function (active) {
window.plugin[pluginName].isHighlightActive = active;
}
};
window.plugin[pluginName].setupCSS = function () {
$('<style>')
.prop('type', 'text/css')
.html('#' + pluginName + '-container {'
+ 'display: block;'
+ 'text-align: center;'
+ 'margin: 6px 3px 1px 3px;'
+ 'padding: 0 4px;}'
+ '.' + pluginName + '-container label {'
+ 'white-space: nowrap;'
+ 'margin: 0 0.3em;}'
+ '.' + pluginName + '-container select {'
+ 'vertical-align: middle;}'
+ '#' + pluginName + '-portals {'
+ 'border-collapse: collapse;'
+ 'empty-cells: show;'
+ 'width: 100%;}'
+ '#' + pluginName + '-portals th, #' + pluginName + '-portals td {'
+ 'padding: 3px; color: white;'
+ ' background-color: #1b415e;'
+ ' border-bottom: 1px solid #0b314e;}')
.appendTo('head');
};
window.plugin[pluginName].setupContent = function () {
window.plugin[pluginName].contentHTML = '<div class="' + pluginName + '-container" id="' + pluginName + '-container">Umbra '
+ '<select id="' + pluginName + '-deploy" onchange="window.plugin.' + pluginName + '.updateDeployed($(this).val())">'
+ '<option value="0">0</option>'
+ '<option value="1">1</option>'
+ '<option value="2">2</option>'
+ '<option value="3">3</option>'
+ '<option value="4">4</option>'
+ '<option value="5">5</option>'
+ '<option value="6">6</option>'
+ '<option value="7">7</option>'
+ '<option value="8">8</option>'
+ '</select>'
+ '</div>';
window.plugin[pluginName].disabledMessage = '<div id="' + pluginName + '-container" class="help" title="Your browser does not support localStorage">Plugin Umbra disabled</div>';
$("#toolbox").append('<a onclick="window.plugin.' + pluginName + '.showList();" title="Show portals for Umbra Deploy Challenge">Umbra portals</a>');
};
window.plugin[pluginName].isPortalInPolygon = function (portal, latLngsObjectsArray) {
var portalCoords = portal.split(',');
var x = portalCoords[0];
var y = portalCoords[1];
var inside = false;
for (var i = 0, j = latLngsObjectsArray.length - 1; i < latLngsObjectsArray.length; j = i++) {
var xi = latLngsObjectsArray[i].lat, yi = latLngsObjectsArray[i].lng;
var xj = latLngsObjectsArray[j].lat, yj = latLngsObjectsArray[j].lng;
var intersect = ((yi > y) != (yj > y)) && (x < (xj - xi) * (y - yi) / (yj - yi) + xi);
if (intersect) inside = !inside;
}
return inside;
};
window.plugin[pluginName].getList = function () {
var drawLayer = null;
var list = [];
if (localStorage['plugin-draw-tools-layer']) {
drawLayer = JSON.parse(localStorage['plugin-draw-tools-layer']);
}
var portals = window.portals;
portalLoop: for (var i in portals) {
if (!portals.hasOwnProperty(i)) continue;
var p = window.portals[i];
var name = p.options.data.title;
var guid = p.options.guid;
var latlng = p._latlng.lat + ',' + p._latlng.lng;
if (drawLayer) {
for (var dl in drawLayer) {
if (drawLayer[dl].type === 'polygon') {
if (!window.plugin[pluginName].isPortalInPolygon(latlng, drawLayer[dl].latLngs)) continue portalLoop;
}
}
}
var b = window.map.getBounds();
// skip if not currently visible
if (p._latlng.lat < b._southWest.lat || p._latlng.lng < b._southWest.lng || p._latlng.lat > b._northEast.lat || p._latlng.lng > b._northEast.lng) continue;
var lat = latlng.split(',')[0];
var lng = latlng.split(',')[1];
list.push({
title: name,
guid: guid,
latlng: latlng
});
}
return list.sort(function (a, b) {
if (a.title > b.title) return 1;
if (a.title < b.title) return -1;
return 0;
});
};
window.plugin[pluginName].countTotal = function () {
var data = window.plugin[pluginName].deployed;
var total = 0;
for (var guid in data) {
total += (data[guid] || 0);
}
return total;
};
window.plugin[pluginName].showList = function () {
var portals = window.plugin[pluginName].getList();
var html = '<table class="' + pluginName + '-container" id="' + pluginName + '-portals"><thead>';
html += '</thead><tbody>';
html += '<tr><th>#</th><th>Portal Name</th>'
+ '<th></th></tr>';
for (var i = 0; i < portals.length; i++) {
var guid = portals[i].guid;
var deployCount = window.plugin[pluginName].deployed[guid] || 0;
html += '<tr><td>' + (i + 1) + '</td>'
+ '<td><a href="https://www.ingress.com/intel?ll=' + portals[i].latlng + '&z=17&pll=' + portals[i].latlng + '">' + portals[i].title + '</a></td>'
+ '<td><select class="' + pluginName + '-deploy" data-guid="' + guid + '" onchange="window.plugin.' + pluginName + '.updateDeployed($(this).val(), \'' + guid + '\')">'
+ '<option value="0"' + (deployCount == 0 ? ' selected' : '') + '>0</option>'
+ '<option value="1"' + (deployCount == 1 ? ' selected' : '') + '>1</option>'
+ '<option value="2"' + (deployCount == 2 ? ' selected' : '') + '>2</option>'
+ '<option value="3"' + (deployCount == 3 ? ' selected' : '') + '>3</option>'
+ '<option value="4"' + (deployCount == 4 ? ' selected' : '') + '>4</option>'
+ '<option value="5"' + (deployCount == 5 ? ' selected' : '') + '>5</option>'
+ '<option value="6"' + (deployCount == 6 ? ' selected' : '') + '>6</option>'
+ '<option value="7"' + (deployCount == 7 ? ' selected' : '') + '>7</option>'
+ '<option value="8"' + (deployCount == 8 ? ' selected' : '') + '>8</option>'
+ '</select></td></tr>';
}
html += '</tbody></table>';
var dialog = window.dialog({
id: pluginName + '-dialog',
title: window.plugin[pluginName].getModalTitle(),
dialogClass: 'ui-dialog-' + pluginName,
html: html,
width: 500
});
};
window.plugin[pluginName].getModalTitle = function () {
return 'Umbra Portals (Total ' + window.plugin[pluginName].countTotal() + ' points)';
};
var setup = function () {
window.plugin[pluginName].setupCSS();
window.plugin[pluginName].setupContent();
window.plugin[pluginName].loadLocal('deployed');
window.addPortalHighlighter('Umbra', window.plugin[pluginName].highlighter);
window.addHook('portalDetailsUpdated', window.plugin[pluginName].onPortalDetailsUpdated);
window.addHook('iitcLoaded', window.plugin[pluginName].registerFieldForSyncing);
};
// PLUGIN END //////////////////////////////////////////////////////////
// Add the script info data to the function as a property
setup.info = plugin_info;
if (!window.bootPlugins) window.bootPlugins = [];
window.bootPlugins.push(setup);
// If IITC has already booted, immediately run the 'setup' function
if (window.iitcLoaded && typeof setup === 'function') setup();
} // wrapper end
// Inject code into site context
var script = document.createElement('script');
var info = {};
if (typeof GM_info !== 'undefined' && GM_info && GM_info.script) {
info.script = {
version: GM_info.script.version,
name: GM_info.script.name,
description: GM_info.script.description
};
}
script.appendChild(document.createTextNode('(' + wrapper + ')(' + JSON.stringify(info) + ');'));
(document.body || document.head || document.documentElement).appendChild(script);