not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1553 lines
65 KiB

  1. /**
  2. * Created by ohayon_m on 17/08/15.
  3. */
  4. define(["activity/sample-ressources", "activity/palettes/template-palette", "activity/palettes/size-palette", "activity/lz-string", "sugar-web/graphics/journalchooser", 'sugar-web/datastore'], function (SampleRessources, templatePalette, sizePalette, lzString, chooser, datastore) {
  5. var FOUND_COLOR = "#84f060";
  6. var MODE_CLASSIC = "classic";
  7. var MODE_SPLITTED = "splitted";
  8. var MODE_EQUAL = "equal";
  9. var MODE_NON_EQUAL = "non_equal";
  10. var INLINE_RES = "#inline#";
  11. var TEMPLATE_SUMS = {
  12. name: "Addition", icon: "addition.svg", cards: [
  13. [{text: "3+4"}, {text: "7"}],
  14. [{text: "5+5"}, {text: "10"}],
  15. [{text: "5+6"}, {text: "11"}],
  16. [{text: "4+4"}, {text: "8"}],
  17. [{text: "4+5"}, {text: "9"}],
  18. [{text: "3+3"}, {text: "6"}],
  19. [{text: "2+2"}, {text: "4"}],
  20. [{text: "1+1"}, {text: "2"}],
  21. [{text: "1+2"}, {text: "3"}],
  22. [{text: "9+9"}, {text: "18"}],
  23. [{text: "10+9"}, {text: "19"}],
  24. [{text: "8+8"}, {text: "16"}],
  25. [{text: "8+9"}, {text: "17"}],
  26. [{text: "7+7"}, {text: "14"}],
  27. [{text: "7+8"}, {text: "15"}],
  28. [{text: "6+6"}, {text: "12"}]
  29. ],
  30. mode: MODE_SPLITTED
  31. };
  32. var TEMPLATE_LETTERS = {
  33. name: "Letters", icon: "letters.svg", cards: [
  34. [{text: "A"}, {text: "a"}],
  35. [{text: "B"}, {text: "b"}],
  36. [{text: "C"}, {text: "c"}],
  37. [{text: "D"}, {text: "d"}],
  38. [{text: "E"}, {text: "e"}],
  39. [{text: "F"}, {text: "f"}],
  40. [{text: "G"}, {text: "g"}],
  41. [{text: "H"}, {text: "h"}],
  42. [{text: "I"}, {text: "i"}],
  43. [{text: "J"}, {text: "j"}],
  44. [{text: "K"}, {text: "k"}],
  45. [{text: "L"}, {text: "l"}],
  46. [{text: "M"}, {text: "m"}],
  47. [{text: "N"}, {text: "n"}],
  48. [{text: "O"}, {text: "o"}],
  49. [{text: "P"}, {text: "p"}],
  50. [{text: "Q"}, {text: "q"}],
  51. [{text: "R"}, {text: "r"}],
  52. [{text: "S"}, {text: "s"}],
  53. [{text: "T"}, {text: "t"}],
  54. [{text: "U"}, {text: "u"}],
  55. [{text: "V"}, {text: "v"}],
  56. [{text: "W"}, {text: "w"}],
  57. [{text: "X"}, {text: "x"}],
  58. [{text: "Y"}, {text: "y"}],
  59. [{text: "Z"}, {text: "z"}]
  60. ],
  61. mode: MODE_SPLITTED
  62. };
  63. var TEMPLATE_SOUNDS = {
  64. name: "Sounds", icon: "sounds.svg", cards: [
  65. [
  66. {image: INLINE_RES + "drumkit1_b", sound: INLINE_RES + "beat1_a"},
  67. {image: INLINE_RES + "drumkit1_b", sound: INLINE_RES + "beat1_a"}
  68. ],
  69. [
  70. {image: INLINE_RES + "drumkit2_b", sound: INLINE_RES + "beat1_b"},
  71. {image: INLINE_RES + "drumkit2_b", sound: INLINE_RES + "beat1_b"}
  72. ],
  73. [
  74. {image: INLINE_RES + "drumkit3_b", sound: INLINE_RES + "beat1_c"},
  75. {image: INLINE_RES + "drumkit3_b", sound: INLINE_RES + "beat1_c"}
  76. ],
  77. [
  78. {image: INLINE_RES + "drumkit4_b", sound: INLINE_RES + "beat8"},
  79. {image: INLINE_RES + "drumkit4_b", sound: INLINE_RES + "beat8"}
  80. ],
  81. [
  82. {image: INLINE_RES + "drumkit5_b", sound: INLINE_RES + "beat10"},
  83. {image: INLINE_RES + "drumkit5_b", sound: INLINE_RES + "beat10"}
  84. ],
  85. [
  86. {image: INLINE_RES + "drumkit6_b", sound: INLINE_RES + "beat3"},
  87. {image: INLINE_RES + "drumkit6_b", sound: INLINE_RES + "beat3"}
  88. ],
  89. [
  90. {image: INLINE_RES + "drumkit7_b", sound: INLINE_RES + "beat4"},
  91. {image: INLINE_RES + "drumkit7_b", sound: INLINE_RES + "beat4"}
  92. ],
  93. [
  94. {image: INLINE_RES + "drumkit8_b", sound: INLINE_RES + "beat14"},
  95. {image: INLINE_RES + "drumkit8_b", sound: INLINE_RES + "beat14"}
  96. ],
  97. [
  98. {image: INLINE_RES + "drumkit9_b", sound: INLINE_RES + "beat6_2"},
  99. {image: INLINE_RES + "drumkit9_b", sound: INLINE_RES + "beat6_2"}
  100. ],
  101. [
  102. {image: INLINE_RES + "drumkit10_b", sound: INLINE_RES + "beat2"},
  103. {image: INLINE_RES + "drumkit10_b", sound: INLINE_RES + "beat2"}
  104. ],
  105. [
  106. {image: INLINE_RES + "drumkit11_b", sound: INLINE_RES + "beat16"},
  107. {image: INLINE_RES + "drumkit11_b", sound: INLINE_RES + "beat16"}
  108. ],
  109. [
  110. {image: INLINE_RES + "drumkit12_b", sound: INLINE_RES + "beat17"},
  111. {image: INLINE_RES + "drumkit12_b", sound: INLINE_RES + "beat17"}
  112. ],
  113. [
  114. {image: INLINE_RES + "guitar1_2", sound: INLINE_RES + "bending_a"},
  115. {image: INLINE_RES + "guitar1_2", sound: INLINE_RES + "bending_a"}
  116. ],
  117. [
  118. {image: INLINE_RES + "guitar2_2", sound: INLINE_RES + "bending_b"},
  119. {image: INLINE_RES + "guitar2_2", sound: INLINE_RES + "bending_b"}
  120. ],
  121. [
  122. {image: INLINE_RES + "guitar3_2", sound: INLINE_RES + "flashcomp2a"},
  123. {image: INLINE_RES + "guitar3_2", sound: INLINE_RES + "flashcomp2a"}
  124. ],
  125. [
  126. {image: INLINE_RES + "guitar4_2", sound: INLINE_RES + "flashcomp2b"},
  127. {image: INLINE_RES + "guitar4_2", sound: INLINE_RES + "flashcomp2b"}
  128. ],
  129. [
  130. {image: INLINE_RES + "guitar5_2", sound: INLINE_RES + "gedaempft"},
  131. {image: INLINE_RES + "guitar5_2", sound: INLINE_RES + "gedaempft"}
  132. ],
  133. [
  134. {image: INLINE_RES + "guitar6_2", sound: INLINE_RES + "ungedaempft"},
  135. {image: INLINE_RES + "guitar6_2", sound: INLINE_RES + "ungedaempft"}
  136. ],
  137. [
  138. {image: INLINE_RES + "guitar7_2", sound: INLINE_RES + "jimi4"},
  139. {image: INLINE_RES + "guitar7_2", sound: INLINE_RES + "jimi4"}
  140. ],
  141. [
  142. {image: INLINE_RES + "guitar8_2", sound: INLINE_RES + "git_hit1"},
  143. {image: INLINE_RES + "guitar8_2", sound: INLINE_RES + "git_hit1"}
  144. ],
  145. [
  146. {image: INLINE_RES + "guitar9_2", sound: INLINE_RES + "git_hit4"},
  147. {image: INLINE_RES + "guitar9_2", sound: INLINE_RES + "git_hit4"}
  148. ],
  149. [
  150. {image: INLINE_RES + "guitar10_2", sound: INLINE_RES + "jimi1"},
  151. {image: INLINE_RES + "guitar10_2", sound: INLINE_RES + "jimi1"}
  152. ],
  153. [
  154. {image: INLINE_RES + "guitar11_2", sound: INLINE_RES + "flasholet4"},
  155. {image: INLINE_RES + "guitar11_2", sound: INLINE_RES + "flasholet4"}
  156. ],
  157. [
  158. {image: INLINE_RES + "guitar12_2", sound: INLINE_RES + "guitcello"},
  159. {image: INLINE_RES + "guitar12_2", sound: INLINE_RES + "guitcello"}
  160. ]
  161. ],
  162. mode: MODE_CLASSIC
  163. };
  164. var MemorizeApp = {
  165. strings: {add: "Add", update: "Update", remove: "Remove"},
  166. ui: {},
  167. templates: [TEMPLATE_SUMS, TEMPLATE_LETTERS, TEMPLATE_SOUNDS],
  168. isHost: false,
  169. editor: {pairMode: MODE_EQUAL, card1: {}, card2: {}, selectedPair: -1},
  170. game: {
  171. template: TEMPLATE_LETTERS,
  172. multiplayer: false,
  173. selectedCards: [],
  174. mode: undefined,
  175. cards: [],
  176. currentPlayer: "",
  177. players: [],
  178. size: 4
  179. },
  180. computeCards: computeCards,
  181. inEditMode: false,
  182. shareActivity: shareActivity,
  183. initUI: initUI,
  184. drawGame: drawGame,
  185. onUsersListChanged: onUsersListChanged,
  186. onDataReceived: onDataReceived
  187. };
  188. function shuffle(array) {
  189. var currentIndex = array.length, temporaryValue, randomIndex;
  190. while (0 !== currentIndex) {
  191. randomIndex = Math.floor(Math.random() * currentIndex);
  192. currentIndex -= 1;
  193. temporaryValue = array[currentIndex];
  194. array[currentIndex] = array[randomIndex];
  195. array[randomIndex] = temporaryValue;
  196. }
  197. return array;
  198. }
  199. function shareActivity(isHost) {
  200. if (!MemorizeApp.presence.isConnected()) {
  201. return;
  202. }
  203. MemorizeApp.inEditMode = false;
  204. leaveEditMode();
  205. disableEditMode();
  206. MemorizeApp.game.multiplayer = true;
  207. MemorizeApp.isHost = isHost;
  208. MemorizeApp.me = MemorizeApp.presence.userInfo;
  209. MemorizeApp.game.currentPlayer = MemorizeApp.me.networkId;
  210. MemorizeApp.ui.gamePlayers.style.display = "block";
  211. }
  212. function computeCards(ignoreSave) {
  213. MemorizeApp.game.cards = [];
  214. MemorizeApp.game.selectedCards = [];
  215. if (!MemorizeApp.game.template) {
  216. return;
  217. }
  218. MemorizeApp.game.mode = MemorizeApp.game.template.mode;
  219. var shuffledTemplate = {name: MemorizeApp.game.template.name, cards: []};
  220. shuffledTemplate.cards = JSON.parse(JSON.stringify(shuffle(MemorizeApp.game.template.cards)));
  221. var cardsNumber = 0;
  222. if (MemorizeApp.game.size % 2 == 0) {
  223. cardsNumber = MemorizeApp.game.size * MemorizeApp.game.size;
  224. } else {
  225. cardsNumber = MemorizeApp.game.size * MemorizeApp.game.size - 2;
  226. }
  227. if (MemorizeApp.game.mode == MODE_CLASSIC) {
  228. var cards = [];
  229. for (var i = 0; i < shuffledTemplate.cards.length && i < cardsNumber / 2; i++) {
  230. var card1 = shuffledTemplate.cards[i][0];
  231. var card2 = shuffledTemplate.cards[i][1];
  232. card1.id = i;
  233. card2.id = i;
  234. cards.push(card1);
  235. cards.push(card2);
  236. }
  237. MemorizeApp.game.cards = shuffle(cards);
  238. }
  239. if (MemorizeApp.game.mode == MODE_SPLITTED) {
  240. var cards1 = [];
  241. var cards2 = [];
  242. for (var i = 0; i < cardsNumber / 2 && i < shuffledTemplate.cards.length; i++) {
  243. var card1 = shuffledTemplate.cards[i][0];
  244. var card2 = shuffledTemplate.cards[i][1];
  245. card1.id = i;
  246. card2.id = i;
  247. cards1.push(card1);
  248. cards2.push(card2);
  249. }
  250. var shuffledCard1 = shuffle(cards1);
  251. for (var i = 0; i < shuffledCard1.length; i++) {
  252. MemorizeApp.game.cards.push(shuffledCard1[i]);
  253. }
  254. var shuffledCard2 = shuffle(cards2);
  255. for (var i = 0; i < shuffledCard2.length; i++) {
  256. MemorizeApp.game.cards.push(shuffledCard2[i]);
  257. }
  258. }
  259. if (!ignoreSave) saveGame();
  260. }
  261. function resizeTextInsideTextCardDivs() {
  262. var elements = document.getElementsByClassName('textCard');
  263. for (var i = 0; i < elements.length; i++) {
  264. var el = elements[i];
  265. while (el.scrollHeight > el.offsetHeight) {
  266. el.style.fontSize = parseInt(el.style.fontSize) - 1 + "px";
  267. }
  268. while (el.scrollWidth > el.offsetWidth) {
  269. el.style.fontSize = parseInt(el.style.fontSize) - 1 + "px";
  270. }
  271. }
  272. }
  273. function generateCardDiv(card, minSize) {
  274. if (card.image) {
  275. if (card.image.indexOf(INLINE_RES) == 0) {
  276. card.image = SampleRessources[card.image.slice(INLINE_RES.length)];
  277. }
  278. var div = document.createElement("div");
  279. div.style.background = "url('" + card.image + "')";
  280. div.style.backgroundRepeat = "no-repeat";
  281. div.style.backgroundSize = "contain";
  282. div.style.webkitBackgroundSize = "contain";
  283. div.style.backgroundPosition = "center center";
  284. div.style.height = minSize + "px";
  285. div.style.width = minSize + "px";
  286. return div;
  287. }
  288. if (card.text) {
  289. if (card.text.indexOf(INLINE_RES) == 0) {
  290. card.text = SampleRessources[card.text.slice(INLINE_RES.length)];
  291. }
  292. var div = document.createElement("div");
  293. div.className = "textCard";
  294. div.style.textAlign = "center";
  295. div.style.display = "block";
  296. div.innerHTML = card.text;
  297. div.style.lineHeight = minSize + "px";
  298. div.style.fontSize = minSize + 'px';
  299. div.style.color = "#000";
  300. div.style.width = minSize + "px";
  301. div.style.height = minSize + "px";
  302. return div;
  303. }
  304. }
  305. function createAudioContextIfMissing() {
  306. if (MemorizeApp.context) {
  307. return;
  308. }
  309. var context = window.AudioContext ||
  310. window.webkitAudioContext ||
  311. window.mozAudioContext ||
  312. window.oAudioContext ||
  313. window.msAudioContext;
  314. if (!context) {
  315. return;
  316. }
  317. context = new context();
  318. MemorizeApp.context = context;
  319. var buffer = context.createBuffer(1, 1, 22050);
  320. var source = context.createBufferSource();
  321. MemorizeApp.source = source;
  322. source.buffer = buffer;
  323. source.connect(context.destination);
  324. source.start(0);
  325. }
  326. function createFullCardDiv(i, minSize, card) {
  327. var fullCardDiv = document.createElement("div");
  328. fullCardDiv.cardPosition = i;
  329. fullCardDiv.webkitPerspective = "500px";
  330. fullCardDiv.perspective = "500px";
  331. fullCardDiv.style.border = "3px solid #fff";
  332. fullCardDiv.style.borderRadius = "6px";
  333. fullCardDiv.style.margin = "5px";
  334. fullCardDiv.style.webkitTransition = "transform 0.5s";
  335. fullCardDiv.style.transition = "transform 0.5s";
  336. fullCardDiv.style.transitionDuration = "0.5s";
  337. fullCardDiv.style.webkitTransitionDuration = "0.5s";
  338. fullCardDiv.style.transformStyle = "preserve-3d";
  339. fullCardDiv.style.webkitTransformStyle = "preserve-3d";
  340. fullCardDiv.style.position = "relative";
  341. fullCardDiv.style.float = "left";
  342. fullCardDiv.style.height = minSize + "px";
  343. fullCardDiv.style.width = minSize + "px";
  344. if (card.solved) {
  345. fullCardDiv.style.webkitTransform = "rotateY(180deg)";
  346. fullCardDiv.style.transform = "rotateY(180deg)";
  347. } else {
  348. fullCardDiv.style.webkitTransform = "";
  349. fullCardDiv.style.transform = "";
  350. }
  351. if (MemorizeApp.game.selectedCards.length != 0) {
  352. if (MemorizeApp.game.selectedCards[0].cardPosition == fullCardDiv.cardPosition) {
  353. fullCardDiv.style.webkitTransform = "";
  354. fullCardDiv.style.transform = "";
  355. }
  356. }
  357. return fullCardDiv;
  358. }
  359. function createFrontDiv(i, middle, minSize) {
  360. var front = document.createElement("div");
  361. if (MemorizeApp.game.mode == MODE_CLASSIC) {
  362. front.style.background = "#777";
  363. }
  364. if (MemorizeApp.game.mode == MODE_SPLITTED) {
  365. if (i < middle) {
  366. front.style.background = "#777 url(icons/number1.svg)";
  367. } else {
  368. front.style.background = "#777 url(icons/number2.svg)";
  369. }
  370. }
  371. front.zIndex = 2;
  372. front.style.borderRadius = "6px";
  373. front.style.webkitBackfaceVisibility = "hidden";
  374. front.style.backfaceVisibility = "hidden";
  375. front.style.backgroundPosition = "center center";
  376. front.style.backgroundRepeat = "no-repeat";
  377. front.style.height = minSize + "px";
  378. front.style.position = "absolute";
  379. front.style.top = "0px";
  380. front.style.left = "0px";
  381. front.style.width = minSize + "px";
  382. return front;
  383. }
  384. function createDiv(i, minSize, card) {
  385. var div = document.createElement("div");
  386. var generatedDiv = generateCardDiv(MemorizeApp.game.cards[i], minSize);
  387. div.appendChild(generatedDiv);
  388. div.style.height = minSize + "px";
  389. div.style.webkitBackfaceVisibility = "hidden";
  390. div.style.backfaceVisibility = "hidden";
  391. div.style.mozBackfaceVisibility = "hidden";
  392. div.style.position = "absolute";
  393. div.style.webkitTransform = "rotateY(180deg)";
  394. div.style.transform = "rotateY(180deg)";
  395. div.style.top = "0px";
  396. div.style.left = "0px";
  397. div.style.borderRadius = "6px";
  398. div.style.width = minSize + "px";
  399. div.style.background = "#fff";
  400. if (card.solved) {
  401. div.style.backgroundColor = FOUND_COLOR;
  402. }
  403. return div;
  404. }
  405. function cardClick(div, fromMe, user) {
  406. var middle = MemorizeApp.game.cards.length / 2;
  407. createAudioContextIfMissing();
  408. var t = div;
  409. if (t.card.solved || MemorizeApp.game.selectedCards.length == 2) {
  410. return;
  411. }
  412. if (MemorizeApp.game.mode == MODE_SPLITTED && MemorizeApp.game.selectedCards.length == 1) {
  413. if (t.cardPosition < middle && MemorizeApp.game.selectedCards[0].cardPosition < middle) {
  414. return;
  415. }
  416. if (t.cardPosition >= middle && MemorizeApp.game.selectedCards[0].cardPosition >= middle) {
  417. return;
  418. }
  419. }
  420. t.style.webkitTransform = "rotateY(180deg)";
  421. t.style.transform = "rotateY(180deg)";
  422. if(MemorizeApp.game.selectedCards.length == 1 && MemorizeApp.game.selectedCards[0] == t){
  423. return;
  424. }
  425. else{
  426. MemorizeApp.game.selectedCards.push(t);
  427. }
  428. if (t.card.sound) {
  429. if (t.card.sound.indexOf(INLINE_RES) == 0) {
  430. t.card.sound = SampleRessources[t.card.sound.slice(INLINE_RES.length)];
  431. }
  432. var b64 = t.card.sound.split("base64,")[1];
  433. b64 = Base64Binary.decodeArrayBuffer(btoa(atob(b64)));
  434. if (MemorizeApp.context) {
  435. MemorizeApp.context.decodeAudioData(b64, function (buffer) {
  436. var source = MemorizeApp.context.createBufferSource(); // creates a sound source
  437. if (MemorizeApp.source) {
  438. try {
  439. MemorizeApp.source.stop(0);
  440. } catch (e) {
  441. }
  442. }
  443. MemorizeApp.source = source;
  444. source.buffer = buffer;
  445. source.connect(MemorizeApp.context.destination);
  446. try {
  447. source.start(0);
  448. } catch (e) {
  449. }
  450. }, function (err) {
  451. console.log("err(decodeAudioData): " + err);
  452. });
  453. }
  454. }
  455. if (MemorizeApp.game.selectedCards.length == 1) {
  456. return;
  457. }
  458. if (MemorizeApp.game.selectedCards[0].card.id == t.card.id) {
  459. MemorizeApp.game.selectedCards[0].card.solved = true;
  460. t.card.solved = true;
  461. for (var i = 0; i < MemorizeApp.game.players.length; i++) {
  462. if (MemorizeApp.game.players[i].networkId == user.networkId) {
  463. MemorizeApp.game.players[i].score = MemorizeApp.game.players[i].score + 1;
  464. }
  465. }
  466. displayUsersAndScores();
  467. var div1 = MemorizeApp.game.selectedCards[0].resultDiv;
  468. var div2 = t.resultDiv;
  469. setTimeout(function () {
  470. div1.style.backgroundColor = FOUND_COLOR;
  471. div2.style.backgroundColor = FOUND_COLOR;
  472. }, 1000);
  473. MemorizeApp.game.selectedCards = [];
  474. saveGame();
  475. return;
  476. }
  477. MemorizeApp.game.currentPlayer = "";
  478. setTimeout(function () {
  479. try {
  480. t.style.webkitTransform = "";
  481. t.style.transform = "";
  482. MemorizeApp.game.selectedCards[0].style.webkitTransform = "";
  483. MemorizeApp.game.selectedCards[0].style.transform = "";
  484. MemorizeApp.game.selectedCards = [];
  485. setTimeout(function () {
  486. if (fromMe) {
  487. for (var i = 0; i < MemorizeApp.game.players.length; i++) {
  488. if (MemorizeApp.game.players[i].online && MemorizeApp.game.players[i].networkId != MemorizeApp.me.networkId) {
  489. MemorizeApp.game.currentPlayer = MemorizeApp.game.players[i].networkId;
  490. sendMessage({
  491. action: "updateCurrentPlayer",
  492. currentPlayer: MemorizeApp.game.currentPlayer
  493. });
  494. displayUsersAndScores();
  495. break;
  496. }
  497. }
  498. }
  499. }, 300);
  500. } catch (e) {
  501. }
  502. }, 2000)
  503. }
  504. function saveGame() {
  505. MemorizeApp.activity.getDatastoreObject().setDataAsText(JSON.stringify({game: MemorizeApp.game}));
  506. MemorizeApp.activity.getDatastoreObject().save(function (error, meta) {
  507. });
  508. }
  509. function onCardClick() {
  510. if (MemorizeApp.game.multiplayer) {
  511. if (MemorizeApp.game.currentPlayer != MemorizeApp.me.networkId) {
  512. return;
  513. }
  514. sendMessage({action: "cardClick", position: this.cardPosition});
  515. if (MemorizeApp.game.players.length == 0 || MemorizeApp.game.players.length == 1) {
  516. cardClick(this, true);
  517. }
  518. for (var i = 0; i < MemorizeApp.game.players.length; i++) {
  519. if (MemorizeApp.game.players[i].networkId == MemorizeApp.me.networkId) {
  520. cardClick(this, true, MemorizeApp.game.players[i]);
  521. }
  522. }
  523. } else {
  524. cardClick(this, true);
  525. }
  526. }
  527. function drawGame() {
  528. if (MemorizeApp.editor.pairMode == MODE_EQUAL) {
  529. MemorizeApp.ui.gameEditorInsertModeButton.style.backgroundImage = "url(icons/pair-non-equals.svg)"
  530. } else {
  531. MemorizeApp.ui.gameEditorInsertModeButton.style.backgroundImage = "url(icons/pair-equals.svg)"
  532. }
  533. if (MemorizeApp.game.template.mode == MODE_CLASSIC) {
  534. MemorizeApp.ui.gameEditorPlayModeButton.style.backgroundImage = "url(icons/grouped_game1.svg)";
  535. } else {
  536. MemorizeApp.ui.gameEditorPlayModeButton.style.backgroundImage = "url(icons/grouped_game2.svg)";
  537. }
  538. MemorizeApp.ui.gameSizeButton.style.background = "url(icons/" + MemorizeApp.game.size + "x" + MemorizeApp.game.size + ".svg)";
  539. MemorizeApp.ui.gameTemplatesButton.style.backgroundImage = "url(icons/" + MemorizeApp.game.template.icon + ")";
  540. MemorizeApp.ui.gameGrid.innerHTML = "";
  541. var gameDiv = MemorizeApp.ui.gameGrid;
  542. var width = document.body.clientWidth;
  543. var height = document.body.clientHeight;
  544. var middle = MemorizeApp.game.cards.length / 2;
  545. var minSize = height;
  546. if (width < height) {
  547. minSize = width;
  548. }
  549. minSize = minSize - (56 + 20) * 3;
  550. minSize = minSize / MemorizeApp.game.size;
  551. for (var i = 0; i < MemorizeApp.game.cards.length; i++) {
  552. var card = MemorizeApp.game.cards[i];
  553. if (i % MemorizeApp.game.size == 0 && i > 0) {
  554. var div = document.createElement("div");
  555. div.style.clear = "both";
  556. gameDiv.appendChild(div);
  557. }
  558. var fullCardDiv = createFullCardDiv(i, minSize, card);
  559. var front = createFrontDiv(i, middle, minSize);
  560. var div = createDiv(i, minSize, card);
  561. fullCardDiv.card = card;
  562. fullCardDiv.resultDiv = div;
  563. var clickEvent = "click";
  564. if (/iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream) {
  565. clickEvent = "touchend";
  566. }
  567. fullCardDiv.addEventListener(clickEvent, onCardClick, false);
  568. fullCardDiv.appendChild(div);
  569. fullCardDiv.appendChild(front);
  570. gameDiv.appendChild(fullCardDiv);
  571. }
  572. gameDiv.style.width = parseInt(minSize + 10) * parseInt(MemorizeApp.game.size + 1) + "px";
  573. gameDiv.style.marginLeft = "auto";
  574. gameDiv.style.marginRight = "auto";
  575. resizeTextInsideTextCardDivs();
  576. }
  577. function onDataReceived(data) {
  578. if (data.user.networkId == MemorizeApp.me.networkId) {
  579. return;
  580. }
  581. data.content = JSON.parse(lzString.decompressFromUTF16(data.content));
  582. if (data.content.action == "updateCurrentPlayer") {
  583. MemorizeApp.game.currentPlayer = data.content.currentPlayer;
  584. displayUsersAndScores();
  585. }
  586. if (data.content.action == "updateGame") {
  587. MemorizeApp.game = data.content.game;
  588. MemorizeApp.ui.gameSizeButton.style.background = "url(icons/" + MemorizeApp.game.size + "x" + MemorizeApp.game.size + ".svg)";
  589. MemorizeApp.ui.gameTemplatesButton.style.backgroundImage = "url(icons/" + MemorizeApp.game.template.icon + ")";
  590. MemorizeApp.drawGame();
  591. displayUsersAndScores();
  592. saveGame();
  593. }
  594. if (data.content.action == "cardClick") {
  595. var notFilteredCards = MemorizeApp.ui.gameGrid.childNodes;
  596. var cards = [];
  597. for (var i = 0; i < notFilteredCards.length; i++) {
  598. if (!notFilteredCards[i].style.clear || notFilteredCards[i].style.clear == "") {
  599. cards.push(notFilteredCards[i]);
  600. }
  601. }
  602. for (var i = 0; i < MemorizeApp.game.players.length; i++) {
  603. if (MemorizeApp.game.players[i].networkId == data.user.networkId) {
  604. cardClick(cards[data.content.position], false, MemorizeApp.game.players[i]);
  605. }
  606. }
  607. }
  608. }
  609. function sendMessage(content) {
  610. if (!MemorizeApp.game.multiplayer) {
  611. return;
  612. }
  613. var sharedId = window.top.sugar.environment.sharedId;
  614. if (!sharedId) {
  615. sharedId = MemorizeApp.presence.getSharedInfo().id
  616. }
  617. MemorizeApp.presence.sendMessage(sharedId, {
  618. user: MemorizeApp.presence.getUserInfo(),
  619. content: lzString.compressToUTF16(JSON.stringify(content))
  620. });
  621. }
  622. function displayUsersAndScores() {
  623. if (MemorizeApp.game.players.length == 0) {
  624. return;
  625. }
  626. var div = document.createElement("div");
  627. var myTurn = false;
  628. if (MemorizeApp.game.currentPlayer == MemorizeApp.me.networkId) {
  629. myTurn = true;
  630. }
  631. var canPlay = document.createElement("div");
  632. canPlay.style.float = "left";
  633. canPlay.style.marginTop = "10px";
  634. canPlay.style.width = "30px";
  635. canPlay.style.marginRight = "3px";
  636. canPlay.style.borderRight = "1px solid #fff";
  637. canPlay.style.height = "30px";
  638. if (myTurn) {
  639. canPlay.style.backgroundImage = "url(icons/play.svg)"
  640. } else {
  641. canPlay.style.backgroundImage = "url(icons/sandclock.svg)"
  642. }
  643. canPlay.style.backgroundPosition = "center center";
  644. canPlay.style.backgroundRepeat = "no-repeat";
  645. div.appendChild(canPlay);
  646. for (var i = 0; i < MemorizeApp.game.players.length; i++) {
  647. var player = MemorizeApp.game.players[i];
  648. var d = document.createElement("div");
  649. d.style.paddingTop = "15px";
  650. var xoColor = generateXOLogoWithColor(player.colorvalue);
  651. var scores = "";
  652. for (var j = 0; j < player.score; j++) {
  653. scores += '<img style="height:13px;" src="icons/pair-add.svg">';
  654. }
  655. d.innerHTML = '<img style="height:23px; vertical-align:middle; " src="' + xoColor + '"><span style="color:#fff;">' + player.name + "</span> " + scores;
  656. d.style.float = "left";
  657. if (i != 0) {
  658. d.style.marginLeft = "10px";
  659. }
  660. div.appendChild(d);
  661. }
  662. MemorizeApp.ui.gamePlayers.innerHTML = "";
  663. MemorizeApp.ui.gamePlayers.appendChild(div);
  664. }
  665. function onUsersListChanged(users) {
  666. for (var i = 0; i < MemorizeApp.game.players.length; i++) {
  667. MemorizeApp.game.players[i].online = false;
  668. }
  669. if (!MemorizeApp.hasLoadedMultiplayer && users.length >= 3) {
  670. document.getElementById("stop-button").click();
  671. return;
  672. }
  673. MemorizeApp.hasLoadedMultiplayer = true;
  674. if (users.length == 1) {
  675. MemorizeApp.isHost = true;
  676. MemorizeApp.game.currentPlayer = MemorizeApp.me.networkId;
  677. MemorizeApp.game.selectedCards = [];
  678. drawGame();
  679. displayUsersAndScores();
  680. return;
  681. }
  682. for (var i = 0; i < users.length; i++) {
  683. var found = false;
  684. for (var j = 0; j < MemorizeApp.game.players.length; j++) {
  685. if (MemorizeApp.game.players[j].networkId == users[i].networkId) {
  686. MemorizeApp.game.players[j].online = true;
  687. found = true;
  688. break;
  689. }
  690. }
  691. if (!found) {
  692. var user = users[i];
  693. user.score = 0;
  694. user.online = true;
  695. MemorizeApp.game.players.push(user);
  696. }
  697. }
  698. if (MemorizeApp.isHost) {
  699. sendMessage({action: "updateGame", game: MemorizeApp.game});
  700. }
  701. displayUsersAndScores();
  702. }
  703. var xoLogo = '<?xml version="1.0" ?><!DOCTYPE svg PUBLIC \'-//W3C//DTD SVG 1.1//EN\' \'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\' [<!ENTITY stroke_color "#010101"><!ENTITY fill_color "#FFFFFF">]><svg enable-background="new 0 0 55 55" height="55px" version="1.1" viewBox="0 0 55 55" width="55px" x="0px" xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" y="0px"><g display="block" id="stock-xo_1_"><path d="M33.233,35.1l10.102,10.1c0.752,0.75,1.217,1.783,1.217,2.932 c0,2.287-1.855,4.143-4.146,4.143c-1.145,0-2.178-0.463-2.932-1.211L27.372,40.961l-10.1,10.1c-0.75,0.75-1.787,1.211-2.934,1.211 c-2.284,0-4.143-1.854-4.143-4.141c0-1.146,0.465-2.184,1.212-2.934l10.104-10.102L11.409,24.995 c-0.747-0.748-1.212-1.785-1.212-2.93c0-2.289,1.854-4.146,4.146-4.146c1.143,0,2.18,0.465,2.93,1.214l10.099,10.102l10.102-10.103 c0.754-0.749,1.787-1.214,2.934-1.214c2.289,0,4.146,1.856,4.146,4.145c0,1.146-0.467,2.18-1.217,2.932L33.233,35.1z" fill="&fill_color;" stroke="&stroke_color;" stroke-width="3.5"/><circle cx="27.371" cy="10.849" fill="&fill_color;" r="8.122" stroke="&stroke_color;" stroke-width="3.5"/></g></svg>';
  704. function generateXOLogoWithColor(color) {
  705. var coloredLogo = xoLogo;
  706. coloredLogo = coloredLogo.replace("#010101", color.stroke);
  707. coloredLogo = coloredLogo.replace("#FFFFFF", color.fill);
  708. return "data:image/svg+xml;base64," + btoa(coloredLogo);
  709. }
  710. function initUI(callback) {
  711. MemorizeApp.ui.gameGrid = document.getElementById("game-grid");
  712. MemorizeApp.ui.gamePlayers = document.getElementById("game-players");
  713. MemorizeApp.ui.gameEditor = document.getElementById("game-editor");
  714. MemorizeApp.ui.gameTemplatesButton = document.getElementById("game-templates-button");
  715. var gt = new templatePalette.TemplatePalette(MemorizeApp.ui.gameTemplatesButton, undefined, MemorizeApp.templates);
  716. gt.addEventListener('template', function (e) {
  717. for (var i = 0; i < MemorizeApp.game.players.length; i++) {
  718. MemorizeApp.game.players[i].score = 0;
  719. }
  720. MemorizeApp.game.template = e.detail.value;
  721. MemorizeApp.ui.gameTemplatesButton.style.backgroundImage = "url(icons/" + e.detail.value.icon + ")";
  722. MemorizeApp.computeCards();
  723. MemorizeApp.drawGame();
  724. sendMessage({action: "updateGame", game: MemorizeApp.game});
  725. });
  726. MemorizeApp.ui.gameSizeButton = document.getElementById("game-size-button");
  727. var sp = new sizePalette.SizePalette(MemorizeApp.ui.gameSizeButton);
  728. sp.addEventListener('size', function (e) {
  729. for (var i = 0; i < MemorizeApp.game.players.length; i++) {
  730. MemorizeApp.game.players[i].score = 0;
  731. }
  732. MemorizeApp.game.size = e.detail.value;
  733. MemorizeApp.ui.gameSizeButton.style.background = "url(icons/" + e.detail.value + "x" + e.detail.value + ".svg)";
  734. MemorizeApp.computeCards();
  735. MemorizeApp.drawGame();
  736. sendMessage({action: "updateGame", game: MemorizeApp.game});
  737. });
  738. MemorizeApp.ui.gameResetButton = document.getElementById("game-reset-button");
  739. MemorizeApp.ui.gameResetButton.addEventListener("click", function () {
  740. for (var i = 0; i < MemorizeApp.game.players.length; i++) {
  741. MemorizeApp.game.players[i].score = 0;
  742. }
  743. MemorizeApp.game.selectedCards = [];
  744. MemorizeApp.game.cards = [];
  745. displayUsersAndScores();
  746. MemorizeApp.computeCards();
  747. MemorizeApp.drawGame();
  748. sendMessage({action: "updateGame", game: MemorizeApp.game});
  749. });
  750. MemorizeApp.ui.gameEditorButton = document.getElementById("game-editor-button");
  751. MemorizeApp.ui.gameEditorInsertModeButton = document.getElementById("game-editor-insert-mode-button");
  752. MemorizeApp.ui.gameEditorPlayModeButton = document.getElementById("game-editor-play-mode-button");
  753. MemorizeApp.ui.gameEditorPlayModeButton.addEventListener("click", function() {
  754. if (MemorizeApp.game.template.mode == MODE_CLASSIC) {
  755. MemorizeApp.game.template.mode = MODE_SPLITTED;
  756. MemorizeApp.ui.gameEditorPlayModeButton.style.backgroundImage = "url(icons/grouped_game2.svg)";
  757. } else {
  758. MemorizeApp.game.template.mode = MODE_CLASSIC;
  759. MemorizeApp.ui.gameEditorPlayModeButton.style.backgroundImage = "url(icons/grouped_game1.svg)";
  760. }
  761. saveGame();
  762. displayEditor();
  763. });
  764. MemorizeApp.ui.gameEditorClearButton = document.getElementById("game-editor-clear-button");
  765. MemorizeApp.ui.gameEditorInsertModeButton.addEventListener("click", function() {
  766. if (MemorizeApp.editor.pairMode == MODE_EQUAL) {
  767. MemorizeApp.editor.pairMode = MODE_NON_EQUAL;
  768. MemorizeApp.ui.gameEditorInsertModeButton.style.backgroundImage = "url(icons/pair-non-equals.svg)"
  769. } else {
  770. MemorizeApp.editor.pairMode = MODE_EQUAL;
  771. MemorizeApp.ui.gameEditorInsertModeButton.style.backgroundImage = "url(icons/pair-equals.svg)"
  772. }
  773. saveGame();
  774. displayEditor();
  775. });
  776. MemorizeApp.ui.gameEditorClearButton.addEventListener("click", function() {
  777. MemorizeApp.game.template.cards = [];
  778. saveGame();
  779. displayEditor();
  780. });
  781. MemorizeApp.ui.gameEditorButton.addEventListener("click", function () {
  782. if (MemorizeApp.inEditMode) {
  783. leaveEditMode();
  784. } else {
  785. enterEditMode();
  786. }
  787. });
  788. MemorizeApp.ui.gameEditorInsertModeButton.disabled = true;
  789. MemorizeApp.ui.gameEditorInsertModeButton.style.opacity = 0.3;
  790. MemorizeApp.ui.gameEditorPlayModeButton.disabled = true;
  791. MemorizeApp.ui.gameEditorPlayModeButton.style.opacity = 0.3;
  792. MemorizeApp.ui.gameEditorClearButton.disabled = true;
  793. MemorizeApp.ui.gameEditorClearButton.style.opacity = 0.3;
  794. //window.onresize = function () {
  795. // setTimeout(function () {
  796. // if (MemorizeApp.inEditMode) {
  797. // displayEditor()
  798. // } else {
  799. // MemorizeApp.drawGame();
  800. // }
  801. // }, 250);
  802. //};
  803. if (callback) {
  804. callback();
  805. }
  806. }
  807. function enterEditMode() {
  808. //TODO palette equality
  809. //TODO palette show
  810. MemorizeApp.inEditMode = true;
  811. MemorizeApp.ui.gameGrid.innerHTML = "";
  812. MemorizeApp.ui.gameGrid.style.display = "none";
  813. MemorizeApp.ui.gameEditor.style.display = "block";
  814. MemorizeApp.game.selectedCards = [];
  815. MemorizeApp.editor.selectedPair = -1;
  816. /* Disable game buttons */
  817. MemorizeApp.ui.gameTemplatesButton.disabled = true;
  818. MemorizeApp.ui.gameTemplatesButton.style.opacity = 0.3;
  819. MemorizeApp.ui.gameSizeButton.disabled = true;
  820. MemorizeApp.ui.gameSizeButton.style.opacity = 0.3;
  821. MemorizeApp.ui.gameResetButton.disabled = true;
  822. MemorizeApp.ui.gameResetButton.style.opacity = 0.3;
  823. /* Enable editor buttons */
  824. MemorizeApp.ui.gameEditorInsertModeButton.disabled = false;
  825. MemorizeApp.ui.gameEditorInsertModeButton.style.opacity = 1;
  826. MemorizeApp.ui.gameEditorPlayModeButton.disabled = false;
  827. MemorizeApp.ui.gameEditorPlayModeButton.style.opacity = 1;
  828. MemorizeApp.ui.gameEditorClearButton.disabled = false;
  829. MemorizeApp.ui.gameEditorClearButton.style.opacity = 1;
  830. MemorizeApp.ui.gameEditorButton.style.backgroundImage = "url(icons/play.svg)";
  831. displayEditor();
  832. }
  833. function generateEditorDiv(card) {
  834. var minSize = document.body.clientWidth;
  835. if (minSize > document.body.clientHeight) {
  836. minSize = document.body.clientHeight;
  837. }
  838. var e = document.createElement("div");
  839. e.style.width = parseInt(minSize / 3.5) + "px";
  840. e.style.marginLeft = "25px";
  841. e.style.float = "left";
  842. e.style.marginTop = "7px";
  843. var d = document.createElement("div");
  844. d.style.width = parseInt(minSize / 3.5) - 10 + "px";
  845. d.style.height = parseInt(minSize / 3.5) - 10 + "px";
  846. d.style.background = "rgb(119, 119, 119)";
  847. d.style.border = "4px solid #000";
  848. d.style.textAlign = "center";
  849. d.style.borderRadius = "9px";
  850. d.style.color = "#fff";
  851. d.style.fontSize = parseInt(minSize / 3.5) - 10 + "px";
  852. d.style.lineHeight = parseInt(minSize / 3.5) - 10 + "px";
  853. d.className = "textCard";
  854. d.style.backgroundRepeat = "no-repeat";
  855. d.style.backgroundSize = "100%";
  856. d.style.backgroundPosition = "center";
  857. if (card && card.text) {
  858. d.innerHTML = card.text;
  859. } else if (card && card.image) {
  860. d.style.backgroundImage = "url('" + card.image + "')";
  861. }
  862. var importPicture = document.createElement("img");
  863. importPicture.style.marginLeft = "5px";
  864. importPicture.style.marginTop = "5px";
  865. importPicture.style.textAlign = "center";
  866. importPicture.setAttribute("src", "icons/import_picture.svg");
  867. importPicture.style.width = "30px";
  868. importPicture.className = "insertImage";
  869. var input = document.createElement("input");
  870. input.setAttribute("type", "text");
  871. input.style.marginRight = "auto";
  872. input.style.marginLeft = "auto";
  873. input.style.width = parseInt(minSize / 3.5) - 50 + "px";
  874. input.style.marginTop = "5px";
  875. input.card = card;
  876. if (card && card.text) {
  877. input.value = card.text;
  878. }
  879. input.linkedDiv = d;
  880. input.onkeyup = function () {
  881. this.linkedDiv.innerHTML = this.value;
  882. this.linkedDiv.style.fontSize = this.linkedDiv.style.width;
  883. this.card.text = this.value;
  884. resizeTextInsideTextCardDivs()
  885. };
  886. e.appendChild(d);
  887. e.appendChild(input);
  888. e.appendChild(importPicture);
  889. return e;
  890. }
  891. function generateAddEditRemoveButton() {
  892. var div = document.createElement("div");
  893. var minSize = document.body.clientWidth;
  894. if (minSize > document.body.clientHeight) {
  895. minSize = document.body.clientHeight;
  896. }
  897. div.style.float = "right";
  898. div.style.height = parseInt(minSize / 3.5) + "px";
  899. div.style.marginRight = "20px";
  900. div.style.marginTop = "7px";
  901. var addButton = document.createElement("div");
  902. var updateButton = document.createElement("div");
  903. var deleteButton = document.createElement("div");
  904. var buttonSize = parseInt((minSize / 3.5) / 5) + "px";
  905. addButton.style.padding = "5px";
  906. addButton.style.userSelect = "none";
  907. addButton.style.webkitUserSelect = "none";
  908. addButton.style.mozUserSelect = "none";
  909. addButton.innerHTML = "<img style='height:" + buttonSize + "; width:" + buttonSize + ";' src='icons/pair-add.svg'><br/>" + MemorizeApp.strings.add;
  910. addButton.onmouseover = function () {
  911. this.style.background = "#888";
  912. };
  913. addButton.onmouseout = function () {
  914. this.style.background = "transparent";
  915. };
  916. addButton.addEventListener("click", function () {
  917. var cards = [];
  918. if (MemorizeApp.editor.pairMode == MODE_EQUAL) {
  919. cards[0] = MemorizeApp.editor.card1;
  920. cards[1] = MemorizeApp.editor.card1;
  921. }
  922. if (MemorizeApp.editor.pairMode == MODE_NON_EQUAL) {
  923. cards[0] = MemorizeApp.editor.card1;
  924. cards[1] = MemorizeApp.editor.card2;
  925. }
  926. if (!cards[0].text && !cards[0].image && !cards[0].sound) {
  927. return;
  928. }
  929. if (!cards[1].text && !cards[1].image && !cards[1].sound) {
  930. return;
  931. }
  932. cards = JSON.parse(JSON.stringify(cards));
  933. MemorizeApp.editor.card1 = {};
  934. MemorizeApp.editor.card2 = {};
  935. MemorizeApp.game.template.cards.push(cards);
  936. MemorizeApp.editor.selectedPair = -1;
  937. saveGame();
  938. displayEditor();
  939. });
  940. updateButton.style.padding = "5px";
  941. updateButton.style.userSelect = "none";
  942. updateButton.style.webkitUserSelect = "none";
  943. updateButton.style.mozUserSelect = "none";
  944. updateButton.innerHTML = "<img style='height:" + buttonSize + "; width:" + buttonSize + ";' src='icons/pair-update.svg'><br/>" + MemorizeApp.strings.update;
  945. updateButton.onmouseover = function () {
  946. this.style.background = "#888";
  947. };
  948. updateButton.onmouseout = function () {
  949. this.style.background = "transparent";
  950. };
  951. updateButton.addEventListener("click", function () {
  952. var cards = [];
  953. if (MemorizeApp.editor.pairMode == MODE_EQUAL) {
  954. cards[0] = MemorizeApp.editor.card1;
  955. cards[1] = MemorizeApp.editor.card1;
  956. }
  957. if (MemorizeApp.editor.pairMode == MODE_NON_EQUAL) {
  958. cards[0] = MemorizeApp.editor.card1;
  959. cards[1] = MemorizeApp.editor.card2;
  960. }
  961. if (!cards[0].text && !cards[0].image && !cards[0].sound) {
  962. return;
  963. }
  964. if (!cards[1].text && !cards[1].image && !cards[1].sound) {
  965. return;
  966. }
  967. cards = JSON.parse(JSON.stringify(cards));
  968. if (MemorizeApp.editor.selectedPair > -1) {
  969. MemorizeApp.game.template.cards[MemorizeApp.editor.selectedPair] = cards
  970. }
  971. saveGame();
  972. displayEditor();
  973. });
  974. deleteButton.style.padding = "5px";
  975. deleteButton.style.userSelect = "none";
  976. deleteButton.style.webkitUserSelect = "none";
  977. deleteButton.style.mozUserSelect = "none";
  978. deleteButton.innerHTML = "<img style='height:" + buttonSize + "; width:" + buttonSize + ";' src='icons/remove.svg'><br/>" + MemorizeApp.strings.remove;
  979. deleteButton.onmouseover = function () {
  980. this.style.background = "#888";
  981. };
  982. deleteButton.onmouseout = function () {
  983. this.style.background = "transparent";
  984. };
  985. deleteButton.addEventListener("click", function () {
  986. if (MemorizeApp.editor.selectedPair > -1) {
  987. MemorizeApp.game.template.cards.splice(MemorizeApp.editor.selectedPair, 1);
  988. }
  989. MemorizeApp.editor.selectedPair = -1;
  990. MemorizeApp.editor.card1 = {};
  991. MemorizeApp.editor.card2 = {};
  992. saveGame();
  993. displayEditor();
  994. });
  995. div.appendChild(addButton);
  996. div.appendChild(updateButton);
  997. div.appendChild(deleteButton);
  998. return div;
  999. }
  1000. function generateCardFromCardsList(pair, minSize, index) {
  1001. minSize = minSize - 10;
  1002. var d = document.createElement("div");
  1003. d.style.display = "inline-block";
  1004. d.style.height = minSize + "px";
  1005. d.style.marginLeft = "5px";
  1006. if (index == MemorizeApp.editor.selectedPair) {
  1007. d.style.border = "3px solid #00f";
  1008. }
  1009. //d.style.marginTop = "5px";
  1010. //d.style.marginLeft = "15px";
  1011. var card1 = document.createElement("div");
  1012. card1.style.backgroundRepeat = "no-repeat";
  1013. card1.style.backgroundSize = "cover";
  1014. card1.style.backgroundPosition = "center center";
  1015. card1.innerHTML = "&nbsp;";
  1016. card1.style.backgroundColor = "#666";
  1017. card1.className = "textCard";
  1018. if (pair[0].text) {
  1019. if (pair[0].text.indexOf(INLINE_RES) == 0) {
  1020. pair[0].text = SampleRessources[pair[0].image.slice(INLINE_RES.length)]
  1021. }
  1022. card1.innerHTML = pair[0].text;
  1023. }
  1024. else if (pair[0].image) {
  1025. if (pair[0].image.indexOf(INLINE_RES) == 0) {
  1026. pair[0].image = SampleRessources[pair[0].image.slice(INLINE_RES.length)]
  1027. }
  1028. card1.style.backgroundImage = "url('" + pair[0].image + "')";
  1029. }
  1030. card1.style.width = parseInt(minSize / 2) + "px";
  1031. card1.style.height = parseInt(minSize / 2) + "px";
  1032. card1.style.lineHeight = card1.style.width + "";
  1033. card1.style.borderRadius = "6px";
  1034. card1.style.textAlign = "center";
  1035. card1.style.fontSize = parseInt(minSize / 8) + "px";
  1036. card1.style.border = "1px solid #000";
  1037. card1.style.color = "#fff";
  1038. card1.style.marginBottom = "5px";
  1039. var card2 = document.createElement("div");
  1040. card2.style.backgroundRepeat = "no-repeat";
  1041. card2.style.backgroundPosition = "center center";
  1042. card2.style.backgroundSize = "cover";
  1043. card2.innerHTML = "&nbsp;";
  1044. card2.style.backgroundColor = "#666";
  1045. card2.className = "textCard";
  1046. if (pair[1].text) {
  1047. if (pair[1].text.indexOf(INLINE_RES) == 0) {
  1048. pair[1].text = SampleRessources[pair[1].image.slice(INLINE_RES.length)]
  1049. }
  1050. card2.innerHTML = pair[1].text;
  1051. }
  1052. else if (pair[1].image) {
  1053. if (pair[1].image.indexOf(INLINE_RES) == 0) {
  1054. pair[1].image = SampleRessources[pair[1].image.slice(INLINE_RES.length)]
  1055. }
  1056. card2.style.backgroundImage = "url('" + pair[1].image + "')";
  1057. }
  1058. card2.style.width = parseInt(minSize / 2) + "px";
  1059. card2.style.height = parseInt(minSize / 2) + "px";
  1060. card2.style.textAlign = "center";
  1061. card2.style.lineHeight = card2.style.width + "";
  1062. card2.style.borderRadius = "6px";
  1063. card2.style.fontSize = parseInt(minSize / 8) + "px";
  1064. card2.style.border = "1px solid #000";
  1065. card2.style.color = "#fff";
  1066. d.appendChild(card1);
  1067. d.appendChild(card2);
  1068. return d;
  1069. }
  1070. function insertimagebuttonsFun(){
  1071. document.getElementsByClassName("insertImage")[0].onclick = function(){
  1072. var cards = [];
  1073. chooser.show(function (entry) {
  1074. // No selection
  1075. if (!entry) {
  1076. return;
  1077. }
  1078. // Get object content
  1079. var dataentry = new datastore.DatastoreObject(entry.objectId);
  1080. dataentry.loadAsText(function (err, metadata, text) {
  1081. //We load the drawing inside an image element
  1082. var element = document.createElement('img');
  1083. if (entry.metadata.activity == 'org.olpcfrance.PaintActivity') {
  1084. element.src = LZString.decompressFromUTF16(JSON.parse(text).src);
  1085. } else {
  1086. element.src = text;
  1087. }
  1088. element.onload = function () {
  1089. document.getElementsByClassName("textCard")[0].style.backgroundImage = 'url(' + element.src + ')';
  1090. if (MemorizeApp.editor.pairMode == MODE_EQUAL) {
  1091. MemorizeApp.editor.card1.image = element.src;
  1092. MemorizeApp.editor.card1.text = "";
  1093. MemorizeApp.editor.card2.image = element.src;
  1094. MemorizeApp.editor.card2.text = "";
  1095. }else{
  1096. MemorizeApp.editor.card1.image = element.src;
  1097. MemorizeApp.editor.card1.text = "";
  1098. }
  1099. var cards = [];
  1100. if (MemorizeApp.editor.pairMode == MODE_EQUAL) {
  1101. cards[0] = MemorizeApp.editor.card1;
  1102. cards[1] = MemorizeApp.editor.card1;
  1103. }
  1104. if (MemorizeApp.editor.pairMode == MODE_NON_EQUAL) {
  1105. cards[0] = MemorizeApp.editor.card1;
  1106. cards[1] = MemorizeApp.editor.card2;
  1107. }
  1108. if (!cards[0].text && !cards[0].image && !cards[0].sound) {
  1109. return;
  1110. }
  1111. if (!cards[1].text && !cards[1].image && !cards[1].sound) {
  1112. return;
  1113. }
  1114. cards = JSON.parse(JSON.stringify(cards));
  1115. if (MemorizeApp.editor.selectedPair > -1) {
  1116. MemorizeApp.game.template.cards[MemorizeApp.editor.selectedPair] = cards
  1117. }
  1118. saveGame();
  1119. displayEditor();
  1120. };
  1121. });
  1122. }, { mimetype: 'image/png' }, { mimetype: 'image/jpeg' }, { activity: 'org.olpcfrance.PaintActivity'});
  1123. }
  1124. if(document.getElementsByClassName("insertImage")[1]){
  1125. document.getElementsByClassName("insertImage")[1].onclick = function(){
  1126. var cards = [];
  1127. chooser.show(function (entry) {
  1128. // No selection
  1129. if (!entry) {
  1130. return;
  1131. }
  1132. // Get object content
  1133. var dataentry = new datastore.DatastoreObject(entry.objectId);
  1134. dataentry.loadAsText(function (err, metadata, text) {
  1135. //We load the drawing inside an image element
  1136. var element = document.createElement('img');
  1137. element.src = text;
  1138. element.onload = function () {
  1139. document.getElementsByClassName("textCard")[1].style.backgroundImage = 'url(' + element.src + ')';
  1140. if (MemorizeApp.editor.pairMode == MODE_EQUAL) {
  1141. MemorizeApp.editor.card1.image = element.src;
  1142. MemorizeApp.editor.card1.text = "";
  1143. MemorizeApp.editor.card2.image = element.src;
  1144. MemorizeApp.editor.card2.text = "";
  1145. }else{
  1146. MemorizeApp.editor.card2.image = element.src;
  1147. MemorizeApp.editor.card2.text = "";
  1148. }
  1149. var cards = [];
  1150. if (MemorizeApp.editor.pairMode == MODE_EQUAL) {
  1151. cards[0] = MemorizeApp.editor.card1;
  1152. cards[1] = MemorizeApp.editor.card1;
  1153. }
  1154. if (MemorizeApp.editor.pairMode == MODE_NON_EQUAL) {
  1155. cards[0] = MemorizeApp.editor.card1;
  1156. cards[1] = MemorizeApp.editor.card2;
  1157. }
  1158. if (!cards[0].text && !cards[0].image && !cards[0].sound) {
  1159. return;
  1160. }
  1161. if (!cards[1].text && !cards[1].image && !cards[1].sound) {
  1162. return;
  1163. }
  1164. cards = JSON.parse(JSON.stringify(cards));
  1165. if (MemorizeApp.editor.selectedPair > -1) {
  1166. MemorizeApp.game.template.cards[MemorizeApp.editor.selectedPair] = cards
  1167. }
  1168. saveGame();
  1169. displayEditor();
  1170. };
  1171. });
  1172. }, { mimetype: 'image/png' }, { mimetype: 'image/jpeg' });
  1173. }
  1174. }
  1175. }
  1176. function generateCardsList() {
  1177. var div = document.createElement("div");
  1178. var minSize = document.body.clientWidth;
  1179. if (minSize > document.body.clientHeight) {
  1180. minSize = document.body.clientHeight;
  1181. }
  1182. div.style.position = "fixed";
  1183. div.style.bottom = 0;
  1184. div.style.width = document.body.clientWidth + "px";
  1185. div.style.height = parseInt(minSize / 3) + "px";
  1186. div.style.padding = "10px";
  1187. div.style.paddingBottom = "20px";
  1188. div.style.marginTop = "7px";
  1189. div.style.background = "#eee";
  1190. div.style.overflowX = "auto";
  1191. div.style.whiteSpace = "nowrap"
  1192. div.style.overflowY = "hidden";
  1193. for (var i = 0; i < MemorizeApp.game.template.cards.length; i++) {
  1194. var card = MemorizeApp.game.template.cards[i];
  1195. var pair = generateCardFromCardsList(card, parseInt(minSize / 3), i);
  1196. pair.cards = card;
  1197. pair.index = i;
  1198. pair.addEventListener("click", function() {
  1199. MemorizeApp.editor.selectedPair = this.index;
  1200. MemorizeApp.editor.pairMode = MODE_NON_EQUAL;
  1201. MemorizeApp.editor.card1 = this.cards[0];
  1202. MemorizeApp.editor.card2 = this.cards[1];
  1203. displayEditor();
  1204. });
  1205. div.appendChild(pair);
  1206. }
  1207. var emptyDiv = document.createElement("div");
  1208. emptyDiv.style.height = parseInt(minSize/6)+"px";
  1209. emptyDiv.style.width = "30px";
  1210. emptyDiv.style.display = "inline-block";
  1211. div.appendChild(emptyDiv);
  1212. return div;
  1213. }
  1214. function generateClearBoth() {
  1215. var d = document.createElement("div");
  1216. d.style.clear = "both";
  1217. return d;
  1218. }
  1219. function displayEditor() {
  1220. MemorizeApp.ui.gameEditor.innerHTML = "";
  1221. if (MemorizeApp.editor.pairMode == MODE_EQUAL) {
  1222. MemorizeApp.ui.gameEditor.appendChild(generateEditorDiv(MemorizeApp.editor.card1));
  1223. }
  1224. if (MemorizeApp.editor.pairMode == MODE_NON_EQUAL) {
  1225. MemorizeApp.ui.gameEditor.appendChild(generateEditorDiv(MemorizeApp.editor.card1));
  1226. MemorizeApp.ui.gameEditor.appendChild(generateEditorDiv(MemorizeApp.editor.card2));
  1227. }
  1228. insertimagebuttonsFun();
  1229. MemorizeApp.ui.gameEditor.appendChild(generateAddEditRemoveButton());
  1230. MemorizeApp.ui.gameEditor.appendChild(generateClearBoth());
  1231. MemorizeApp.ui.gameEditor.appendChild(generateCardsList());
  1232. resizeTextInsideTextCardDivs()
  1233. }
  1234. function leaveEditMode() {
  1235. MemorizeApp.editor.card1 = {};
  1236. MemorizeApp.editor.card2 = {};
  1237. MemorizeApp.inEditMode = false;
  1238. MemorizeApp.ui.gameEditor.innerHTML = "";
  1239. MemorizeApp.ui.gameEditor.style.display = "none";
  1240. MemorizeApp.ui.gameGrid.style.display = "block";
  1241. /* Enable game buttons */
  1242. MemorizeApp.ui.gameTemplatesButton.disabled = false;
  1243. MemorizeApp.ui.gameTemplatesButton.style.opacity = 1;
  1244. MemorizeApp.ui.gameSizeButton.disabled = false;
  1245. MemorizeApp.ui.gameSizeButton.style.opacity = 1;
  1246. MemorizeApp.ui.gameResetButton.disabled = false;
  1247. MemorizeApp.ui.gameResetButton.style.opacity = 1;
  1248. /* Disable editor buttons */
  1249. MemorizeApp.ui.gameEditorInsertModeButton.disabled = true;
  1250. MemorizeApp.ui.gameEditorInsertModeButton.style.opacity = 0.3;
  1251. MemorizeApp.ui.gameEditorPlayModeButton.disabled = true;
  1252. MemorizeApp.ui.gameEditorPlayModeButton.style.opacity = 0.3;
  1253. MemorizeApp.ui.gameEditorClearButton.disabled = true;
  1254. MemorizeApp.ui.gameEditorClearButton.style.opacity = 0.3;
  1255. MemorizeApp.ui.gameEditorButton.style.backgroundImage = "url(icons/cog.svg)";
  1256. MemorizeApp.drawGame();
  1257. }
  1258. function disableEditMode() {
  1259. /* Disable All editor buttons */
  1260. MemorizeApp.ui.gameEditorButton.disabled = true;
  1261. MemorizeApp.ui.gameEditorButton.style.opacity = 0.3;
  1262. MemorizeApp.ui.gameEditorButton.style.backgroundImage = "url(icons/cog.svg)";
  1263. MemorizeApp.ui.gameEditorInsertModeButton.disabled = true;
  1264. MemorizeApp.ui.gameEditorInsertModeButton.style.opacity = 0.3;
  1265. MemorizeApp.ui.gameEditorPlayModeButton.disabled = true;
  1266. MemorizeApp.ui.gameEditorPlayModeButton.style.opacity = 0.3;
  1267. MemorizeApp.ui.gameEditorClearButton.disabled = true;
  1268. MemorizeApp.ui.gameEditorClearButton.style.opacity = 0.3;
  1269. }
  1270. var Base64Binary = {
  1271. _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
  1272. _arrayBufferToBase64: function (buffer) {
  1273. var binary = '';
  1274. var bytes = new Uint8Array(buffer);
  1275. var len = bytes.byteLength;
  1276. for (var i = 0; i < len; i++) {
  1277. binary += String.fromCharCode(bytes[i]);
  1278. }
  1279. return window.btoa(binary);
  1280. },
  1281. /* will return a Uint8Array type */
  1282. decodeArrayBuffer: function (input) {
  1283. var bytes = (input.length / 4) * 3;
  1284. var ab = new ArrayBuffer(bytes);
  1285. this.decode(input, ab);
  1286. return ab;
  1287. },
  1288. removePaddingChars: function (input) {
  1289. var lkey = this._keyStr.indexOf(input.charAt(input.length - 1));
  1290. if (lkey == 64) {
  1291. return input.substring(0, input.length - 1);
  1292. }
  1293. return input;
  1294. },
  1295. decode: function (input, arrayBuffer) {
  1296. //get last chars to see if are valid
  1297. input = this.removePaddingChars(input);
  1298. input = this.removePaddingChars(input);
  1299. var bytes = parseInt((input.length / 4) * 3, 10);
  1300. var uarray;
  1301. var chr1, chr2, chr3;
  1302. var enc1, enc2, enc3, enc4;
  1303. var i = 0;
  1304. var j = 0;
  1305. if (arrayBuffer)
  1306. uarray = new Uint8Array(arrayBuffer);
  1307. else
  1308. uarray = new Uint8Array(bytes);
  1309. input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
  1310. for (i = 0; i < bytes; i += 3) {
  1311. enc1 = this._keyStr.indexOf(input.charAt(j++));
  1312. enc2 = this._keyStr.indexOf(input.charAt(j++));
  1313. enc3 = this._keyStr.indexOf(input.charAt(j++));
  1314. enc4 = this._keyStr.indexOf(input.charAt(j++));
  1315. chr1 = (enc1 << 2) | (enc2 >> 4);
  1316. chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
  1317. chr3 = ((enc3 & 3) << 6) | enc4;
  1318. uarray[i] = chr1;
  1319. if (enc3 != 64) uarray[i + 1] = chr2;
  1320. if (enc4 != 64) uarray[i + 2] = chr3;
  1321. }
  1322. return uarray;
  1323. }
  1324. }
  1325. return MemorizeApp;
  1326. }
  1327. )
  1328. ;