-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMM-Rugby.js
505 lines (449 loc) · 17.5 KB
/
MMM-Rugby.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
Module.register("MMM-Rugby", {
defaults: {
title: "MMM-Rugby",
updateInterval: 1000 * 60 * 60 * 24,
rotateInterval: 60000,
sport: "mru",
rankingLimit: 10,
matchesLimit: 10,
competitions: [],
collectionType: "apiSport", // free
apiSports: {
apiSportStandingLeagueId: 16,
apiSportKey: "your-api-key",
apiSportTZ: "Africa/Johannesburg",
numberofGamesToDisplay: 10,
apiSportsNumRankings: 10,
apiSportDaysPast: 7,
apiSportsDaysFuture: 14
}
},
getStyles: function () {
return ["MMM-Rugby.css"]
},
getTranslations: function () {
return {
fr: "translations/fr.json",
en: "translations/en.json"
}
},
getTemplate: function () { },
start: function () {
var self = this;
Log.info(`Starting module: ${this.name}`);
this.currentTable = 1;
this.dataSets = {
free: {
rankingData: [],
matchData: []
},
apiSport: {
rankingData: [],
matchData: []
}
};
this.currentTables = {
free: 1,
apiSport: 3
};
this.getData();
this.scheduleUpdate();
setInterval(function () {
self.rotateTables();
}, this.config.rotateInterval);
},
getData: function () {
if (this.config.collectionType === "free") {
this.getRankingData();
this.getMatchData();
} else if (this.config.collectionType === "apiSport") {
this.getApiSportData();
}
},
refreshData: function () {
if (this.config.collectionType === "free") {
this.getRankingData();
this.getMatchData();
} else if (this.config.collectionType === "apiSport") {
this.refreshApiSportData();
}
},
getRankingData: function () {
this.sendSocketNotification("GET_RANKING_DATA", this.config)
},
getMatchData: function () {
this.sendSocketNotification("GET_MATCH_DATA", this.config)
},
getApiSportData: function () {
this.sendSocketNotification("GET_API_SPORT_DATA", this.config)
},
refreshApiSportData: function () {
this.sendSocketNotification("REFRESH_API_SPORT_DATA", this.config)
},
refreshApiSportLeagues: function () {
this.sendSocketNotitication("GET_API_SPORT_LEAGUE", this.config)
},
scheduleUpdate: function () {
// Define intervals based on the current day of the week
const currentDate = new Date();
const dayOfWeek = currentDate.getDay(); // Sunday is 0, Saturday is 6
let interval;
if (dayOfWeek === 6 || dayOfWeek == 0) {
// Sunday and Saturday, update every half an hour
interval = 30 * 60 * 1000; // 30 minutes in milliseconds
} else {
// Monday to Friday, update every hour
interval = 60 * 60 * 1000; // 1 hour in milliseconds
}
if (this.config.autoUpdate) {
// Second interval for daily updates
const oneDayMilliseconds = 24 * 60 * 60 * 1000; // milliseconds in a day
setInterval(() => {
this.refreshApiSportLeagues();
}, oneDayMilliseconds);
setTimeout(() => {
setInterval(() => {
this.refreshData();
}, interval);
}, 2000);
} else {
// Second interval for daily updates
const oneDayMilliseconds = 24 * 60 * 60 * 1000; // milliseconds in a day
setInterval(() => {
this.refreshApiSportLeagues();
}, oneDayMilliseconds);
setTimeout(() => {
setInterval(() => {
this.refreshData();
}, this.config.updateInterval);
}, 2000);
}
},
socketNotificationReceived: function (notification, payload) {
var self = this;
if (notification === "RUGBY_RANKING_DATA") {
this.dataSets[this.config.collectionType].rankingData = payload;
this.table1 = this.createTable1(this.dataSets.free.rankingData);
//this.table1 = this.createTable1(payload)
//self.updateDom()
}
if (notification === "RUGBY_MATCH_DATA") {
this.dataSets[this.config.collectionType].matchData = payload;
this.table2 = this.createTable2(this.dataSets.free.matchData);
//this.table2 = this.createTable2(payload)
//self.updateDom();
}
if (notification === "API_SPORT_GAME_DATA") {
this.dataSets[this.config.collectionType].matchData = payload;
this.table2 = this.createTable4(this.dataSets.apiSport.matchData);
} else if (notification === "API_SPORT_STANDING_DATA") {
this.dataSets[this.config.collectionType].rankingData = payload;
this.table1 = this.createTable3(this.dataSets.apiSport.rankingData);
}
this.updateDom();
},
rotateTables: function () {
// Toggle visibility of tables
if (this.currentTable === 1) {
this.currentTable = 2;
this.table1.style.display = "none";
this.table2.style.display = "block";
} else {
this.currentTable = 1;
this.table1.style.display = "block";
this.table2.style.display = "none";
}
},
createTable1: function (dataSet) {
var MMMRugbyDiv = document.createElement('div');
MMMRugbyDiv.classList.add('MMM-RugbyDiv');
var rugbyHeader = document.createElement('div')
rugbyHeader.className = 'medium'
var headerSpan = document.createElement('span')
headerSpan.classList.add('medium', 'bright')
headerSpan.setAttribute('align', 'right')
headerSpan.innerHTML = this.translate("World Rugby Standings")
rugbyHeader.appendChild(headerSpan)
MMMRugbyDiv.appendChild(rugbyHeader)
// Create table for first set of data
var table1 = document.createElement("table");
var table1Header = document.createElement("thead");
var table1Body = document.createElement("tbody");
// Create header row for Position
var table1HeaderRow = document.createElement("tr");
const posHead = document.createElement('td');
posHead.setAttribute('align', 'center');
posHead.innerHTML = this.translate("Rank");
table1HeaderRow.appendChild(posHead)
// Create header row for Flag
const flagHead = document.createElement('td');
flagHead.setAttribute('align', 'center');
flagHead.innerHTML = this.translate('Flag');
table1HeaderRow.appendChild(flagHead)
// Create header row for Team
const teamHead = document.createElement('td');
teamHead.setAttribute('align', 'center');
teamHead.innerHTML = this.translate('Team');
table1HeaderRow.appendChild(teamHead)
// Create header row for Points
const pointsHead = document.createElement('td');
pointsHead.setAttribute('align', 'center');
pointsHead.innerHTML = this.translate('Points');
table1HeaderRow.appendChild(pointsHead);
// Append header row to table1 header
table1Header.appendChild(table1HeaderRow);
// Populate table1 body with data
dataSet.forEach(function (data) {
var row = document.createElement("tr");
const pos = document.createElement("td");
pos.setAttribute('align', 'left');
pos.innerHTML = data.position;
row.appendChild(pos);
const teamFlagCell = document.createElement('td');
teamFlagCell.setAttribute('align', 'left');
const teamFlag = document.createElement('img');
teamFlag.className = 'MMM-Rugbylogo';
teamFlag.src = data.flag
teamFlag.width = 20;
teamFlag.height = 20;
teamFlagCell.appendChild(teamFlag);
row.appendChild(teamFlagCell);
const teamName = document.createElement('td');
teamName.setAttribute('align', 'left');
teamName.innerHTML = data.name;
row.appendChild(teamName);
var teampoints = data.points.toFixed(2)
const teamPoints = document.createElement('td')
teamPoints.setAttribute('align', 'right')
teamPoints.innerHTML = teampoints
row.appendChild(teamPoints)
table1Body.appendChild(row);
});
table1.appendChild(table1Header);
table1.appendChild(table1Body);
MMMRugbyDiv.appendChild(table1)
return MMMRugbyDiv;
},
createTable2: function (dataSet) {
var self = this;
var CompetitionType = "";
// Determine the competition type
switch (this.config.sport) {
case "mru":
CompetitionType = "Mens Rugby Union";
break;
case "mrs":
CompetitionType = "Mens Sevens Series";
break;
case "wrs":
CompetitionType = "Woman's Sevens Series";
break;
case "jmu":
CompetitionType = "U20 Six Nations";
break;
}
let container = document.createElement("div");
container.classList.add("MMMRugbyDiv");
var rugbyHeader = document.createElement('div')
rugbyHeader.className = 'medium'
var headerSpan = document.createElement('span')
headerSpan.className = 'medium'
headerSpan.setAttribute('align', 'right')
headerSpan.innerHTML = CompetitionType
rugbyHeader.appendChild(headerSpan)
container.appendChild(rugbyHeader)
var table = document.createElement("table");
table.id = "misSportMatchesTable";
// Create table header
var thead = document.createElement("thead");
thead.classList.add('xsmall', 'bright');
thead.innerHTML = `
<tr>
<th>${this.translate("Date")}</th>
<th>${this.translate("Time")}</th>
${self.config.sport === 'mru' && self.config.competitions.length > 1 ? `<th>${this.translate("Competition")}</th>` : ''}
<th>${this.translate("Home")}</th>
<th>${this.translate("Away")}</th>
<th>${this.translate("Score")}</th>
</tr>
`;
table.appendChild(thead);
// Create table body
var tbody = document.createElement("tbody");
dataSet.forEach(function (match) {
var row = document.createElement("tr");
row.classList.add('xsmall', 'bright');
var homeTeamScore = match.team1Score !== null ? match.team1Score : 0;
var awayTeamScore = match.team2Score !== null ? match.team2Score : 0;
row.innerHTML = `
<td>${match.matchDateTime}</td>
<td>${match.eventStart}</td>
${self.config.sport === 'mru' && self.config.competitions.length > 1 ? `<td>${match.competition}</td>` : ''}
<td><img src="${match.team1Flag}" class="team-flag" /> ${match.team1Name}</td>
<td><img src="${match.team2Flag}" class="team-flag" /> ${match.team2Name}</td>
<td>${homeTeamScore} - ${awayTeamScore}</td>
`;
tbody.appendChild(row);
});
table.appendChild(tbody);
container.appendChild(table);
return container;
},
createTable3: function (dataSet) {
let container = document.createElement('div');
container.classList.add("MMMRugbyDiv");
var headerDiv = document.createElement("div");
headerDiv.id = "rankingData";
headerDiv.classList.add('bright');
let firstData = dataSet[0][0];
var currentDate = new Date().toLocaleDateString();
const firstLeagueFlag = dataSet[0].leagueFlag;
const leagueName = dataSet[0].league;
headerDiv.innerHTML = `
<span>${leagueName}</span>
<img src="${firstLeagueFlag}"/>
<span>${currentDate}</span>
`;
container.appendChild(headerDiv);
// Table
var table = document.createElement("table");
table.id = "MMMRugby-rankingTable";
var thead = document.createElement("thead");
thead.classList.add('xsmall', 'bright')
thead.innerHTML = `
<tr>
<th>${this.translate("Rank")}</th>
<th>${this.translate("Flag")}</th>
<th>${this.translate("Team")}</th>
<th>${this.translate("Country")}</th>
<th>${this.translate("Pld")}</th>
<th>${this.translate("W")}</th>
<th>${this.translate("D")}</th>
<th>${this.translate("L")}</th>
<th>${this.translate("GD")}</th>
<th>${this.translate("Pts")}</th>
</tr>
`;
table.appendChild(thead);
var tbody = document.createElement("tbody");
dataSet.forEach(function (entry) {
var row = document.createElement("tr");
var goalDiff = entry.teamStats.goalsFor - entry.teamStats.goalsAgainst
row.classList.add('xsmall', 'bright')
row.innerHTML = `
<td>${entry.rank}</td>
<td><img src="${entry.teamFlag}"/> </td>
<td>${entry.teamName}</td>
<td>${entry.teamCountry}</td>
<td>${entry.teamStats.gamesPlayed}</td>
<td>${entry.teamStats.win}</td>
<td>${entry.teamStats.draw}</td>
<td>${entry.teamStats.lose}</td>
<td>${goalDiff}</td>
<td>${entry.teamStats.points}</td>
`;
tbody.appendChild(row);
});
table.appendChild(tbody);
container.appendChild(table);
return container;
},
createTable4: function (dataSet) {
let container = document.createElement('div');
container.classList.add("MMMRugbyDiv");
var headerDiv = document.createElement("div");
const leagueName = dataSet[0].leagueName;
const firstLeagueFlag = dataSet[0].leagueFlag;
var currentDate = new Date().toLocaleDateString();
headerDiv.id = "matchData";
headerDiv.classList.add('medium', 'bright');
headerDiv.innerHTML = `
<span>${leagueName}</span>
<img src="${firstLeagueFlag}"/>
<span>${currentDate}</span>
`
container.appendChild(headerDiv);
var table = document.createElement("table");
table.id = "rankingMatchesTable";
// Create table header
var thead = document.createElement("thead");
thead.classList.add('xsmall', 'bright')
thead.innerHTML = `
<tr>
<th>${this.translate("Date")}</th>
<th>${this.translate("Time")}</th>
<th>${this.translate("Home")}</th>
<th>${this.translate("Away")}</th>
<th>${this.translate("Score")}</th>
</tr>
`;
table.appendChild(thead);
// Create table body
var tbody = document.createElement("tbody");
dataSet.forEach(function (match) {
var row = document.createElement("tr");
row.classList.add('xsmall', 'bright')
var homeTeamScore = match.homeTeamScore !== null ? match.homeTeamScore : 0;
var awayTeamScore = match.awayTeamScore !== null ? match.awayTeamScore : 0;
row.innerHTML = `
<td>${match.matchDate}</td>
<td>${match.matchTime}</td>
<td><img src="${match.homeTeamFlag}" class="team-flag" /> ${match.homeTeam}</td>
<td><img src="${match.awayTeamFlag}" class="team-flag" /> ${match.awayTeam}</td>
<td>${homeTeamScore} - ${awayTeamScore}</td>
`;
tbody.appendChild(row);
});
table.appendChild(tbody);
container.appendChild(table);
return container;
},
getDom: function () {
var wrapper = document.createElement("div");
if (this.config.collectionType === "free") {
if (!this.table1) {
this.table1 = document.createElement("table");
}
if (!this.table2) {
this.table2 = document.createElement("table");
}
if (this.currentTable === 1) {
this.table1.style.display = "block";
this.table2.style.display = "none";
}
else {
this.table1.style.display = "none";
this.table2.style.display = "block";
}
if (!wrapper.contains(this.table1)) {
wrapper.appendChild(this.table1);
}
if (!wrapper.contains(this.table2)) {
wrapper.appendChild(this.table2);
}
//wrapper.appendChild(this.table1);
//wrapper.appendChild(this.table2);
} else if (this.config.collectionType === "apiSport") {
if (!this.table1) {
this.table1 = document.createElement("table");
}
if (!this.table2) {
this.table2 = document.createElement("table");
}
if (this.currentTable === 1) {
this.table1.style.display = "block";
this.table2.style.display = "none";
} else {
this.table1.style.display = "none";
this.table2.style.display = "block";
}
if (!wrapper.contains(this.table1)) {
wrapper.appendChild(this.table1);
}
if (!wrapper.contains(this.table2)) {
wrapper.appendChild(this.table2);
}
}
return wrapper;
}
})