globals.js 21 KB

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