-
Notifications
You must be signed in to change notification settings - Fork 6
/
NHL-MyTeam-Widget.js
1186 lines (1060 loc) Β· 33.8 KB
/
NHL-MyTeam-Widget.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
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
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: blue; icon-glyph: hockey-puck;
/********************************************************
* script : NHL-MyTeam-Widget.js
* version : 5.2.0
* description: Widget for Scriptable.app, which shows
* the next games for your NHL team
* author : @thisisevanfox
* support : https://git.io/JtkA1
* date : 2024-10-21
*******************************************************/
/********************************************************
******************** USER SETTINGS *********************
************ PLEASE MODIFY BEFORE FIRST RUN ************
*******************************************************/
// Type the abbreviation of your NHL team here.
// Atlantic Division: BOS, BUF, DET, FLA, MTL, OTT, TBL, TOR
// Metropolitan Division: CAR, CBJ, NJD, NYI, NYR, PHI, PIT, WSH
// Central Division: CHI, COL, DAL, MIN, NSH, STL, UTA, WPG
// Pacific Division: ANA, CGY, EDM, LAK, SJS, SEA, VAN, VGK
const MY_NHL_TEAM = "ENTER_TEAM_ABBREVIATION_HERE";
// Start year of current season
// For season 2024-25, the value must be "20242025"
// For season 2025-26, the value must be "20242025"
const CURRENT_SEASON = "20242025";
// Indicator if livescores should be shown.
// If you don't want to be spoilered set it to false.
// Default: true
const SHOW_LIVE_SCORES = true;
// Indicator if all scores and stats should be shown.
// If you don't want to be spoilered set it to false.
// Default: true
const SHOW_STATS_AND_STANDINGS = true;
// Indicator if the home team should show first (like it's common in Europe)
// Default: true (home team shows first, e.g. "home - away")
// false (away team shows first, e.g. "away @ home")
const SHOW_HOME_TEAM_FIRST = true;
// Indicator if the descriptions of the stats should be shown
// Default: true (W: x - L: x - OTL: x)
// false (x - x - x)
const SHOW_STATS_DESCRIPTION = true;
// URL to shares app
// Default: "nhl://" (Official NHL app)
// If you don't want anything to open, type:
// const WIDGET_URL = "";
const WIDGET_URL = "nhl://";
// Set appearance of the widget. Default apperance is set to the system color scheme.
// Device.isUsingDarkAppearance() = System color scheme (default)
// true = Widget will be in dark mode.
// false = Widget will be in light mode.
const DARK_MODE = Device.isUsingDarkAppearance();
// Indicator if caching of logos is actived (saves datavolume)
// Default: true
const CACHING_ACTIVE = true;
// Indicator if no-background.js is installed
// Default: false
// @see: https://github.com/supermamon/scriptable-no-background
const NO_BACKGROUND_INSTALLED = false;
// Indicator if no-background.js should be active
// Only matters if NO_BACKGROUND_INSTALLED is true.
const NO_BACKGROUND_ACTIVE = true;
// Indicator if no-background.js should be active for whole widget
// No background for widget and no background for stacks in the widget
// Only matters if NO_BACKGROUND_INSTALLED is true.
const NO_BACKGROUND_FULL_ACTIVE = false;
/********************************************************
********************************************************
*********** DO NOT CHANGE ANYTHING FROM HERE ***********
********************************************************
*******************************************************/
const { transparent } = NO_BACKGROUND_INSTALLED
? importModule("no-background")
: emptyFunction();
const WIDGET_BACKGROUND = DARK_MODE ? new Color("gray") : new Color("#D6D6D6");
const STACK_BACKGROUND = DARK_MODE
? new Color("#1D1D1D")
: new Color("#FFFFFF"); //Smaller Container Background
let oNhlWidget;
if (config.runsInWidget) {
if (config.widgetFamily === "small") {
oNhlWidget = await createSmallWidget();
}
if (config.widgetFamily === "medium") {
oNhlWidget = await createMediumWidget();
}
if (config.widgetFamily === "large") {
oNhlWidget = await createLargeWidget();
}
Script.setWidget(oNhlWidget);
} else {
oNhlWidget = await createMediumWidget();
oNhlWidget.presentMedium();
// oNhlWidget = await createSmallWidget();
// oNhlWidget.presentSmall();
// oNhlWidget = await createLargeWidget();
// oNhlWidget.presentLarge();
}
/**
* Creates small sized widget.
*
* @return {ListWidget}
*/
async function createSmallWidget() {
// Initialise widget
const oWidget = new ListWidget();
oWidget.backgroundColor = DARK_MODE
? new Color("1D1D1D")
: new Color("#D6D6D6");
oWidget.setPadding(10, 10, 10, 10);
if (WIDGET_URL.length > 0) {
oWidget.url = WIDGET_URL;
}
await addSmallWidgetData(oWidget);
return oWidget;
}
/**
* Creates medium sized widget.
*
* @return {ListWidget}
*/
async function createMediumWidget() {
// Initialise widget
const oWidget = new ListWidget();
if (NO_BACKGROUND_INSTALLED && NO_BACKGROUND_ACTIVE) {
oWidget.backgroundImage = await transparent(Script.name());
} else {
oWidget.backgroundColor = WIDGET_BACKGROUND;
}
oWidget.setPadding(10, 10, 10, 10);
if (WIDGET_URL.length > 0) {
oWidget.url = WIDGET_URL;
}
await addMediumWidgetData(oWidget);
return oWidget;
}
/**
* Add data to small sized widget.
*
* @param {ListWidget} oWidget
*/
async function addSmallWidgetData(oWidget) {
const oGameData = await prepareData();
if (oGameData != null) {
let oMyTeam;
let oOpponentTeam;
if (oGameData.homeTeam.abbrev == MY_NHL_TEAM) {
oMyTeam = oGameData.homeTeam;
oOpponentTeam = oGameData.awayTeam;
} else {
oOpponentTeam = oGameData.homeTeam;
oMyTeam = oGameData.awayTeam;
}
const oUpperStack = oWidget.addStack();
oUpperStack.layoutHorizontally();
const oUpperTextStack = oUpperStack.addStack();
oUpperTextStack.layoutVertically();
const dGameDate = new Date(oGameData.gameDate);
const dLocalDate = dGameDate.toLocaleString([], {
year: "numeric",
month: "2-digit",
day: "2-digit",
hour: "2-digit",
minute: "2-digit",
});
const oGameDateText = oUpperTextStack.addText(
`${dLocalDate.split(",")[0]}`
);
oGameDateText.font = Font.boldSystemFont(11);
oGameDateText.textColor = getColorForCurrentAppearance();
if (!!dLocalDate.split(",")[1]) {
const oGameTimeText = oUpperTextStack.addText(
`${dLocalDate.split(",")[1].trim()}`
);
oGameTimeText.font = Font.boldSystemFont(11);
oGameTimeText.textColor = getColorForCurrentAppearance();
}
if (oGameData.venue != "") {
const oVenueText = oUpperTextStack.addText(`@ ${oGameData.venue}`);
oVenueText.font = Font.boldSystemFont(11);
oVenueText.textColor = getColorForCurrentAppearance();
}
oUpperStack.addSpacer();
const oOpponentLogoImage = await loadLogo(
oOpponentTeam.logoLink,
oOpponentTeam.abbrev
);
const oOpponentLogo = oUpperStack.addImage(oOpponentLogoImage);
oOpponentLogo.imageSize = new Size(40, 40);
if (SHOW_STATS_AND_STANDINGS) {
oWidget.addSpacer(4);
let sOpponentStatsText;
if (SHOW_STATS_DESCRIPTION) {
sOpponentStatsText =
"W: " +
oOpponentTeam.record.wins +
" - L: " +
oOpponentTeam.record.losses +
" - OTL: " +
oOpponentTeam.record.ot;
} else {
sOpponentStatsText =
oOpponentTeam.record.wins +
" - " +
oOpponentTeam.record.losses +
" - " +
oOpponentTeam.record.ot;
}
const oOpponentTeamStatsText = oWidget.addText(sOpponentStatsText);
oOpponentTeamStatsText.font = Font.systemFont(11);
oOpponentTeamStatsText.textColor = getColorForCurrentAppearance();
const oOpponentTeamStandingsText = oWidget.addText(
"Div.: " +
oOpponentTeam.record.divisionRank +
"." +
" | Lea.: " +
oOpponentTeam.record.leagueRank +
"."
);
oOpponentTeamStandingsText.font = Font.systemFont(11);
oOpponentTeamStandingsText.textColor = getColorForCurrentAppearance();
if (oOpponentTeam.topscorer.name != null) {
const oOpponentTeamTopScorerText = oWidget.addText(
`${oOpponentTeam.topscorer.name} (${oOpponentTeam.topscorer.points})`
);
oOpponentTeamTopScorerText.font = Font.systemFont(11);
oOpponentTeamTopScorerText.textColor = getColorForCurrentAppearance();
}
}
if (SHOW_STATS_AND_STANDINGS) {
const oDivider = oWidget.addText(`___________________________`);
oDivider.font = Font.boldSystemFont(6);
oDivider.textColor = getColorForCurrentAppearance();
oWidget.addSpacer(6);
const oBottomStack = oWidget.addStack();
oBottomStack.layoutHorizontally();
const oBottomTextStack = oBottomStack.addStack();
oBottomTextStack.layoutVertically();
let sMyTeamStatsText;
if (SHOW_STATS_DESCRIPTION) {
sMyTeamStatsText =
"W: " +
oMyTeam.record.wins +
" - L: " +
oMyTeam.record.losses +
" - OTL: " +
oMyTeam.record.ot;
} else {
sMyTeamStatsText =
oMyTeam.record.wins +
" - " +
oMyTeam.record.losses +
" - " +
oMyTeam.record.ot;
}
const oMyTeamStatsText = oBottomTextStack.addText(sMyTeamStatsText);
oMyTeamStatsText.font = Font.systemFont(9);
oMyTeamStatsText.textColor = getColorForCurrentAppearance();
const oMyTeamStandingsText = oBottomTextStack.addText(
"Div.: " +
oMyTeam.record.divisionRank +
"." +
" | Lea.: " +
oMyTeam.record.leagueRank +
"."
);
oMyTeamStandingsText.font = Font.systemFont(9);
oMyTeamStandingsText.textColor = getColorForCurrentAppearance();
if (oMyTeam.topscorer.name != null) {
const oMyTeamTopScorerText = oBottomTextStack.addText(
`${oMyTeam.topscorer.name} (${oMyTeam.topscorer.points})`
);
oMyTeamTopScorerText.font = Font.systemFont(9);
oMyTeamTopScorerText.textColor = getColorForCurrentAppearance();
}
oBottomStack.addSpacer();
const oMyTeamLogoImage = await loadLogo(
oMyTeam.logoLink,
oMyTeam.abbrev
);
const oMyTeamLogo = oBottomStack.addImage(oMyTeamLogoImage);
oMyTeamLogo.imageSize = new Size(25, 25);
}
} else {
const oHeadingText = oWidget.addText(`No upcoming games. Season ended.`);
oHeadingText.font = Font.boldSystemFont(11);
oHeadingText.textColor = getColorForCurrentAppearance();
oWidget.addSpacer();
}
}
/**
* Add data to medium sized widget.
*
* @param {ListWidget} oWidget
*/
async function addMediumWidgetData(oWidget) {
const oGameData = await prepareData();
const oTopRow = oWidget.addStack();
await setStackBackground(oTopRow);
oTopRow.cornerRadius = 12;
oTopRow.size = new Size(308, 15);
oTopRow.setPadding(7, 7, 7, 7);
oTopRow.layoutVertically();
const oSpacerStack1 = oTopRow.addStack();
oSpacerStack1.layoutHorizontally();
oSpacerStack1.addSpacer();
if (oGameData != null) {
const oHeadingStack = oTopRow.addStack();
oHeadingStack.layoutHorizontally();
oHeadingStack.addSpacer();
oHeadingStack.setPadding(7, 7, 7, 7);
let oHeadingText;
if (
oGameData.currentPeriod != undefined &&
oGameData.currentPeriod != null &&
oGameData.currentPeriod != "" &&
SHOW_LIVE_SCORES
) {
oHeadingText = oHeadingStack.addText(
`Period: ${oGameData.currentPeriod} - ${oGameData.timeRemaining}`
);
} else {
const dGameDate = new Date(oGameData.gameDate);
const dNow = new Date();
const bGameIsToday = dNow.toDateString() === dGameDate.toDateString();
const sDatePart = bGameIsToday ? "Today" :
dGameDate.toLocaleString([], {
year: "numeric",
month: "2-digit",
day: "2-digit"
});
const sTimePart = dGameDate.toLocaleString([], {
hour: "2-digit",
minute: "2-digit",
});
const sGameDateText = `${sDatePart}, ${sTimePart}`;
const sVenueText = oGameData.venue != "" ? ` @ ${oGameData.venue}` : ``;
oHeadingText = oHeadingStack.addText(`${sGameDateText}${sVenueText}`);
}
oHeadingText.font = Font.boldSystemFont(11);
oHeadingText.textColor = getColorForCurrentAppearance();
oHeadingStack.addSpacer();
const oSpacerStack2 = oTopRow.addStack();
oSpacerStack2.layoutHorizontally();
oSpacerStack2.addSpacer();
oWidget.addSpacer();
const oNextGameStack = oWidget.addStack();
oNextGameStack.layoutHorizontally();
oNextGameStack.cornerRadius = 12;
if (SHOW_HOME_TEAM_FIRST) {
await addHomeTeamStack(oNextGameStack, oGameData);
oNextGameStack.addSpacer();
await addAwayTeamStack(oNextGameStack, oGameData);
} else {
await addAwayTeamStack(oNextGameStack, oGameData);
oNextGameStack.addSpacer();
await addHomeTeamStack(oNextGameStack, oGameData);
}
oWidget.addSpacer();
const oFutureGamesStack = oWidget.addStack();
oFutureGamesStack.layoutHorizontally();
oFutureGamesStack.centerAlignContent();
await setStackBackground(oFutureGamesStack);
oFutureGamesStack.cornerRadius = 12;
oFutureGamesStack.setPadding(3, 7, 3, 7);
oFutureGamesStack.addSpacer();
oFutureGamesStack.size = new Size(308, 15);
for (let i = 0; i < oGameData.nextGames.length; i++) {
const oNextGame = oGameData.nextGames[i];
const oFutureGame = oFutureGamesStack.addStack();
oFutureGame.layoutHorizontally();
oFutureGame.addSpacer();
const oFutureGameLogoImage = await loadLogo(
oNextGame.opponent.logoLink,
oNextGame.opponent.abbrev
);
const oNextGameLogo = oFutureGame.addImage(oFutureGameLogoImage);
oNextGameLogo.imageSize = new Size(15, 15);
const dGameDate = new Date(oNextGame.gameDate);
const dLocalDate = dGameDate.toLocaleString([], {
month: "2-digit",
day: "2-digit",
});
const oNextGameText = oFutureGame.addText(` ${dLocalDate}`);
oNextGameText.font = Font.systemFont(11);
oNextGameText.textColor = getColorForCurrentAppearance();
oFutureGame.addSpacer();
}
oFutureGamesStack.addSpacer();
} else {
const oHeadingStack = oTopRow.addStack();
oHeadingStack.layoutHorizontally();
oHeadingStack.addSpacer();
oHeadingStack.setPadding(7, 7, 7, 7);
const oHeadingText = oHeadingStack.addText(
`No upcoming games. Season ended.`
);
oHeadingText.font = Font.boldSystemFont(11);
oHeadingText.textColor = getColorForCurrentAppearance();
oHeadingStack.addSpacer();
const oSpacerStack2 = oTopRow.addStack();
oSpacerStack2.layoutHorizontally();
oSpacerStack2.addSpacer();
oWidget.addSpacer();
}
}
/**
* Adds stack for home team to the medium sized widget.
*
* @param {Object} oNextGameStack
* @param {Object} oGameData
*/
async function addHomeTeamStack(oNextGameStack, oGameData) {
const oHomeTeamStack = oNextGameStack.addStack();
oHomeTeamStack.layoutVertically();
oHomeTeamStack.centerAlignContent();
oHomeTeamStack.setPadding(7, 7, 7, 7);
await setStackBackground(oHomeTeamStack);
oHomeTeamStack.cornerRadius = 12;
oHomeTeamStack.size = new Size(150, 0);
const oHomeTeamLogoStack = oHomeTeamStack.addStack();
oHomeTeamLogoStack.layoutHorizontally();
const oHomeLogoImage = await loadLogo(
oGameData.homeTeam.logoLink,
oGameData.homeTeam.abbrev
);
const oHomeLogo = oHomeTeamLogoStack.addImage(oHomeLogoImage);
oHomeLogo.imageSize = new Size(40, 40);
if (SHOW_LIVE_SCORES) {
oHomeTeamLogoStack.addSpacer(45);
const oHomeTeamGoalsText = oHomeTeamLogoStack.addText(
!oGameData.currentPeriod ? `-` : `${oGameData.homeTeam.goals}`
);
oHomeTeamGoalsText.font = Font.boldSystemFont(35);
oHomeTeamGoalsText.textColor = getColorForCurrentAppearance();
}
if (SHOW_STATS_AND_STANDINGS) {
let sHomeTeamStatsText;
if (SHOW_STATS_DESCRIPTION) {
sHomeTeamStatsText =
"W: " +
oGameData.homeTeam.record.wins +
" - L: " +
oGameData.homeTeam.record.losses +
" - OTL: " +
oGameData.homeTeam.record.ot;
} else {
sHomeTeamStatsText =
oGameData.homeTeam.record.wins +
" - " +
oGameData.homeTeam.record.losses +
" - " +
oGameData.homeTeam.record.ot;
}
const oHomeTeamStatsText = oHomeTeamStack.addText(sHomeTeamStatsText);
oHomeTeamStatsText.font = Font.systemFont(11);
oHomeTeamStatsText.textColor = getColorForCurrentAppearance();
const oHomeTeamStandingsText = oHomeTeamStack.addText(
"Division: " +
oGameData.homeTeam.record.divisionRank +
"." +
" | League: " +
oGameData.homeTeam.record.leagueRank +
"."
);
oHomeTeamStandingsText.font = Font.systemFont(9);
oHomeTeamStandingsText.textColor = getColorForCurrentAppearance();
if (oGameData.homeTeam.topscorer.name != null) {
const oHomeTeamTopScorerText = oHomeTeamStack.addText(
`${oGameData.homeTeam.topscorer.name} (${oGameData.homeTeam.topscorer.points})`
);
oHomeTeamTopScorerText.centerAlignText();
oHomeTeamTopScorerText.font = Font.systemFont(9);
oHomeTeamTopScorerText.textColor = getColorForCurrentAppearance();
}
}
}
/**
* Adds stack for away team to the medium sized widget.
*
* @param {Object} oNextGameStack
* @param {Object} oGameData
*/
async function addAwayTeamStack(oNextGameStack, oGameData) {
const oAwayTeamStack = oNextGameStack.addStack();
oAwayTeamStack.layoutVertically();
oAwayTeamStack.centerAlignContent();
oAwayTeamStack.setPadding(7, 7, 7, 7);
await setStackBackground(oAwayTeamStack);
oAwayTeamStack.cornerRadius = 12;
oAwayTeamStack.size = new Size(150, 0);
const oAwayTeamLogoStack = oAwayTeamStack.addStack();
oAwayTeamLogoStack.layoutHorizontally();
const oAwayLogoImage = await loadLogo(
oGameData.awayTeam.logoLink,
oGameData.awayTeam.abbrev
);
const oAwayLogo = oAwayTeamLogoStack.addImage(oAwayLogoImage);
oAwayLogo.imageSize = new Size(40, 40);
if (SHOW_LIVE_SCORES) {
oAwayTeamLogoStack.addSpacer(45);
const oAwayTeamGoalsText = oAwayTeamLogoStack.addText(
!oGameData.currentPeriod ? `-` : `${oGameData.awayTeam.goals}`
);
oAwayTeamGoalsText.font = Font.boldSystemFont(35);
oAwayTeamGoalsText.textColor = getColorForCurrentAppearance();
}
if (SHOW_STATS_AND_STANDINGS) {
let sAwayTeamStatsText;
if (SHOW_STATS_DESCRIPTION) {
sAwayTeamStatsText =
"W: " +
oGameData.awayTeam.record.wins +
" - L: " +
oGameData.awayTeam.record.losses +
" - OTL: " +
oGameData.awayTeam.record.ot;
} else {
sAwayTeamStatsText =
oGameData.awayTeam.record.wins +
" - " +
oGameData.awayTeam.record.losses +
" - " +
oGameData.awayTeam.record.ot;
}
const oAwayTeamStatsText = oAwayTeamStack.addText(sAwayTeamStatsText);
oAwayTeamStatsText.font = Font.systemFont(11);
oAwayTeamStatsText.textColor = getColorForCurrentAppearance();
const oAwayTeamStandingsText = oAwayTeamStack.addText(
"Division: " +
oGameData.awayTeam.record.divisionRank +
"." +
" | League: " +
oGameData.awayTeam.record.leagueRank +
"."
);
oAwayTeamStandingsText.font = Font.systemFont(9);
oAwayTeamStandingsText.textColor = getColorForCurrentAppearance();
if (oGameData.awayTeam.topscorer.name != null) {
const oAwayTeamTopScorerText = oAwayTeamStack.addText(
`${oGameData.awayTeam.topscorer.name} (${oGameData.awayTeam.topscorer.points})`
);
oAwayTeamTopScorerText.font = Font.systemFont(9);
oAwayTeamTopScorerText.textColor = getColorForCurrentAppearance();
}
}
}
/**
* Prepares data.
*
* @return {Object[]}
*/
async function prepareData() {
const oData = {
gameDate: "",
venue: "",
currentPeriod: 0,
timeRemaining: "",
nextGames: [],
homeTeam: {
abbrev: "",
logoLink: "",
record: {},
goals: "",
topscorer: {
name: null,
points: "",
},
},
awayTeam: {
abbrev: "",
logoLink: "",
record: {},
goals: "",
topscorer: {
name: null,
points: "",
},
},
};
const oTeamData = getTeamData();
const aScheduleData = await fetchScheduleData();
const oStandings = await fetchCurrentStandings();
if (
aScheduleData &&
aScheduleData.length > 0
) {
const oNextGame = aScheduleData[0];
if (oNextGame != undefined) {
const oHomeTeam = oNextGame.homeTeam;
const oAwayTeam = oNextGame.awayTeam;
const oHomeTeamTopScorer = await fetchTopScorerByAbbreviation(oHomeTeam.abbrev);
const oAwayTeamTopScorer = await fetchTopScorerByAbbreviation(oAwayTeam.abbrev);
const oHomeTeamStandings = filterStandingsByAbbreviation(
oHomeTeam.abbrev,
oStandings
);
const oAwayTeamStandings = filterStandingsByAbbreviation(
oAwayTeam.abbrev,
oStandings
);
oData.gameDate = oNextGame.startTimeUTC;
oData.venue = oNextGame.venue.default;
oData.nextGames = getNextGames(aScheduleData.splice(0, aScheduleData.length), oTeamData);
oData.homeTeam.abbrev = oHomeTeam.abbrev;
oData.homeTeam.logoLink = oTeamData[oHomeTeam.abbrev].logo;
oData.homeTeam.record = oHomeTeamStandings;
oData.awayTeam.abbrev = oAwayTeam.abbrev;
oData.awayTeam.logoLink = oTeamData[oAwayTeam.abbrev].logo;
oData.awayTeam.record = oAwayTeamStandings;
if (oHomeTeamTopScorer != null) {
oData.homeTeam.topscorer.name = `${oHomeTeamTopScorer.firstName.default} ${oHomeTeamTopScorer.lastName.default}`;
oData.homeTeam.topscorer.points = oHomeTeamTopScorer.points;
}
if (oAwayTeamTopScorer != null) {
oData.awayTeam.topscorer.name = `${oAwayTeamTopScorer.firstName.default} ${oAwayTeamTopScorer.lastName.default}`;
oData.awayTeam.topscorer.points = oAwayTeamTopScorer.points;
}
if (SHOW_LIVE_SCORES) {
const oLiveData = await fetchLiveData(oNextGame.id);
if (oLiveData) {
oData.currentPeriod = oLiveData.period;
oData.timeRemaining = oLiveData.clock?.timeRemaining;
oData.homeTeam.goals = oLiveData.homeTeam?.score;
oData.awayTeam.goals = oLiveData.awayTeam?.score;
}
}
}
} else {
return null;
}
return oData;
}
/**
* Returns next games.
*
* @param {Object[]} aGames
* @param {Object} oTeamData
* @return {Object[]}
*/
function getNextGames(aGames, oTeamData) {
const aNextGames = [];
const iLength = aGames.length < 5 ? aGames.length : 5;
for (let i = 1; i < iLength; i++) {
let oData = {
gameDate: "",
opponent: {
abbrev: "",
logoLink: "",
},
};
const oGame = aGames[i];
oData.gameDate = new Date(oGame.startTimeUTC);
if (oGame.awayTeam.abbrev == MY_NHL_TEAM) {
oData.opponent.abbrev = oGame.homeTeam.abbrev;
} else {
// Yeey, it's a homegame for my team :-)
oData.opponent.abbrev = oGame.awayTeam.abbrev;
}
oData.opponent.logoLink = oTeamData[oData.opponent.abbrev]?.logo;
aNextGames.push(oData);
}
return aNextGames;
}
/**
* Filters standings by team id.
*
* @param {String} sTeamId
* @param {Object} oStandings
* @return {Object}
*/
function filterStandingsByAbbreviation(sAbbreviation, oStandings) {
let oResult = null;
if (oStandings && oStandings?.standings) {
const oTeamStanding = oStandings.standings.find(standing => standing.teamAbbrev.default === sAbbreviation);
if (!!oTeamStanding) {
oResult = {
wins: oTeamStanding.wins,
losses: oTeamStanding.losses,
ot: oTeamStanding.otLosses,
divisionRank: oTeamStanding.divisionSequence,
leagueRank: oTeamStanding.leagueSequence,
};
}
}
if (oResult === null) {
oResult = {
wins: 0,
losses: 0,
ot: 0,
divisionRank: 0,
leagueRank: 0,
};
}
return oResult;
}
/**
* Fetches schedule data from NHL api.
*
* @return {Object}
*/
async function fetchScheduleData() {
const sUrl = `https://api-web.nhle.com/v1/club-schedule-season/${MY_NHL_TEAM}/${CURRENT_SEASON}`;
const oRequest = new Request(sUrl);
const oData = await oRequest.loadJSON();
const todayDate = new Date();
todayDate.setUTCHours(0, 0, 0, 0);
const aGames = oData.games?.filter(game => {
const gameDate = new Date(game.gameDate + 'T00:00:00Z');
return (
gameDate.getUTCFullYear() > todayDate.getUTCFullYear() ||
(gameDate.getUTCFullYear() === todayDate.getUTCFullYear() &&
gameDate.getUTCMonth() > todayDate.getUTCMonth()) ||
(gameDate.getUTCFullYear() === todayDate.getUTCFullYear() &&
gameDate.getUTCMonth() === todayDate.getUTCMonth() &&
gameDate.getUTCDate() >= todayDate.getUTCDate())
);
});
return aGames;
}
/**
* Fetches top scorer data from NHL api.
*
* @param {string} sAbbreviation
* @return {Object}
*/
async function fetchTopScorerByAbbreviation(sAbbreviation) {
const sUrl = `https://api-web.nhle.com/v1/club-stats/${sAbbreviation}/now`;
const oRequest = new Request(sUrl);
const oData = await oRequest.loadJSON();
let oResult = null;
if (!!oData) {
oResult = oData.skaters.sort((a, b) => a.points - b.points).reverse()[0];
}
return oResult;
}
/**
* Fetches live standings data from NHL api.
*
* @param {string} sGameId
* @return {Object}
*/
async function fetchLiveData(sGameId) {
const sUrl = `https://api-web.nhle.com/v1/gamecenter/${sGameId}/boxscore`;
const oRequest = new Request(sUrl);
const oLiveData = await oRequest.loadJSON();
return !oLiveData ? null : oLiveData;
}
/**
* Fetches conference and league standings data from NHL api.
*
* @return {Object}
*/
async function fetchCurrentStandings() {
const sUrl = `https://api-web.nhle.com/v1/standings/now`;
const oRequest = new Request(sUrl);
return await oRequest.loadJSON();
}
/**
* Loads image from thesportsdb.com or from local cache.
*
* @param {String} sImageUrl
* @param {String} sTeamAbbreviation
* @return {Object}
*/
async function loadLogo(sImageUrl, sTeamAbbreviation) {
let oResult;
if (CACHING_ACTIVE) {
// Set up the file manager.
const oFiles = FileManager.local();
// Set up cache
const sCachePath = oFiles.joinPath(
oFiles.cacheDirectory(),
sTeamAbbreviation + "_NHL"
);
const bCacheExists = oFiles.fileExists(sCachePath);
try {
if (bCacheExists) {
oResult = oFiles.readImage(sCachePath);
} else {
const oRequest = new Request(sImageUrl);
oResult = await oRequest.loadImage();
try {
oFiles.writeImage(sCachePath, oResult);
console.log("Created cache entry for logo of " + sTeamAbbreviation);
} catch (e) {
console.log(e);
}
}
} catch (oError) {
console.error(oError);
if (bCacheExists) {
oResult = oFiles.readImage(sCachePath);
} else {
console.log("Fetching logo for " + sTeamAbbreviation + " failed.");
}
}
} else {
const oRequest = new Request(sImageUrl);
oResult = await oRequest.loadImage();
}
return oResult;
}
/**
* Sets background for stack.
*
* @param {String} oStack
*/
async function setStackBackground(oStack) {
if (
NO_BACKGROUND_INSTALLED &&
NO_BACKGROUND_ACTIVE &&
NO_BACKGROUND_FULL_ACTIVE
) {
oStack.backgroundImage = await transparent(Script.name());
} else {
oStack.backgroundColor = STACK_BACKGROUND;
}
}
/**
* Returns color object depending if dark mode is active or not.
*
* @return {Object}
*/
function getColorForCurrentAppearance() {
return DARK_MODE ? Color.white() : Color.black();
}
/**
* Placeholder function when no-background.js isn't installed.
*
* @return {Object}
*/
function emptyFunction() {
// Silence
return {};
}
/**
* Returns static team data.
*
* @return {Object}
*/
function getTeamData() {
return {
// New Jersey Devils
NJD: {
id: "1",
logo:
"https://www.thesportsdb.com/images/media/team/badge/z4rsvp1619536740.png/preview",
},
// New York Islanders
NYI: {
id: "2",
logo:
"https://www.thesportsdb.com/images/media/team/badge/hqn8511619536714.png/preview",
},
// New York Rangers
NYR: {
id: "3",
logo:
"https://www.thesportsdb.com/images/media/team/badge/bez4251546192693.png/preview",
},
// Philadelphia Flyers
PHI: {
id: "4",
logo:
"https://www.thesportsdb.com/images/media/team/badge/qxxppp1421794965.png/preview",
},
// Pittsburgh Penguins
PIT: {
id: "5",
logo:
"https://www.thesportsdb.com/images/media/team/badge/dsj3on1546192477.png/preview",
},
// Boston Bruins
BOS: {
id: "6",
logo:
"https://www.thesportsdb.com/images/media/team/badge/b1r86e1720023232.png/preview",
},