globals.js 20 KB

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