index-app.js 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  1. import page from '/lib/page.mjs';
  2. class IndexApp {
  3. constructor() {
  4. console.log("app constructor");
  5. this.worlds = {};
  6. this.language = _LangManager.language;
  7. this.options = {
  8. query: 'pathname=' + window.location.pathname.slice(1,
  9. window.location.pathname.lastIndexOf("/")),
  10. secure: window.location.protocol === "https:",
  11. reconnection: false,
  12. path: '',
  13. transports: ['websocket']
  14. }
  15. //window.location.host
  16. var socket = io.connect(window._app.reflectorHost, this.options);
  17. const parse = (msg) => {
  18. this.parseAppInstancesData(msg)
  19. }
  20. socket.on('getWebAppUpdate', msg => parse.call(self, msg));
  21. socket.on("connect", function () {
  22. let noty = new Noty({
  23. text: 'Connected to Reflector!',
  24. timeout: 2000,
  25. theme: 'mint',
  26. layout: 'bottomRight',
  27. type: 'success'
  28. });
  29. noty.show();
  30. })
  31. socket.on('connect_error', function (err) {
  32. console.log(err);
  33. var errDiv = document.createElement("div");
  34. errDiv.innerHTML = "<div class='vwf-err' style='z-index: 10; position: absolute; top: 80px; right: 50px'>Connection error to Reflector!" + err + "</div>";
  35. document.querySelector('body').appendChild(errDiv);
  36. let noty = new Noty({
  37. text: 'Connection error to Reflector! ' + err,
  38. theme: 'mint',
  39. layout: 'bottomRight',
  40. type: 'error'
  41. });
  42. noty.show();
  43. });
  44. }
  45. async generateFrontPage() {
  46. let infoEl = document.createElement("div");
  47. infoEl.setAttribute("id", "info");
  48. let lang = _LangManager.locale;
  49. let infoElHTML = await _app.helpers.getHtmlText('/web/locale/' + lang + '/index.html');
  50. infoEl.innerHTML = infoElHTML;
  51. document.body.appendChild(infoEl);
  52. document.querySelector('#ruLang').addEventListener('click', function (e) {
  53. _LangManager.locale = 'ru';
  54. window.location.reload(true);
  55. });
  56. document.querySelector('#enLang').addEventListener('click', function (e) {
  57. _LangManager.locale = 'en';
  58. window.location.reload(true);
  59. });
  60. }
  61. async initApp() {
  62. let appEl = document.createElement("div");
  63. appEl.setAttribute("id", "app");
  64. let appElHTML = await _app.helpers.getHtmlText('/web/app.html');
  65. appEl.innerHTML = appElHTML;
  66. document.body.appendChild(appEl);
  67. this.initUser();
  68. this.initUserGUI();
  69. this.initWorldsListGUI();
  70. //this.getAppDetailsFromDB();
  71. }
  72. initUser() {
  73. _LCSDB.on('auth', (ack) => {
  74. if (ack.pub) {
  75. let alias = _LCSUSER.is.alias;
  76. let userEl = document.querySelector('#userGUI');
  77. userEl._status = 'Welcome ' + alias + '!';
  78. //userEl.style.backgroundColor = '#e6e6e6';
  79. userEl.$update();
  80. document.querySelector('#worldGUI').$update();
  81. document.querySelector('#main').$update();
  82. _LCSUSER.get('profile').once(function (data) { console.log(data) })
  83. var el = document.getElementById("loginGUI");
  84. if (el) {
  85. el.remove();
  86. }
  87. _LCSUSER.get('profile').not(function (key) {
  88. let profile = { 'alias': alias };
  89. _LCSUSER.get('profile').put(profile);
  90. })
  91. new Noty({
  92. text: alias + ' is succesfully authenticated!',
  93. timeout: 2000,
  94. theme: 'mint',
  95. layout: 'bottomRight',
  96. type: 'success'
  97. }).show();
  98. //this.getAppDetailsFromUserDB();
  99. }
  100. console.log(_LCSUSER.is);
  101. });
  102. }
  103. initUserGUI() {
  104. let worldGUI =
  105. {
  106. $type: "div",
  107. id: "worldGUI",
  108. class: "mdc-layout-grid mdc-layout-grid--align-left",
  109. _status: '',
  110. $init: function () {
  111. this._status = "init";
  112. },
  113. $update: function () {
  114. let guiForAll = [
  115. window._app.widgets.buttonStroked(
  116. {
  117. "label": 'Default World Protos',
  118. "onclick": function (e) {
  119. e.preventDefault();
  120. _app.indexApp.getAppDetailsFromDefaultDB('protos');
  121. }
  122. }),
  123. window._app.widgets.buttonStroked(
  124. {
  125. "label": 'Default World States',
  126. "onclick": function (e) {
  127. e.preventDefault();
  128. _app.indexApp.getAppDetailsFromDefaultDB('states');
  129. }
  130. })
  131. ];
  132. var guiUser = [];
  133. if (_LCSUSER.is) {
  134. guiUser = []
  135. }
  136. this.$components = [
  137. {
  138. $type: "h1",
  139. class: "mdc-typography--headline4",
  140. $text: "Worlds list"
  141. }
  142. ].concat(guiForAll).concat(guiUser)
  143. }
  144. }
  145. let userGUI =
  146. {
  147. $type: "div",
  148. id: "userGUI",
  149. // style:"background-color: #ffeb3b",
  150. class: "mdc-layout-grid mdc-layout-grid--align-left",
  151. _status: "",
  152. $init: function () {
  153. this._status = "Welcome!"
  154. },
  155. $update: function () {
  156. var gui = {};
  157. if (_LCSUSER.is) {
  158. gui = [
  159. window._app.widgets.buttonRaised(
  160. {
  161. "label": 'Sign OUT',
  162. "onclick": function (e) {
  163. _LCSUSER.leave().then(ack => {
  164. if (ack.ok == 0) {
  165. window.sessionStorage.removeItem('alias');
  166. window.sessionStorage.removeItem('tmp');
  167. window.location.reload(true);
  168. }
  169. });
  170. }
  171. }),
  172. {
  173. $type: "p"
  174. },
  175. window._app.widgets.buttonStroked(
  176. {
  177. "label": 'PROFILE',
  178. "onclick": function (e) {
  179. e.preventDefault();
  180. window.location.pathname = "/profile"
  181. }
  182. }),
  183. {
  184. $type: "p"
  185. },
  186. window._app.widgets.buttonStroked(
  187. {
  188. "label": 'My World protos',
  189. "onclick": function (e) {
  190. e.preventDefault();
  191. let alias = _LCSUSER.is.alias;
  192. page.redirect('/' + alias + '/worlds/protos');
  193. //_app.indexApp.getWorldsProtosFromUserDB(alias);
  194. }
  195. }),
  196. window._app.widgets.buttonStroked(
  197. {
  198. "label": 'My World states',
  199. "onclick": function (e) {
  200. e.preventDefault();
  201. let alias = _LCSUSER.is.alias;
  202. page.redirect('/' + alias + '/worlds/states');
  203. //_app.indexApp.getWorldsFromUserDB(alias);
  204. }
  205. })
  206. ]
  207. }
  208. this.$components = [
  209. window._app.widgets.buttonRaised(
  210. {
  211. "label": 'Connection settings',
  212. "onclick": function (e) {
  213. e.preventDefault();
  214. window.location.pathname = '/settings';
  215. }
  216. }),
  217. {
  218. $type: "h1",
  219. class: "mdc-typography--headline3",
  220. $text: this._status
  221. }
  222. ].concat(gui)
  223. }
  224. }
  225. let loginGUI =
  226. {
  227. $type: "div",
  228. id: "loginGUI",
  229. //style:"background-color: #efefef",
  230. class: "mdc-layout-grid mdc-layout-grid--align-left",
  231. _alias: null,
  232. _pass: null,
  233. _passField: null,
  234. _aliasField: null,
  235. _initData: function () {
  236. this._alias = '';
  237. this._pass = '';
  238. // if (window.sessionStorage.getItem('alias')) {
  239. // this._alias = window.sessionStorage.getItem('alias')
  240. // }
  241. // if (window.sessionStorage.getItem('tmp')) {
  242. // this._pass = window.sessionStorage.getItem('tmp')
  243. // }
  244. },
  245. $init: function () {
  246. this._initData();
  247. },
  248. $update: function () {
  249. this.$components = [
  250. {
  251. $type: "div",
  252. class: "mdc-layout-grid__inner",
  253. $components: [
  254. {
  255. $type: "div",
  256. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-12",
  257. $components: [
  258. {
  259. $type: "span",
  260. class: "mdc-typography--headline5",
  261. $text: "Login: "
  262. },
  263. window._app.widgets.inputTextFieldOutlined({
  264. "id": 'aliasInput',
  265. "label": "Login",
  266. "value": this._alias,
  267. "type": "text",
  268. "init": function() {
  269. this._aliasField = new mdc.textField.MDCTextField(this);
  270. }
  271. }),
  272. ]
  273. },
  274. {
  275. $type: "div",
  276. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-12",
  277. $components: [
  278. {
  279. $type: "span",
  280. class: "mdc-typography--headline5",
  281. $text: "Password: "
  282. },
  283. window._app.widgets.inputTextFieldOutlined({
  284. "id": 'passwordInput',
  285. "label": "Password",
  286. "value": this._pass,
  287. "type": "password",
  288. "init": function() {
  289. this._passField = new mdc.textField.MDCTextField(this);
  290. }
  291. }),
  292. ]
  293. },
  294. {
  295. $type: "div",
  296. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-12",
  297. $components: [
  298. window._app.widgets.buttonRaised(
  299. {
  300. "label": 'Sign UP',
  301. "onclick": function (e) {
  302. e.preventDefault();
  303. let alias = this._aliasField.value;
  304. let pass = this._passField.value
  305. if (pass.length < 7) {
  306. new Noty({
  307. text: "Your passphrase needs to be longer than 7 letters",
  308. timeout: 2000,
  309. theme: 'mint',
  310. layout: 'bottomRight',
  311. type: 'error'
  312. }).show();
  313. } else {
  314. //
  315. _LCSUSER.create(alias, pass,
  316. (ack) => {
  317. if (!ack.wait) { }
  318. if (ack.err) {
  319. console.log(ack.err)
  320. return ack.err
  321. };
  322. if (ack.pub) {
  323. _LCSUSER.auth(alias, pass);
  324. _LCSDB.get('users').get(alias).put({
  325. 'alias': alias,
  326. 'pub': ack.pub
  327. });
  328. }
  329. });
  330. }
  331. }
  332. }),
  333. {
  334. $type: "span",
  335. $text: " "
  336. },
  337. window._app.widgets.buttonRaised(
  338. {
  339. "label": 'Sign IN',
  340. "onclick": function (e) {
  341. e.preventDefault();
  342. _LCSUSER.auth(this._aliasField.value, this._passField.value, ack => {
  343. if (ack.err) {
  344. new Noty({
  345. text: ack.err,
  346. timeout: 2000,
  347. theme: 'mint',
  348. layout: 'bottomRight',
  349. type: 'error'
  350. }).show();
  351. }
  352. });
  353. }
  354. })
  355. ]
  356. }
  357. ]
  358. }
  359. ]
  360. }
  361. }
  362. document.querySelector("#userLobby").$cell({
  363. id: "userLobby",
  364. $cell: true,
  365. $type: "div",
  366. $components: [
  367. userGUI, loginGUI, _app.widgets.divider, worldGUI]
  368. }
  369. );
  370. }
  371. parseAppInstancesData(data) {
  372. if (data == "{}") {
  373. var el = document.querySelector(".instance");
  374. if (el) {
  375. var topEl = el.parentNode;
  376. topEl.removeChild(el);
  377. }
  378. // let removeElements = elms => Array.from(elms).forEach(el => el.remove());
  379. }
  380. let jsonObj = JSON.parse(data);
  381. var parsed = {};
  382. for (var prop in jsonObj) {
  383. var name = prop.split('/')[1];
  384. if (parsed[name]) {
  385. parsed[name][prop] = jsonObj[prop];
  386. } else {
  387. parsed[name] = {};
  388. parsed[name][prop] = jsonObj[prop];
  389. }
  390. }
  391. //console.log(parsed);
  392. if (Object.entries(this.worlds).length !== 0)
  393. document.querySelector("#main")._emptyLists();
  394. for (var prop in parsed) {
  395. var name = prop;
  396. let obj = Object.entries(parsed[prop]);
  397. var lists = {};
  398. obj.forEach(el => {
  399. let sotSave = prop;
  400. if (el[1].loadInfo['save_name']) {
  401. let saveName = prop + '/load/' + el[1].loadInfo.save_name;
  402. if (!lists[saveName])
  403. lists[saveName] = {};
  404. lists[saveName][el[0]] = el[1]
  405. } else {
  406. if (!lists[name])
  407. lists[name] = {};
  408. lists[name][el[0]] = el[1]
  409. }
  410. });
  411. // console.log(lists);
  412. Object.entries(lists).forEach(list => {
  413. let element = document.getElementById(list[0] + 'List');
  414. if (element) {
  415. element._setListData(list[1]);
  416. }
  417. })
  418. }
  419. // console.log(data)
  420. }
  421. initWorldsListGUI() {
  422. var self = this;
  423. let worldsListGUI = {
  424. $cell: true,
  425. $type: "div",
  426. id: "main",
  427. _status: "",
  428. _jsonData: {},
  429. _emptyLists: function () {
  430. Object.entries(this._jsonData).forEach(function (element) {
  431. //console.log(element);
  432. let el = document.getElementById(element[0] + 'List');
  433. if (el)
  434. el._setListData({});
  435. });
  436. },
  437. $init: function () {
  438. this._jsonData = {} //data//JSON.parse(data);
  439. },
  440. _makeWorldCard: function (m) {
  441. let langID = localStorage.getItem('krestianstvo_locale');
  442. var appInfo = m
  443. if (langID) {
  444. if (m[1][langID]) {
  445. appInfo = [m[0], m[1][langID], m[1].user, m[1].type]
  446. }
  447. }
  448. return {
  449. $cell: true,
  450. $type: "div",
  451. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-4",
  452. $components: [
  453. this._worldCardDef(appInfo)
  454. ]
  455. }
  456. },
  457. $update: function () {
  458. this.$components = [
  459. {
  460. $type: "div",
  461. class: "mdc-layout-grid",
  462. $components: [
  463. {
  464. $type: "div",
  465. class: "mdc-layout-grid__inner",
  466. $components: [
  467. {
  468. $type: "div",
  469. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-12",
  470. $components: [
  471. {
  472. $type: "H3",
  473. $text: this._status
  474. }
  475. ]
  476. }
  477. ]
  478. },
  479. {
  480. $type: "div",
  481. class: "mdc-layout-grid__inner",
  482. $components: Object.entries(this._jsonData).map(this._makeWorldCard)
  483. }
  484. ]
  485. },
  486. ]
  487. },
  488. _worldCardDef: function (desc) {
  489. var userGUI = [];
  490. if (desc[3] == 'proto') {
  491. userGUI.push(
  492. {
  493. $type: "a",
  494. class: "mdc-button mdc-button--compact mdc-card__action",
  495. $text: "States",
  496. onclick: function (e) {
  497. //console.log('clone');
  498. self.showOnlySaveStates(desc[0], desc[2]);
  499. //self.refresh();
  500. }
  501. }
  502. )
  503. }
  504. if (_LCSUSER.is) {
  505. if (_LCSUSER.is.alias == desc[2]) {
  506. userGUI.push(
  507. {
  508. $type: "a",
  509. class: "mdc-button mdc-button--compact mdc-card__action",
  510. $text: "Edit info",
  511. //href: "/" + desc[2] + '/worlds/' + desc[0] + '/edit', ///:user/worlds/:name/edit
  512. onclick: function (e) {
  513. //'/:user/:type/:name/edit/:file'
  514. if (desc[3] == 'proto') {
  515. window.location.pathname = "/" + desc[2] + '/proto/' + desc[0] + '/edit/info_json'
  516. } else if (desc[3] == 'saveState') {
  517. let names = desc[0].split('/');
  518. let filename = ('savestate_/' + names[0] + '/' + names[2] + '_info_vwf_json').split('/').join("~");
  519. window.location.pathname = "/" + desc[2] + '/state/' + names[0] + '/edit/' + filename;
  520. }
  521. //self.refresh();
  522. }
  523. }
  524. );
  525. if (desc[3] == 'proto') {
  526. userGUI.push(
  527. {
  528. $type: "a",
  529. class: "mdc-button mdc-button--compact mdc-card__action",
  530. $text: "Edit proto",
  531. //href: "/" + desc[2] + '/worlds/' + desc[0] + '/edit', ///:user/worlds/:name/edit
  532. onclick: function (e) {
  533. window.location.pathname = "/" + desc[2] + '/proto/' + desc[0] + '/edit/index_vwf_yaml'
  534. }
  535. }
  536. );
  537. }
  538. }
  539. if (desc[3] == 'proto') {
  540. userGUI.push(
  541. {
  542. $type: "a",
  543. class: "mdc-button mdc-button--compact mdc-card__action",
  544. $text: self.language.t('clone proto'),//"clone",
  545. onclick: function (e) {
  546. //console.log('clone');
  547. _app.cloneWorldPrototype(desc[0], desc[2]);
  548. //self.refresh();
  549. }
  550. }
  551. )
  552. } else if (desc[3] == 'saveState') {
  553. // userGUI.push(
  554. // {
  555. // $type: "a",
  556. // class: "mdc-button mdc-button--compact mdc-card__action mdc-button--outlined",
  557. // $text: "Clone",
  558. // onclick: function (e) {
  559. // //console.log('clone');
  560. // //self.cloneWorldState(desc[0], desc[2]);
  561. // //self.refresh();
  562. // }
  563. // })
  564. }
  565. }
  566. return {
  567. $cell: true,
  568. $type: "div",
  569. class: "mdc-card world-card",
  570. _appInfo: desc,
  571. $components: [
  572. {
  573. $type: "section",
  574. class: "mdc-card__media world-card__16-9-media",
  575. $init: function () {
  576. if (desc[1].imgUrl !== "") {
  577. this.style.backgroundImage = 'linear-gradient(0deg, rgba(0, 0, 0, 0.3), rgba(0, 0, 0, 0.3) ), url(' + desc[1].imgUrl + ')';
  578. }
  579. }
  580. },
  581. {
  582. $type: "section",
  583. class: "mdc-card__primary",
  584. $components: [
  585. {
  586. $type: "h1",
  587. class: "mdc-card__title mdc-card__title--large",
  588. $text: desc[1].title
  589. },
  590. {
  591. $type: "h2",
  592. class: "mdc-card__subtitle mdc-theme--text-secondary-on-background",
  593. $text: desc[1].text
  594. }
  595. ]
  596. },
  597. {
  598. $type: "section",
  599. class: "mdc-card__actions",
  600. $components: [
  601. {
  602. $type: "a",
  603. class: "mdc-button mdc-button--compact mdc-card__action mdc-button--outlined",
  604. $text: self.language.t('start'),//"Start new",
  605. target: "_blank",
  606. href: "/" + desc[2] + '/' + desc[0],
  607. onclick: function (e) {
  608. self.refresh();
  609. }
  610. }
  611. ].concat(userGUI)
  612. },
  613. {
  614. $type: "section",
  615. class: "mdc-card__actions",
  616. $components: [
  617. {
  618. $type: "ul",
  619. _listData: {},
  620. _setListData: function (data) {
  621. this._listData = data;
  622. },
  623. class: "mdc-list mdc-list--two-line",
  624. 'aria-orientation': "vertical",
  625. id: desc[0] + 'List',
  626. $update: function () {
  627. var connectText = {}
  628. let cardListData = Object.entries(this._listData).filter(el => el[1].user == this._appInfo[2]);
  629. if (cardListData.length !== 0) {
  630. connectText = {
  631. // $type: "span",
  632. // class: "mdc-theme--text-secondary",
  633. // $text: "...or connect to:"
  634. }
  635. }
  636. this.$components = [
  637. {
  638. $type: "hr",
  639. class: "mdc-list-divider"
  640. }
  641. ].concat(cardListData.map(this._worldListItem))
  642. // [connectText]
  643. // }].concat(Object.entries(this._listData).map(this._worldListItem))
  644. },
  645. _worldListItem: function (m) {
  646. return {
  647. $type: "li",
  648. class: "mdc-list-item",
  649. $components: [
  650. {
  651. $type: "span",
  652. class: "world-link mdc-list-item__text",
  653. $components: [
  654. {
  655. $type: "span",
  656. class: "mdc-list-item__primary-text",
  657. $components: [
  658. {
  659. $type: "a",
  660. $text: m[0],
  661. target: "_blank",
  662. href: window.location.protocol + "//" + window.location.host + "/" + m[1].user + m[0],
  663. onclick: function (e) {
  664. //self.refresh();
  665. }
  666. },
  667. ]
  668. },
  669. {
  670. $type: "span",
  671. class: "mdc-list-item__secondary-text",
  672. $text: self.language.t('users') + m[1].clients
  673. }
  674. ]
  675. }
  676. ]
  677. }
  678. }
  679. }
  680. ]
  681. }
  682. ]
  683. }
  684. }
  685. }
  686. document.querySelector("#main").$cell({
  687. $cell: true,
  688. $type: "div",
  689. $components: [worldsListGUI]
  690. })
  691. }
  692. async showOnlySaveStates(index, userAlias) {
  693. let userPub = await _LCSDB.get('users').get(userAlias).get('pub').once().then();
  694. var db = _LCSDB.user(userPub);
  695. if (_LCSUSER.is) {
  696. if (_LCSUSER.is.alias == userAlias)
  697. db = _LCSUSER;
  698. }
  699. this.worlds = {};
  700. document.querySelector("#main")._jsonData = Object.assign({}, this.worlds);
  701. document.querySelector("#main")._status = "Save states of the World: " + index + ' for user: ' + userAlias;
  702. document.querySelector("#main").$update();
  703. //let userAlias = _LCSUSER.is.alias;
  704. db.get('documents').get(index).once(save => {
  705. if (save) {
  706. let saves = Object.keys(save).filter(el => el.includes('_info_vwf_json'));
  707. console.log(saves);
  708. if (saves) {
  709. saves.forEach(el => {
  710. db.get('documents').get(index).get(el).once(res => {
  711. if (res) {
  712. let fileName = el.split('/')[2].replace('_info_vwf_json', "");
  713. let world = JSON.parse(res.file);
  714. let root = Object.keys(world)[0];
  715. world[root].user = userAlias;
  716. world[root].type = 'saveState';
  717. this.worlds[index + '/load/' + fileName] = world[root];
  718. document.querySelector("#main")._jsonData = Object.assign({}, this.worlds);
  719. }
  720. })
  721. })
  722. }
  723. }
  724. })
  725. }
  726. async getWorldsProtosFromUserDB(userAlias) {
  727. let userPub = await _LCSDB.get('users').get(userAlias).get('pub').once().then();
  728. console.log('get user worlds for: ' + userAlias);
  729. this.worlds = {};
  730. document.querySelector("#main")._jsonData = Object.assign({}, this.worlds);
  731. if (!userPub) {
  732. document.querySelector("#main")._status = "no such user";
  733. document.querySelector("#main").$update();
  734. }
  735. if (userPub) {
  736. document.querySelector("#main")._status = "Worlds protos for: " + userAlias;
  737. document.querySelector("#main").$update();
  738. var db = _LCSDB.user(userPub);
  739. if (_LCSUSER.is) {
  740. if (_LCSUSER.is.alias == userAlias)
  741. db = _LCSUSER;
  742. }
  743. db.get('worlds').map().once((w, index) => {
  744. if (w) {
  745. db.get('worlds').get(index).get('info_json').once(res => {
  746. if (res) {
  747. let world = JSON.parse(res.file);
  748. let root = Object.keys(world)[0];
  749. world[root].user = userAlias;
  750. world[root].type = 'proto';
  751. this.worlds[index] = world[root];
  752. document.querySelector("#main")._jsonData = Object.assign({}, this.worlds);
  753. }
  754. })
  755. }
  756. })
  757. }
  758. }
  759. async getWorldsFromUserDB(userAlias) {
  760. let userPub = await _LCSDB.get('users').get(userAlias).get('pub').once().then();
  761. console.log('get user worlds for: ' + userAlias);
  762. this.worlds = {};
  763. document.querySelector("#main")._jsonData = Object.assign({}, this.worlds);
  764. if (!userPub) {
  765. document.querySelector("#main")._status = "no such user";
  766. document.querySelector("#main").$update();
  767. }
  768. if (userPub) {
  769. document.querySelector("#main")._status = "Worlds states for: " + userAlias;
  770. document.querySelector("#main").$update();
  771. var db = _LCSDB.user(userPub);
  772. if (_LCSUSER.is) {
  773. if (_LCSUSER.is.alias == userAlias)
  774. db = _LCSUSER;
  775. }
  776. db.get('worlds').map().once((w, index) => {
  777. if (w) {
  778. db.get('documents').get(index).once(save => {
  779. if (save) {
  780. let saves = Object.keys(save).filter(el => el.includes('_info_vwf_json'));
  781. console.log(saves);
  782. if (saves) {
  783. saves.forEach(el => {
  784. db.get('documents').get(index).get(el).once(res => {
  785. if (res) {
  786. let fileName = el.split('/')[2].replace('_info_vwf_json', "");
  787. let world = JSON.parse(res.file);
  788. let root = Object.keys(world)[0];
  789. world[root].user = userAlias;
  790. world[root].type = 'saveState';
  791. this.worlds[index + '/load/' + fileName] = world[root];
  792. document.querySelector("#main")._jsonData = Object.assign({}, this.worlds);
  793. }
  794. })
  795. })
  796. }
  797. }
  798. })
  799. }
  800. })
  801. }
  802. }
  803. async getAppDetailsFromDefaultDB(type) {
  804. let defaultUserPUB = await _LCSDB.get('lcs/app').path('pub').once().then();
  805. var userAlias = await _LCSDB.user(defaultUserPUB).get('alias').once().then();
  806. page.redirect('/' + userAlias + '/worlds/' + type);
  807. }
  808. async getAppDetailsFromDB() {
  809. let defaultUserPUB = await _LCSDB.get('lcs/app').path('pub').once().then();
  810. var userAlias = await _LCSDB.user(defaultUserPUB).get('alias').once().then();
  811. if (userAlias)
  812. this.getWorldsProtosFromUserDB(userAlias);
  813. }
  814. refresh() {
  815. // socket.emit('getWebAppUpdate', "");
  816. }
  817. }
  818. export { IndexApp }
  819. //export {getAppDetails, generateFrontPage, setLanguage, initLocale};