This repository has been archived by the owner on Sep 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
388 lines (330 loc) · 11.5 KB
/
app.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
(function Game(){
"use strict";
var scenario = document.getElementById('scenario'),
action = document.getElementById('action'),
options = document.getElementById('options'),
detailContainer = document.getElementById('detail-container'),
detailImageContainer = document.getElementById('detail-image'),
detailTextContainer = document.getElementById('detail-text'),
gameContainer = document.getElementById('game-container'),
logoContainer = document.getElementById('logo-container'),
score = document.getElementById('score'),
chapterVal = parseInt(localStorage.getItem('chapter'),10),
gameState = {},
DEBUG = false;
setupGame(chapterVal);
function setupColorScheme(cv) {
var cssColorRule = document.createElement("style");
cssColorRule.type = "text/css";
switch(cv) {
case 1:
cssColorRule.innerHTML = "body {color: #D4D4D4 !important; background-color: #202020 !important;}";
break;
case 2:
cssColorRule.innerHTML = "body {color: #E1E1E1 !important; background-color: #646464 !important;}";
break;
case 3:
cssColorRule.innerHTML = "body {color: #595959 !important; background-color: #FFFFFF !important;}";
break;
}
document.body.appendChild(cssColorRule);
}
function createButton(data, div, enabled) {
var s = document.createElement("span");
if (typeof data === "string") { s.innerHTML = data; }
else if (typeof data === "object") { s.innerHTML = data.option; }
s.classList.add("button");
if (data.requirements && Object.keys(data.requirements).length != 0) {
for (var r in data.requirements) {
if ((r === "money" && data.requirements[r] > gameState.money) ||
(r === "cost" && data.requirements[r]*gameState.family > gameState.money) ||
(r === "family" && data.requirements[r] > gameState.family) ||
(r === "inventory" && data.requirements[r] > gameState.inventory)) {
s.classList.add("buttonDisabledColor");
}
}
}
if (!hasClass(s, "buttonDisabledColor")) {
s.addEventListener("click", function() {
if (typeof data === "object") {
gameState.chosenOption = data;
updateScore(data);
if (gameState.currentChallenges.length === 0) {
gameState.currentChallenges = chooseChallenges(gameState.chosenOption.data.challenges,gameState.currentScenario.challenges);
}
}
requestAnimationFrame(advanceState);
});
}
div.appendChild(s);
}
function createButtons(answerObj, div) {
if (answerObj.length === 3) {
var colSize = "col-sm-4";
} else if (answerObj.length === 2) {
var colSize = "col-sm-6";
}
var enableButton = true;
for (let i=0; i < answerObj.length; i++) {
var d = document.createElement("div");
d.classList.add(colSize);
createButton(answerObj[i], d, enableButton);
div.appendChild(d);
}
}
function clearDiv(arr) {
for (let i=0; i<arr.length; i++) {
arr[i].innerHTML = "";
}
}
function resetAnim(div, anim) {
if (hasClass(div, anim)) {
div.classList.remove(anim);
void div.offsetWidth;
div.classList.add(anim);
}
}
function hasClass(element, cls) {
return (' ' + element.className + ' ').indexOf(' ' + cls + ' ') > -1;
}
//TEMPORARY DEBUG FUNCTION
function updateScore(obj) {
if (obj.data.family) {
gameState.family += obj.data.family;
if (gameState.family < 0) { gameState.family = 0; }
}
if (obj.data.money) {
gameState.money += obj.data.money;
if (gameState.money < 0) { gameState.money = 0; }
}
if (obj.data.cost) {
gameState.money += obj.data.cost*gameState.family;
if (gameState.money < 0) { gameState.money = 0; }
}
if (obj.data.inventory) {
gameState.inventory += obj.data.inventory;
if (gameState.inventory < 0 ) { gameState.inventory = 0; }
}
if (obj.data.challenges) {
gameState.numChallenges = obj.data.challenges;
}
if (DEBUG) {
console.log("Family: " + gameState.family + " Money: " + gameState.money + " Inventory: " + gameState.inventory);
}
}
function createPText(data, div) {
if (typeof data === 'string') {
let p = document.createElement("p");
p.innerHTML = data;
div.appendChild(p);
} else if (typeof data === 'object') {
var p = document.createElement("p");
var spanA = document.createElement("span");
var spanB = document.createElement("span");
var link = document.createElement("span");
link.classList.add("text-link");
spanA.innerHTML = data.textA;
spanB.innerHTML = data.textB;
link.innerHTML = data.textLink;
link.addEventListener("click", function() {
var di = new Image();
di.onload = function() {
detailImageContainer.src = this.src;
detailTextContainer.innerHTML = data.detailText;
detailContainer.classList.remove("hidden");
resetAnim(detailContainer, 'fadeIn');
};
di.src = data.detailImage;
})
p.appendChild(spanA);
p.appendChild(link);
p.appendChild(spanB);
div.appendChild(p);
}
}
/* displayScenario accepts an object that contains all
question/options/data/response information for one round. It returns a
promise generated from this object and resolves it on click. */
function displayScenario(obj) {
createPText(gameState.currentScenario.scenario, scenario);
createPText(gameState.currentScenario.action, action);
createButtons(gameState.currentScenario.answers, options);
}
function displayResponse(obj) {
clearDiv([scenario,action,options,detailContainer]);
//clear animation
resetAnim(gameContainer, 'fadeIn');
createPText(gameState.chosenOption.response, scenario);
createButton("continue", action, true);
}
function chooseChallenges(numChallenges, challengeArr) {
var arr = []
while(arr.length < numChallenges){
var randomnumber = Math.floor(Math.random()*challengeArr.length)
arr[arr.length] = challengeArr.splice(randomnumber,1)[0];
}
return arr;
}
function initGame(chapter) {
//use this space to call on the arrays from questionBank - or maybe put
//it in a seperate function and use Promise.all in setupGame then this
//becomes initGameState
gameState.chapter = chapter;
gameState.family = 5;
gameState.money = 0;
gameState.inventory = 0;
gameState.numChallenges = 0;
gameState.challengeCounter = 0;
gameState.INVENTORY_MAX = 10;
gameState.stateList = [startScenario, runScenario, displayScenario, displayResponse, completeScenario];
gameState.winner = false;
gameState.playEntering = false;
gameState.playing = false;
gameState.playLeaving = false;
//not sure if I want this here or not
gameState.iterator = -1;
gameState.textElements = [];
gameState.chosenOption = {};
//NEEDED: state for max questions, and max eras?
gameState.currentScenario = {};
gameState.currentChallenges = [];
}
function retrieveQB(chapter) {
if (DEBUG) { console.log("retrieving QB"); }
return new Promise(function(resolve, reject) {
var httpRequest = new XMLHttpRequest();
httpRequest.open('GET','data.json');
httpRequest.onload = function() {
if (httpRequest.status >= 200 && httpRequest.status < 300) {
var json = JSON.parse(httpRequest.responseText);
gameState.questionBank = json.filter(function(row) {
if (row.chapter === chapter) {
return true;
} else {
return false;
}
})
resolve();
} else {
reject({
status: this.status,
statusText: httpRequest.statusText
});
}
};
httpRequest.onerror = function() {
reject({
status: this.status,
statusText: httpRequest.statusText
});
};
httpRequest.send();
});
}
function startScenario() {
//set state
gameState.playEntering = true;
//gameState.currentChallenges = [];
if (gameState.currentChallenges.length > 0) {
//gameState.currentChallenges = chooseChallenges(gameState.numChallenges,gameState.currentScenario.challenges);
gameState.currentScenario = gameState.currentChallenges.shift();
} else {
gameState.iterator++;
//grab new object
gameState.currentScenario = getNewScenario(gameState.questionBank);
}
//start run state
//Does it make sense to pass this state parameter in? Or just use it as
//a 'global'
requestAnimationFrame(advanceState);
}
function runScenario(obj) {
//async tasks - basically newState below
gameState.playEntering = false;
gameState.playing = true;
requestAnimationFrame(advanceState);
}
//This isn't the right name for this function... Sounds too much like the
//game is over
function completeScenario() {
gameState.playing = false;
gameState.playLeaving = true;
//clear div
clearDiv([scenario,action,options,detailContainer]);
//clear animation
resetAnim(gameContainer, 'fadeIn');
//save out object
//[[TODO]]
//save out state and send to data collection
//[[TODO]]
//call stateEnded?
//check win conditions if (gameState.winner = true)... else:
if (gameState.iterator >= gameState.questionBank.length-1) {
gameExit();
} else {
requestAnimationFrame(advanceState);
}
}
function gameExit() {
if (gameState.chapter < 3) {
if (gameState.chapter === 1) { document.body.classList.add('backgroundFade1to2'); }
if (gameState.chapter === 2) { document.body.classList.add('backgroundFade2to3'); }
setTimeout(function() {
setupGame(gameState.chapter+1);
}, 1500);
} else {
scenario.innerHTML = "You Win!"
var a = document.createElement("a");
a.innerHTML = "Return to beginning";
a.setAttribute("href","index.html");
options.appendChild(a);
}
}
//should I pass in the entire question bank, or part of it?
function getNewScenario(qb) {
console.log(qb);
return qb[gameState.iterator];
}
function advanceState() {
if (typeof gameState.stateList != "undefined" && gameState.stateList.length > 0) {
var stateFn = gameState.stateList.shift();
stateFn();
} else {
gameState.stateList = [startScenario, runScenario, displayScenario, displayResponse, completeScenario];
requestAnimationFrame(advanceState);
}
}
function introChapter() {
if (DEBUG) { advanceState(); }
else {
var chapter = gameState.chapter;
var logo;
switch(chapter) {
case 1:
logo = document.getElementById("ica-logo-o");
break;
case 2:
logo = document.getElementById("ica-logo-op");
break;
case 3:
logo = document.getElementById("ica-logo-opg");
break;
}
logo.classList.remove("hidden");
logo.classList.add('logoFadeIn');
setTimeout(function() {
logo.classList.add('logoFadeOut');
}, 4000)
setTimeout(function() {
// clearDiv([logoContainer]);
resetAnim(gameContainer, 'fadeIn');
logo.classList.add("hidden");
advanceState();
}, 5000);
}
}
function setupGame(cv) {
return Promise.all([setupColorScheme(cv), initGame(cv), retrieveQB(cv)])
.then(introChapter);
}
})();