-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMMM-Liquipedia-Matches.js
127 lines (110 loc) · 3.39 KB
/
MMM-Liquipedia-Matches.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
Module.register("MMM-Liquipedia-Matches",{
defaults: {
matchUpdateInterval : 60*60*1000, //Once every hour should be a good enough default
displayCount : 5,
requiredProfiles: 0,
language: config.language,
game: "dota2",
sourceUrl : null,
requiredTeams: []
},
start: function() {
var self = this;
moment.locale(self.config.language);
self.sendSocketNotification("LOAD_LIQUIPEDIA_MATCHES", self.config);
setInterval(function() {
self.sendSocketNotification("LOAD_LIQUIPEDIA_MATCHES", self.config);
}, self.config.matchUpdateInterval);
},
notificationReceived: function(notification, payload, sender) {
if (notification == "ALL_MODULES_STARTED" && MM.getModules().withClass("clock").length != 1) {
this.sendNotification("SHOW_ALERT", {
title : this.name + ": " + this.translate("CONFIG_ERROR_TITLE"),
message : this.translate("CONFIG_ERROR_MESSAGE")
});
}
if (notification == "CLOCK_MINUTE") {
this.updateDom();
}
},
getTemplate: function () {
return "MMM-Liquipedia-Matches.njk";
},
getScripts: function() {
return ["moment.js"];
},
getStyles: function() {
return ["MMM-Liquipedia-Matches.css"];
},
getTranslations: function() {
return {
sv: "translations/sv.json",
en: "translations/en.json"
};
},
getTemplateData: function () {
var self = this;
if (self.matches == undefined) {
return { matches : [] };
}
return {
matches : self.matches.matches.filter(self.matchFilter(config)).slice(0, self.config.displayCount).map(function(match) {
let matchDate = new Date(match.date);
return {
team1 : match.team1,
team2 : match.team2,
tournament : match.tournament,
starts : self.timeRemaining(matchDate),
live : new Date().getTime() > matchDate.getTime()
}
}),
logoPath: '/modules/' + this.name + '/public/logos/' + self.config.game + '/'
}
},
socketNotificationReceived: function (notification, payload) {
if (notification == "LIQUIPEDIA_MATCHES" && payload.game == this.config.game) {
this.matches = { matches : payload.data };
this.updateDom();
} else if (notification == "LIQUIPEDIA_MATCHES_ERROR" && payload.game == this.config.game) {
this.sendNotification("SHOW_ALERT", {
type : "notification",
title : this.name + ": " + this.translate("REQUEST_ERROR_TITLE"),
message : this.translate("REQUEST_ERROR_MESSAGE", {
statusCode : payload.statusCode,
url : payload.url,
}),
timer : 5000
});
}
},
timeRemaining : function(date) {
let momentDate = moment(date);
return momentDate.fromNow();
},
matchFilter : function() {
let self = this;
if (self.config.requiredTeams.length > 0) {
return self.teamsFilter(self.config.requiredTeams.map(team => self.normalize(team)));
} else {
return self.profileFilter(self.config.requiredProfiles);
}
},
teamsFilter : function(requiredTeams) {
let self = this;
return function(match) {
return requiredTeams.indexOf(self.normalize(match.team1.name)) != -1 || requiredTeams.indexOf(self.normalize(match.team2.name)) != -1;
};
},
profileFilter : function(requiredProfiles) {
return function(match) {
var profiles = (match.team1.hasProfile ? 1 : 0) + (match.team2.hasProfile ? 1 : 0);
return profiles >= requiredProfiles && match.team1.name && match.team2.name;
};
},
normalize : function(name) {
if (!name) {
return "";
}
return name.toLowerCase().replace(/[^a-z0-9]/, "");
}
});