cube-code.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. /**
  2. * Copyright 2021, 2024 5 Mode
  3. *
  4. * This file is part of MacSwap.
  5. *
  6. * MacSwap is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * MacSwap is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with MacSwap. If not, see <https://www.gnu.org/licenses/>.
  18. *
  19. * cube-code.js
  20. *
  21. * Cube Code for home.php.
  22. *
  23. * @author Daniele Bonini <my25mb@aol.com>
  24. * @copyrights (c) 2021, 2024, 5 Mode
  25. */
  26. var cubes = []; // Cube array
  27. var offers = []; // Cube array
  28. var curcube; // Current cube
  29. var _selectedcube = 0; // Selected cube
  30. var totcubes = 0;
  31. /*
  32. * myCube Class
  33. *
  34. * @param string myname Current name of the cube
  35. * @param array mymap Face map
  36. * @param string myformalName Formal name of the cube
  37. * @returns myCube
  38. */
  39. function myCube(myname, myformalName, myAPP_HOST) {
  40. // Examples of cube moves:
  41. // 0, 0, 1, 0, 0
  42. // 0, 0, 5, 0, 0 hcur = [3, 4]
  43. // 1, 2, 3, 4, 1 vcur = [3, 5] shift(hlist[1,4]); set jolly = hlist[4];
  44. // 0, 0, 6, 0, 0 jolly = 1
  45. // 0, 0, 1, 0, 0
  46. // 1 turn 1 face up
  47. // 0, 0, 5, 0, 0
  48. // 0, 0, 3, 0, 0 hcur = [6, 4]
  49. // 5, 2, 6, 4, 5 vcur = [6, 3] shift(vlist[1,4]); set jolly = vlist[4];
  50. // 0, 0, 1, 0, 0 jolly = 5
  51. // 0, 0, 5, 0, 0
  52. // 1 turn 1 face left
  53. // 0, 0, 4, 0, 0
  54. // 0, 0, 5, 0, 0 hcur = [2, 3]
  55. // 4, 1, 2, 3, 4 vcur = [2, 5] shift(hlist[1,4]); set jolly = hlist[4];
  56. // 0, 0, 6, 0, 0 jolly = 4
  57. // 0, 0, 4, 0, 0
  58. // 1 turn 1 face left
  59. // 0, 0, 3, 0, 0
  60. // 0, 0, 5, 0, 0 hcur = [1, 2]
  61. // 3, 4, 1, 2, 3 vcur = [1, 5] shift(hlist[1,4]); set jolly = hlist[4];
  62. // 0, 0, 6, 0, 0 jolly = 3
  63. // 0, 0, 3, 0, 0
  64. // 1 turn 1 face rght
  65. // 0, 0, 4, 0, 0
  66. // 0, 0, 5, 0, 0 hcur = [1, 2]
  67. // 4, 1, 2, 3, 4 vcur = [1, 5] shift(hlist[1,4]); set jolly = hlist[4];
  68. // 0, 0, 6, 0, 0 jolly = 3
  69. // 0, 0, 4, 0, 0
  70. // Properties
  71. this.name = myname;
  72. this.map = ["Address", "Contacts", "Other Info", "Menu", "Pictures", "Password"];
  73. this.formalName = myformalName;
  74. this.guid = "";
  75. this.password = "";
  76. this.xml = "";
  77. this.hcube = [0,1,2,3,0];
  78. this.vcube = [0,5,2,4,0];
  79. this.hlist = [1,2,3,0];
  80. this.vlist = [5,2,4,0];
  81. this.jolly = 0;
  82. this.hcur = [this.hcube[2], this.hcube[3]];
  83. this.vcur = [this.vcube[2], this.vcube[3]];
  84. // Methods
  85. this.updateCube = myupdateCube;
  86. this.turnLeft = myturnLeft;
  87. this.turnRight = myturnRight;
  88. this.turnUp = myturnUp;
  89. this.turnDown = myturnDown;
  90. this.getFace = mygetFace;
  91. this.gethcube = mygethcube;
  92. this.getvcube = mygetvcube;
  93. this.gethcur = mygethcur;
  94. this.getvcur = mygetvcur;
  95. this.getname = mygetname;
  96. this.getguid = mygetguid;
  97. this.getpassword = mygetpassword;
  98. this.getxml = mygetxml;
  99. this.savedata = mysavedata;
  100. this.start = mystart;
  101. // -- Methods End
  102. /*
  103. * Execute horizontal / vertical movement of the cube
  104. *
  105. * @param char Update direction
  106. * @returns void
  107. */
  108. function myupdateCube(tthis, upddir) {
  109. if (upddir=='h') {
  110. tthis.hcube[0] = tthis.jolly;
  111. for(i=1;i<=tthis.hlist.length;i++) {
  112. tthis.hcube[i] = tthis.hlist[i-1];
  113. }
  114. tthis.vcube[2] = tthis.hcube[2];
  115. tthis.vcube[0] = tthis.jolly;
  116. tthis.vcube[4] = tthis.jolly;
  117. } else {
  118. tthis.vcube[0] = tthis.jolly;
  119. for(i=1;i<=tthis.vlist.length;i++) {
  120. tthis.vcube[i] = tthis.vlist[i-1];
  121. }
  122. tthis.hcube[2] = tthis.vcube[2];
  123. tthis.hcube[0] = tthis.jolly;
  124. tthis.hcube[4] = tthis.jolly;
  125. }
  126. tthis.hcur = [tthis.hcube[2], tthis.hcube[3]];
  127. tthis.vcur = [tthis.vcube[2], tthis.vcube[3]];
  128. }
  129. /*
  130. * Move the current cube to the left
  131. *
  132. * @returns void
  133. */
  134. function myturnLeft() {
  135. var newhlist = [];
  136. var newfirst = this.hlist.pop();
  137. newhlist[0] = newfirst;
  138. newhlist[1] = this.hlist[0];
  139. newhlist[2] = this.hlist[1];
  140. newhlist[3] = this.hlist[2];
  141. this.hlist = newhlist;
  142. this.jolly = this.hlist[3];
  143. // Save the horizzontal movement..
  144. this.updateCube(this, 'h');
  145. }
  146. /*
  147. * Move the current cube to the right
  148. *
  149. * @returns void
  150. */
  151. function myturnRight() {
  152. var newhlist = [];
  153. var newlast = this.hlist.shift();
  154. newhlist[0] = this.hlist[0];
  155. newhlist[1] = this.hlist[1];
  156. newhlist[2] = this.hlist[2];
  157. newhlist[3] = newlast;
  158. this.hlist = newhlist;
  159. this.jolly = this.hlist[3];
  160. // Save the horizzontal movement..
  161. this.updateCube(this, 'h');
  162. }
  163. /*
  164. * Move the current cube upward
  165. *
  166. * @returns void
  167. */
  168. function myturnUp() {
  169. var newvlist = [];
  170. var newfirst = this.vlist.pop();
  171. newvlist[0] = newfirst;
  172. newvlist[1] = this.vlist[0];
  173. newvlist[2] = this.vlist[1];
  174. newvlist[3] = this.vlist[2];
  175. this.vlist = newvlist;
  176. this.jolly = this.vlist[3];
  177. // Save the vertical movement..
  178. this.updateCube(this, 'v');
  179. }
  180. /*
  181. * Move the current cube downward
  182. *
  183. * @returns void
  184. */
  185. function myturnDown() {
  186. var newvlist = [];
  187. var newlast = this.vlist.shift();
  188. newvlist[0] = this.vlist[0];
  189. newvlist[1] = this.vlist[1];
  190. newvlist[2] = this.vlist[2];
  191. newvlist[3] = newlast;
  192. this.vlist = newvlist;
  193. this.jolly = this.vlist[3];
  194. // Save the vertical movement..
  195. this.updateCube(this, 'v');
  196. }
  197. /*
  198. * Get the cube face associated to the given coordinates [v0|v1|h0|h1]
  199. *
  200. * @returns string
  201. */
  202. function mygetFace(iface) {
  203. // v0 = front
  204. // v1 = up
  205. // h0 = front
  206. // h2 = right
  207. switch (iface) {
  208. case "h0":
  209. return this.map[this.hcur[0]];
  210. break;
  211. case "h1":
  212. return this.map[this.hcur[1]];
  213. break;
  214. case "v0":
  215. return this.map[this.vcur[0]];
  216. break;
  217. case "v1":
  218. return this.map[this.vcur[1]];
  219. break;
  220. }
  221. }
  222. /*
  223. * Expose the current horizontal list of the cube, for debug purpose
  224. *
  225. * @returns string
  226. */
  227. function mygethcube() {
  228. return this.hcube.toString();
  229. }
  230. /*
  231. * Expose the current vertical list of the cube, for debug purpose
  232. *
  233. * @returns string
  234. */
  235. function mygetvcube() {
  236. return this.vcube.toString();
  237. }
  238. /*
  239. * Expose the current horizontal position of the cube, for debug purpose
  240. *
  241. * @returns string
  242. */
  243. function mygethcur() {
  244. return this.hcur.toString();
  245. }
  246. /*
  247. * Expose the current vertical position of the cube, for debug purpose
  248. *
  249. * @returns string
  250. */
  251. function mygetvcur() {
  252. return this.vcur.toString();
  253. }
  254. /*
  255. * Get the cube name
  256. *
  257. * @returns string
  258. */
  259. function mygetname() {
  260. return this.name;
  261. }
  262. /*
  263. * Save the cube data locally
  264. *
  265. * @returns void
  266. */
  267. function mysavedata() {
  268. //fileName = myformalName;
  269. var ffileName = this.formalName;
  270. var xxml = this.xml;
  271. //alert(ffileName);
  272. $.ajax({
  273. method: "POST",
  274. async: false,
  275. url: "/putxml",
  276. dataType: "json",
  277. jsonp: false,
  278. data: {
  279. f: ffileName,
  280. xml: xxml
  281. },
  282. success: function( data ) {
  283. // Handle 'no match' indicated by [ "" ] response
  284. //response( data.length === 1 && data[0].length === 0 ? [] : data );
  285. if (data.length < 1 || data.length === 1) {
  286. alert("An error happened saving the data (#1)!");
  287. } else if (data.length === 2 && data[0] === 200) {
  288. //alert("Data saved successfully!");
  289. } else {
  290. alert("An error happened saving the data (#2)!");
  291. //alert(data[0]);
  292. //alert(data[1]);
  293. //alert(data[2]);
  294. //alert(data[3]);
  295. //alert(data[4]);
  296. }
  297. },
  298. error: function (responseData, textStatus, errorThrown) {
  299. alert("POST failed: " + errorThrown + " data: " + responseData.responseText);
  300. }
  301. });
  302. }
  303. /*
  304. * Cube init procedure:
  305. * - read the xml data associated with the cube
  306. *
  307. * @returns void
  308. */
  309. function mystart() {
  310. bConnectionOK = false;
  311. var xhttp = new XMLHttpRequest();
  312. //alert(this.formalName);
  313. var xmluri = "https://" + myAPP_HOST + "/getxml?f=" + this.formalName;
  314. //alert(xmluri);
  315. xhttp.open("GET", xmluri, true);
  316. xhttp.send();
  317. xhttp.onreadystatechange = function () {
  318. if (this.readyState == 4 && this.status == 200) {
  319. bConnectionOK = true;
  320. xml = this.responseText
  321. var z;
  322. var myObj;
  323. var bOffer;
  324. if (myformalName.indexOf("-o-")>-1) {
  325. // case of an offer
  326. k = parseInt(myformalName.substr(6));
  327. z = parseInt(myformalName.substr(12));
  328. myObj = offers[k-1][z-1];
  329. bOffer = true;
  330. } else {
  331. z = parseInt(myformalName.substr(6));
  332. myObj = cubes[z-1];
  333. bOffer = false;
  334. }
  335. myObj.xml = xml;
  336. re = new RegExp("\<guid\>(.*)\<\/guid\>", "igsu");
  337. a = re.exec(xml);
  338. if (!a || a.length===0) {
  339. document.write("Data access error (#2)<br><br>");
  340. return;
  341. } else {
  342. myObj.guid = a[1];
  343. }
  344. re = new RegExp("\<password\>(.*)\<\/password\>", "igsu");
  345. a = re.exec(xml);
  346. if (!a || a.length===0) {
  347. document.write("Data access error (#3)<br><br>");
  348. return;
  349. } else {
  350. myObj.password = a[1]
  351. }
  352. if (bOffer) {
  353. offers[k-1][z-1] = myObj;
  354. } else {
  355. cubes[z-1] = myObj;
  356. }
  357. /*
  358. cubes[z-1].xml = xml;
  359. re = new RegExp("\<guid\>(.*)\<\/guid\>", "igsu");
  360. a = re.exec(xml);
  361. if (!a || a.length===0) {
  362. document.write("Data access error (#2)<br><br>");
  363. return;
  364. } else {
  365. //alert(a);
  366. cubes[z-1].guid = a[1];
  367. }
  368. re = new RegExp("\<password\>(.*)\<\/password\>", "igsu");
  369. a = re.exec(xml);
  370. if (!a || a.length===0) {
  371. document.write("Data access error (#3)<br><br>");
  372. return;
  373. } else {
  374. //alert(a);
  375. cubes[z-1].password = a[1];
  376. }
  377. */
  378. } else {
  379. if ((this.readyState == 4 && this.status == 0) && (!bConnectionOK)) {
  380. document.write("Data access error (#1)<br><br>");
  381. }
  382. }
  383. }
  384. //if (offers[0]) {
  385. // alert(offers[0].xml);
  386. //}
  387. }
  388. /*
  389. * Get the guid of the cube
  390. *
  391. * @returns string
  392. */
  393. function mygetguid() {
  394. return this.guid;
  395. }
  396. /*
  397. * Get the editor password of the cube
  398. *
  399. * @returns string
  400. */
  401. function mygetpassword() {
  402. return this.password;
  403. }
  404. /*
  405. * Get the xml data associated with the cube
  406. *
  407. * @returns string
  408. */
  409. function mygetxml() {
  410. return this.xml;
  411. }
  412. }
  413. // -- End myCube class
  414. /*
  415. * Select a cube
  416. *
  417. * @param <interfaceEl> tthis selected cube
  418. * @returns void
  419. */
  420. function _selCube(tthis) {
  421. //alert($(tthis).attr("order"));
  422. var _cubeorder = parseInt($(tthis).attr("order"));
  423. _selectedcube = _cubeorder-1;
  424. curcube = cubes[_selectedcube];
  425. // Debug info..
  426. $("#curcube").val(curcube.getname());
  427. //alert("curcube="+_selectedcube);
  428. }
  429. /*
  430. * Select a cube offer
  431. *
  432. * @param <interfaceEl> tthis selected cube
  433. * @returns void
  434. */
  435. function _selOfferCube(tthis) {
  436. //alert($(tthis).attr("order"));
  437. var _cubeorder = parseInt($(tthis).attr("cubeorder"));
  438. var _offerorder = parseInt($(tthis).attr("order"));
  439. _selectedcube = _cubeorder-1;
  440. _selectedoffer = _offerorder-1;
  441. curcube = offers[_selectedcube][_selectedoffer];
  442. //alert(curcube.getxml());
  443. // Debug info..
  444. $("#curcube").val(curcube.getname());
  445. //alert("curcube="+_selectedcube);
  446. }
  447. /*
  448. function _fetchData() {
  449. data1_exists = $("#_read_xml_cube1").val() !== "";
  450. data2_exists = $("#_read_xml_cube2").val() !== "";
  451. data3_exists = $("#_read_xml_cube3").val() !== "";
  452. data4_exists = true;
  453. data5_exists = true;
  454. //data4_exists = $("#_read_xml_cube4").val() !== "";
  455. //data5_exists = $("#_read_xml_cube5").val() !== "";
  456. if (data1_exists &&
  457. data2_exists &&
  458. data3_exists &&
  459. data4_exists &&
  460. data5_exists ) {
  461. cubes[0].xml = $("#_read_xml_cube1").val();
  462. cubes[1].xml = $("#_read_xml_cube2").val();
  463. cubes[2].xml = $("#_read_xml_cube3").val();
  464. cubes[3].xml = $("#_read_xml_cube4").val();
  465. cubes[4].xml = $("#_read_xml_cube5").val();
  466. clearInterval(fetchDataIntervalId);
  467. } else {
  468. fetchDataIntervalId = setInterval("_fetchData()", 2000);
  469. }
  470. }*/