-
Notifications
You must be signed in to change notification settings - Fork 1
/
MMM-JEOPARDY.js
156 lines (127 loc) · 5.07 KB
/
MMM-JEOPARDY.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
/* Magic Mirror
* Module: MMM-JEOPARDY
*
* By Mykle1
*
*/
Module.register("MMM-JEOPARDY", {
// Module config defaults.
defaults: {
rotateInterval: 30 * 1000, // New Jeopardy clue rotation.
useHeader: false, // true if you want a header
header: "THIS IS JEOPARDY!", // Any text you want
maxWidth: "250px",
animationSpeed: 3000, // Clue fade in and out speed
initialLoadDelay: 4250,
retryDelay: 2500,
updateInterval: 60 * 60 * 1000, // 1 hour = 100 clues per call
},
getStyles: function() {
return ["MMM-JEOPARDY.css"];
},
start: function() {
Log.info("Starting module: " + this.name);
requiresVersion: "2.1.0",
// Set locale.
this.url = "http://jservice.io/api/random?count=100";
this.jeopardy = [];
this.activeItem = 0;
this.rotateInterval = null;
this.scheduleUpdate();
},
getDom: function() {
var wrapper = document.createElement("div");
wrapper.className = "wrapper";
wrapper.style.maxWidth = this.config.maxWidth;
if (!this.loaded) {
wrapper.innerHTML = "This is JEOPARDY!";
wrapper.classList.add("bright", "light", "small");
return wrapper;
}
if (this.config.useHeader != false) {
var header = document.createElement("header");
header.classList.add("xsmall", "bright", "light");
header.innerHTML = this.config.header;
wrapper.appendChild(header);
}
var jeopardyKeys = Object.keys(this.jeopardy);
if (jeopardyKeys.length > 0) {
if (this.activeItem >= jeopardyKeys.length) {
this.activeItem = 0;
}
var jeopardy = this.jeopardy[jeopardyKeys[this.activeItem]];
var top = document.createElement("div");
top.classList.add("list-row");
var pic = document.createElement("div");
var img = document.createElement("img");
img.classList.add("img");
img.src = "modules/MMM-JEOPARDY/pix/2.JPG";
pic.appendChild(img);
wrapper.appendChild(pic);
var category = document.createElement("div");
var str = jeopardy.category.title;
var res = str.toUpperCase();
category.classList.add("xsmall", "bright");
category.innerHTML = "Category:  " + res; // jeopardy.category.title;
wrapper.appendChild(category);
var jeopardyValue = document.createElement("div");
jeopardyValue.classList.add("xsmall", "bright");
jeopardyValue.innerHTML = (jeopardy.value != null) ? "For: $" + jeopardy.value : "For: $200";
wrapper.appendChild(jeopardyValue);
var jeopardyClue = document.createElement("div");
jeopardyClue.classList.add("xsmall", "bright");
jeopardyClue.innerHTML = "The clue is:  " + jeopardy.question + ".";
wrapper.appendChild(jeopardyClue);
var jeopardyAnswer = document.createElement("div");
jeopardyAnswer.classList.add("small", "bright");
setTimeout(function() {
jeopardyAnswer.innerHTML = "What is " + jeopardy.answer + "?"
}, 20 * 1000);
wrapper.appendChild(jeopardyAnswer);
}
return wrapper;
},
///// Add this function to the modules you want to control with voice //////
notificationReceived: function(notification, payload) {
if (notification === 'HIDE_JEOPARDY') {
this.hide(1000);
// this.updateDom(300);
} else if (notification === 'SHOW_JEOPARDY') {
this.show(1000);
// this.updateDom(300);
}
},
processJEOPARDY: function(data) {
this.today = data.Today;
this.jeopardy = data;
// console.log(this.jeopardy); checking my data
this.loaded = true;
},
scheduleCarousel: function() {
console.log("Carousel fucktion!");
this.rotateInterval = setInterval(() => {
this.activeItem++;
this.updateDom(this.config.animationSpeed);
}, this.config.rotateInterval);
},
scheduleUpdate: function() {
setInterval(() => {
this.getJEOPARDY();
}, this.config.updateInterval);
this.getJEOPARDY(this.config.initialLoadDelay);
var self = this;
},
getJEOPARDY: function() {
this.sendSocketNotification('GET_JEOPARDY', this.url);
},
socketNotificationReceived: function(notification, payload) {
if (notification === "JEOPARDY_RESULT") {
this.processJEOPARDY(payload);
if (this.rotateInterval == null) {
this.scheduleCarousel();
}
this.updateDom(this.config.animationSpeed);
}
this.updateDom(this.config.initialLoadDelay);
},
});