-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser_json.js
532 lines (495 loc) · 13.2 KB
/
parser_json.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
/**
* parser_json.js
*
* functions to get information from the game json file
*/
/**
* Returns all the scenes of the JSON file (GameJson)
* @returns scene[]
*/
function getScenes() {
var json = GameJson;
return json.Document.Process.Scenes;
}
/**
* Returns path of the background image from the scene object specified
* @param {Scene object} scene
* @returns path
*/
function getSceneImage(scene) {
return getGameFolderURL() + scene.Image;
}
/**
* Returns the id of the specified scene
* @param {Scene object} scene
* @returns scene id
*/
function getSceneId(scene) {
return scene.id;
}
/**
* Note : may replace getSceneId
* Returns the id of an element
* @param {object} element
* @returns element id
*/
function getElementId(element) {
return element.id;
}
/**
* Returns an array containing the current width and height of the scene
* @param {Scene object} scene
* @returns tabular containing the width and height of the scene
*/
function getImageSize(scene) {
let image_size = [];
image_size.push({
'width': scene.ImageSize[0],
'height': scene.ImageSize[1]
});
return image_size;
}
/**
* Returns the intial scene object from the game JSON
* NOTE : different from get_scene_by_id because there is a special type if
* a scene is an initial scene (i.e. SceneType = 1; Final = 2; Other = 0)
* @returns initial scene
*/
function getInitialScene() {
const scenes = getScenes();
const length = Object.keys(scenes).length;
for (var i = 0; i < length; i++) {
if (scenes[i].SceneType == 1) {
return scenes[i];
}
}
}
/**
* Returns the click areas described in the game JSON for the scene object 'scene'
* @param {Scene object} scene
* @returns click areas of the scene
*/
function getClickAreas(scene) {
return scene.ClickAreas;
}
/**
* Returns the back click areas described in the game JSON for the scene object 'scene'
* @param {Scene object} scene
* @returns back click areas of the scene
*/
function getBackClickAreas(scene) {
return scene.BackClickAreas;
}
/**
* Returns the FIRST back click areas described in the game JSON for the scene object 'scene'
* @param {Scene object} scene
* @returns first back click area of the scene
*/
function getBackClickArea(scene){
return scene.BackClickAreas[0];
}
/**
* Returns the gifs described in the game JSON for the scene object 'scene'
* @param {Scene object} scene
* @returns gifs of the scene
*/
function getGifs(scene) {
return scene.Gifs;
}
/**
* Returns the objects described in the game JSON for the scene object 'scene'
* @param {Scene object} scene
* @returns objects of the scene
*/
function getObjects(scene) {
return scene.Objects;
}
/**
* Returns the textAreas described in the game JSON for the scene object 'scene'
* @param {Scene object} scene
* @returns text areas of the scene
*/
function getTexts(scene) {
return scene.TextAreas;
}
/**
* Returns all the transitions
* @param {Scene object} scene
* @returns transition[]
*/
function getTransitions() {
var json = GameJson;
return json.Document.Process.Transitions;
}
/**
* Plays the sound described in the parsed part of JSON element
* @param {JSON object} element
* @returns sound path
*/
function getSoundPath(element) {
return element.Sound.Path;
}
/**
* Searches for the ClickArea (in game JSON) whose path matches 'path'
* and returns the scene id to which it points
* @param {string} clickAreaPath
* @returns id of the next scene
*/
function getPointedScene(clickAreaPath) {
let scene = GameJson.Document.Process;
let len = scene.Transitions.length;
for (let i = 0; i < len; i++) {
if (scene.Transitions[i].Transition.Which == "ClickAreaToScene") {
let elem = scene.Transitions[i].Transition;
len = elem.ClickAreaToScene.To.length;
if (elem.ClickAreaToScene.From == clickAreaPath) {
while (elem.ClickAreaToScene.To[len]!=".") {
len--;
}
return parseInt(elem.ClickAreaToScene.To.substring(len+1, elem.ClickAreaToScene.To.length));
}
}
}
return -1
}
/**
* Returns a tabular of all the puzzle objects in the scene id
* @param {Number} id
* @returns Objects[]
*/
function getPuzzlepieces(id){
const scene = getSceneByID(id);
let tab = [];
for(let i = 0; i < scene.Objects.length; i++){
if(scene.Objects[i].PuzzlePiece){
tab.push(scene.Objects[i]);
}
}
return tab;
}
/**
* Returns the id of the scene to go after solving the gif of the scene id
* @param {Number} id
* @returns next scene id
*/
function getGifPointedScene(id){
const scene = getSceneByID(id);
let transitions = getTransitions();
const len = transitions.length;
for(let i =0 ; i<len; i++){
let sceneto = transitions[i].Transition.SceneToScene;
if(!(sceneto === undefined)){
if(!(sceneto.Riddle === undefined)){
if(sceneto.Riddle.Which=="Gif" && getLastNumberTransition(sceneto.From) == id){
return getLastNumberTransition(sceneto.To);
}
}
}
}
return 2000000;
}
/**
* Return the type of the puzzle present in the given scene and the id of the nex transition
* @param {Scene object} id
* @returns tabular containing the type of the puzzle, and the id of the next transition
*/
function whatPuzzleItIs(id){
const scene = getSceneByID(id);
if (!(scene.Gifs.length == 0)){
return ["Gif",id];
}
let transitions = getTransitions();
const len = transitions.length;
for(let i =0 ; i<len; i++){
if(!(transitions[i].Transition.SceneToScene === undefined)){
let comp_id = getLastNumberTransition(transitions[i].Transition.SceneToScene.From);
if(comp_id == id){
if(!(transitions[i].Transition.SceneToScene.Riddle === undefined)){
return [transitions[i].Transition.SceneToScene.Riddle.Which,transitions[i].id];
}
}
}
}
return "";
}
/**
* Given a path (ex : "Game/Scene.14") return the last number (in the example 14)
* @param {String} str
* @returns the last number of str (as an integer)
*/
function getLastNumberTransition(str){
let len = str.length;
while(str[len] !="."){
len--;
}
return parseInt(str.substring(len+1,str.length));
}
/**
* Given a path (ex : "Game/Scene.14") return id of the scene if it is one (in the example : 14)
* @param {String} path
* @returns scene id, or -1 if not a scene path
*/
function getSceneIdFromPath(path){
let len = path.length;
let len2 = len;
let len3 = len;
while(len >= 0){
if(path[len] == "."){
len3 = len2;
len2 = len;
len--;
}
if(path[len] == "/"){
if(path.substring(len+1,len2) == "Scene"){
return path.substring(len2+1,len3);
}
len3 = len2;
len2 = len;
len--;
}
len--;
}
return -1;
}
// ------------------------------------ Get <...> By Id -------------------------------------
/**
* Returns the path to the background image of the scene whose id is 'id'
* @param {Number} id
* @returns path of the background image as a String
*/
function getSceneBackgroundById(id) {
return getSceneImage(getSceneByID(id));
}
/**
* Returns the scene object whose identifier in the game JSON is 'id'
* Throws an error if no scene matches the given id
* Note : scene identifiers start at 0
* @param {Number} id
*/
function getSceneByID(id) {
var json = GameJson;
const scenes = getScenes(json);
const length = Object.keys(scenes).length;
for (var i = 0; i < length; i++) {
if (scenes[i].id == id) {
return scenes[i];
}
}
console.error("Error : Scene " + id + " not found");
}
/**
* Returns the width and height of the scene background image
* @param {Number} id
* @returns tabular containing the width and height of the background image
*/
function getImageSizeByID(id) {
return getImageSize(getSceneByID(id));
}
/**
* Returns an array where each element contains the four positions of the
* four edges of the click zone and the id pointed by the click zone,
* relatively to the image size.
* @param {Number} id,
* @param {bool} back
* @returns array of class ClickZone/BackClickZone
*/
function getClickZonesByScenesId(id,back) {
const scene = getSceneByID(id);
let areas;
if(back){
areas = scene.BackClickAreas;
if(areas === null){
areas = [];
}
}
else{
areas = scene.ClickAreas;
}
/*Commentaires:
La position de l'image est bien relative par rapport à la taille de l'image.
Le size est calculé proportionnellement par rapport à la longueur de l'image.
*/
let array = [];
for(var i = 0; i < areas.length; i++){
let currentArea = areas[i];
let heightPourcentage = currentArea.Size[1] * scene.ImageSize[0] / scene.ImageSize[1];
let clickzone = 0;
let id;
if(back){
clickzone = new BackClickZone(currentArea.Pos[0],currentArea.Pos[1],currentArea.Size[0] + currentArea.Pos[0],heightPourcentage + currentArea.Pos[1], getElementId(currentArea));
}else{
clickzone = new ClickZone(currentArea.Pos[0],currentArea.Pos[1],currentArea.Size[0] + currentArea.Pos[0],heightPourcentage + currentArea.Pos[1],getPointedScene(currentArea.Path), getElementId(currentArea));
id = getPointedScene(currentArea.Path);
}
array.push(clickzone);
}
return array;
}
/**
* Returns the ClickArea whose id is 'id'
* Throws an error if not found
* @param {Number} clickArea
* @param {Number} id
* @returns ClickArea
*/
function getClickAreaByID(clickArea, id) {
for (var i = 0; i < clickArea.length; i++) {
if (clickArea[i].id == id) {
return clickArea[i];
}
}
throw "clickArea " + id + " not found";
}
/**
* Returns the BackClickArea whose id is 'id'
* Throws an error if not found
* @param {Number} backClickArea
* @param {Number} id*
* @returns BackClickArea
*/
function getBackClickAreaByID(backClickArea, id) {
for (var i = 0; i < backClickArea.length; i++) {
if (backClickArea[i].id == id) {
return backClickArea[i];
}
}
throw "BackClickArea " + id + " not found";
}
/**
* Returns the gif whose id is 'id'
* Throws an error if not found
* @param {Number} gifs
* @param {Number} id
* @returns Gif
*/
function getGifByID(gifs, id) {
for (var i = 0; i < gifs.length; i++) {
if (gifs[i].id == id) {
return gifs[i];
}
}
throw "Gif " + id + " not found";
}
/**
* Returns the object whose id is 'id'
* Throws an error if not found
* @param {Number} objects
* @param {Number} id
* @returns Object
*/
function getObjectByID(objects, id) {
for (var i = 0; i < objects.length; i++) {
if (objects[i].id == id) {
return objects[i];
}
}
throw "Object " + id + " not found";
}
/**
* Returns the text whose id is 'id'
* Throws an error if not found
* @param {Number} texts
* @param {Number} id
* @returns text
*/
function getTextByID(texts, id) {
for (var i = 0; i < texts.length; i++) {
if (texts[i].id == id) {
return texts[i];
}
}
throw "Text " + id + " not found";
}
/**
* Returns the transitions whose id is 'id'
* Throws an error if not found
* @param {Number} transitions
* @param {Number} id
* @returns transition
*
*/
function getTransitionByID(transitions, id) {
for (var i = 0; i < transitions.length; i++) {
if (transitions[i].id == id) {
return transitions[i];
}
}
throw "Transition " + id + " not found";
}
/**
* Find if we have a fade transition for the transition between source and
* destination
* @param {object} transitions
* @param {Number} source
* @param {Number} destination
* @returns true if it is a fade transition, false otherwise
*/
function findTransition(transitions, source, destination) {
for (var i = 0; i < transitions.length; i++) {
if((getSceneIdFromPath(transitions[i].Transition[transitions[i].Transition.Which].To) == destination) && (getSceneIdFromPath(transitions[i].Transition[transitions[i].Transition.Which].From) == source)){
return transitions[i].Fade == 1;
}
}
return false
throw "Transition from " + source + " to " + destination + " not found";
}
/**
* Returns the start text that has to be displayed at the
* begining of the scene whose id is 'scene_id'
* @param {Number} sceneId
* @returns text
*/
function getSceneTextBySceneId(sceneId) {
const scene = getSceneByID(sceneId);
const text = scene.StartText;
return text;
}
/**
* Returns the text in the text areas of the
* scene whose id is "sceneId"
* @param {Number} sceneId
* @returns tabular of text areas
*/
function getSceneTextAreasBySceneId(sceneId) {
const scene = getSceneByID(sceneId);
const text_areas = [];
for (var i = 0; i < scene.TextAreas.length; i++) {
text_areas[i] = scene.TextAreas[i].Text;
}
return text_areas;
}
/**
* Returns the text to show depending on whether it is a Question, a Success or a Failure
* in the scene sceneId
* @param {Number} sceneId
* @param {Number} text
* @returns string
*/
function getDigicodeQSF(sceneId,text){
var transitions = getTransitions();
var sceneToScene;
var len = transitions.length;
var riddleText;
for (var i = 0; i < len; i++) {
if(transitions[i].Transition.Which == "SceneToScene"){
sceneToScene = transitions[i].Transition.SceneToScene;
var startSceneId = getLastNumberTransition(sceneToScene.From);
if (startSceneId == sceneId) {
riddleText = sceneToScene.Riddle.Text;
}
}
}
switch (text) {
case "QUESTION":
return riddleText.Question;
break;
case "SUCCESS":
return riddleText.IfCorrect;
break;
case "FAILURE":
return riddleText.IfWrong;
break;
default:
return"";
}
}