-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathundercards.user.js
More file actions
1251 lines (1195 loc) · 44.3 KB
/
undercards.user.js
File metadata and controls
1251 lines (1195 loc) · 44.3 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
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
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ==UserScript==
// @name UnderCards script
// @description Various changes to undercards game
// @require https://raw.githubusercontent.com/feildmaster/SimpleToast/1.10.1/simpletoast.js
// @require https://raw.githubusercontent.com/feildmaster/UnderScript/0.17/utilities.js
// @version 0.17.3
// @author feildmaster
// @history 0.17.3 - Fixed smooth scrolling for small resolutions, when your deck isn't full (Thanks Liryax)
// @history 0.17.2 - Fixed a display issue when opening packs
// @history 0.17.1 - Fixed smooth scrolling for small resolutions
// @history 0.17 - Added "quick open" for packs (ctrl+click), smooth scrolling deck list, fixed hover boxes being odd with scrolling,
// "Open all" packs (shift+click), displays a toast with compressed results
// @history 0.16 - Prevent custom game screen from timing out (as often), fixed 'endTurn' hotkeys
// @history 0.15.1 - Fixed small game lists taking up so much space
// @history 0.15 - Added a "mention" button (Thanks LampLighter), fix display of chat window, some settings I made with Ceveno in mind
// @history 0.14 - Utilize the full home page space (for viewing spectator games)
// @history 0.13.1 - Fixed chat bugs caused by this script, fixed end turn button moving
// @history 0.13 - Ignore chat messags? Yes please. (Thanks CoolEdge)
// @history 0.12 - New look for "Skin Shop" & Added "Dust Counter" (Thanks Jake Horror)
// @history 0.11.5 - The following now work again: end turn "fixes", deck auto-sort, deck preview.
// @history 0.11.4 - Fix issue where script was not loading
// @history 0.11.3 - Fix mines (and other potential cards) staying around on the baord for too long
// @history 0.11.2 - End turn once per turn, and add a 3 second delay. Fix middle click
// @history 0.11.1 - Peaking at cards now adds them to the battle log, join queue button stays disabled when the server is restarting
// @history 0.11 - Fix transparent deck preview, automatically sort deck
// @history 0.10.3 - Fix refreshing page, Log artifact activations
// @history 0.10.2 - Bump version so broken updates work (hopefully)
// @history 0.10.1 - Moved file to proper extension (makes fresh installs easier)
// @history 0.10 - Added deck card preview
// @history 0.9.2 - Fixed enemy names *again* (copy/pasting is bad)
// @history 0.9.1 - Spectate result music is now disabled if you disable music playing.
// @history 0.9.0 - Added detailed history log, log is top-bottom now, battle end is now a toast
// @history 0.8.5 - Added some game debug
// @history 0.8.4 - Removed "remember deck" feature (upstream), fixed event log
// @history 0.8.3 - Script works now
// @history 0.8.2 - Fix the queue disconnecting.
// @history 0.8.1 - Rework loading jQuery performance
// @history 0.8 - Better performance and reliability. Disable the join queue buttons until they are ready
// @history 0.7 - updated to new restrictions, thanks cloudflare -_-
// @history 0.6 - some upgrades to the battle log, fixed url
// @history 0.5.4 - Don't scroll the battle log with the page (possibly make this configurable later)
// @history 0.5.3 - Remove the chat stuff, the new chat is better.
// @history 0.5.2 - do the same for the chat window
// @history 0.5.1 - don't cover the battle screen
// @history 0.5 - remember chat messages on page-change, added a battle log, lots of code changes
// @history 0.4 - Remember "event deck" too!, also fixed bugs.
// @history 0.3 - Lowered "game found" volume
// @history 0.2 - Added EndTurn hotkey (space, middle click), focus chat (enter)
// @history 0.1 - Made deck selection smart
// @match https://*.undercards.net/*
// @homepage https://feildmaster.github.io/UnderScript/
// @source https://github.com/feildmaster/UnderScript
// @supportURL https://github.com/feildmaster/UnderScript/issues
// @downloadURL https://feildmaster.github.io/UnderScript/undercards.user.js
// @namespace https://feildmaster.com/
// @grant none
// ==/UserScript==
// === Variables start
const hotkeys = [];
// === Variables end
eventManager.on("getWaitingQueue", function lowerVolume() {
// Lower the volume, the music changing is enough as is
audioQueue.volume = 0.3;
});
eventManager.on("PlayingGame", function bindHotkeys() {
// Binds to Space, Middle Click
const hotkey = new Hotkey("End turn").run((e) => {
if (!$(e.target).is("#endTurnBtn") && userTurn === userId) endTurn();
});
if (!localStorage.getItem('setting.disable.endTurn.space')) {
hotkey.bindKey(32);
}
if (!localStorage.getItem('setting.disable.endTurn.middleClick')) {
hotkey.bindClick(2);
}
hotkeys.push(hotkey);
});
eventManager.on('PlayingGame', function fixEndTurn() {
const oEndTurn = endTurn;
let endedTurn = false;
endTurn = function restrictedEndTurn() {
if (endedTurn || $('#endTurnBtn').prop('disabled')) return;
endedTurn = true;
oEndTurn();
};
eventManager.on('getTurnStart', function turnStarted() {
if (userTurn !== userId) return;
endedTurn = false;
if (turn > 3 && !localStorage.getItem('setting.disable.endTurnDelay')) {
$('#endTurnBtn').prop('disabled', true);
setTimeout(() => {
$('#endTurnBtn').prop('disabled', false);
}, 3000);
}
});
});
eventManager.on("GameStart", function battleLogger() {
const ignoreEvents = Object.keys({
getConnectedFirst: '',
refreshTimer: 'Never need to know about this one',
getPlayableCards: 'On turn start, selects cards player can play',
getTurn: 'Turn update',
getCardDrawed: 'Add card to your hand',
updateSpell: '',
updateMonster: 'monster on board updated',
getFakeDeath: 'Card "died" and respawns 1 second later',
getMonsterTemp: "You're about to play a monster",
getSpellTemp: "You're about to play a spell",
getTempCancel: 'Temp card cancelled',
getShowMulligan: 'Switching out hands, ignore it',
getHideMulligan: 'Hide the mulligan, gets called twice',
getUpdateHand: 'Updates full hand',
getError: 'Takes you to "home" on errors, can be turned into a toast',
getGameError: 'Takes you to "play" on game errors, can be turned into a toast',
});
let turn = 0, currentTurn = 0, players = {}, monsters = {}, lastEffect, other = {}, finished = false;
const yourDust = $('<span>')
const enemyDust = $('<span>');
function addDust(player) {
const display = player === userId ? yourDust : enemyDust;
const dust = typeof players[player].dust === 'undefined' ? players[player].dust = 0 : players[player].dust += 1;
display.html(dust);
}
const make = {
player: function makePlayer(player, title = false) {
const c = $('<span>');
c.append(player.username);
c.addClass(player.class);
if (!title) {
c.css('text-decoration', 'underline');
// show lives, show health, show gold, show hand, possibly deck size as well
const data = `${player.hp} hp, ${player.gold} gold<br />${player.dust} dust`;
c.hover(hover.show(data, '2px solid white'));
}
return c;
},
card: function makeCard(card) {
const c = $('<span>');
c.append(card.name);
c.css('text-decoration', 'underline');
let data = `<table class="cardBoard ${card.paralyzed ? 'paralyzed' : ''}">`;
data += `<tr><td class="cardName resize ${card.classe || card.class}" colspan="3">${card.name}`;
if (card.shiny) {
// TODO: rainbow
}
// TODO: skins
data += `</td><td class="cardCost">${card.cost}</td></tr>`;
data += `<tr><td id="cardImage" colspan="4">`;
const status = fn.cardStatus(card);
if (status.length) {
// add status images
status.forEach((s, i) => {
data += `<img class="infoPowers" style="z-index:20;right:${4 + i * 20}px;" src="images/powers/${s}.png"/>`;
});
}
data += `<img src="images/cards/${card.image}.png"/></td></tr>`;
data += `<tr><td class="cardDesc" colspan="4">${card.desc || ''}`
if (card.silence) {
data += '<img class="silenced" title="Silence" src="images/silence.png">';
}
data += '</td></tr>';
if (!card.typeCard) {
data += `<tr><td id="cardATQ">${card.attack}</td><td id="cardRarity" colspan="2"><img src="images/rarity/${card.rarity}.png" /></td><td id="cardHP" class="${card.hp!==card.maxHp ? "damaged" : ""}">${card.hp}</td></tr>`;
} else {
data += `<tr><td id="cardRarity" colspan="4"><img src="images/rarity/${card.rarity}.png" /></td></tr>`;
}
data += `</table>`;
c.hover(hover.show(data));
return c;
},
};
eventManager.on('GameEvent', function logEvent(data) {
if (finished) { // Sometimes we get events after the battle is over
fn.debug(`Extra action: ${data.action}`, 'debugging.events.extra');
return;
}
debug(data.action, 'debugging.events.name');
const emitted = eventManager.emit(data.action, data).ran;
if (!emitted) {
fn.debug(`Unknown action: ${data.action}`);
}
});
eventManager.on('getAllGameInfos getGameStarted getReconnection', function initBattle(data) {
debug(data, 'debugging.raw.game');
let you, enemy;
// Battle logging happens after the game runs
if (this.event === 'getGameStarted') {
you = {
id: data.yourId,
username: data.yourUsername,
hp: 30, // This is wrong with artifacts? Maybe?
gold: 2, // This is wrong with artifacts? Maybe?
};
enemy = {
id: data.enemyId,
username: data.enemyUsername,
hp: 30, // This is wrong with artifacts? Maybe?
gold: 2, // This is wrong with artifacts? Maybe?
};
} else {
you = JSON.parse(data.you);
enemy = JSON.parse(data.enemy);
// Set gold
const gold = JSON.parse(data.golds);
you.gold = gold[you.id];
enemy.gold = gold[enemy.id];
// Set lives
const lives = JSON.parse(data.lives);
you.lives = lives[you.id];
enemy.lives = lives[enemy.id];
// populate monsters
let count = 0;
JSON.parse(data.board).forEach(function (card) {
count += 1;
if (card === null) return;
// id, attack, hp, maxHp, originalattack, originalHp, typeCard, name, image, cost, originalCost, rarity, shiny, quantity
card.desc = getDescription(card);
card.owner = count <= 4 ? enemy.id : you.id;
monsters[card.id] = card;
});
}
you.level = data.yourLevel;
you.class = data.yourClass;
you.rank = data.yourRank;
enemy.level = data.enemyLevel;
enemy.class = data.enemyClass;
enemy.rank = data.enemyRank;
// yourArtifacts, yourAvatar {id, image, name, rarity, ucpCost}, division, oldDivision, profileSkin {id, name, image, ucpCost}
debug({you, enemy}, 'debugging.game');
turn = data.turn || 0;
players[you.id] = you;
players[enemy.id] = enemy;
const dustImg = $('<img style="width: 20px; height: 16px;" title="Number of cards turned to dust." src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAQCAYAAAAWGF8bAAAAZElEQVQ4jb2UUQ7AIAhDqfH+V95+NDEdrMSg/UQqr5hoFugZytanWnSwq+4RZIyzDwDW+jnCLBmLSSUhD+KIH8JdsmiwJGQiBVD+KOU7vE9YukMv3vXIMPNjKBLpUd/S38Wr7wVZPk/6kF1cXAAAAABJRU5ErkJggg==">');
$('.rightPart').append(dustImg, ' ');
$(`#user${opponentId} .rightPart`).append(enemyDust, ' ');
$(`#user${userId} .rightPart`).append(yourDust, ' ', $(`#user${userId} .rightPart > button:last`));
addDust(you.id);
addDust(enemy.id);
// Test changing ID's at endTurn instead of startTurn
other[you.id] = enemy.id;
other[enemy.id] = you.id;
// Initialize the log
log.init();
$("div#history div.handle").html('').append(`[${data.gameType}] `, make.player(you), ' vs ', make.player(enemy));
log.add(`Turn ${turn}`);
if (data.userTurn) {
currentTurn = data.userTurn;
log.add(make.player(players[data.userTurn]), "'s turn");
}
});
eventManager.on('getFight getFightPlayer', function fight(data) {
const target = this.event === 'getFightPlayer' ? make.player(players[data.defendPlayer]) : make.card(monsters[data.defendMonster]);
log.add(make.card(monsters[data.attackMonster]), ' attacked ', target);
});
eventManager.on('getUpdatePlayerHp', function updateHP(data) {
debug(data, 'debugging.raw.updateHP');
const oHp = players[data.playerId].hp;
const hp = data.isDamage ? oHp - data.hp : data.hp - oHp;
players[data.playerId].hp = data.hp;
if (oHp !== data.hp) { // If the player isn't at 0 hp already
log.add(make.player(players[data.playerId]), ` ${data.isDamage ? "lost" : "gained"} ${hp} hp`);
}
if (data.hp === 0 && players[data.playerId].lives > 1 && !players[data.playerId].hasOwnProperty("lostLife")) { // If they have extra lives, and they didn't lose a life already
log.add(make.player(players[data.playerId]), ' lost a life');
players[data.playerId].lostLife = true;
}
});
eventManager.on('getDoingEffect', function doEffect(data) {
debug(data, 'debugging.raw.effect');
// affecteds: [ids]; monsters affected
// playerAffected1: id; player affected
// playerAffected2: id; player affected
// TODO: Figure out how to do this better
if (lastEffect === 'm' + data.monsterId) return;
lastEffect = 'm' + data.monsterId;
log.add(make.card(monsters[data.monsterId]), "'s effect activated");
});
eventManager.on('getArtifactDoingEffect', function doEffect(data) {
debug(data, 'debugging.raw.effectArtifact');
if (lastEffect === 'a' + data.playerId) return;
lastEffect = 'a' + data.playerId;
log.add(make.player(players[data.playerId]), "'s artifact activated");
});
eventManager.on('getSoulDoingEffect', function soulEffect(data) {
debug(data, 'debugging.raw.effectSoul');
if (lastEffect === 's' + data.playerId) return;
lastEffect = 's' + data.playerId;
log.add(make.player(players[data.playerId]), "'s soul activated");
// affecteds
// playerAffected1
// playerAffected2
});
eventManager.on('getTurnStart', function turnStart(data) {
debug(data, 'debugging.raw.turnStart');
lastEffect = 0;
if (data.numTurn !== turn) {
log.add(`Turn ${data.numTurn}`);
}
currentTurn = data.idPlayer; // It would (kindof) help to actually update who's turn it is
turn = data.numTurn;
log.add(make.player(players[currentTurn]), "'s turn");
});
eventManager.on('getTurnEnd', function turnEnd(data) {
debug(data, 'debugging.raw.turnEnd');
// Lets switch the turn NOW, rather than later, the purpose of this is currently unknown... It just sounded like a good idea, also delete the "lostLife" flag...
if (time <= 0) {
log.add(make.player(players[currentTurn]), ' timed out');
}
delete players[currentTurn].lostLife;
currentTurn = other[data.idPlayer];
delete players[currentTurn].lostLife;
lastEffect = 0;
});
eventManager.on('getUpdateBoard', function updateGame(data) {
debug(data, 'debugging.raw.boardUpdate');
const oldMonsters = monsters;
monsters = {};
// TOOD: stuff....
let count = 0;
JSON.parse(data.board).forEach(function (card) {
count += 1;
if (card === null) return;
card.desc = getDescription(card);
card.owner = count <= 4 ? opponentId : userId;
monsters[card.id] = card;
});
});
eventManager.on('getMonsterDestroyed', function monsterKilled(data) {
debug(data, 'debugging.raw.kill');
// monsterId: #
log.add(make.card(monsters[data.monsterId]), ' was killed');
addDust(monsters[data.monsterId].owner);
delete monsters[data.monsterId];
});
eventManager.on('getCardBoard', function playCard(data) { // Adds card to X, Y (0(enemy), 1(you))
debug(data, 'debugging.raw.boardAdd');
const card = JSON.parse(data.card);
card.desc = getDescription(card);
card.owner = data.idPlayer;
monsters[card.id] = card;
log.add(make.player(players[data.idPlayer]), ' played ', make.card(card));
});
eventManager.on('getSpellPlayed', function useSpell(data) {
debug(data, 'debugging.raw.spell');
// immediately calls "getDoingEffect" and "getUpdateBoard"
const card = JSON.parse(data.card);
card.desc = getDescription(card);
monsters[card.id] = card;
log.add(make.player(players[data.idPlayer]), ' used ', make.card(card));
});
eventManager.on('getShowCard', function showCard(data) {
const card = JSON.parse(data.card);
card.desc = getDescription(card);
log.add(make.player(players[data.idPlayer]), ' exposed ', make.card(card));
});
eventManager.on('getCardDestroyedHandFull', function destroyCard(data) {
debug(data, 'debugging.raw.fullHand');
const card = JSON.parse(data.card);
card.desc = getDescription(card);
debug(data.card);
// This event gets called for *all* discards. Have to do smarter logic here (not just currentTurn!)
log.add(make.player(players[currentTurn]), ' discarded ', make.card(card));
});
eventManager.on('getPlayersStats', function updatePlayer(data) { // TODO: When does this get called?
debug(data, 'debugging.raw.stats');
let key, temp = JSON.parse(data.handsSize);
for (key in temp) {
// TODO: hand size monitoring
//players[key].hand
}
// TODO: deck monitoring (decksSize)
temp = JSON.parse(data.golds);
for (key in temp) {
players[key].gold = temp[key];
}
temp = JSON.parse(data.lives);
for (key in temp) {
players[key].lives = temp[key];
}
// data.artifcats
// data.turn
});
eventManager.on('getVictory getDefeat', function gameEnd(data) {
debug(data, 'debugging.raw.end');
finished = true;
if (data.disconnected) {
log.add(make.player(players[opponentId]), " left the game");
}
const you = make.player(players[userId]);
const enemy = make.player(players[opponentId]);
if (this.event === 'getDefeat') {
log.add(enemy, ' beat ', you);
} else {
log.add(you, ' beat ', enemy);
}
});
eventManager.on('getResult', function endSpectating(data) {
debug(data, 'debugging.raw.end');
finished = true;
if (data.cause === "Surrender") {
log.add(`${data.looser} surrendered.`);
} else if (data.cause === "Disconnection") {
log.add(`${data.looser} disconnected.`);
}
if (typeof music !== 'undefined') {
music.addEventListener('playing', function () {
if (localStorage.getItem('gameMusicDisabled')) {
music.pause();
}
});
}
// TODO: colorize
log.add(`${data.winner} beat ${data.looser}`);
const toast = {
title: 'Game Finished',
text: 'Return Home',
buttons: {
className: 'skiptranslate',
text: '🏠',
onclick: () => {
document.location.href = "/";
},
},
};
if (!localStorage.getItem('setting.disableResultToast') && fn.toast(toast)) {
BootstrapDialog.closeAll();
}
});
eventManager.on(ignoreEvents.join(' '), function ignore(data) {
debug(data, 'debugging.raw.ignore');
debug(data, `debugging.raw.ignore.${this.event}`);
});
eventManager.on('getTurnEnd', function hideSpells() {
// Fixes a bug with "mines" and any other potential cards that don't get cleared correctly.
$('#board .spellPlayed').remove();
})
});
// === Index hook
onPage('', function adjustSpectateView() {
const spectate = $('.spectateTable');
const tbody = $('.spectateTable tbody');
const footer = $('.mainContent footer');
function doAdjustment() {
tbody.css({height: 'auto', 'max-height': `${footer.offset().top - spectate.offset().top}px`});
}
$('.mainContent > br').remove();
doAdjustment();
$(window).on('resize.script', doAdjustment);
});
// === Chat hooks
if (typeof onMessage === 'function') {
debug('Chat detected');
let toast;
const ignorePrefix = 'underscript.ignore.';
const ignoreList = {};
const context = (() => {
function decode(string) {
return $('<textarea>').html(string).val();
}
$('head').append($(`<style type="text/css">
.chatContext { background: #F4F4F4; margin: 10px; color: #333; border: 1px dashed #000; position: absolute; z-index: 20; text-align: center; border-radius: 10px; }
.chatContext header { padding: 0px 5px; height: auto; }
.chatContext li { list-style: none; margin: 0; padding: 3px; border-top: 1px solid #CCC; cursor: pointer; }
.chatContext .disabled { background-color: #ccc; cursor: not-allowed; }
.chatContext li:not(.disabled):hover { background-color: #003366; color: #F2F2F2; }
.chatContext :last-child { border-radius: 0 0 10px 10px; }
</style>`));
const container = $('<div class="chatContext">');
const profile = $('<li>Profile</li>');
const ignore = $('<li>Ignore</li>');
const mention = $('<li>Mention</li>');
const header = $('<header>');
container.append(header, profile, mention, ignore).hide();
$('body').append(container);
function open(event) {
if (event.ctrlKey) return;
if (toast) {
toast.close();
}
close();
const { id, name, staff } = event.data;
event.preventDefault();
// get top/left coordinates
header.html(name);
let left = event.pageX;
const containerWidth = container.outerWidth(true);
if (left + containerWidth > window.innerWidth) {
left = left - containerWidth;
}
container.css({
top: `${event.pageY}px`,
left: `${left}px`,
});
container.show();
const disabled = staff || id === selfId;
container.on('click.script.chatContext', 'li', (e) => {
if (e.target === profile[0]) {
getInfo(event.target);
} else if (e.target === mention[0]) {
const input = $(event.target).closest('.chat-box').find('.chat-text');
let text = input.val();
if (text.length !== 0 && text[text.length - 1] !== ' ') {
text += ' ';
}
text += decode(name) + ' ';
input.val(text).focus();
} else if (e.target === ignore[0]) {
if (disabled) return; // If it's disabled it's disabled...
if (!ignoreList.hasOwnProperty(id)) {
ignoreList[id] = name;
localStorage.setItem(`${ignorePrefix}${id}`, name);
} else {
localStorage.removeItem(`${ignorePrefix}${id}`);
}
updateIgnoreText(id);
}
close();
});
if (disabled) {
ignore.addClass('disabled');
} else {
ignore.removeClass('disabled');
}
updateIgnoreText(id);
$('html').on('mousedown.chatContext', (event) => {
if ($(event.target).closest('.chatContext').length === 0) {
close();
}
});
}
function updateIgnoreText(id) {
if (ignoreList.hasOwnProperty(id)) {
ignore.html('Unignore');
} else {
ignore.html('Ignore');
}
}
function close() {
container.hide();
container.off('.chatContext');
$('html').off('chatContext');
}
return {
open,
close,
};
})();
// Load Ingore List
for (let i = 0; i < localStorage.length; i++) {
const key = localStorage.key(i);
if (key.startsWith(ignorePrefix)) {
ignoreList[key.substr(ignorePrefix.length)] = localStorage.getItem(key);
}
}
function processMessage(message, room) {
const id = message.id;
const user = message.user;
const name = user.username;
let staff = false;
user.groups.some((group) => {
return staff = group.priority <= 6; // This is so hacky...
});
let info = $(`#${room} #message-${id} #info-${user.id}`);
if (!info.length) {
info = $(`#${room} #message-${id} #info-${id}`);
}
info.on('contextmenu.script.chatContext', {
staff,
name,
id: user.id,
}, context.open);
if (!staff && user.id !== selfId && ignoreList.hasOwnProperty(user.id)) {
$(`#${room} #message-${id} .chat-message`).html('<span class="gray">Message Ignored</span>').removeClass().addClass('chat-message');
}
}
eventManager.on('Chat:getHistory', (data) => {
JSON.parse(data.history).forEach((message) => {
processMessage(message, data.room);
});
});
eventManager.on('Chat:getMessage', (data) => {
processMessage(JSON.parse(data.chatMessage), data.room);
});
const oHandler = socketChat.onmessage;
socketChat.onmessage = (event) => {
oHandler(event);
const data = JSON.parse(event.data);
eventManager.emit('ChatMessage', data);
eventManager.emit(`Chat:${data.action}`, data);
}
toast = fn.infoToast({
text: 'You can right click users in chat to ignore them!',
onClose: () => {
toast = null; // Remove from memory
}
}, 'underscript.ignoreNotice', '1');
// Fix chat window being all funky with sizes
$('<style>').html('.chat-messages { height: calc(100% - 30px); }').appendTo('head');
}
// === Play hooks
onPage("Play", function () {
// TODO: Better "game found" support
debug("On play page");
let queues, disable = true;
let restarting = false;
eventManager.on("jQuery", function onPlay() {
if (disable) {
queues = $("button.btn.btn-primary");
queues.prop("disabled", true);
restarting = $('p.infoMessage:contains("The server will restart in")').length === 1;
if (restarting) {
queues.hover(hover.show('Joining is disabled due to server restart.'));
}
}
});
(function hook() {
if (typeof socketQueue === "undefined") {
debug("Timeout hook");
return setTimeout(hook);
}
socket = socketQueue;
const oOpen = socketQueue.onopen;
socketQueue.onopen = function onOpenScript(event) {
disable = false;
oOpen(event);
if (queues && !restarting) queues.prop("disabled", false);
};
const oHandler = socketQueue.onmessage;
socketQueue.onmessage = function onMessageScript(event) {
const data = JSON.parse(event.data);
oHandler(event);
eventManager.emit(data.action, data);
};
})();
});
// === Game hooks
onPage("Game", function () {
debug("Playing Game");
eventManager.emit("GameStart");
eventManager.emit("PlayingGame");
(function hook() {
if (typeof socket === 'undefined') {
debug("Timeout hook");
return setTimeout(hook);
}
const oHandler = socket.onmessage;
socket.onmessage = function onMessageScript(event) {
const data = JSON.parse(event.data);
//eventManager.emit('PreGameEvent', data, true);
oHandler(event);
eventManager.emit('GameEvent', data);
};
})();
});
onPage('GamesList', function keepAlive() {
setInterval(() => {
socket.send(JSON.stringify({ping: "pong"}));
}, 10000);
});
onPage('GamesList', function fixEnter() {
let toast = fn.infoToast({
text: 'You can now press enter on the Create Game window.',
onClose: () => {
toast = null;
}
}, 'underscript.notice.customGame', '1');
$('#state1 button:contains(Create)').on('mouseup.script', () => {
// Wait for the dialog to show up...
$(window).one('shown.bs.modal', (e) => {
const input = $('.bootstrap-dialog-message input');
if (!input.length) return; // This is just to prevent errors... though this is an error in itself
$(input[0]).focus();
input.on('keydown.script', (e) => {
if (e.which === 13) {
if (toast) {
toast.close();
}
e.preventDefault();
$('.bootstrap-dialog-footer-buttons button:first').trigger('click');
}
});
});
});
});
// Deck hook
onPage('Decks', function () {
debug('Deck editor');
function hoverCard(element) {
const id = element.attr('id');
const shiny = element.hasClass('shiny') ? '.shiny' : '';
const card = $(`table#${id}${shiny}:lt(1)`).clone();
if (card.length !== 1) return;
card.find('#quantity').remove();
if (card.css('opacity') !== '1') card.css('opacity', 1);
loadCard(card);
return hover.show(card);
}
// Initial load
eventManager.on('jQuery', () => {
$('li.list-group-item').each(function (index) {
const element = $(this);
element.hover(hoverCard(element));
});
$(document).ajaxSuccess((event, xhr, settings) => {
const data = JSON.parse(settings.data);
if (data.action === 'removeCard') { // Card was removed, hide element
hover.hide();
} else if (data.action === 'addCard') { // Card was added
const element = $(`#deckCards${data.classe} li:last`);
element.hover(hoverCard(element, true));
const list = $(`#deckCards${data.classe}`);
list.append(list.children('li').detach().sort(function (a, b) {
const card1 = $(`table#${$(a).attr('id')}`);
const card2 = $(`table#${$(b).attr('id')}`);
const card1cost = parseInt(card1.find('.cardCost').html(), 10);
const card2cost = parseInt(card2.find('.cardCost').html(), 10);
if (card1cost === card2cost) {
const card1name = card1.find('.cardName').html();
const card2name = card2.find('.cardName').html();
if (card1name == card2name) return 0;
return card1name > card2name ? 1 : -1;
}
return card1cost > card2cost ? 1 : -1;
}));
}
});
});
const oLoad = window.onload;
window.onload = () => {
oLoad();
const cardList = $('#yourCardList');
cardList.css({
'position': 'absolute',
'max-width': '180px',
});
const oOffset = cardList.offset().top - 5;
const oOffsetDeck = $('#deckCardsKINDNESS').offset().top - 5;
$(window).on('scroll.script', () => {
if (window.innerHeight < maxHeight) {
// Calculated here because cardList can change height
const maxHeight = 5 + cardList.height() + $('body > footer').height();
// Lock to the deck offset instead
if (window.pageYOffset > oOffsetDeck) {
$('.deckCardsList').css({
position: 'fixed',
width: '180px',
top: 5,
});
} else {
$('.deckCardsList').css({
position: '',
width: '',
top: '',
});
}
cardList.css({
position: '',
top: '',
});
} else if (window.pageYOffset > oOffset) {
cardList.css({
position: 'fixed',
top: 5,
});
} else {
cardList.css({
position: 'absolute',
top: '',
});
}
});
};
});
// Spectate hooks
onPage("gameSpectate", function () {
debug("Spectating Game");
eventManager.emit("GameStart");
(function hook() {
if (typeof socket === "undefined") {
debug("Timeout hook");
return setTimeout(hook);
}
const oHandler = socket.onmessage;
socket.onmessage = function onMessageScript(event) {
const data = JSON.parse(event.data);
//eventManager.emit('PreGameEvent', data, true);
oHandler(event);
eventManager.emit('GameEvent', data);
};
})();
});
onPage('Decks', function deckStorage() {
const container = $('<p>');
const buttons = [];
let pending = [];
function processNext() {
const card = pending.shift();
if (card) {
if (card.action === 'clear') {
removeAllCards();
} else if (card.action === 'remove') {
removeCard(parseInt(card.id), card.shiny === true);
} else {
addCard(parseInt(card.id), card.shiny === true);
}
}
}
$(document).ajaxComplete((e, xhr, settings) => {
if (settings.url !== 'DecksConfig') return;
const data = JSON.parse(settings.data);
if (!['addCard', 'removeCard', 'removeAllCards'].includes(data.action)) return;
if (data.status === "error") {
pending = [];
return;
}
processNext();
});
for (let i = 1, x = Math.max(parseInt(localStorage.getItem('underscript.storage.rows') || '1'), 1) * 3; i <= x; i++) {
buttons.push($(`<button>${i}</button>`)
.addClass('btn btn-sm btn-danger')
.css({
'margin-top': '5px',
'margin-right': '2px',
width: '58px',
}));
}
buttons.forEach((button) => {
container.append(button);
});
$('#yourCardList > button').after(container);
$('#yourCardList > br').remove();
$("#selectClasses").change(loadStorage);
function saveDeck(i) {
const soul = $('#selectClasses').find(':selected').text();
const deck = [];
$(`#deckCards${soul} li`).each((i, e) => {
const card = {
id: parseInt($(e).attr('id')),
};
if ($(e).hasClass('shiny')) {
card.shiny = true;
}
deck.push(card);
});
if (!deck.length) return;
localStorage.setItem(`underscript.deck.${selfId}.${soul}.${i}`, JSON.stringify(deck));
}
function loadDeck(i) {
pending = []; // Clear pending
const soul = $('#selectClasses').find(':selected').text();
let deck = JSON.parse(localStorage.getItem(`underscript.deck.${selfId}.${soul}.${i}`));
const cDeck = $(`#deckCards${soul} li`);
if (cDeck.length) {
const builtDeck = [];
// Build deck options
cDeck.each((i, e) => {
const id = parseInt($(e).attr('id'));
const shiny = $(e).hasClass('shiny');
builtDeck.push({
id, shiny,
action: 'remove',
});
});
// Compare the decks
const temp = deck.slice(0);
const removals = builtDeck.filter((card) => {
return !temp.some((card2, i) => {
const found = card2.id === card.id && (card.shiny && card2.shiny || true);
if (found) { // Remove the item
temp.splice(i, 1);
}
return found;
});
});
// Check what we need to do
if (!removals.length && !temp.length) { // There's nothing
return;
} else if (removals.length > 13) { // Too much to do (Cards in deck + 1)
pending.push({
action: 'clear',
});
} else {
pending.push.apply(pending, removals);
deck = temp;
}
}
pending.push.apply(pending, deck);
processNext();
}
function cards(list) {
const names = [];
list.forEach((card) => {
const name = $(`table#${card.id}:lt(1)`).find('.cardName').text() || '<span style="color: red;">Disenchanted/Missing</span>';
names.push(`- ${card.shiny ? '<span style="color: yellow;">S</span> ':''}${name}`);
});
return names.join('<br />');
}
function loadStorage() {
for (let i = 0; i < buttons.length; i++) {
loadButton(i);
}
}
function loadButton(i) {
const soul = $('#selectClasses').find(':selected').text();
const deckKey = `underscript.deck.${selfId}.${soul}.${i}`;
const nameKey = `${deckKey}.name`;
const button = buttons[i];
button.off('.deckStorage'); // Remove any lingering events
function refreshHover() {
hover.hide();
button.trigger('mouseenter');
}
function saveButton() {
saveDeck(i);
loadButton(i); // I should be able to reset data without doing all this again...
refreshHover();
}
function hoverButton(e) {
let text = '';
if (e.type === 'mouseenter') {
const deck = JSON.parse(localStorage.getItem(deckKey));
if (deck) {
text = `
<div id="deckName">${localStorage.getItem(nameKey) || (`${soul}-${i + 1}`)}</div>
<div><input id="deckNameInput" maxlength="28" style="border: 0; border-bottom: 2px solid #00617c; background: #000; width: 100%; display: none;" type="text" placeholder="${soul}-${i + 1}" value="${localStorage.getItem(nameKey) || ''}"></div>
<div>Click to load (${deck.length})</div>
<div style="font-size: 13px;">${cards(deck)}</div>
<div style="font-style: italic; color: #b3b3b3;">
* Right Click to name deck<br />
* Shift Click to re-save deck<br />
* CTRL Click to erase deck
</div>
`;
} else {
text = `
<div id="name">${soul}-${i + 1}</div>
<div>Click to save current deck</div>`;
}
}
hover.show(text)(e);
}
if (!localStorage.getItem(deckKey)) {
button.addClass('btn-danger')
.removeClass('btn-primary')
.hover(hoverButton)
.one('click.script.deckStorage', saveButton);
} else {
button.removeClass('btn-danger')
.addClass('btn-primary')
.hover(hoverButton)