-
Notifications
You must be signed in to change notification settings - Fork 0
/
MMM-Clash-Royale.js
132 lines (107 loc) · 4.09 KB
/
MMM-Clash-Royale.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
/* global Log, Module */
/* Magic Mirror
* Module: MMM-Clash-Royale
*
* By Ian Perrin http://ianperrin.com
* MIT Licensed.
*/
Module.register("MMM-Clash-Royale",{
// Set the minimum MagicMirror module version for this module.
requiresVersion: "2.0.0",
// Module config defaults.
defaults: {
updateInterval: 5 * 60 * 1000, // every 5 minutes
animationSpeed: 1000, // 1 second to fade between cards
initialLoadDelay: 0, // 0 seconds delay
grayscale: true, // show cards in grayscale
apiBase: "http://www.clashapi.xyz", // base URL for the api
showName: true, // shows the card name
showDescription: false, // shows the card description
debug: false, // output debugging messages to console and log
},
// our cards
cards: false,
// A loading boolean.
loading: true,
// Subclass getStyles method.
getStyles: function () {
return ["MMM-Clash-Royale.css"];
},
// Subclass start method.
start: function() {
Log.info("Starting module: " + this.name);
this.getData();
this.scheduleUpdate(this.config.updateInterval);
},
getData: function(){
this.sendSocketNotification("GET_DATA", this.config);
},
scheduleUpdate: function(delay) {
var nextLoad = this.config.updateInterval;
if (typeof delay !== "undefined" && delay >= 0) {
nextLoad = delay;
}
var self = this;
setTimeout(function() {
self.getData();
}, nextLoad);
},
// Subclass socketNotificationReceived method.
socketNotificationReceived: function(notification, payload){
if(notification === "RANDOM_DECK"){
console.log(payload);
this.cards = payload;
this.loading = (!this.cards);
this.updateDom(this.config.animationSpeed);
}
},
getDom: function() {
var wrapper = document.createElement("div");
wrapper.classList.add("small");
if (this.loading) {
wrapper.innerHTML = this.translate('LOADING');
wrapper.classList.add("dimmed","light");
return wrapper;
}
var cardList = document.createElement("ul");
cardList.classList.add("cards");
// Loop through card array
for (var i = this.cards.length - 1; i >= 0; i--) {
// get current card
var card = this.cards[i];
// card wrapper
var cardWrapper = document.createElement("li");
cardWrapper.classList.add("small", "card");
// show image for current card
var cardImgDiv = document.createElement("div");
cardImgDiv.classList.add("image");
if(card.elixirCost) {
cardImgDiv.setAttribute("data-badge",card.elixirCost);
}
var cardImg = document.createElement("img");
cardImg.src = this.config.apiBase + "/images/cards/" + card.idName + ".png";
if(this.config.grayscale){
cardImg.classList.add("grayscale");
}
cardImgDiv.appendChild(cardImg);
cardWrapper.appendChild(cardImgDiv);
// show name for current card
if (this.config.showName) {
var cardName = document.createElement("div");
cardName.innerHTML = card.name;
cardName.classList.add("name");
cardWrapper.appendChild(cardName);
}
// show description for current card
if (this.config.showDescription) {
var cardDescription = document.createElement("div");
cardDescription.innerHTML = card.description;
cardDescription.classList.add("light", "description");
cardWrapper.appendChild(cardDescription);
}
cardList.appendChild(cardWrapper);
}
wrapper.appendChild(cardList);
return wrapper;
},
});