globals.js 20 KB

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