cube-code.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. /**
  2. * Copyright 2021, 2024 5 Mode
  3. *
  4. * This file is part of SnipSwap.
  5. *
  6. * SnipSwap 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. * SnipSwap 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 SnipSwap. 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.beauty = "";
  75. this.guid = "";
  76. this.password = "";
  77. this.cats = "";
  78. this.desc = "";
  79. this.title = "";
  80. this.label = "";
  81. this.link = "";
  82. this.xml = "";
  83. this.hcube = [0,1,2,3,0];
  84. this.vcube = [0,5,2,4,0];
  85. this.hlist = [1,2,3,0];
  86. this.vlist = [5,2,4,0];
  87. this.jolly = 0;
  88. this.hcur = [this.hcube[2], this.hcube[3]];
  89. this.vcur = [this.vcube[2], this.vcube[3]];
  90. // Methods
  91. this.updateCube = myupdateCube;
  92. this.turnLeft = myturnLeft;
  93. this.turnRight = myturnRight;
  94. this.turnUp = myturnUp;
  95. this.turnDown = myturnDown;
  96. this.getFace = mygetFace;
  97. this.gethcube = mygethcube;
  98. this.getvcube = mygetvcube;
  99. this.gethcur = mygethcur;
  100. this.getvcur = mygetvcur;
  101. this.getname = mygetname;
  102. this.getbeauty = mygetbeauty;
  103. this.getguid = mygetguid;
  104. this.getpassword = mygetpassword;
  105. this.getxml = mygetxml;
  106. this.savedata = mysavedata;
  107. this.start = mystart;
  108. // -- Methods End
  109. /*
  110. * Execute horizontal / vertical movement of the cube
  111. *
  112. * @param char Update direction
  113. * @returns void
  114. */
  115. function myupdateCube(tthis, upddir) {
  116. if (upddir=='h') {
  117. tthis.hcube[0] = tthis.jolly;
  118. for(i=1;i<=tthis.hlist.length;i++) {
  119. tthis.hcube[i] = tthis.hlist[i-1];
  120. }
  121. tthis.vcube[2] = tthis.hcube[2];
  122. tthis.vcube[0] = tthis.jolly;
  123. tthis.vcube[4] = tthis.jolly;
  124. } else {
  125. tthis.vcube[0] = tthis.jolly;
  126. for(i=1;i<=tthis.vlist.length;i++) {
  127. tthis.vcube[i] = tthis.vlist[i-1];
  128. }
  129. tthis.hcube[2] = tthis.vcube[2];
  130. tthis.hcube[0] = tthis.jolly;
  131. tthis.hcube[4] = tthis.jolly;
  132. }
  133. tthis.hcur = [tthis.hcube[2], tthis.hcube[3]];
  134. tthis.vcur = [tthis.vcube[2], tthis.vcube[3]];
  135. }
  136. /*
  137. * Move the current cube to the left
  138. *
  139. * @returns void
  140. */
  141. function myturnLeft() {
  142. var newhlist = [];
  143. var newfirst = this.hlist.pop();
  144. newhlist[0] = newfirst;
  145. newhlist[1] = this.hlist[0];
  146. newhlist[2] = this.hlist[1];
  147. newhlist[3] = this.hlist[2];
  148. this.hlist = newhlist;
  149. this.jolly = this.hlist[3];
  150. // Save the horizzontal movement..
  151. this.updateCube(this, 'h');
  152. }
  153. /*
  154. * Move the current cube to the right
  155. *
  156. * @returns void
  157. */
  158. function myturnRight() {
  159. var newhlist = [];
  160. var newlast = this.hlist.shift();
  161. newhlist[0] = this.hlist[0];
  162. newhlist[1] = this.hlist[1];
  163. newhlist[2] = this.hlist[2];
  164. newhlist[3] = newlast;
  165. this.hlist = newhlist;
  166. this.jolly = this.hlist[3];
  167. // Save the horizzontal movement..
  168. this.updateCube(this, 'h');
  169. }
  170. /*
  171. * Move the current cube upward
  172. *
  173. * @returns void
  174. */
  175. function myturnUp() {
  176. var newvlist = [];
  177. var newfirst = this.vlist.pop();
  178. newvlist[0] = newfirst;
  179. newvlist[1] = this.vlist[0];
  180. newvlist[2] = this.vlist[1];
  181. newvlist[3] = this.vlist[2];
  182. this.vlist = newvlist;
  183. this.jolly = this.vlist[3];
  184. // Save the vertical movement..
  185. this.updateCube(this, 'v');
  186. }
  187. /*
  188. * Move the current cube downward
  189. *
  190. * @returns void
  191. */
  192. function myturnDown() {
  193. var newvlist = [];
  194. var newlast = this.vlist.shift();
  195. newvlist[0] = this.vlist[0];
  196. newvlist[1] = this.vlist[1];
  197. newvlist[2] = this.vlist[2];
  198. newvlist[3] = newlast;
  199. this.vlist = newvlist;
  200. this.jolly = this.vlist[3];
  201. // Save the vertical movement..
  202. this.updateCube(this, 'v');
  203. }
  204. /*
  205. * Get the cube face associated to the given coordinates [v0|v1|h0|h1]
  206. *
  207. * @returns string
  208. */
  209. function mygetFace(iface) {
  210. // v0 = front
  211. // v1 = up
  212. // h0 = front
  213. // h2 = right
  214. switch (iface) {
  215. case "h0":
  216. return this.map[this.hcur[0]];
  217. break;
  218. case "h1":
  219. return this.map[this.hcur[1]];
  220. break;
  221. case "v0":
  222. return this.map[this.vcur[0]];
  223. break;
  224. case "v1":
  225. return this.map[this.vcur[1]];
  226. break;
  227. }
  228. }
  229. /*
  230. * Expose the current horizontal list of the cube, for debug purpose
  231. *
  232. * @returns string
  233. */
  234. function mygethcube() {
  235. return this.hcube.toString();
  236. }
  237. /*
  238. * Expose the current vertical list of the cube, for debug purpose
  239. *
  240. * @returns string
  241. */
  242. function mygetvcube() {
  243. return this.vcube.toString();
  244. }
  245. /*
  246. * Expose the current horizontal position of the cube, for debug purpose
  247. *
  248. * @returns string
  249. */
  250. function mygethcur() {
  251. return this.hcur.toString();
  252. }
  253. /*
  254. * Expose the current vertical position of the cube, for debug purpose
  255. *
  256. * @returns string
  257. */
  258. function mygetvcur() {
  259. return this.vcur.toString();
  260. }
  261. /*
  262. * Get the cube name
  263. *
  264. * @returns string
  265. */
  266. function mygetname() {
  267. return this.name;
  268. }
  269. /*
  270. * Save the cube data locally
  271. *
  272. * @returns void
  273. */
  274. function mysavedata() {
  275. //fileName = myformalName;
  276. var ffileName = this.formalName;
  277. var xxml = this.xml;
  278. //alert(ffileName);
  279. $.ajax({
  280. method: "POST",
  281. async: false,
  282. url: "/putxml",
  283. dataType: "json",
  284. jsonp: false,
  285. data: {
  286. f: ffileName,
  287. xml: xxml
  288. },
  289. success: function( data ) {
  290. // Handle 'no match' indicated by [ "" ] response
  291. //response( data.length === 1 && data[0].length === 0 ? [] : data );
  292. if (data.length < 1 || data.length === 1) {
  293. alert("An error happened saving the data (#1)!");
  294. } else if (data.length === 2 && data[0] === 200) {
  295. //alert("Data saved successfully!");
  296. } else {
  297. alert("An error happened saving the data (#2)!");
  298. //alert(data[0]);
  299. //alert(data[1]);
  300. //alert(data[2]);
  301. //alert(data[3]);
  302. //alert(data[4]);
  303. }
  304. },
  305. error: function (responseData, textStatus, errorThrown) {
  306. alert("POST failed: " + errorThrown + " data: " + responseData.responseText);
  307. }
  308. });
  309. }
  310. /*
  311. * Cube init procedure:
  312. * - read the xml data associated with the cube
  313. *
  314. * @returns void
  315. */
  316. function mystart() {
  317. bConnectionOK = false;
  318. var xhttp = new XMLHttpRequest();
  319. //alert(myname);
  320. var xmluri = "http://" + myAPP_HOST + "/getxml?f=" + this.formalName;
  321. //alert(xmluri);
  322. xhttp.open("GET", xmluri, true);
  323. xhttp.send();
  324. xhttp.onreadystatechange = function () {
  325. if (this.readyState == 4 && this.status == 200) {
  326. bConnectionOK = true;
  327. xml = this.responseText
  328. var z;
  329. var myObj;
  330. z = parseInt(myname.substr(5));
  331. //alert(z);
  332. myObj = cubes[z-1];
  333. myObj.xml = xml;
  334. re = new RegExp("\<beauty\>(.*)\<\/beauty\>", "igsu");
  335. a = re.exec(xml);
  336. if (!a || a.length===0) {
  337. document.write("Data access error (#1a)<br><br>");
  338. return;
  339. } else {
  340. myObj.beauty = a[1];
  341. }
  342. re = new RegExp("\<guid\>(.*)\<\/guid\>", "igsu");
  343. a = re.exec(xml);
  344. if (!a || a.length===0) {
  345. document.write("Data access error (#2)<br><br>");
  346. return;
  347. } else {
  348. myObj.guid = a[1];
  349. }
  350. re = new RegExp("\<password\>(.*)\<\/password\>", "igsu");
  351. a = re.exec(xml);
  352. if (!a || a.length===0) {
  353. document.write("Data access error (#3)<br><br>");
  354. return;
  355. } else {
  356. myObj.password = a[1];
  357. }
  358. re = new RegExp("\<cats\>(.*)\<\/cats\>", "igsu");
  359. a = re.exec(xml);
  360. if (!a || a.length===0) {
  361. document.write("Data access error (#4)<br><br>");
  362. return;
  363. } else {
  364. myObj.cats = a[1];
  365. }
  366. re = new RegExp("\<desc\>(.*)\<\/desc\>", "igsu");
  367. a = re.exec(xml);
  368. if (!a || a.length===0) {
  369. document.write("Data access error (#5)<br><br>");
  370. return;
  371. } else {
  372. myObj.desc = a[1];
  373. }
  374. re = new RegExp("\<title\>(.*)\<\/title\>", "igsu");
  375. a = re.exec(xml);
  376. if (!a || a.length===0) {
  377. document.write("Data access error (#6)<br><br>");
  378. return;
  379. } else {
  380. myObj.title = a[1];
  381. }
  382. re = new RegExp("\<label\>(.*)\<\/label\>", "igsu");
  383. a = re.exec(xml);
  384. if (!a || a.length===0) {
  385. document.write("Data access error (#7)<br><br>");
  386. return;
  387. } else {
  388. myObj.label = a[1];
  389. }
  390. re = new RegExp("\<link\>(.*)\<\/link\>", "igsu");
  391. a = re.exec(xml);
  392. if (!a || a.length===0) {
  393. document.write("Data access error (#8)<br><br>");
  394. return;
  395. } else {
  396. myObj.link = a[1];
  397. }
  398. cubes[z-1] = myObj;
  399. } else {
  400. if ((this.readyState == 4 && this.status == 0) && (!bConnectionOK)) {
  401. document.write("Data access error (#1)<br><br>");
  402. }
  403. }
  404. }
  405. //if (offers[0]) {
  406. // alert(offers[0].xml);
  407. //}
  408. }
  409. /*
  410. * Get the beauty of the cube
  411. *
  412. * @returns string
  413. */
  414. function mygetbeauty() {
  415. return this.beauty;
  416. }
  417. /*
  418. * Get the guid of the cube
  419. *
  420. * @returns string
  421. */
  422. function mygetguid() {
  423. return this.guid;
  424. }
  425. /*
  426. * Get the editor password of the cube
  427. *
  428. * @returns string
  429. */
  430. function mygetpassword() {
  431. return this.password;
  432. }
  433. /*
  434. * Get the xml data associated with the cube
  435. *
  436. * @returns string
  437. */
  438. function mygetxml() {
  439. return this.xml;
  440. }
  441. }
  442. // -- End myCube class
  443. /*
  444. * Select a cube
  445. *
  446. * @param <interfaceEl> tthis selected cube
  447. * @returns void
  448. */
  449. function _selCube(tthis) {
  450. //alert($(tthis).attr("order"));
  451. var _cubeorder = parseInt($(tthis).attr("order"));
  452. _selectedcube = _cubeorder-1;
  453. curcube = cubes[_selectedcube];
  454. //curcube.start();
  455. }
  456. function updCubeXML(xmlStr, nodeName, newVal) {
  457. switch (nodeName) {
  458. case "title":
  459. re = /(\<title>).*(\<\/title>)/gs;
  460. break;
  461. case "desc":
  462. re = /(\<desc>).*(\<\/desc>)/gs;
  463. break;
  464. case "code":
  465. re = /(\<code>\<\!\[CDATA\[).*(\]\]\>\<\/code>)/gs;
  466. break;
  467. case "tags":
  468. re = /(\<tags>).*(\<\/tags>)/gs;
  469. break;
  470. case "cats":
  471. re = /(\<cats>).*(\<\/cats>)/gs;
  472. break;
  473. case "label":
  474. re = /(\<label>).*(\<\/label>)/gs;
  475. break;
  476. case "link":
  477. re = /(\<link>).*(\<\/link>)/gs;
  478. break;
  479. case "email":
  480. re = /(\<email>).*(\<\/email>)/gs;
  481. break;
  482. }
  483. xmlStr = xmlStr.replace(re, "$1" + newVal + "$2");
  484. return xmlStr;
  485. }