globals.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. /**************************************************************
  2. * LInE - Free Education, Private Data - http://www.usp.br/line
  3. *
  4. * This file holds all global elements to the game.
  5. *
  6. * Generating game levels in menu:
  7. * .....................................................
  8. * ...............square....................circle...... } = gameShape
  9. * .........../...........\....................|........ } = gameType (game)
  10. * ........One.............Two................One....... }
  11. * ......./...\.........../...\............./....\......
  12. * ......A.....B.........A.....B...........A......B..... = gameMode (game mode)
  13. * .(floor)..(stack)..(top)..(bottom)..(floor)..(stack).
  14. * .......\./.............\./................\./........
  15. * ........|...............|..................|.........
  16. * ......./.\..............|................/.|.\.......
  17. * ...Plus...Minus.......Equals........Plus.Minus.Mixed. = gameOperation (game math operation)
  18. * .......\./..............|................\.|./.......
  19. * ........|...............|..................|.........
  20. * ......1,2,3.........1,2,3,4,5..........1,2,3,4,5..... = gameDifficulty (difficulty level)
  21. * .....................................................
  22. *
  23. * About levels in map:
  24. *
  25. * ..................(game.levels)......................
  26. * ......................__|__..........................
  27. * .....................|.|.|.|.........................
  28. * ...................0,1,2,3,4,5....................... = mapPosition (map positions)
  29. * ...................|.........|.......................
  30. * ................(start)....(end).....................
  31. **************************************************************/
  32. /**
  33. * Turns console messages ON/OFF (for debug purposes only)
  34. * @type {boolean}
  35. */
  36. const debugMode = false;
  37. /** FOR MOODLE <br>
  38. *
  39. * iFractions can run on a server or inside moodle through iAssign. <br>
  40. * This variable should be set according to where it is suposed to run: <br>
  41. * - if true, on moodle <br>
  42. * - if false, on a server
  43. */
  44. const moodle = false;
  45. /**
  46. * HTMLCanvasElement : Canvas where all the game elements are rendered.
  47. * @type {object}
  48. */
  49. let canvas;
  50. /**
  51. * Name of the selected game.<br>
  52. * Can be: 'squareOne', 'squareTwo' or 'circleOne'.
  53. *
  54. * @type {string}
  55. */
  56. let gameType;
  57. /**
  58. * Used for text and game information.<br>
  59. * Shape that makes the name of the game - e.g in 'squareOne' it is 'square'.<br>
  60. * Can be: 'circle' or 'square'.
  61. *
  62. * @type {string}
  63. */
  64. let gameShape;
  65. /**
  66. * Holds selected game mode.<br>
  67. * In squareOne/circleOne can be: 'A' (click on the floor) or 'B' (click on the amount to go/stacked figures).<br>
  68. * In squareTwo can be: 'A' (more subdivisions on top) or 'B' (more subdivisions on bottom).
  69. *
  70. * @type {string}
  71. */
  72. let gameMode;
  73. /**
  74. * Holds game math operation.<br>
  75. * In squareOne can be: 'Plus' (green tractor goes right) or 'Minus' (red tractor goes left).<br>
  76. * In circleOne can be: 'Plus' (green tractor goes right), 'Minus' (red tractor goes left) or 'Mixed' (green tractor goes both sides).<br>
  77. * In squareTwo can be: 'Equals' (compares two rectangle subdivisions).
  78. *
  79. * @type {string}
  80. */
  81. let gameOperation;
  82. /**
  83. * Holds game overall difficulty. 1 (easier) -> n (harder).<br>
  84. * In squareOne can be: 1..3.<br>
  85. * In circleOne/squareTwo can be: 1..5.
  86. *
  87. * @type {number}
  88. */
  89. let gameDifficulty;
  90. /**
  91. * Turns displaying the fraction labels on levels ON/OFF
  92. * @type {boolean}
  93. */
  94. let fractionLabel = true;
  95. /**
  96. * When true, the character can move to next position in the map
  97. * @type {boolean}
  98. */
  99. let mapMove;
  100. /**
  101. * Character position on the map, aka game levels (1..4: valid; 5: end)
  102. * @type {number}
  103. */
  104. let mapPosition;
  105. /**
  106. * Number of finished levels in the map
  107. * @type {number}
  108. */
  109. let completedLevels;
  110. /**
  111. * Turns game audio ON/OFF
  112. * @type {boolean}
  113. */
  114. let audioStatus = false;
  115. /**
  116. * Player's name
  117. * @type {string}
  118. */
  119. let playerName;
  120. /**
  121. * String that contains the selected language.<br>
  122. * It is the name of the language file.
  123. * @type {string}
  124. */
  125. let langString;
  126. /**
  127. * Holds the current state.<br>
  128. * Is used as if it was a 'this' inside state functions.
  129. * @type {object}
  130. */
  131. let self;
  132. const medSrc = 'assets/img/'; // Base directory for media
  133. /**
  134. * Metadata for all games
  135. * @type {object}
  136. */
  137. const info = {
  138. all: {
  139. squareOne: {
  140. gameShape: 'square',
  141. gameType: 'squareOne',
  142. gameTypeUrl: 'game0',
  143. gameMode: ['A', 'B'],
  144. gameModeUrl: ['mode0', 'mode1'],
  145. gameOperation: ['Plus', 'Minus'],
  146. gameOperationUrl: ['operation_plus', 'operation_minus'],
  147. gameDifficulty: 3
  148. },
  149. circleOne: {
  150. gameShape: 'circle',
  151. gameType: 'circleOne',
  152. gameTypeUrl: 'game1',
  153. gameMode: ['A', 'B'],
  154. gameModeUrl: ['mode2', 'mode3'],
  155. gameOperation: ['Plus', 'Minus', 'Mixed'],
  156. gameOperationUrl: ['operation_plus', 'operation_minus', 'operation_mixed'],
  157. gameDifficulty: 5
  158. },
  159. squareTwo: {
  160. gameShape: 'square',
  161. gameType: 'squareTwo',
  162. gameTypeUrl: 'game2',
  163. gameMode: ['A', 'B'],
  164. gameModeUrl: ['mode4', 'mode5'],
  165. gameOperation: ['Equals'],
  166. gameOperationUrl: ['operation_equals'],
  167. gameDifficulty: 5
  168. },
  169. },
  170. gameShape: [],
  171. gameType: [],
  172. gameTypeUrl: [],
  173. gameMode: [],
  174. gameModeUrl: [],
  175. gameOperation: [],
  176. gameOperationUrl: [],
  177. gameDifficulty: [],
  178. };
  179. // Called once when the game starts
  180. (function () {
  181. for (key in info.all) {
  182. info.gameShape.push(info.all[key].gameShape);
  183. info.gameType.push(info.all[key].gameType);
  184. info.gameTypeUrl.push(info.all[key].gameTypeUrl);
  185. info.gameMode.push(info.all[key].gameMode);
  186. info.gameModeUrl.push(info.all[key].gameMode);
  187. info.gameOperation.push(info.all[key].gameOperation);
  188. info.gameOperationUrl.push(info.all[key].gameOperationUrl);
  189. info.gameDifficulty.push(info.all[key].gameDifficulty);
  190. }
  191. })()
  192. /**
  193. * Preset colors for graphic elements.
  194. * @type {object}
  195. */
  196. const colors = {
  197. // Blues
  198. blueBckg: '#cce5ff', // Background color
  199. blueBckgOff: '#adc8e6',
  200. blueBckgInsideLevel: '#a8c0e6', // Background color in squareOne (used for floor gap)
  201. blue: '#003cb3', // Subtitle
  202. blueMenuLine: '#b7cdf4',
  203. darkBlue: '#183780', // Line color that indicates right and fraction numbers
  204. // Reds
  205. red: '#b30000', // Linecolor that indicates left
  206. lightRed: '#d27979', // squareTwo figures
  207. darkRed: '#330000', // squareTwo figures and some titles
  208. // Greens
  209. green: '#00804d', // Title
  210. lightGreen: '#83afaf', // squareTwo figures
  211. darkGreen: '#1e2f2f', // squareTwo figures
  212. intenseGreen: '#00d600',
  213. // Basics
  214. white: '#efeff5',
  215. gray: '#708090',
  216. black: '#000',
  217. yellow: '#ffef1f'
  218. };
  219. /**
  220. * Preset text styles for game text.<br>
  221. * Contains: font, size, text color and text align.
  222. * @type {object}
  223. */
  224. const textStyles = {
  225. h1_green: { font: '32px Arial,sans-serif', fill: colors.green, align: 'center' }, // Menu title
  226. h2_green: { font: '26px Arial,sans-serif', fill: colors.green, align: 'center' }, // Flag labels (langState)
  227. h1_white: { font: '32px Arial,sans-serif', fill: colors.white, align: 'center' }, // Ok button (nameState)
  228. h2_white: { font: '26px Arial,sans-serif', fill: colors.white, align: 'center' }, // Difficulty buttons (menuState)
  229. h3__white: { font: '23px Arial,sans-serif', fill: colors.white, align: 'center' }, // Difficulty numbers (menuState)
  230. h4_white: { font: '20px Arial,sans-serif', fill: colors.white, align: 'center' }, // Difficulty numbers (menuState)
  231. p_white: { font: '14px Arial,sans-serif', fill: colors.white, align: 'center' }, // Enter button (menuState)
  232. h2_brown: { font: '26px Arial,sans-serif', fill: colors.darkRed, align: 'center' }, // Map difficulty label
  233. h4_brown: { font: '20px Arial,sans-serif', fill: colors.darkRed, align: 'center' }, // Menu overtitle
  234. p_brown: { font: '14px Arial,sans-serif', fill: colors.darkRed, align: 'center' }, // Map difficulty label
  235. h2_blue_2: { font: '26px Arial,sans-serif', fill: colors.blue, align: 'center' }, // Menu subtitle
  236. h4_blue_2: { font: '20px Arial,sans-serif', fill: colors.blue, align: 'center' }, // Menu subtitle
  237. h2_blue: { font: '26px Arial,sans-serif', fill: colors.darkBlue, align: 'center' }, // Fractions
  238. h4_blue: { font: '20px Arial,sans-serif', fill: colors.darkBlue, align: 'center' }, // Fractions
  239. p_blue: { font: '14px Arial,sans-serif', fill: colors.darkBlue, align: 'center' } // Fractions
  240. };
  241. /**
  242. * List of URL for all media in the game
  243. * divided 1st by the 'state' that loads the media
  244. * and 2nd by the 'media type' for that state.
  245. *
  246. * @type {object}
  247. */
  248. const url = {
  249. /**
  250. * url.<state>
  251. * where <state> can be: boot, menu, squareOne, squareTwo, circleOne.
  252. */
  253. boot: {
  254. /**
  255. * url.<state>.<media type>
  256. * where <media type> can be: image, sprite, audio <br><br>
  257. *
  258. * image: [ [name, source], ... ] <br>
  259. * sprite: [ [name, source, number of frames], ... ] <br>
  260. * audio: [ [name, [source, alternative source] ], ... ]
  261. */
  262. image: [
  263. // Scene
  264. ['bgimage', medSrc + 'scene/bg.jpg'],
  265. ['bgmap', medSrc + 'scene/bg_map.png'],
  266. ['broken_sign', medSrc + 'scene/broken_sign.png'],
  267. ['bush', medSrc + 'scene/bush.png'],
  268. ['cloud', medSrc + 'scene/cloud.png'],
  269. ['floor', medSrc + 'scene/floor.png'],
  270. ['place_off', medSrc + 'scene/place_off.png'],
  271. ['place_on', medSrc + 'scene/place_on.png'],
  272. ['rock', medSrc + 'scene/rock.png'],
  273. ['road', medSrc + 'scene/road.png'],
  274. ['sign', medSrc + 'scene/sign.png'],
  275. ['tree1', medSrc + 'scene/tree.png'],
  276. ['tree2', medSrc + 'scene/tree2.png'],
  277. ['tree3', medSrc + 'scene/tree3.png'],
  278. ['tree4', medSrc + 'scene/tree4.png'],
  279. // Flags
  280. ['flag_BR', medSrc + 'flag/BRAZ.jpg'],
  281. ['flag_FR', medSrc + 'flag/FRAN.jpg'],
  282. ['flag_IT', medSrc + 'flag/ITAL.png'],
  283. ['flag_PE', medSrc + 'flag/PERU.jpg'],
  284. ['flag_US', medSrc + 'flag/UNST.jpg'],
  285. // Navigation icons on the top of the page
  286. ['back', medSrc + 'navig_icon/back.png'],
  287. ['help', medSrc + 'navig_icon/help.png'],
  288. ['home', medSrc + 'navig_icon/home.png'],
  289. ['language', medSrc + 'navig_icon/language.png'],
  290. ['menu', medSrc + 'navig_icon/menu.png'],
  291. // Interactive icons
  292. ['arrow_down', medSrc + 'interac_icon/down.png'],
  293. ['close', medSrc + 'interac_icon/close.png'],
  294. ['error', medSrc + 'interac_icon/error.png'],
  295. ['help_pointer', medSrc + 'interac_icon/pointer.png'],
  296. ['info', medSrc + 'interac_icon/info.png'],
  297. ['ok', medSrc + 'interac_icon/ok.png'],
  298. // Menu icons - Games
  299. ['game0', medSrc + 'levels/squareOne.png'], // Square I
  300. ['game1', medSrc + 'levels/circleOne.png'], // Circle I
  301. ['game2', medSrc + 'levels/squareTwo.png'], // Square II
  302. // Menu icons - Info box
  303. ['c1-A', medSrc + 'info_box/c1-A.png'],
  304. ['c1-A-h', medSrc + 'info_box/c1-A-h.png'],
  305. ['c1-B-h', medSrc + 'info_box/c1-B-h.png'],
  306. ['c1-diff-1', medSrc + 'info_box/c1-diff-1.png'],
  307. ['c1-diff-5', medSrc + 'info_box/c1-diff-5.png'],
  308. ['c1-label', medSrc + 'info_box/c1-label.png'],
  309. ['map-c1s2', medSrc + 'info_box/map-c1s2.png'],
  310. ['map-s1', medSrc + 'info_box/map-s1.png'],
  311. ['s1-A', medSrc + 'info_box/s1-A.png'],
  312. ['s1-A-h', medSrc + 'info_box/s1-A-h.png'],
  313. ['s1-B-h', medSrc + 'info_box/s1-B-h.png'],
  314. ['s1-diff-1', medSrc + 'info_box/s1-diff-1.png'],
  315. ['s1-diff-3', medSrc + 'info_box/s1-diff-3.png'],
  316. ['s1-label', medSrc + 'info_box/s1-label.png'],
  317. ['s2', medSrc + 'info_box/s2.png'],
  318. ['s2-A-h', medSrc + 'info_box/s2-A-h.png'],
  319. ['s2-B-h', medSrc + 'info_box/s2-B-h.png'],
  320. ['s2-diff-1', medSrc + 'info_box/s2-diff-1.png'],
  321. ['s2-diff-5', medSrc + 'info_box/s2-diff-5.png'],
  322. ['s2-label', medSrc + 'info_box/s2-label.png'],
  323. ['operation_plus', medSrc + 'info_box/operation_plus.png'],
  324. ['operation_minus', medSrc + 'info_box/operation_minus.png'],
  325. ['operation_mixed', medSrc + 'info_box/operation_mixed.png'],
  326. ['operation_equals', medSrc + 'info_box/operation_equals.png'],
  327. ],
  328. sprite: [
  329. // Game Sprites
  330. ['kid_walk', medSrc + 'character/kid/walk.png', 26],
  331. // Navigation icons on the top of the page
  332. ['audio', medSrc + 'navig_icon/audio.png', 2],
  333. // Interactive icons
  334. ['select', medSrc + 'interac_icon/selectionBox.png', 2],
  335. // Menu icons - Game modes
  336. ['mode0', medSrc + 'levels/squareOne_1.png', 2], // Square I : A
  337. ['mode1', medSrc + 'levels/squareOne_2.png', 2], // Square I : B
  338. ['mode2', medSrc + 'levels/circleOne_1.png', 2], // Circle I : A
  339. ['mode3', medSrc + 'levels/circleOne_2.png', 2], // Circle I : B
  340. ['mode4', medSrc + 'levels/squareTwo_1.png', 2], // Square II : A
  341. ['mode5', medSrc + 'levels/squareTwo_2.png', 2], // Square II : B
  342. // Menu icons - Math operations
  343. ['operation_plus', medSrc + 'levels/operation_plus.png', 2], // Square/circle I : right
  344. ['operation_minus', medSrc + 'levels/operation_minus.png', 2], // Square/circle I : left
  345. ['operation_mixed', medSrc + 'levels/operation_mixed.png', 2], // Circle I : mixed
  346. ['operation_equals', medSrc + 'levels/operation_equals.png', 2], // Square II : equals
  347. ],
  348. audio: [
  349. // Sound effects
  350. ['beepSound', ['assets/audio/beep.ogg', 'assets/audio/beep.mp3']],
  351. ['okSound', ['assets/audio/ok.ogg', 'assets/audio/ok.mp3']],
  352. ['errorSound', ['assets/audio/error.ogg', 'assets/audio/error.mp3']]
  353. ]
  354. },
  355. squareOne: {
  356. image: [
  357. // Map buildings
  358. ['farm', medSrc + 'scene/farm.png'],
  359. ['garage', medSrc + 'scene/garage.png']
  360. ],
  361. sprite: [
  362. // Game sprites
  363. ['tractor', medSrc + 'character/tractor/tractor.png', 15]
  364. ],
  365. audio: []
  366. },
  367. squareTwo: {
  368. image: [
  369. // Map buildings
  370. ['house', medSrc + 'scene/house.png'],
  371. ['school', medSrc + 'scene/school.png']
  372. ],
  373. sprite: [
  374. // Game sprites
  375. ['kid_standing', medSrc + 'character/kid/lost.png', 6],
  376. ['kid_run', medSrc + 'character/kid/run.png', 12]
  377. ],
  378. audio: []
  379. },
  380. circleOne: {
  381. image: [
  382. // Map buildings
  383. ['house', medSrc + 'scene/house.png'],
  384. ['school', medSrc + 'scene/school.png'],
  385. // Game images
  386. ['balloon', medSrc + 'character/balloon/airballoon_upper.png'],
  387. ['balloon_basket', medSrc + 'character/balloon/airballoon_base.png']
  388. ],
  389. sprite: [
  390. // Game sprites
  391. ['kid_run', medSrc + 'character/kid/run.png', 12]
  392. ],
  393. audio: []
  394. },
  395. };
  396. /**
  397. * Manages navigation icons on the top of the screen
  398. * @namespace
  399. */
  400. const navigationIcons = {
  401. /**
  402. * Add navigation icons.<br>
  403. * * The icons on the left are ordered from left to right. <br>
  404. * * The icons on the right are ordered from right to left.
  405. *
  406. * @param {boolean} leftIcon0 1st left icon (back)
  407. * @param {boolean} leftIcon1 2nd left icon (main menu)
  408. * @param {boolean} leftIcon2 3rd left icon (solve game)
  409. * @param {boolean} rightIcon0 1st right icon (audio)
  410. * @param {boolean} rightIcon1 2nd right icon (lang)
  411. * @param {undefined|string} state state to be called by the 'back' button (must exist if param 'leftIcon0' is true)
  412. * @param {undefined|function} help function in the current game state that display correct answer
  413. */
  414. add: function (leftIcon0, leftIcon1, leftIcon2, rightIcon0, rightIcon1, state, help) {
  415. let left_x = 10;
  416. let right_x = defaultWidth - 50 - 10;
  417. this.iconsList = [];
  418. // 'Descriptive labels' for the navigation icons
  419. this.left_text = game.add.text(left_x, 73, '', textStyles.h4_brown);
  420. this.left_text.align = 'left';
  421. this.right_text = game.add.text(right_x + 50, 73, '', textStyles.h4_brown);
  422. this.right_text.align = 'right';
  423. // Left icons
  424. if (leftIcon0) { // Return to previous screen
  425. if (!state) {
  426. console.error('Game error: You tried to add a \'back\' icon without the \'state\' parameter.');
  427. } else {
  428. this.state = state;
  429. this.iconsList.push(game.add.image(left_x, 10, 'back'));
  430. left_x += 50;
  431. }
  432. }
  433. if (leftIcon1) { // Return to main menu screen
  434. this.iconsList.push(game.add.image(left_x, 10, 'menu'));
  435. left_x += 50;
  436. }
  437. if (leftIcon2) { // Shows solution to the game
  438. if (!help) {
  439. console.error('Game error: You tried to add a \'game solution\' icon without the \'help\' parameter.');
  440. } else {
  441. this.help = help;
  442. this.iconsList.push(game.add.image(left_x, 10, 'help'));
  443. left_x += 50;
  444. }
  445. }
  446. // Right icons
  447. if (rightIcon0) { // Turns game audio on/off
  448. this.audioIcon = game.add.sprite(right_x, 10, 'audio', 1);
  449. this.audioIcon.curFrame = audioStatus ? 0 : 1;
  450. this.iconsList.push(this.audioIcon);
  451. right_x -= 50;
  452. }
  453. if (rightIcon1) { // Return to select language screen
  454. this.iconsList.push(game.add.image(right_x, 10, 'language'));
  455. right_x -= 50;
  456. }
  457. },
  458. /**
  459. * When 'back' icon is clicked go to this state
  460. *
  461. * @param {string} state name of the next state
  462. */
  463. callState: function (state) {
  464. if (audioStatus) game.audio.beepSound.play();
  465. game.event.clear(self);
  466. game.state.start(state);
  467. },
  468. /**
  469. * Called by mouse click event
  470. *
  471. * @param {number} x contains the mouse x coordinate
  472. * @param {number} y contains the mouse y coordinate
  473. */
  474. onInputDown: function (x, y) {
  475. navigationIcons.iconsList.forEach(cur => {
  476. if (game.math.isOverIcon(x, y, cur)) {
  477. const name = cur.name;
  478. switch (name) {
  479. case 'back': navigationIcons.callState(navigationIcons.state); break;
  480. case 'menu': navigationIcons.callState('menu'); break;
  481. case 'help': navigationIcons.help(); break;
  482. case 'language': navigationIcons.callState('lang'); break;
  483. case 'audio':
  484. if (audioStatus) {
  485. audioStatus = false;
  486. navigationIcons.audioIcon.curFrame = 1;
  487. } else {
  488. audioStatus = true;
  489. if (audioStatus) game.audio.beepSound.play();
  490. navigationIcons.audioIcon.curFrame = 0;
  491. }
  492. game.render.all();
  493. break;
  494. default: console.error('Game error: error in navigation icon');
  495. }
  496. }
  497. });
  498. },
  499. /**
  500. * Called by mouse move event
  501. *
  502. * @param {number} x contains the mouse x coordinate
  503. * @param {number} y contains the mouse y coordinate
  504. */
  505. onInputOver: function (x, y) {
  506. let flag = false;
  507. navigationIcons.iconsList.forEach(cur => {
  508. if (game.math.isOverIcon(x, y, cur)) {
  509. flag = true;
  510. let name = cur.name;
  511. switch (name) {
  512. case 'back': navigationIcons.left_text.name = game.lang.nav_back; break;
  513. case 'menu': navigationIcons.left_text.name = game.lang.nav_menu; break;
  514. case 'help': navigationIcons.left_text.name = game.lang.nav_help; break;
  515. case 'language': navigationIcons.right_text.name = game.lang.nav_lang; break;
  516. case 'audio': navigationIcons.right_text.name = game.lang.audio; break;
  517. }
  518. }
  519. });
  520. if (!flag) {
  521. navigationIcons.left_text.name = '';
  522. navigationIcons.right_text.name = '';
  523. } else {
  524. document.body.style.cursor = 'pointer';
  525. }
  526. }
  527. };
  528. /**
  529. * Sends game information to database
  530. *
  531. * @param {string} extraData player information for the current game
  532. */
  533. const sendToDB = function (extraData) {
  534. // FOR MOODLE
  535. if (moodle) {
  536. if (self.result) moodleVar.hits[mapPosition - 1]++;
  537. else moodleVar.errors[mapPosition - 1]++;
  538. moodleVar.time[mapPosition - 1] += game.timer.elapsed;
  539. const url = iLMparameters.iLM_PARAM_ServerToGetAnswerURL;
  540. const grade = '' + getEvaluation();
  541. const report = getAnswer();
  542. const data = 'return_get_answer=1'+
  543. '&iLM_PARAM_ActivityEvaluation='+encodeURIComponent(grade)+
  544. '&iLM_PARAM_ArchiveContent='+encodeURIComponent(report);
  545. const init = { method: 'POST', body: data, headers: { 'Content-type': 'application/x-www-form-urlencoded; charset=UTF-8' } };
  546. fetch(url, init)
  547. .then(response => {
  548. if (response.ok) {
  549. if (debugMode) console.log("Processing...");
  550. } else {
  551. console.error("Game error: Network response was not ok.");
  552. }
  553. })
  554. .catch(error => {
  555. console.error('Game error: problem with fetch operation - ' + error.message + '.');
  556. });
  557. } else {
  558. // Create some variables we need to send to our PHP file
  559. // Attention: this names must be compactible to data table (MySQL server)
  560. // @see php/save.php
  561. const data = 'line_ip=143.107.45.11' // INSERT database server IP
  562. + '&line_name=' + playerName
  563. + '&line_lang=' + langString
  564. + extraData;
  565. const url = 'php/save.php';
  566. const init = { method: 'POST', body: data, headers: { 'Content-type': 'application/x-www-form-urlencoded' } };
  567. fetch(url, init)
  568. .then(response => {
  569. if (response.ok) {
  570. if (debugMode) console.log("Processing...");
  571. response.text().then(text => { if (debugMode) { console.log(text); } })
  572. } else {
  573. console.error("Game error: Network response was not ok.");
  574. }
  575. })
  576. .catch(error => {
  577. console.error('Game error: problem with fetch operation - ' + error.message + '.');
  578. });
  579. }
  580. };