-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMMM-ShiftStats.js
More file actions
166 lines (136 loc) · 4.17 KB
/
MMM-ShiftStats.js
File metadata and controls
166 lines (136 loc) · 4.17 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
shiftStats = {};
Module.register("MMM-ShiftStats",{
defaults: {
apiKey: null,
type: 'Regular Season',
teamName: null,
sport: null,
mode: 'standings',
maxGames: 6,
teamNameClass: "light",
updateInterval: 12 * 60 * 60 * 1000
},
// search for team name, look for newest team, pick playoffs if there are games, otherwise reg season, otherwise exhibition?
// teamSearch response comes with references.season which should get you the info you need. season has multiple season.stats
// which has .Exhibition and etc. which contains games_played.
start: function() {
this.sendSocketNotification('SHIFTSTATS_CONFIG', this.config);
this.sendSocketNotification('SHIFTSTATS_UPDATE');
},
getStyles: function() {
return ["shiftstats.css"];
},
getDom: function() {
if (!shiftStats.standings || !shiftStats.games) {
let wrapper = document.createElement("div");
wrapper.innerHTML = 'Loading...';
wrapper.className += "dimmed light small";
return wrapper;
} else {
let table = document.createElement('table');
table.className = "small";
if (this.config.colored) {
table.className += " colored";
}
if (this.config.mode == 'games') {
table = showGames(table, this.config)
} else {
table = showStandings(table)
}
return table;
}
},
socketNotificationReceived: function(notification, payload) {
if (notification === 'SHIFTSTATS_STANDINGS') {
shiftStats = payload;
this.updateDom();
}
}
});
function showGames(table, config) {
let now = new Date()
let mostRecent = []
shiftStats.games.games.forEach((game) => {
if (new Date(game.datetime) < now) {
mostRecent.push({
game: game,
home_team: shiftStats.games.references.team.find(team => team.id == game.home_team_id),
away_team: shiftStats.games.references.team.find(team => team.id == game.away_team_id),
})
if (mostRecent.length > config.maxGames) {
mostRecent.shift()
}
}
})
let row
let cell
mostRecent.forEach((game) => {
row = document.createElement('tr')
cell = document.createElement('td')
cell.innerHTML = game.home_team.name
cell.className = config.teamNameClass
row.appendChild(cell)
cell = document.createElement('td')
cell.innerHTML = game.game.stats.home_score
cell.className = 'center'
row.appendChild(cell)
cell = document.createElement('td')
cell.innerHTML = game.game.stats.away_score
cell.className = 'center'
row.appendChild(cell)
cell = document.createElement('td')
cell.innerHTML = game.away_team.name
cell.className = `left ${config.teamNameClass}`
row.appendChild(cell)
table.appendChild(row)
})
return table
}
function showStandings(table) {
let row = document.createElement('tr')
let cell = document.createElement('th')
cell.innerHTML = 'Team'
row.appendChild(cell)
cell = document.createElement('th')
cell.className = 'center'
cell.innerHTML = 'W'
row.appendChild(cell)
cell = document.createElement('th')
cell.className = 'center'
cell.innerHTML = 'L'
row.appendChild(cell)
cell = document.createElement('th')
cell.className = 'center'
cell.innerHTML = 'OTW'
row.appendChild(cell)
cell = document.createElement('th')
cell.className = 'center'
cell.innerHTML = 'OTL'
row.appendChild(cell)
table.appendChild(row)
shiftStats.standings.teams.forEach((team) => {
row = document.createElement('tr')
cell = document.createElement('td')
cell.innerHTML = team.name
cell.className = 'light'
row.appendChild(cell)
cell = document.createElement('td')
cell.innerHTML = team.stats['Regular Season'].wins
cell.className = 'center'
row.appendChild(cell)
cell = document.createElement('td')
cell.innerHTML = team.stats['Regular Season'].losses
cell.className = 'center'
row.appendChild(cell)
cell = document.createElement('td')
cell.innerHTML = team.stats['Regular Season'].otw
cell.className = 'center'
row.appendChild(cell)
cell = document.createElement('td')
cell.innerHTML = team.stats['Regular Season'].otl
cell.className = 'center'
row.appendChild(cell)
table.appendChild(row)
})
return table
}