globals.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  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.<br>
  257. * Divided: 1st by the state that loads the media / 2nd by the media type.
  258. * @type {object}
  259. */
  260. const url = {
  261. //src: 'assets/img/', // Base directory for media
  262. boot: {
  263. image: [
  264. // Scene
  265. ['bgimage', medSrc + 'scene/bg.jpg'],
  266. ['bgmap', medSrc + 'scene/bg_map.png'],
  267. ['broken_sign', medSrc + 'scene/broken_sign.png'],
  268. ['bush', medSrc + 'scene/bush.png'],
  269. ['cloud', medSrc + 'scene/cloud.png'],
  270. ['floor', medSrc + 'scene/floor.png'],
  271. ['place_off', medSrc + 'scene/place_off.png'],
  272. ['place_on', medSrc + 'scene/place_on.png'],
  273. ['rock', medSrc + 'scene/rock.png'],
  274. ['road', medSrc + 'scene/road.png'],
  275. ['sign', medSrc + 'scene/sign.png'],
  276. ['tree1', medSrc + 'scene/tree.png'],
  277. ['tree2', medSrc + 'scene/tree2.png'],
  278. ['tree3', medSrc + 'scene/tree3.png'],
  279. ['tree4', medSrc + 'scene/tree4.png'],
  280. // Flags
  281. ['flag_BR', medSrc + 'flag/BRAZ.jpg'],
  282. ['flag_FR', medSrc + 'flag/FRAN.jpg'],
  283. ['flag_IT', medSrc + 'flag/ITAL.png'],
  284. ['flag_PE', medSrc + 'flag/PERU.jpg'],
  285. ['flag_US', medSrc + 'flag/UNST.jpg'],
  286. // Navigation icons on the top of the page
  287. ['back', medSrc + 'navig_icon/back.png'],
  288. ['help', medSrc + 'navig_icon/help.png'],
  289. ['home', medSrc + 'navig_icon/home.png'],
  290. ['language', medSrc + 'navig_icon/language.png'],
  291. ['menu', medSrc + 'navig_icon/menu.png'],
  292. // Interactive icons
  293. ['arrow_down', medSrc + 'interac_icon/down.png'],
  294. ['close', medSrc + 'interac_icon/close.png'],
  295. ['error', medSrc + 'interac_icon/error.png'],
  296. ['help_pointer', medSrc + 'interac_icon/pointer.png'],
  297. ['info', medSrc + 'interac_icon/info.png'],
  298. ['ok', medSrc + 'interac_icon/ok.png'],
  299. ],
  300. sprite: [
  301. // Game Sprites
  302. ['kid_walk', medSrc + 'character/kid/walk.png', 26],
  303. // Navigation icons on the top of the page
  304. ['audio', medSrc + 'navig_icon/audio.png', 2],
  305. // Interactive icons
  306. ['select', medSrc + 'interac_icon/selectionBox.png', 2]
  307. ],
  308. audio: [
  309. // Sound effects
  310. ['beepSound', ['assets/audio/beep.ogg', 'assets/audio/beep.mp3']],
  311. ['okSound', ['assets/audio/ok.ogg', 'assets/audio/ok.mp3']],
  312. ['errorSound', ['assets/audio/error.ogg', 'assets/audio/error.mp3']]
  313. ]
  314. },
  315. menu: {
  316. image: [
  317. // Game
  318. ['game0', medSrc + 'levels/squareOne.png'], // Square I
  319. ['game1', medSrc + 'levels/circleOne.png'], // Circle I
  320. ['game2', medSrc + 'levels/squareTwo.png'], // Square II
  321. // Info box icons
  322. ['c1-A', medSrc + 'info_box/c1-A.png'],
  323. ['c1-A-h', medSrc + 'info_box/c1-A-h.png'],
  324. ['c1-B-h', medSrc + 'info_box/c1-B-h.png'],
  325. ['c1-diff-1', medSrc + 'info_box/c1-diff-1.png'],
  326. ['c1-diff-5', medSrc + 'info_box/c1-diff-5.png'],
  327. ['c1-label', medSrc + 'info_box/c1-label.png'],
  328. ['map-c1s2', medSrc + 'info_box/map-c1s2.png'],
  329. ['map-s1', medSrc + 'info_box/map-s1.png'],
  330. ['s1-A', medSrc + 'info_box/s1-A.png'],
  331. ['s1-A-h', medSrc + 'info_box/s1-A-h.png'],
  332. ['s1-B-h', medSrc + 'info_box/s1-B-h.png'],
  333. ['s1-diff-1', medSrc + 'info_box/s1-diff-1.png'],
  334. ['s1-diff-3', medSrc + 'info_box/s1-diff-3.png'],
  335. ['s1-label', medSrc + 'info_box/s1-label.png'],
  336. ['s2', medSrc + 'info_box/s2.png'],
  337. ['s2-A-h', medSrc + 'info_box/s2-A-h.png'],
  338. ['s2-B-h', medSrc + 'info_box/s2-B-h.png'],
  339. ['s2-diff-1', medSrc + 'info_box/s2-diff-1.png'],
  340. ['s2-diff-5', medSrc + 'info_box/s2-diff-5.png'],
  341. ['s2-label', medSrc + 'info_box/s2-label.png'],
  342. ['operation_plus', medSrc + 'info_box/operation_plus.png'],
  343. ['operation_minus', medSrc + 'info_box/operation_minus.png'],
  344. ['operation_mixed', medSrc + 'info_box/operation_mixed.png'],
  345. ['operation_equals', medSrc + 'info_box/operation_equals.png'],
  346. ],
  347. sprite: [
  348. // Game modes
  349. ['mode0', medSrc + 'levels/squareOne_1.png', 2], // Square I : A
  350. ['mode1', medSrc + 'levels/squareOne_2.png', 2], // Square I : B
  351. ['mode2', medSrc + 'levels/circleOne_1.png', 2], // Circle I : A
  352. ['mode3', medSrc + 'levels/circleOne_2.png', 2], // Circle I : B
  353. ['mode4', medSrc + 'levels/squareTwo_1.png', 2], // Square II : A
  354. ['mode5', medSrc + 'levels/squareTwo_2.png', 2], // Square II : B
  355. // Math operations
  356. ['operation_plus', medSrc + 'levels/operation_plus.png', 2], // Square/circle I : right
  357. ['operation_minus', medSrc + 'levels/operation_minus.png', 2], // Square/circle I : left
  358. ['operation_mixed', medSrc + 'levels/operation_mixed.png', 2], // Circle I : mixed
  359. ['operation_equals', medSrc + 'levels/operation_equals.png', 2], // Square II : equals
  360. ],
  361. audio: []
  362. },
  363. squareOne: {
  364. image: [
  365. // Scene
  366. ['farm', medSrc + 'scene/farm.png'],
  367. ['garage', medSrc + 'scene/garage.png']
  368. ],
  369. sprite: [
  370. // Game sprites
  371. ['tractor', medSrc + 'character/tractor/tractor.png', 15]
  372. ],
  373. audio: []
  374. },
  375. squareTwo: {
  376. image: [
  377. // Scene
  378. ['house', medSrc + 'scene/house.png'],
  379. ['school', medSrc + 'scene/school.png']
  380. ],
  381. sprite: [
  382. // Game sprites
  383. ['kid_standing', medSrc + 'character/kid/lost.png', 6],
  384. ['kid_run', medSrc + 'character/kid/run.png', 12]
  385. ],
  386. audio: []
  387. },
  388. circleOne: {
  389. image: [
  390. // Scene
  391. ['house', medSrc + 'scene/house.png'],
  392. ['school', medSrc + 'scene/school.png'],
  393. // Game images
  394. ['balloon', medSrc + 'character/balloon/airballoon_upper.png'],
  395. ['balloon_basket', medSrc + 'character/balloon/airballoon_base.png']
  396. ],
  397. sprite: [
  398. // Game sprites
  399. ['kid_run', medSrc + 'character/kid/run.png', 12]
  400. ],
  401. audio: []
  402. }
  403. };
  404. /**
  405. * Manages navigation icons on the top of the screen
  406. * @namespace
  407. */
  408. const navigationIcons = {
  409. /**
  410. * Add navigation icons.<br>
  411. * * The icons on the left are ordered from left to right. <br>
  412. * * The icons on the right are ordered from right to left.
  413. *
  414. * @param {boolean} leftIcon0 1st left icon (back)
  415. * @param {boolean} leftIcon1 2nd left icon (main menu)
  416. * @param {boolean} leftIcon2 3rd left icon (solve game)
  417. * @param {boolean} rightIcon0 1st right icon (audio)
  418. * @param {boolean} rightIcon1 2nd right icon (lang)
  419. * @param {undefined|string} state state to be called by the 'back' button (must exist if param 'leftIcon0' is true)
  420. * @param {undefined|function} help function in the current game state that display correct answer
  421. */
  422. add: function (leftIcon0, leftIcon1, leftIcon2, rightIcon0, rightIcon1, state, help) {
  423. let left_x = 10;
  424. let right_x = defaultWidth - 50 - 10;
  425. this.iconsList = [];
  426. // 'Descriptive labels' for the navigation icons
  427. this.left_text = game.add.text(left_x, 73, '', textStyles.h4_brown);
  428. this.left_text.align = 'left';
  429. this.right_text = game.add.text(right_x + 50, 73, '', textStyles.h4_brown);
  430. this.right_text.align = 'right';
  431. // Left icons
  432. if (leftIcon0) { // Return to previous screen
  433. if (!state) {
  434. console.error('Game error: You tried to add a \'back\' icon without the \'state\' parameter.');
  435. } else {
  436. this.state = state;
  437. this.iconsList.push(game.add.image(left_x, 10, 'back'));
  438. left_x += 50;
  439. }
  440. }
  441. if (leftIcon1) { // Return to main menu screen
  442. this.iconsList.push(game.add.image(left_x, 10, 'menu'));
  443. left_x += 50;
  444. }
  445. if (leftIcon2) { // Shows solution to the game
  446. if (!help) {
  447. console.error('Game error: You tried to add a \'game solution\' icon without the \'help\' parameter.');
  448. } else {
  449. this.help = help;
  450. this.iconsList.push(game.add.image(left_x, 10, 'help'));
  451. left_x += 50;
  452. }
  453. }
  454. // Right icons
  455. if (rightIcon0) { // Turns game audio on/off
  456. this.audioIcon = game.add.sprite(right_x, 10, 'audio', 1);
  457. this.audioIcon.curFrame = audioStatus ? 0 : 1;
  458. this.iconsList.push(this.audioIcon);
  459. right_x -= 50;
  460. }
  461. if (rightIcon1) { // Return to select language screen
  462. this.iconsList.push(game.add.image(right_x, 10, 'language'));
  463. right_x -= 50;
  464. }
  465. },
  466. /**
  467. * When 'back' icon is clicked go to this state
  468. *
  469. * @param {string} state name of the next state
  470. */
  471. callState: function (state) {
  472. if (audioStatus) game.audio.beepSound.play();
  473. game.event.clear(self);
  474. game.state.start(state);
  475. },
  476. /**
  477. * Called by mouse click event
  478. *
  479. * @param {number} x contains the mouse x coordinate
  480. * @param {number} y contains the mouse y coordinate
  481. */
  482. onInputDown: function (x, y) {
  483. navigationIcons.iconsList.forEach(cur => {
  484. if (game.math.isOverIcon(x, y, cur)) {
  485. const name = cur.name;
  486. switch (name) {
  487. case 'back': navigationIcons.callState(navigationIcons.state); break;
  488. case 'menu': navigationIcons.callState('menu'); break;
  489. case 'help': navigationIcons.help(); break;
  490. case 'language': navigationIcons.callState('lang'); break;
  491. case 'audio':
  492. if (audioStatus) {
  493. audioStatus = false;
  494. navigationIcons.audioIcon.curFrame = 1;
  495. } else {
  496. audioStatus = true;
  497. if (audioStatus) game.audio.beepSound.play();
  498. navigationIcons.audioIcon.curFrame = 0;
  499. }
  500. game.render.all();
  501. break;
  502. default: console.error('Game error: error in navigation icon');
  503. }
  504. }
  505. });
  506. },
  507. /**
  508. * Called by mouse move event
  509. *
  510. * @param {number} x contains the mouse x coordinate
  511. * @param {number} y contains the mouse y coordinate
  512. */
  513. onInputOver: function (x, y) {
  514. let flag = false;
  515. navigationIcons.iconsList.forEach(cur => {
  516. if (game.math.isOverIcon(x, y, cur)) {
  517. flag = true;
  518. let name = cur.name;
  519. switch (name) {
  520. case 'back': navigationIcons.left_text.name = game.lang.nav_back; break;
  521. case 'menu': navigationIcons.left_text.name = game.lang.nav_menu; break;
  522. case 'help': navigationIcons.left_text.name = game.lang.nav_help; break;
  523. case 'language': navigationIcons.right_text.name = game.lang.nav_lang; break;
  524. case 'audio': navigationIcons.right_text.name = game.lang.audio; break;
  525. }
  526. }
  527. });
  528. if (!flag) {
  529. navigationIcons.left_text.name = '';
  530. navigationIcons.right_text.name = '';
  531. } else {
  532. document.body.style.cursor = 'pointer';
  533. }
  534. }
  535. };
  536. /**
  537. * Sends game information to database
  538. *
  539. * @param {string} extraData player information for the current game
  540. */
  541. const sendToDB = function (extraData) {
  542. // FOR MOODLE
  543. if (moodle) {
  544. if (self.result) moodleVar.hits[mapPosition - 1]++;
  545. else moodleVar.errors[mapPosition - 1]++;
  546. moodleVar.time[mapPosition - 1] += game.timer.elapsed;
  547. } else {
  548. // Create some variables we need to send to our PHP file
  549. // Attention: this names must be compactible to data table (MySQL server)
  550. // @see php/save.php
  551. const data = 'line_ip=143.107.45.11' // INSERT database server IP
  552. + '&line_name=' + playerName
  553. + '&line_lang=' + langString
  554. + extraData;
  555. const url = 'php/save.php';
  556. const init = { method: 'POST', body: data, headers: { 'Content-type': 'application/x-www-form-urlencoded' } };
  557. fetch(url, init)
  558. .then(response => {
  559. if (response.ok) {
  560. if (debugMode) console.log("Processing...");
  561. response.text().then(text => { if (debugMode) { console.log(text); } })
  562. } else {
  563. console.error("Game error: Network response was not ok.");
  564. }
  565. })
  566. .catch(error => {
  567. console.error('Game error: problem with fetch operation - ' + error.message + '.');
  568. });
  569. }
  570. };