cube-code.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /**
  2. * Copyright 2021, 2024 5 Mode
  3. *
  4. * This file is part of 5 Cube.
  5. *
  6. * 5 Cube 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. * 5 Cube 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 5 Cube. 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 curcube; // Current cube
  28. var _selectedcube = 0; // Selected cube
  29. var totcubes = 0;
  30. /*
  31. * myCube Class
  32. *
  33. * @param string myname Current name of the cube
  34. * @param array mymap Face map
  35. * @param string myformalName Formal name of the cube
  36. * @returns myCube
  37. */
  38. function myCube(myname, myformalName, myAPP_HOST) {
  39. // Examples of cube moves:
  40. // 0, 0, 1, 0, 0
  41. // 0, 0, 5, 0, 0 hcur = [3, 4]
  42. // 1, 2, 3, 4, 1 vcur = [3, 5] shift(hlist[1,4]); set jolly = hlist[4];
  43. // 0, 0, 6, 0, 0 jolly = 1
  44. // 0, 0, 1, 0, 0
  45. // 1 turn 1 face up
  46. // 0, 0, 5, 0, 0
  47. // 0, 0, 3, 0, 0 hcur = [6, 4]
  48. // 5, 2, 6, 4, 5 vcur = [6, 3] shift(vlist[1,4]); set jolly = vlist[4];
  49. // 0, 0, 1, 0, 0 jolly = 5
  50. // 0, 0, 5, 0, 0
  51. // 1 turn 1 face left
  52. // 0, 0, 4, 0, 0
  53. // 0, 0, 5, 0, 0 hcur = [2, 3]
  54. // 4, 1, 2, 3, 4 vcur = [2, 5] shift(hlist[1,4]); set jolly = hlist[4];
  55. // 0, 0, 6, 0, 0 jolly = 4
  56. // 0, 0, 4, 0, 0
  57. // 1 turn 1 face left
  58. // 0, 0, 3, 0, 0
  59. // 0, 0, 5, 0, 0 hcur = [1, 2]
  60. // 3, 4, 1, 2, 3 vcur = [1, 5] shift(hlist[1,4]); set jolly = hlist[4];
  61. // 0, 0, 6, 0, 0 jolly = 3
  62. // 0, 0, 3, 0, 0
  63. // 1 turn 1 face rght
  64. // 0, 0, 4, 0, 0
  65. // 0, 0, 5, 0, 0 hcur = [1, 2]
  66. // 4, 1, 2, 3, 4 vcur = [1, 5] shift(hlist[1,4]); set jolly = hlist[4];
  67. // 0, 0, 6, 0, 0 jolly = 3
  68. // 0, 0, 4, 0, 0
  69. // Properties
  70. this.name = myname;
  71. this.map = ["Address", "Contacts", "Other Info", "Menu", "Pictures", "Password"];
  72. this.formalName = myformalName;
  73. this.guid = "";
  74. this.password = "";
  75. this.xml = "";
  76. this.hcube = [0,1,2,3,0];
  77. this.vcube = [0,5,2,4,0];
  78. this.hlist = [1,2,3,0];
  79. this.vlist = [5,2,4,0];
  80. this.jolly = 0;
  81. this.hcur = [this.hcube[2], this.hcube[3]];
  82. this.vcur = [this.vcube[2], this.vcube[3]];
  83. // Methods
  84. this.updateCube = myupdateCube;
  85. this.turnLeft = myturnLeft;
  86. this.turnRight = myturnRight;
  87. this.turnUp = myturnUp;
  88. this.turnDown = myturnDown;
  89. this.getFace = mygetFace;
  90. this.gethcube = mygethcube;
  91. this.getvcube = mygetvcube;
  92. this.gethcur = mygethcur;
  93. this.getvcur = mygetvcur;
  94. this.getname = mygetname;
  95. this.getguid = mygetguid;
  96. this.getpassword = mygetpassword;
  97. this.getxml = mygetxml;
  98. this.savedata = mysavedata;
  99. this.start = mystart;
  100. // -- Methods End
  101. /*
  102. * Execute horizontal / vertical movement of the cube
  103. *
  104. * @param char Update direction
  105. * @returns void
  106. */
  107. function myupdateCube(tthis, upddir) {
  108. if (upddir=='h') {
  109. tthis.hcube[0] = tthis.jolly;
  110. for(i=1;i<=tthis.hlist.length;i++) {
  111. tthis.hcube[i] = tthis.hlist[i-1];
  112. }
  113. tthis.vcube[2] = tthis.hcube[2];
  114. tthis.vcube[0] = tthis.jolly;
  115. tthis.vcube[4] = tthis.jolly;
  116. } else {
  117. tthis.vcube[0] = tthis.jolly;
  118. for(i=1;i<=tthis.vlist.length;i++) {
  119. tthis.vcube[i] = tthis.vlist[i-1];
  120. }
  121. tthis.hcube[2] = tthis.vcube[2];
  122. tthis.hcube[0] = tthis.jolly;
  123. tthis.hcube[4] = tthis.jolly;
  124. }
  125. tthis.hcur = [tthis.hcube[2], tthis.hcube[3]];
  126. tthis.vcur = [tthis.vcube[2], tthis.vcube[3]];
  127. }
  128. /*
  129. * Move the current cube to the left
  130. *
  131. * @returns void
  132. */
  133. function myturnLeft() {
  134. var newhlist = [];
  135. var newfirst = this.hlist.pop();
  136. newhlist[0] = newfirst;
  137. newhlist[1] = this.hlist[0];
  138. newhlist[2] = this.hlist[1];
  139. newhlist[3] = this.hlist[2];
  140. this.hlist = newhlist;
  141. this.jolly = this.hlist[3];
  142. // Save the horizzontal movement..
  143. this.updateCube(this, 'h');
  144. }
  145. /*
  146. * Move the current cube to the right
  147. *
  148. * @returns void
  149. */
  150. function myturnRight() {
  151. var newhlist = [];
  152. var newlast = this.hlist.shift();
  153. newhlist[0] = this.hlist[0];
  154. newhlist[1] = this.hlist[1];
  155. newhlist[2] = this.hlist[2];
  156. newhlist[3] = newlast;
  157. this.hlist = newhlist;
  158. this.jolly = this.hlist[3];
  159. // Save the horizzontal movement..
  160. this.updateCube(this, 'h');
  161. }
  162. /*
  163. * Move the current cube upward
  164. *
  165. * @returns void
  166. */
  167. function myturnUp() {
  168. var newvlist = [];
  169. var newfirst = this.vlist.pop();
  170. newvlist[0] = newfirst;
  171. newvlist[1] = this.vlist[0];
  172. newvlist[2] = this.vlist[1];
  173. newvlist[3] = this.vlist[2];
  174. this.vlist = newvlist;
  175. this.jolly = this.vlist[3];
  176. // Save the vertical movement..
  177. this.updateCube(this, 'v');
  178. }
  179. /*
  180. * Move the current cube downward
  181. *
  182. * @returns void
  183. */
  184. function myturnDown() {
  185. var newvlist = [];
  186. var newlast = this.vlist.shift();
  187. newvlist[0] = this.vlist[0];
  188. newvlist[1] = this.vlist[1];
  189. newvlist[2] = this.vlist[2];
  190. newvlist[3] = newlast;
  191. this.vlist = newvlist;
  192. this.jolly = this.vlist[3];
  193. // Save the vertical movement..
  194. this.updateCube(this, 'v');
  195. }
  196. /*
  197. * Get the cube face associated to the given coordinates [v0|v1|h0|h1]
  198. *
  199. * @returns string
  200. */
  201. function mygetFace(iface) {
  202. // v0 = front
  203. // v1 = up
  204. // h0 = front
  205. // h2 = right
  206. switch (iface) {
  207. case "h0":
  208. return this.map[this.hcur[0]];
  209. break;
  210. case "h1":
  211. return this.map[this.hcur[1]];
  212. break;
  213. case "v0":
  214. return this.map[this.vcur[0]];
  215. break;
  216. case "v1":
  217. return this.map[this.vcur[1]];
  218. break;
  219. }
  220. }
  221. /*
  222. * Expose the current horizontal list of the cube, for debug purpose
  223. *
  224. * @returns string
  225. */
  226. function mygethcube() {
  227. return this.hcube.toString();
  228. }
  229. /*
  230. * Expose the current vertical list of the cube, for debug purpose
  231. *
  232. * @returns string
  233. */
  234. function mygetvcube() {
  235. return this.vcube.toString();
  236. }
  237. /*
  238. * Expose the current horizontal position of the cube, for debug purpose
  239. *
  240. * @returns string
  241. */
  242. function mygethcur() {
  243. return this.hcur.toString();
  244. }
  245. /*
  246. * Expose the current vertical position of the cube, for debug purpose
  247. *
  248. * @returns string
  249. */
  250. function mygetvcur() {
  251. return this.vcur.toString();
  252. }
  253. /*
  254. * Get the cube name
  255. *
  256. * @returns string
  257. */
  258. function mygetname() {
  259. return this.name;
  260. }
  261. /*
  262. * Save the cube data locally
  263. *
  264. * @returns void
  265. */
  266. function mysavedata() {
  267. //fileName = myformalName;
  268. var ffileName = this.formalName;
  269. var xxml = this.xml;
  270. $.ajax({
  271. method: "POST",
  272. async: false,
  273. url: "/putxml",
  274. dataType: "json",
  275. jsonp: false,
  276. data: {
  277. f: ffileName,
  278. xml: xxml
  279. },
  280. success: function( data ) {
  281. // Handle 'no match' indicated by [ "" ] response
  282. //response( data.length === 1 && data[0].length === 0 ? [] : data );
  283. if (data.length < 1 || data.length === 1) {
  284. alert("An error happened saving the data (#1)!");
  285. } else if (data.length === 2 && data[0] === 200) {
  286. //alert("Data saved successfully!");
  287. } else {
  288. alert("An error happened saving the data (#2)!");
  289. //alert(data[0]);
  290. //alert(data[1]);
  291. //alert(data[2]);
  292. //alert(data[3]);
  293. //alert(data[4]);
  294. }
  295. },
  296. error: function (responseData, textStatus, errorThrown) {
  297. alert("POST failed: " + errorThrown + " data: " + responseData.responseText);
  298. }
  299. });
  300. }
  301. /*
  302. * Cube init procedure:
  303. * - read the xml data associated with the cube
  304. *
  305. * @returns void
  306. */
  307. function mystart() {
  308. bConnectionOK = false;
  309. var xhttp = new XMLHttpRequest();
  310. var xmluri = "https://" + myAPP_HOST + "/getxml?f=" + this.formalName;
  311. //alert(xmluri);
  312. xhttp.open("GET", xmluri, true);
  313. xhttp.send();
  314. xhttp.onreadystatechange = function () {
  315. if (this.readyState == 4 && this.status == 200) {
  316. bConnectionOK = true;
  317. xml = this.responseText
  318. cubes[parseInt(myformalName.substr(4))-1].xml = xml;
  319. //$("#_read_xml_" + myformalName).val(xml);
  320. //alert(this.responseText);
  321. re = new RegExp("\<guid\>(.*)\<\/guid\>", "igsu");
  322. a = re.exec(xml);
  323. if (!a || a.length===0) {
  324. document.write("Data access error (#2)<br><br>");
  325. return;
  326. } else {
  327. //alert(a);
  328. cubes[parseInt(myformalName.substr(4))-1].guid = a[1];
  329. //alert(cubes[parseInt(myformalName.substr(4))-1].guid);
  330. }
  331. re = new RegExp("\<password\>(.*)\<\/password\>", "igsu");
  332. a = re.exec(xml);
  333. if (!a || a.length===0) {
  334. document.write("Data access error (#3)<br><br>");
  335. return;
  336. } else {
  337. //alert(a);
  338. cubes[parseInt(myformalName.substr(4))-1].password = a[1];
  339. //alert(cubes[parseInt(myformalName.substr(4))-1].guid);
  340. }
  341. } else {
  342. if ((this.readyState == 4 && this.status == 0) && (!bConnectionOK)) {
  343. document.write("Data access error (#1)<br><br>");
  344. }
  345. }
  346. }
  347. }
  348. /*
  349. * Get the guid of the cube
  350. *
  351. * @returns string
  352. */
  353. function mygetguid() {
  354. return this.guid;
  355. }
  356. /*
  357. * Get the editor password of the cube
  358. *
  359. * @returns string
  360. */
  361. function mygetpassword() {
  362. return this.password;
  363. }
  364. /*
  365. * Get the xml data associated with the cube
  366. *
  367. * @returns string
  368. */
  369. function mygetxml() {
  370. return this.xml;
  371. }
  372. }
  373. // -- End myCube class
  374. /*
  375. * Select a cube
  376. *
  377. * @param <interfaceEl> tthis selected cube
  378. * @returns void
  379. */
  380. function _selCube(tthis) {
  381. //alert($(tthis).attr("order"));
  382. var _cubeorder = parseInt($(tthis).attr("order"));
  383. _selectedcube = _cubeorder-1;
  384. curcube = cubes[_selectedcube];
  385. // Debug info..
  386. $("#curcube").val(curcube.getname());
  387. //alert("curcube="+_selectedcube);
  388. }
  389. /*
  390. function _fetchData() {
  391. data1_exists = $("#_read_xml_cube1").val() !== "";
  392. data2_exists = $("#_read_xml_cube2").val() !== "";
  393. data3_exists = $("#_read_xml_cube3").val() !== "";
  394. data4_exists = true;
  395. data5_exists = true;
  396. //data4_exists = $("#_read_xml_cube4").val() !== "";
  397. //data5_exists = $("#_read_xml_cube5").val() !== "";
  398. if (data1_exists &&
  399. data2_exists &&
  400. data3_exists &&
  401. data4_exists &&
  402. data5_exists ) {
  403. cubes[0].xml = $("#_read_xml_cube1").val();
  404. cubes[1].xml = $("#_read_xml_cube2").val();
  405. cubes[2].xml = $("#_read_xml_cube3").val();
  406. cubes[3].xml = $("#_read_xml_cube4").val();
  407. cubes[4].xml = $("#_read_xml_cube5").val();
  408. clearInterval(fetchDataIntervalId);
  409. } else {
  410. fetchDataIntervalId = setInterval("_fetchData()", 2000);
  411. }
  412. }*/