forked from illmaren/FHGHQ
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dbfunctions.js
860 lines (812 loc) · 23.8 KB
/
dbfunctions.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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
// Here we keep all functions related to database requests
const SQLite = require('better-sqlite3');
const sql = SQLite('./.data/global-data.db');
const XMLHttpRequest = require('xhr2');
// const discordbot = require('./discordbot.js');
const socket = require('./socket.js');
let regionNames = [
'DeadLandsHex', // 3
'CallahansPassageHex', // 4
'MarbanHollow', // 5
'UmbralWildwoodHex', // 6
'MooringCountyHex', // 7
'HeartlandsHex', // 8
'LochMorHex', // 9
'LinnMercyHex', // 10
'ReachingTrailHex', // 11
'StonecradleHex', // 12
'FarranacCoastHex', // 13
'WestgateHex', // 14
'FishermansRowHex', // 15
'OarbreakerHex', // 16
'GreatMarchHex', // 17
'TempestIslandHex', // 18
'GodcroftsHex', // 19
'EndlessShoreHex', // 20
'AllodsBightHex', // 21
'WeatheredExpanseHex', // 22
'DrownedValeHex', // 23
'ShackledChasmHex', // 24
'ViperPitHex',
'MorgensCrossingHex',
'OriginHex',
'AshFieldsHex',
'RedRiverHex',
'ClansheadValleyHex',
'HowlCountyHex',
'BasinSionnachHex',
'SpeakingWoodsHex',
'CallumsCapeHex',
'NevishLineHex',
'KalokaiHex',
'AcrithiaHex',
'TerminusHex',
'TheFingersHex',
];
function UpdateMapList() {
const request = new XMLHttpRequest();
request.responseType = 'json';
request.open(
'GET',
'https://war-service-live.foxholeservices.com/api/worldconquest/maps/',
true
);
request.onload = function () {
if (request.status >= 200 && request.status < 400) {
try {
const obj = [];
for (let i = 0; i < this.response.length; i++) {
obj.push(this.response[i]);
}
regionNames = obj;
} catch (err) {
console.log(err);
}
}
};
request.send();
}
UpdateMapList(); // Getting the map list on launch.
setInterval(UpdateMapList, 60000 /* 100000 */); // Updating map list every minute.
// Checks if user is logged on
exports.logincheck = function logincheck(request) {
// console.log(request.headers);
if (request.headers.cookie == undefined) {
return false;
}
if (!request.headers.cookie.includes('salt')) {
return false;
}
const cookiestring = request.headers.cookie;
const id = cookiestring
.substring(
cookiestring.indexOf(' steamid=') + 9,
cookiestring.indexOf(' steamid=') + 26
)
.replace(';', '');
let salt = cookiestring
.substring(
cookiestring.indexOf(' salt=') + 6,
cookiestring.indexOf(' salt=') + 15
)
.replace(';', '');
const query = sql.prepare('SELECT * FROM users WHERE id= ?;').get(id);
// console.log("Validating id "+query.id+" | "+id);
// console.log("Validating salt "+query.salt+" | "+salt);
//console.log(id, salt, query);
if (query == undefined) {
return false;
}
query.salt = query.salt.slice(0, 9);
salt = salt.slice(0, 9);
// console.log(id==query.id)
if (query == undefined) {
return false;
}
if (id == query.id && salt == query.salt) {
return true;
}
return false;
};
exports.offlinecheck = function (online, io) {
const users = sql
.prepare("SELECT * FROM users WHERE id LIKE 'anonymous%';")
.all();
const now = new Date();
for (let i = 0; i < users.length; i++) {
if (users[i].id != 'anonymous') {
// console.log(users[i].id);
if (online.includes(users[i].id)) {
sql
.prepare('UPDATE users SET avatar = ? WHERE id = ?;')
.run(new Date().toString(), users[i].id);
} else {
const timediff = now - new Date(users[i].avatar);
if (timediff > 1800000) {
// console.log("Deleting user "+users[i].id+" | "+users[i].name);
const rooms = sql
.prepare('SELECT * FROM userglobal where userid = ?')
.all(users[i].id);
sql
.prepare('DELETE FROM userglobal WHERE userid = ?')
.run(users[i].id);
for (let j = 0; j < rooms.length; j++) {
io.to(rooms[j].globalid).emit('leaveroomroom', users[i].id);
}
sql.prepare('DELETE FROM users WHERE id = ?').run(users[i].id);
}
}
}
}
};
// Check if this user already exists in the database
exports.existscheck = function (id) {
const result = sql.prepare('SELECT * FROM users WHERE id= ?;').get(id);
// console.log(result);
if (result == undefined) {
return false;
}
return true;
};
// Gets account from the database
exports.getaccount = function (id) {
const result = sql.prepare('SELECT * FROM users WHERE id= ?;').get(id);
// console.log(result);
return result;
};
// Gets membership of a user
exports.getmembership = function (id, globalid) {
const result1 = sql
.prepare('SELECT * FROM global WHERE id = ?;')
.get(globalid);
if (result1 == undefined) {
return 8;
}
const result = sql
.prepare('SELECT rank FROM userglobal WHERE userid = ? AND globalid = ?;')
.get(id, globalid);
if (result == undefined) {
return 7;
}
return result.rank;
};
// Get info on rooms connected to a user
exports.getrooms = function (id) {
const rooms = sql
.prepare('SELECT * FROM userglobal WHERE userid = ?;')
.all(id);
const globalinfo = [];
// console.log(rooms);
if (rooms.length > 0) {
for (let i = 0; i < rooms.length; i++) {
const room = exports.getroominfo(rooms[i].globalid);
// console.log(room)
const settings = JSON.parse(room.settings);
const object = {
roomname: settings.name,
admin: room.adminname,
adminid: room.adminid,
rank: rooms[i].rank,
globalid: rooms[i].globalid,
};
// console.log(object);
globalinfo.push(object);
}
}
return globalinfo;
};
// Just gets the names of the rooms the user is in
exports.getroomsshort = function (id) {
const rooms = sql
.prepare('SELECT * FROM userglobal WHERE userid = ?;')
.all(id);
return rooms;
};
// Get meta info on room
exports.getroominfo = function (id) {
// console.log(id);
const result = sql.prepare('SELECT * FROM global WHERE id = ?;').get(id);
if (result != undefined) {
const adminname = sql
.prepare('SELECT * FROM users WHERE id = ?;')
.get(result.admin).name;
const packet = {
adminname,
adminid: result.admin,
settings: result.settings,
};
return packet;
}
return false;
};
// Get private info on room
exports.getprivateroominfo = function (id) {
const result = sql.prepare('SELECT * FROM global WHERE id = ?;').get(id);
// console.log(result)
const refinery = parse(result.refinery);
const production = parse(result.production);
const storage = parse(result.storage);
const stockpiles = parse(result.stockpiles);
const techtree = parse(result.techtree);
const fobs = parse(result.fobs);
const requests = parse(result.requests);
const misc = parse(result.misc);
const arty = parse(result.arty);
const squads = parse(result.squads);
const logi = parse(result.logi);
const events = parse(result.events);
return {
refinery,
production,
storage,
stockpiles,
techtree,
requests,
fobs,
misc,
arty,
squads,
logi,
events,
};
};
exports.getallevents = function () {
const events = sql
.prepare('select * from events order by date desc limit 400')
.all();
return { events };
};
// Create a room
exports.createroom = function (id, admin, settings) {
const insertroom = sql.prepare(
'INSERT INTO global (id, admin, settings, techtree,refinery, production, storage, stockpiles,fobs, requests, misc, arty,squads,logi,events) VALUES (@id, @admin, @settings, "", "","", "", "","", "", "", "", @squads, "","[]");'
);
settings = JSON.stringify(settings);
const squads = {
vehicles: {},
squads: [{ icon: 0, name: 'New Squad', users: [admin] }],
};
const e = {
id,
admin,
settings,
squads: JSON.stringify(squads),
};
if (admin.includes('anonymous')) {
e.admin = 'anonymous';
}
// console.log(e)
insertroom.run(e);
exports.insertrelation.run(e.admin + id, e.admin, id, 1);
if (admin.includes('anonymous')) {
exports.insertrelation.run(admin + id, admin, id, 3);
}
};
// Checks password
exports.checkpassword = function (id, password) {
let result = sql.prepare('SELECT settings FROM global WHERE id = ?;').get(id);
result = JSON.parse(result.settings);
// console.log(password);
// console.log(result.password);
if (password == result.password) {
return true;
}
return false;
};
// Leave a room
exports.leaveroom = function (id, globalid) {
const rank = exports.getmembership(id, globalid);
// console.log("exports rank: "+rank);
if (rank != 6) {
deleterelation.run(id, globalid);
// discordbot.leaveroom(id,globalid);
if (rank == 1) {
// discordbot.deleteroom(id,globalid);
socket.deleteroom(globalid);
exports.deleteroom(globalid);
}
}
};
// Delete a room
exports.deleteroom = function (id) {
sql.prepare('DELETE FROM userglobal WHERE globalid = ?').run(id);
sql.prepare('DELETE FROM global WHERE id = ?').run(id);
};
// Get members of a room
exports.getroomusers = function (id) {
const users = sql
.prepare(
'SELECT users.id, users.name, users.avatar, userglobal.rank, userglobal.role FROM users INNER JOIN userglobal WHERE users.id=userglobal.userid AND userglobal.globalid= ? ORDER BY (userglobal.rank) ASC;'
)
.all(id);
// console.log(users);
return users;
};
// Gets data of a user that requests access
exports.getrequester = function (id) {
const user = sql.prepare('SELECT * FROM users WHERE id = ?;').get(id);
const packet = {
id: user.id,
name: user.name,
avatar: user.avatar,
rank: 5,
};
return packet;
};
/* PortBase(4)
StaticBase1(5)
StaticBase2(6)
StaticBase3(7)
ForwardBase1(8)
ForwardBase2(9)
ForwardBase3(10)
Hospital (11)
VehicleFactory (12)
Armory (13)
SupplyStation (14)
Workshop (15)
ManufacturingPlant(16)
Refinery (17)
Shipyard (18)
TechCenter (19)
SalvageField (20)
ComponentField (21)
FuelField (22)
SulfurField (23)
WorldMapTent (24)
TravelTent (25)
TrainingArea (26)
SpecialBase (27) v0.14
ObservationTower(28) v0.14
Fort (29) v0.14
TroopShip (30) v0.14
ScrapMine (31) v0.16
SulfurMine (32) v0.16
StorageFacility (33) v0.17
Factory (34) v0.17
Garrison Station(35) v0.20
Ammo Factory (36) v0.20
Rocket Site (37) v0.20 */
// Get static icons
exports.getstatic = function () {
const staticdata = sql.prepare('SELECT * FROM apidata_static').all();
for (let i = 0; i < staticdata.length; i++) {
staticdata[i].data = JSON.parse(staticdata[i].data);
}
return staticdata;
};
// Get dynamic data
exports.getdynamic = function () {
const dynamic = sql.prepare('SELECT * FROM apidata_dynamic').all();
for (let i = 0; i < dynamic.length; i++) {
dynamic[i].data = JSON.parse(dynamic[i].data);
if (regionNames.includes(dynamic[i].regionName)) {
dynamic[i].active = true;
} else {
dynamic[i].active = false;
}
}
return dynamic;
};
exports.getevents = function () {
const events = sql
.prepare('select * from events order by date desc limit 500')
.all();
return events;
};
// Changes rank of users
exports.changeusers = function (subjid, objects, globalid) {
const subjrank = exports.getmembership(subjid, globalid);
const admin = subjid == exports.getroominfo(globalid).adminid;
if (subjrank < 3) {
for (let i = 0; i < objects.length; i++) {
const objrank = exports.getmembership(objects[i].id, globalid);
if (objrank != 7 && objrank != 8) {
const newrank = objects[i].rank;
if (newrank != undefined && newrank < 6) {
if (subjrank == 1 && newrank != 1) {
updaterank.run(newrank, objects[i].id, globalid);
}
if (subjrank == 2 && newrank != 1 && objrank != 2 && newrank != 2) {
updaterank.run(newrank, objects[i].id, globalid);
}
}
}
}
socket.updateusers(objects, globalid);
}
};
// Changes role
exports.setRole = function (user, globalid) {
const role = JSON.stringify(user.role);
updaterole.run(role, user.id, globalid);
};
// Transfer room ownership
exports.hailnewking = function (id, newadmin, globalid) {
const admin = id == exports.getroominfo(globalid).adminid;
const membership = exports.getmembership(newadmin, globalid);
if (admin && membership < 7) {
// console.log("newadmin = "+newadmin+", oldadmin = "+id+", globalid"+globalid);
sql
.prepare(
'UPDATE userglobal SET rank = 1 WHERE userid = ? AND globalid =?;'
)
.run(newadmin, globalid);
sql
.prepare(
'UPDATE userglobal SET rank = 2 WHERE userid = ? AND globalid =?;'
)
.run(id, globalid);
sql
.prepare('UPDATE global SET admin = ? WHERE id = ?;')
.run(newadmin, globalid);
socket.hailnewking(newadmin, globalid);
return true;
}
return false;
};
// Change levels of tech
exports.changetech = function (packet) {
sql
.prepare('UPDATE global SET techtree = ? WHERE id = ?;')
.run(packet.techtree, packet.globalid);
};
// Creates/updates an object
exports.updateObject = function (packet) {
// console.log(packet)
const global = sql
.prepare('SELECT * FROM global WHERE id = ?')
.get(packet.globalid);
// console.log(global);
if (packet.type.includes('misc')) {
let misc = {};
if (global.misc != '') {
misc = JSON.parse(global.misc);
}
const kind = packet.type.slice(5);
if (misc[kind] == undefined) {
misc[kind] = {};
}
misc[kind][packet.key] = packet.object;
misc = JSON.stringify(misc);
sql
.prepare('UPDATE global SET misc = ? WHERE id = ?;')
.run(misc, packet.globalid);
} else {
let type = {};
if (global[packet.type] != '') {
type = JSON.parse(global[packet.type]);
}
type[packet.key] = packet.object;
type = JSON.stringify(type);
sql
.prepare(`UPDATE global SET ${packet.type} = ? WHERE id = ?;`)
.run(type, packet.globalid);
}
};
// Deletes logi request
exports.deleteObject = function (packet) {
// console.log(packet)
const global = sql
.prepare('SELECT * FROM global WHERE id = ?')
.get(packet.globalid);
if (packet.type.includes('misc')) {
let misc = {};
if (global.misc != '') {
misc = JSON.parse(global.misc);
}
const kind = packet.type.slice(5);
if (misc[kind] == undefined) {
misc[kind] = {};
}
delete misc[kind][packet.key];
misc = JSON.stringify(misc);
sql
.prepare('UPDATE global SET misc = ? WHERE id = ?;')
.run(misc, packet.globalid);
} else {
let type = {};
if (global[packet.type] != '') {
type = JSON.parse(global[packet.type]);
}
delete type[packet.key];
type = JSON.stringify(type);
sql
.prepare(`UPDATE global SET ${packet.type} = ? WHERE id = ?;`)
.run(type, packet.globalid);
}
};
// Adds arty calculations
exports.addArtyResult = function (packet) {
// console.log(packet)
const global = sql
.prepare('SELECT arty FROM global WHERE id = ?')
.get(packet.globalid);
if (global.arty == '') {
global.arty = [];
} else {
global.arty = JSON.parse(global.arty);
}
if (global.arty.constructor != Array) {
global.arty = [];
}
global.arty.push(packet.totalstring);
if (global.arty.length > 5) {
global.arty.splice(0, 1);
}
global.arty = JSON.stringify(global.arty);
sql
.prepare('UPDATE global SET arty = ? WHERE id = ?;')
.run(global.arty, packet.globalid);
};
// Updates squads
exports.updateSquads = function (packet) {
const global = sql
.prepare('SELECT * FROM global WHERE id = ?')
.get(packet.globalid);
if (global.squads == '' || global.squads == '[]') {
global.squads = {};
} else {
global.squads = JSON.parse(global.squads);
}
global.squads[packet.type] = packet.data;
global.squads = JSON.stringify(global.squads);
// console.log("Updating squads",global.squads)
// let squads = JSON.stringify(packet.squads);
sql
.prepare('UPDATE global SET squads = ? WHERE id = ?;')
.run(global.squads, packet.globalid);
};
// Adds a private event
exports.submitEvent = function (packet) {
// console.log(packet)
const global = sql
.prepare('SELECT * FROM global WHERE id = ?')
.get(packet.globalid);
switch (packet.type) {
case 0: // TECH EVENT
// discordbot.techEvent(global, packet.packet);
break;
}
let events = [];
if (global.events != '') {
events = JSON.parse(global.events);
}
events.push({ type: packet.type, date: packet.date, packet: packet.packet });
if (events.length > 400) {
events = events.splice(1);
}
// console.log(packet)
events = JSON.stringify(events);
sql
.prepare('UPDATE global SET events = ? WHERE id = ?')
.run(events, packet.globalid);
};
exports.submitOpTimer = function (packet) {
const global = sql
.prepare('SELECT * FROM global WHERE id = ?')
.get(packet.globalid);
let settings = JSON.parse(global.settings);
settings.optimer = packet.date;
global.settings = settings;
// discordbot.startOpTimer(global);
settings = JSON.stringify(settings);
sql
.prepare('UPDATE global SET settings = ? WHERE id = ?')
.run(settings, packet.globalid);
};
exports.toggleSecure = function (globalid, data) {
const global = sql.prepare('SELECT * FROM global WHERE id = ?').get(globalid);
let settings = JSON.parse(global.settings);
settings.secure = data;
settings = JSON.stringify(settings);
sql
.prepare('UPDATE global SET settings = ? WHERE id = ?')
.run(settings, globalid);
if (data == 1) {
sql
.prepare(
'DELETE FROM userglobal WHERE userid LIKE "anonymous%" AND globalid = ?;'
)
.run(globalid);
const users = exports.getroomusers(globalid);
return users;
}
};
exports.clearRoom = function (globalid) {
const global = sql.prepare('SELECT * FROM global WHERE id = ?').get(globalid);
const e = {
id: global.id,
admin: global.admin,
settings: global.settings,
};
sql
.prepare(
'INSERT OR REPLACE INTO global (id, admin, settings, techtree,refinery, production, storage, stockpiles,fobs, requests, misc, arty,squads,logi,events) VALUES (@id, @admin, @settings, "", "","", "", "","", "", "", "", "", "","[]");'
)
.run(e);
};
exports.clearMap = function (globalid) {
const global = sql.prepare('SELECT * FROM global WHERE id = ?').get(globalid);
let misc = {};
if (global.misc != '') {
misc = JSON.parse(global.misc);
if (misc.rld != undefined) {
misc.rld = {};
}
if (misc.icon != undefined) {
misc.icon = {};
}
}
misc = JSON.stringify(misc);
sql
.prepare('UPDATE global SET fobs="",requests="",misc=? WHERE id = ?')
.run(misc, globalid);
};
exports.changeSettings = function (globalid, type, data) {
const global = sql.prepare('SELECT * FROM global WHERE id = ?').get(globalid);
let settings = JSON.parse(global.settings);
settings[type] = data;
settings = JSON.stringify(settings);
sql
.prepare('UPDATE global SET settings = ? WHERE id = ?')
.run(settings, globalid);
};
exports.deleteSettings = function (globalid, type) {
const global = sql.prepare('SELECT * FROM global WHERE id = ?').get(globalid);
let settings = JSON.parse(global.settings);
// if (type == 'link') {
// discordbot.disconnectDiscord(globalid, settings);
// }
delete settings[type];
settings = JSON.stringify(settings);
// console.log("Deleting optimer")
// console.log(settings)
sql
.prepare('UPDATE global SET settings = ? WHERE id = ?')
.run(settings, globalid);
};
exports.getRoomByToken = function (token, link) {
const text = `%"token":"${token}"%`;
const global = sql
.prepare('SELECT * FROM global WHERE settings LIKE ?;')
.get(text);
// console.log(global)
if (global != undefined) {
global.settings = JSON.parse(global.settings);
global.settings.link = link;
delete global.settings.token;
const settings = JSON.stringify(global.settings);
sql
.prepare('UPDATE global SET settings = ? WHERE id = ?')
.run(settings, global.id);
}
return global;
};
exports.getRoomFromDiscordChannel = function (channelid) {
const text = `%"channelid":"${channelid}"%`;
const global = sql
.prepare('SELECT * FROM global WHERE settings LIKE ?;')
.get(text);
return global;
};
// Adds chat messsage
exports.addMessage = function (packet, category, globalid) {
// console.log(packet)
const global = sql.prepare('SELECT * FROM global WHERE id = ?').get(globalid);
// console.log(global);
let misc = {};
if (global.misc != '') {
misc = JSON.parse(global.misc);
}
if (misc.chat == undefined) {
misc.chat = {};
}
if (misc.chat[category] == undefined) {
misc.chat[category] = [];
}
misc.chat[category].push(packet);
misc = JSON.stringify(misc);
sql.prepare('UPDATE global SET misc = ? WHERE id = ?;').run(misc, globalid);
};
// SQL Constant commands
exports.insertuser = sql.prepare(
'INSERT INTO users (id, salt, name, avatar) VALUES (?, ?, ?, ?);'
);
exports.updateuser = sql.prepare(
'UPDATE users SET name = ?, avatar = ? WHERE id = ?;'
);
exports.insertrelation = sql.prepare(
'INSERT OR REPLACE INTO userglobal (id, userid, globalid, rank, role) VALUES (?, ?, ?, ?, "[0,0]");'
);
const deleterelation = sql.prepare(
'DELETE FROM userglobal WHERE userid = ? AND globalid = ?;'
);
const updaterank = sql.prepare(
'UPDATE userglobal SET rank = ? WHERE userid = ? AND globalid =?;'
);
const updaterole = sql.prepare(
'UPDATE userglobal SET role = ? WHERE userid = ? AND globalid =?;'
);
function parse(string) {
// console.log(string)
if (string == '') {
return {};
}
return JSON.parse(string);
}
setInterval(CheckOpTimers, 60000 /* 100000 */);
function CheckOpTimers() {
let date = new Date();
date.setHours(date.getHours() + 1);
date = JSON.stringify(date);
date = date.substring(0, 17);
// console.log(date)
const globallist = sql
.prepare(
'SELECT settings FROM global WHERE settings LIKE ? AND settings LIKE "%channelid%" '
)
.all(`%"optimer":${date}%`);
if (globallist.length > 0) {
for (let i = 0; i < globallist.length; i++) {
const settings = JSON.parse(globallist[i].settings);
// discordbot.emitNotifyOp(settings);
}
}
// console.log("Op check global list")
// console.log(globallist)
}
exports.GetWar = function (warnumber) {
const war = sql
.prepare('SELECT * FROM warhistory WHERE warnumber = ?')
.get(warnumber);
return war;
};
exports.GetTownName = function (obj, regionId) {
const region = sql
.prepare('SELECT * FROM apidata_static WHERE regionId = ?')
.get(regionId);
// console.log("Town name - region",region)
const data = JSON.parse(region.data);
const name = GetTownName(data.mapTextItems, obj);
// console.log("Town name - got it",name)
return name;
};
exports.GetCounts = function () {
const steamcount = sql
.prepare('SELECT count(id) FROM users WHERE id NOT LIKE "anon%";')
.get();
const nosteamcount = sql
.prepare('SELECT count(id) FROM users WHERE id LIKE "anonymous_%";')
.get();
return {
steamcount: steamcount['count(id)'],
nosteamcount: nosteamcount['count(id)'],
};
};
function GetTownName(labellist, town) {
function compare(a, b) {
if (a.distance < b.distance) {
return -1;
}
if (a.distance > b.distance) {
return 1;
}
return 0;
}
for (let i = 0; i < labellist.length; i++) {
const xdif = Math.abs(town.x - labellist[i].x);
const ydif = Math.abs(town.y - labellist[i].y);
const distance = Math.sqrt(Math.pow(xdif, 2) + Math.pow(ydif, 2));
labellist[i].distance = distance;
}
labellist.sort(compare);
try {
return labellist[0].text;
} catch (err) {
return 'undefined';
}
}
exports.cunt = function test(string) {
console.log(`Database functions module ${string}`);
};