world-app.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. import page from '/lib/page.mjs';
  2. class WorldApp {
  3. constructor(userAlias, worldName, saveName) {
  4. console.log("app constructor");
  5. this.userAlias = userAlias;
  6. this.worldName = worldName;
  7. this.saveName = saveName;
  8. //this.worlds = {};
  9. this.language = _LangManager.language;
  10. this.options = {
  11. query: 'pathname=' + window.location.pathname.slice(1,
  12. window.location.pathname.lastIndexOf("/")),
  13. secure: window.location.protocol === "https:",
  14. reconnection: false,
  15. path: '',
  16. transports: ['websocket']
  17. }
  18. //window.location.host
  19. var socket = io.connect(window._app.reflectorHost, this.options);
  20. const parse = (msg) => {
  21. this.parseAppInstancesData(msg);
  22. }
  23. socket.on('getWebAppUpdate', msg => parse.call(this, msg));
  24. socket.on("connect", function () {
  25. let noty = new Noty({
  26. text: 'Connected to Reflector!',
  27. timeout: 2000,
  28. theme: 'mint',
  29. layout: 'bottomRight',
  30. type: 'success'
  31. });
  32. noty.show();
  33. })
  34. socket.on('connect_error', function (err) {
  35. console.log(err);
  36. var errDiv = document.createElement("div");
  37. errDiv.innerHTML = "<div class='vwf-err' style='z-index: 10; position: absolute; top: 80px; right: 50px'>Connection error to Reflector!" + err + "</div>";
  38. document.querySelector('body').appendChild(errDiv);
  39. let noty = new Noty({
  40. text: 'Connection error to Reflector! ' + err,
  41. theme: 'mint',
  42. layout: 'bottomRight',
  43. type: 'error'
  44. });
  45. noty.show();
  46. });
  47. }
  48. createWorldStatesGUI() {
  49. let self = this;
  50. let worldStatesGUI = {
  51. $cell: true,
  52. id: "worldStatesGUI",
  53. $type: "div",
  54. $components: [],
  55. _states: {},
  56. _refresh: function (data) {
  57. this._states = data
  58. },
  59. $init: async function () {
  60. //this._refresh();
  61. },
  62. _makeWorldCard: function (data) {
  63. let cardID = data[1].userAlias + '_' + data[1].worldName + '_' + data[0];
  64. let card = self.createWorldCard(cardID, 'min');
  65. card._worldInfo = data[1];
  66. card.$update();
  67. return {
  68. $cell: true,
  69. $type: "div",
  70. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-4",
  71. $components: [
  72. card
  73. //self.createWorldCard(data[1].userAlias, data[1].worldName, data[0])
  74. //this._worldCardDef(appInfo)
  75. ]
  76. }
  77. //console.log(data);
  78. },
  79. $update: function () {
  80. this.$components = [
  81. {
  82. $type: "div",
  83. class: "mdc-layout-grid",
  84. $components: [
  85. {
  86. $type: "div",
  87. class: "mdc-layout-grid__inner",
  88. $components: [
  89. {
  90. $type: "div",
  91. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-12",
  92. $components: [
  93. {
  94. $type: "H3",
  95. $text: 'States'
  96. }
  97. ]
  98. }
  99. ]
  100. },
  101. {
  102. $type: "div",
  103. class: "mdc-layout-grid__inner",
  104. $components: Object.entries(this._states)
  105. .filter(el =>Object.keys(el[1]).length !== 0)
  106. .sort(function (el1, el2) {
  107. return parseInt(el2[1].created) - parseInt(el1[1].created)
  108. })
  109. .map(this._makeWorldCard)
  110. }
  111. ]
  112. },
  113. ]
  114. }
  115. }
  116. return worldStatesGUI
  117. }
  118. createWorldCard(id, type) {
  119. let self = this;
  120. let onlineGUI = {
  121. $cell: true,
  122. id: "onlineGUI_" + id,
  123. class: "online",
  124. $type: "div",
  125. _instances: {},
  126. _worldListItem: function (m) {
  127. return {
  128. $type: "li",
  129. class: "mdc-list-item",
  130. $components: [
  131. {
  132. $type: "span",
  133. class: "world-link mdc-list-item__text",
  134. $components: [
  135. {
  136. $type: "span",
  137. class: "mdc-list-item__primary-text",
  138. $components: [
  139. {
  140. $type: "a",
  141. $text: m[0],
  142. target: "_blank",
  143. href: window.location.protocol + "//" + window.location.host + "/" + m[1].user + m[0],
  144. onclick: function (e) {
  145. //self.refresh();
  146. }
  147. },
  148. ]
  149. },
  150. {
  151. $type: "span",
  152. class: "mdc-list-item__secondary-text",
  153. $text: self.language.t('users') + m[1].clients
  154. }
  155. ]
  156. }
  157. ]
  158. }
  159. },
  160. $components: [],
  161. _refresh: function (data) {
  162. if (data) {
  163. if (Object.entries(data).length !== 0) {
  164. if (this._worldInfo) {
  165. let insts = Object.entries(data).filter(el => el[0] == this._worldInfo.worldName);
  166. if (insts.length !== 0)
  167. this._instances = insts[0][1];
  168. }
  169. } else {
  170. this._instances = {}
  171. }
  172. }
  173. },
  174. $init: function () {
  175. this._refresh();
  176. },
  177. $update: function () {
  178. if (this._instances) {
  179. let cardListData = Object.entries(this._instances).filter(el => el[1].user == this._worldInfo.userAlias);
  180. this.$components = [
  181. {
  182. $type: "hr",
  183. class: "mdc-list-divider"
  184. }
  185. ].concat(cardListData.map(this._worldListItem))
  186. }
  187. }
  188. }
  189. return {
  190. $cell: true,
  191. id: 'worldCard_' + id,
  192. $type: "div",
  193. _worldInfo: {},
  194. _refresh: function (data) {
  195. this._worldInfo = data
  196. },
  197. // _getWorldInfo: async function () {
  198. // //get space for user
  199. // let info = await _app.getWorldInfo(user, space);
  200. // this._refresh(info);
  201. // },
  202. // _getStateInfo: async function () {
  203. // //get space for user
  204. // let info = await _app.getStateInfo(user, space, saveName);
  205. // this._refresh(info);
  206. // },
  207. $init: function () {
  208. //get space for user
  209. // if (!saveName) {
  210. // this._getWorldInfo();
  211. // } else {
  212. // this._getStateInfo();
  213. // }
  214. },
  215. $update: function () {
  216. console.log(this._worldInfo);
  217. this.$components = [this._updateCard()]
  218. },
  219. $components: [],
  220. _updateCard: function () {
  221. let desc = this._worldInfo;
  222. if (!desc || Object.keys(desc).length == 0) {
  223. return {
  224. $type: "h1",
  225. class: "mdc-typography--headline4",
  226. $text: "ERROR: NO WORLD!"
  227. }
  228. }
  229. let userGUI = [];
  230. let online = [];
  231. let cardInfo = {
  232. "title": ""
  233. };
  234. if (type == "full") {
  235. } else {
  236. userGUI.push({
  237. $type: "a",
  238. class: "mdc-button mdc-button--compact mdc-card__action mdc-button--outlined",
  239. $text: "Details",
  240. onclick: function (e) {
  241. e.preventDefault();
  242. window.location.pathname = "/" + desc.userAlias + '/' + desc.worldName + '/about'
  243. }
  244. });
  245. }
  246. userGUI.push({
  247. $type: "a",
  248. class: "mdc-button mdc-button--compact mdc-card__action mdc-button--outlined",
  249. $text: self.language.t('start'),//"Start new",
  250. target: "_blank",
  251. href: "/" + desc.userAlias + '/' + desc.worldName,
  252. onclick: function (e) {
  253. //self.refresh();
  254. }
  255. });
  256. if (desc.type == 'saveState') {
  257. cardInfo.title = desc.worldName.split('/')[2];
  258. }
  259. if (desc.type == 'proto') {
  260. cardInfo.title = desc.worldName;
  261. userGUI.push(
  262. {
  263. $type: "a",
  264. class: "mdc-button mdc-button--compact mdc-card__action",
  265. $text: "States",
  266. onclick: async function (e) {
  267. //console.log('clone');
  268. document.querySelector('#worldStatesGUI')._refresh({});
  269. let data = await _app.getSaveStates(desc.userAlias, desc.worldName);
  270. document.querySelector('#worldStatesGUI')._refresh(data);
  271. //self.showOnlySaveStates(desc.worldName, desc.userAlias);
  272. //self.refresh();
  273. }
  274. }
  275. )
  276. }
  277. online.push(onlineGUI);
  278. return {
  279. $cell: true,
  280. $type: "div",
  281. class: "mdc-card world-card",
  282. $components: [
  283. {
  284. $type: "section",
  285. class: "mdc-card__media world-card__16-9-media",
  286. $init: function () {
  287. if (desc.info.imgUrl !== "") {
  288. this.style.backgroundImage = 'linear-gradient(0deg, rgba(0, 0, 0, 0.3), rgba(0, 0, 0, 0.3) ), url(' + desc.info.imgUrl + ')';
  289. }
  290. }
  291. },
  292. {
  293. $type: "section",
  294. class: "mdc-card__primary",
  295. $components: [
  296. {
  297. $type: "h1",
  298. class: "mdc-card__title mdc-card__title--large",
  299. $text: desc.info.title
  300. },
  301. {
  302. $type: "h2",
  303. class: "mdc-card__subtitle mdc-theme--text-secondary-on-background",
  304. $text: desc.info.text
  305. },
  306. {
  307. $type: "span",
  308. class: "mdc-card__subtitle mdc-theme--text-secondary-on-background",
  309. $text: 'id: '
  310. },
  311. {
  312. $type: "input",
  313. type: "text",
  314. disabled: "",
  315. style: "font-size:18px",
  316. value: cardInfo.title
  317. },
  318. {
  319. $type: "p",
  320. },
  321. {
  322. $type: "span",
  323. class: "mdc-card__subtitle mdc-theme--text-secondary-on-background",
  324. $text: 'created: ' + (new Date(desc.created)).toUTCString()
  325. },
  326. {
  327. $type: "p",
  328. }
  329. // ,{
  330. // $type: "span",
  331. // class: "mdc-card__subtitle mdc-theme--text-secondary-on-background",
  332. // $text: 'modified: ' + (new Date(desc[5])).toUTCString()
  333. // }
  334. ]
  335. },
  336. {
  337. $type: "section",
  338. class: "mdc-card__actions",
  339. $components: [
  340. ].concat(userGUI)
  341. },
  342. {
  343. $type: "section",
  344. class: "mdc-card__actions",
  345. $components: [
  346. {
  347. $type: 'div',
  348. $text: 'online now: '
  349. }
  350. ].concat(online)
  351. }
  352. ]
  353. }
  354. }
  355. }
  356. }
  357. async initWorldGUI() {
  358. let self = this;
  359. let user = this.userAlias;
  360. let space = this.worldName;
  361. let saveName = this.saveName;
  362. let el = document.createElement("div");
  363. el.setAttribute("id", "aboutWorld");
  364. document.body.appendChild(el);
  365. let cardID = user + '_' + space + '_' + (saveName ? saveName : "");
  366. let worldCardGUI = this.createWorldCard(cardID, 'full');
  367. let worldStatesGUI = [];
  368. var info = {};
  369. if (!saveName) {
  370. info = await _app.getWorldInfo(user, space);
  371. } else {
  372. info = await _app.getStateInfo(user, space, saveName);
  373. }
  374. worldCardGUI._worldInfo = info;
  375. worldCardGUI.$update();
  376. if (!saveName) {
  377. let statesData = await _app.getSaveStates(user, space);
  378. let worldStates = this.createWorldStatesGUI();
  379. worldStates._states = statesData;
  380. worldStates.$update();
  381. worldStatesGUI.push(worldStates);
  382. }
  383. let actionsGUI = {
  384. $cell: true,
  385. id: "worldActionsGUI",
  386. $type: "div",
  387. $components: [],
  388. _worldInfo: {},
  389. _refresh: function () {
  390. this._worldInfo = {
  391. 'userAlias': self.userAlias,
  392. 'worldName': self.saveName ? self.worldName + '/load/' + self.saveName : self.worldName,
  393. 'type': self.saveName ? 'saveState' : 'proto'
  394. }
  395. // let worldCard = document.querySelector('#worldCard');
  396. // if(worldCard){
  397. // this._worldInfo = worldCard._worldInfo;
  398. // }
  399. },
  400. $init: function () {
  401. if (_LCSUSER.is) {
  402. this._refresh();
  403. }
  404. },
  405. $update: function () {
  406. let desc = this._worldInfo;
  407. let userGUI = [];
  408. // if(!desc){
  409. // this.$components = [];
  410. // return
  411. // }
  412. if (_LCSUSER.is) {
  413. if (_LCSUSER.is.alias == desc.userAlias) {
  414. userGUI.push(
  415. {
  416. $type: "a",
  417. class: "mdc-button mdc-button--compact mdc-card__action",
  418. $text: "Edit info",
  419. //href: "/" + desc[2] + '/worlds/' + desc[0] + '/edit', ///:user/worlds/:name/edit
  420. onclick: function (e) {
  421. //'/:user/:type/:name/edit/:file'
  422. if (desc.type == 'proto') {
  423. window.location.pathname = "/" + desc.userAlias + '/proto/' + desc.worldName + '/edit/info_json'
  424. } else if (desc.type == 'saveState') {
  425. let names = desc.worldName.split('/');
  426. let filename = ('savestate_/' + names[0] + '/' + names[2] + '_info_vwf_json').split('/').join("~");
  427. window.location.pathname = "/" + desc.userAlias + '/state/' + names[0] + '/edit/' + filename;
  428. }
  429. //self.refresh();
  430. }
  431. }
  432. );
  433. if (desc.type == 'proto') {
  434. userGUI.push(
  435. {
  436. $type: "a",
  437. class: "mdc-button mdc-button--compact mdc-card__action",
  438. $text: "Edit proto",
  439. //href: "/" + desc[2] + '/worlds/' + desc[0] + '/edit', ///:user/worlds/:name/edit
  440. onclick: function (e) {
  441. window.location.pathname = "/" + desc.userAlias + '/proto/' + desc.worldName + '/edit/index_vwf_yaml'
  442. }
  443. }
  444. );
  445. userGUI.push(
  446. {
  447. $type: "a",
  448. class: "mdc-button mdc-button--compact mdc-card__action",
  449. $text: "Delete",
  450. //href: "/" + desc[2] + '/worlds/' + desc[0] + '/edit', ///:user/worlds/:name/edit
  451. onclick: function (e) {
  452. _app.deleteWorld(desc.worldName, 'proto');
  453. }
  454. }
  455. );
  456. }
  457. if (desc.type == 'saveState') {
  458. userGUI.push(
  459. {
  460. $type: "a",
  461. class: "mdc-button mdc-button--compact mdc-card__action",
  462. $text: "Delete",
  463. //href: "/" + desc[2] + '/worlds/' + desc[0] + '/edit', ///:user/worlds/:name/edit
  464. onclick: function (e) {
  465. _app.deleteWorld(desc.worldName, 'state');
  466. }
  467. }
  468. );
  469. }
  470. }
  471. if (desc.type == 'proto') {
  472. userGUI.push(
  473. {
  474. $type: "a",
  475. class: "mdc-button mdc-button--compact mdc-card__action",
  476. $text: self.language.t('clone proto'),//"clone",
  477. onclick: function (e) {
  478. //console.log('clone');
  479. _app.cloneWorldPrototype(desc.worldName, desc.userAlias);
  480. //self.refresh();
  481. }
  482. }
  483. )
  484. } else if (desc.type == 'saveState') {
  485. // userGUI.push(
  486. // {
  487. // $type: "a",
  488. // class: "mdc-button mdc-button--compact mdc-card__action mdc-button--outlined",
  489. // $text: "Clone",
  490. // onclick: function (e) {
  491. // //console.log('clone');
  492. // //self.cloneWorldState(desc[0], desc[2]);
  493. // //self.refresh();
  494. // }
  495. // })
  496. }
  497. }
  498. this.$components = [
  499. {
  500. $type: "div",
  501. $text: "World actions:"
  502. }
  503. ].concat(userGUI)
  504. }
  505. }
  506. _LCSDB.on('auth',
  507. async function (ack) {
  508. if (_LCSUSER.is) {
  509. document.querySelector('#worldActionsGUI')._refresh();
  510. }
  511. })
  512. document.querySelector("#aboutWorld").$cell({
  513. id: 'aboutWorld',
  514. $cell: true,
  515. $type: "div",
  516. $components: [
  517. {
  518. $type: "div",
  519. class: "mdc-layout-grid",
  520. $components: [
  521. {
  522. $type: "div",
  523. class: "mdc-layout-grid__inner",
  524. $components: [
  525. {
  526. $type: "div",
  527. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-12",
  528. $components: [
  529. {
  530. $type: "h1",
  531. class: "mdc-typography--headline4",
  532. $text: self.worldName + ' by ' + self.userAlias
  533. }
  534. ]
  535. },
  536. {
  537. $type: "div",
  538. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-4",
  539. $components: [
  540. worldCardGUI
  541. ]
  542. },
  543. {
  544. $type: "div",
  545. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-12",
  546. $components: [
  547. actionsGUI
  548. ]
  549. },
  550. {
  551. $type: "div",
  552. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-12",
  553. $components: [
  554. ].concat(worldStatesGUI)
  555. },
  556. ]
  557. }
  558. ]
  559. }
  560. ]
  561. })
  562. }
  563. parseAppInstancesData(data) {
  564. let parcedData = _app.parseAppInstancesData(data);
  565. //if (Object.entries(parcedData).length !== 0)
  566. let onlineGUIs = document.querySelectorAll('.online');
  567. onlineGUIs.forEach(function (item) {
  568. item._refresh(parcedData)
  569. });
  570. // if (onlineGUI)
  571. // document.querySelector("#onlineGUI")._refresh(parcedData)
  572. // if (Object.entries(this.worlds).length !== 0) {
  573. // document.querySelector("#main")._emptyLists();
  574. // }
  575. // if (parcedData == "{}") {
  576. // var el = document.querySelector(".instance");
  577. // if (el) {
  578. // var topEl = el.parentNode;
  579. // topEl.removeChild(el);
  580. // }
  581. // // let removeElements = elms => Array.from(elms).forEach(el => el.remove());
  582. // }
  583. // console.log(parcedData)
  584. }
  585. }
  586. export { WorldApp }