world-app.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. }
  11. createWorldStatesGUI() {
  12. let self = this;
  13. let worldStatesGUI = {
  14. $cell: true,
  15. id: "worldStatesGUI",
  16. $type: "div",
  17. $components: [],
  18. _states: {},
  19. _refresh: function (data) {
  20. this._states = data
  21. },
  22. $init: async function () {
  23. //this._refresh();
  24. },
  25. _makeWorldCard: function (data) {
  26. let cardID = data[1].userAlias + '_' + data[1].worldName + '_' + data[0];
  27. let card = _app.indexApp.createWorldCard(cardID, 'min');
  28. card._worldInfo = data[1];
  29. card.$update();
  30. return {
  31. $cell: true,
  32. $type: "div",
  33. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-4",
  34. $components: [
  35. card
  36. //self.createWorldCard(data[1].userAlias, data[1].worldName, data[0])
  37. //this._worldCardDef(appInfo)
  38. ]
  39. }
  40. //console.log(data);
  41. },
  42. $update: function () {
  43. this.$components = [
  44. {
  45. $type: "div",
  46. class: "mdc-layout-grid",
  47. $components: [
  48. {
  49. $type: "div",
  50. class: "mdc-layout-grid__inner",
  51. $components: [
  52. {
  53. $type: "div",
  54. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-12",
  55. $components: [
  56. {
  57. $type: "H3",
  58. $text: 'States'
  59. }
  60. ]
  61. }
  62. ]
  63. },
  64. {
  65. $type: "div",
  66. class: "mdc-layout-grid__inner",
  67. $components: Object.entries(this._states)
  68. .filter(el =>Object.keys(el[1]).length !== 0)
  69. .sort(function (el1, el2) {
  70. return parseInt(el2[1].created) - parseInt(el1[1].created)
  71. })
  72. .map(this._makeWorldCard)
  73. }
  74. ]
  75. }
  76. ]
  77. }
  78. }
  79. return worldStatesGUI
  80. }
  81. async initWorldGUI() {
  82. // _LCSDB.on('auth',
  83. // function (ack) {
  84. // document.querySelector('#worldActionsGUI')._refresh();
  85. // })
  86. let self = this;
  87. let user = this.userAlias;
  88. let space = this.worldName;
  89. let saveName = this.saveName;
  90. let el = document.createElement("div");
  91. el.setAttribute("id", "aboutWorld");
  92. document.body.appendChild(el);
  93. let cardID = user + '_' + space + '_' + (saveName ? saveName : "");
  94. let worldCardGUI = _app.indexApp.createWorldCard(cardID, 'full');
  95. let worldStatesGUI = [];
  96. var info = {};
  97. if (!saveName) {
  98. info = await _app.getWorldInfo(user, space);
  99. } else {
  100. info = await _app.getStateInfo(user, space, saveName);
  101. }
  102. worldCardGUI._worldInfo = info;
  103. worldCardGUI.$update();
  104. if (!saveName) {
  105. let statesData = await _app.getSaveStates(user, space);
  106. let worldStates = this.createWorldStatesGUI();
  107. worldStates._states = statesData;
  108. worldStates.$update();
  109. worldStatesGUI.push(worldStates);
  110. }
  111. let actionsGUI = {
  112. $cell: true,
  113. id: "worldActionsGUI",
  114. $type: "div",
  115. $components: [],
  116. _worldInfo: {},
  117. _refresh: function () {
  118. this._worldInfo = {
  119. 'userAlias': self.userAlias,
  120. 'worldName': self.saveName ? self.worldName + '/load/' + self.saveName : self.worldName,
  121. 'type': self.saveName ? 'saveState' : 'proto'
  122. }
  123. // let worldCard = document.querySelector('#worldCard');
  124. // if(worldCard){
  125. // this._worldInfo = worldCard._worldInfo;
  126. // }
  127. },
  128. $init: function () {
  129. if (_LCSUSER.is) {
  130. this._refresh();
  131. }
  132. },
  133. $update: function () {
  134. let desc = this._worldInfo;
  135. let userGUI = [];
  136. // if(!desc){
  137. // this.$components = [];
  138. // return
  139. // }
  140. if (_LCSUSER.is) {
  141. if (_LCSUSER.is.alias == desc.userAlias) {
  142. userGUI.push(
  143. {
  144. $type: "a",
  145. class: "mdc-button mdc-button--raised mdc-card__action actionButton",
  146. $text: "Edit info",
  147. //href: "/" + desc[2] + '/worlds/' + desc[0] + '/edit', ///:user/worlds/:name/edit
  148. onclick: function (e) {
  149. //'/:user/:type/:name/edit/:file'
  150. if (desc.type == 'proto') {
  151. window.location.pathname = "/" + desc.userAlias + '/proto/' + desc.worldName + '/edit/info_json'
  152. } else if (desc.type == 'saveState') {
  153. let names = desc.worldName.split('/');
  154. let filename = ('savestate_/' + names[0] + '/' + names[2] + '_info_vwf_json').split('/').join("~");
  155. window.location.pathname = "/" + desc.userAlias + '/state/' + names[0] + '/edit/' + filename;
  156. }
  157. //self.refresh();
  158. }
  159. }
  160. );
  161. if (desc.type == 'proto') {
  162. userGUI.push(
  163. {
  164. $type: "a",
  165. class: "mdc-button mdc-button--raised mdc-card__action actionButton",
  166. $text: "Edit proto",
  167. //href: "/" + desc[2] + '/worlds/' + desc[0] + '/edit', ///:user/worlds/:name/edit
  168. onclick: function (e) {
  169. window.location.pathname = "/" + desc.userAlias + '/proto/' + desc.worldName + '/edit/index_vwf_yaml'
  170. }
  171. }
  172. );
  173. userGUI.push(
  174. {
  175. $type: "a",
  176. class: "mdc-button mdc-button--raised mdc-card__action actionButton",
  177. $text: "Delete",
  178. //href: "/" + desc[2] + '/worlds/' + desc[0] + '/edit', ///:user/worlds/:name/edit
  179. onclick: function (e) {
  180. _app.deleteWorld(desc.worldName, 'proto');
  181. }
  182. }
  183. );
  184. }
  185. if (desc.type == 'saveState') {
  186. userGUI.push(
  187. {
  188. $type: "a",
  189. class: "mdc-button mdc-button--raised mdc-card__action actionButton",
  190. $text: "Delete",
  191. //href: "/" + desc[2] + '/worlds/' + desc[0] + '/edit', ///:user/worlds/:name/edit
  192. onclick: function (e) {
  193. _app.deleteWorld(desc.worldName, 'state');
  194. }
  195. }
  196. );
  197. }
  198. }
  199. if (desc.type == 'proto') {
  200. userGUI.push(
  201. {
  202. $type: "a",
  203. class: "mdc-button mdc-button--raised mdc-card__action actionButton",
  204. $text: self.language.t('clone proto'),//"clone",
  205. onclick: function (e) {
  206. //console.log('clone');
  207. _app.cloneWorldPrototype(desc.worldName, desc.userAlias);
  208. //self.refresh();
  209. }
  210. }
  211. )
  212. } else if (desc.type == 'saveState') {
  213. // userGUI.push(
  214. // {
  215. // $type: "a",
  216. // class: "mdc-button mdc-button--compact mdc-card__action mdc-button--outlined",
  217. // $text: "Clone",
  218. // onclick: function (e) {
  219. // //console.log('clone');
  220. // //self.cloneWorldState(desc[0], desc[2]);
  221. // //self.refresh();
  222. // }
  223. // })
  224. }
  225. }
  226. this.$components = [
  227. {
  228. $type: "div",
  229. $text: "World actions:"
  230. }
  231. ].concat(userGUI)
  232. }
  233. }
  234. document.querySelector("#aboutWorld").$cell({
  235. id: 'aboutWorld',
  236. $cell: true,
  237. $type: "div",
  238. $components: [
  239. {
  240. $type: "div",
  241. class: "mdc-layout-grid",
  242. $components: [
  243. {
  244. $type: "div",
  245. class: "mdc-layout-grid__inner",
  246. $components: [
  247. {
  248. $type: "div",
  249. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-12",
  250. $components: [
  251. {
  252. $type: "h1",
  253. class: "mdc-typography--headline4",
  254. $text: self.worldName + ' by ' + self.userAlias
  255. }
  256. ]
  257. },
  258. {
  259. $type: "div",
  260. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-4",
  261. $components: [
  262. worldCardGUI
  263. ]
  264. },
  265. {
  266. $type: "div",
  267. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-12",
  268. $components: [
  269. actionsGUI
  270. ]
  271. },
  272. {
  273. $type: "div",
  274. class: "mdc-layout-grid__cell mdc-layout-grid__cell--span-12",
  275. $components: [
  276. ].concat(worldStatesGUI)
  277. },
  278. ]
  279. }
  280. ]
  281. }
  282. ]
  283. })
  284. }
  285. }
  286. export { WorldApp }