Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 

404 рядки
8.6 KiB

  1. define([
  2. 'security/io',
  3. 'misc/messages',
  4. 'security/connections',
  5. 'leaderboard/leaderboard',
  6. 'config/skins'
  7. ], function(
  8. io,
  9. messages,
  10. connections,
  11. leaderboard,
  12. skins
  13. ) {
  14. return {
  15. type: 'auth',
  16. username: null,
  17. charname: null,
  18. characters: {},
  19. characterList: [],
  20. stash: null,
  21. play: function(data) {
  22. if (this.username == null)
  23. return;
  24. var character = this.characters[data.data.name];
  25. if (!character)
  26. return;
  27. if (character.permadead)
  28. return;
  29. character.stash = this.stash;
  30. character.account = this.username;
  31. this.charname = character.name;
  32. data.callback();
  33. this.obj.player.sessionStart = +new Date;
  34. this.obj.player.spawn(character);
  35. var prophecies = this.obj.prophecies ? this.obj.prophecies.simplify().list : [];
  36. leaderboard.setLevel(character.name, this.obj.stats.values.level, prophecies);
  37. },
  38. doSave: function(extensionObj) {
  39. var simple = this.obj.getSimple(true, true);
  40. simple.components.spliceWhere(c => c.type == 'stash');
  41. //Don't save modified stat values
  42. var stats = simple.components.find(c => c.type == 'stats');
  43. stats.values = extend(true, {}, stats.values);
  44. var statKeys = Object.keys(stats.values);
  45. var sLen = statKeys.length;
  46. for (var i = 0; i < sLen; i++) {
  47. var s = statKeys[i];
  48. if ((s.indexOf('xp') > -1) || (s == 'level') || (s == 'hp') || (s == 'mana'))
  49. continue;
  50. delete stats.values[s];
  51. }
  52. //Calculate and store the ttl for effects
  53. var time = +new Date;
  54. simple.components.find(e => e.type == 'effects').effects.forEach(function(e) {
  55. e.expire = time + (e.ttl * 350);
  56. });
  57. var callback = null;
  58. if (extensionObj) {
  59. callback = extensionObj.callback;
  60. delete extensionObj.callback;
  61. }
  62. extend(true, simple, extensionObj);
  63. io.set({
  64. ent: this.charname,
  65. field: 'character',
  66. value: JSON.stringify(simple),
  67. callback: callback
  68. });
  69. //Save stash
  70. io.set({
  71. ent: this.username,
  72. field: 'stash',
  73. value: JSON.stringify(this.obj.stash.items)
  74. });
  75. },
  76. simplify: function() {
  77. return {
  78. type: 'auth',
  79. username: this.username,
  80. charname: this.charname,
  81. skins: this.skins
  82. };
  83. },
  84. getCharacterList: function(data) {
  85. if (this.username == null)
  86. return;
  87. io.get({
  88. ent: this.username,
  89. field: 'characterList',
  90. callback: this.onGetCharacterList.bind(this, data)
  91. });
  92. },
  93. onGetCharacterList: function(data, result) {
  94. var characters = JSON.parse(result || '[]');
  95. this.characterList = characters;
  96. var result = characters
  97. .map(c => ({
  98. name: c.name ? c.name : c,
  99. level: leaderboard.getLevel(c.name ? c.name : c)
  100. }));
  101. data.callback(result);
  102. },
  103. getCharacter: function(data) {
  104. io.get({
  105. ent: data.data.name,
  106. field: 'character',
  107. callback: this.onGetCharacter.bind(this, data)
  108. });
  109. },
  110. onGetCharacter: function(data, result) {
  111. var character = JSON.parse(result || '{}');
  112. this.characters[data.data.name] = character;
  113. this.getStash(data, character);
  114. },
  115. getStash: function(data, character) {
  116. io.get({
  117. ent: this.username,
  118. field: 'stash',
  119. callback: this.onGetStash.bind(this, data, character)
  120. });
  121. },
  122. onGetStash: function(data, character, result) {
  123. this.stash = JSON.parse(result || '[]');
  124. if (this.skins != null)
  125. data.callback(character);
  126. else {
  127. data.callback = data.callback.bind(null, character);
  128. this.getSkins(data);
  129. }
  130. },
  131. getSkins: function(msg) {
  132. io.get({
  133. ent: this.username,
  134. field: 'skins',
  135. callback: this.onGetSkins.bind(this, msg)
  136. });
  137. },
  138. onGetSkins: function(msg, result) {
  139. this.skins = JSON.parse(result || '[]');
  140. var skinList = skins.getSkinList(this.skins);
  141. msg.callback(skinList);
  142. },
  143. saveSkin: function(skinId) {
  144. if (!this.skins) {
  145. this.getSkins({
  146. callback: this.saveSkin.bind(this, skinId)
  147. });
  148. return;
  149. }
  150. this.skins.push(skinId);
  151. io.set({
  152. ent: this.username,
  153. field: 'skins',
  154. value: JSON.stringify(this.skins),
  155. callback: this.onSaveSkin.bind(this)
  156. });
  157. },
  158. onSaveSkin: function() {
  159. },
  160. doesOwnSkin: function(skinId) {
  161. return this.skins.some(s => s == skinId);
  162. },
  163. login: function(msg) {
  164. var credentials = msg.data;
  165. if ((credentials.username == '') | (credentials.password == '')) {
  166. msg.callback(messages.login.allFields);
  167. return;
  168. }
  169. this.username = credentials.username;
  170. io.get({
  171. ent: credentials.username,
  172. field: 'login',
  173. callback: this.onLogin.bind(this, msg)
  174. });
  175. },
  176. onLogin: function(msg, result) {
  177. var credentials = msg.data;
  178. if (!result)
  179. msg.callback(messages.login.incorrect);
  180. else {
  181. if (result == credentials.password) {
  182. this.username = credentials.username;
  183. connections.logOut(this.obj);
  184. msg.callback();
  185. } else
  186. msg.callback(messages.login.incorrect);
  187. }
  188. },
  189. register: function(msg) {
  190. var credentials = msg.data;
  191. if ((credentials.username == '') || (credentials.password == '')) {
  192. msg.callback(messages.login.allFields);
  193. return;
  194. }
  195. var illegal = ["'", '"', '/', '(', ')', '[', ']', '{', '}', ':', ';', '<', '>'];
  196. for (var i = 0; i < illegal.length; i++) {
  197. if ((credentials.username.indexOf(illegal[i]) > -1) || (credentials.password.indexOf(illegal[i]) > -1)) {
  198. msg.callback(messages.login.illegal);
  199. return;
  200. }
  201. }
  202. io.get({
  203. ent: credentials.username,
  204. field: 'login',
  205. callback: this.onCheckExists.bind(this, msg)
  206. });
  207. },
  208. onCheckExists: function(msg, result) {
  209. if (result) {
  210. msg.callback(messages.login.exists);
  211. return;
  212. }
  213. var credentials = msg.data;
  214. io.set({
  215. ent: credentials.username,
  216. field: 'login',
  217. value: credentials.password,
  218. callback: this.onRegister.bind(this, msg)
  219. });
  220. },
  221. onRegister: function(msg, result) {
  222. io.set({
  223. ent: msg.data.username,
  224. field: 'characterList',
  225. value: '[]',
  226. callback: this.onCreateCharacterList.bind(this, msg)
  227. });
  228. },
  229. onCreateCharacterList: function(msg, result) {
  230. this.username = msg.data.username;
  231. connections.logOut(this.obj);
  232. msg.callback();
  233. },
  234. createCharacter: function(msg) {
  235. var data = msg.data;
  236. io.get({
  237. ent: data.name,
  238. field: 'character',
  239. callback: this.onCheckCharacterExists.bind(this, msg)
  240. });
  241. },
  242. onCheckCharacterExists: function(msg, result) {
  243. if (result) {
  244. msg.callback(messages.login.charExists);
  245. return;
  246. }
  247. var data = msg.data;
  248. this.obj.name = data.name;
  249. this.obj.class = data.class;
  250. this.obj.costume = data.costume;
  251. this.obj.cell = skins.getCell(this.obj.class, this.obj.costume);
  252. this.obj.previewSpritesheet = skins.getSpritesheet(this.obj.class);
  253. var simple = this.obj.getSimple(true);
  254. simple.components.push({
  255. type: 'prophecies',
  256. list: data.prophecies || []
  257. });
  258. io.set({
  259. ent: data.name,
  260. field: 'character',
  261. value: JSON.stringify(simple),
  262. callback: this.onCreateCharacter.bind(this, msg)
  263. });
  264. },
  265. onCreateCharacter: function(msg, result) {
  266. var name = msg.data.name;
  267. var simple = this.obj.getSimple(true);
  268. simple.components.push({
  269. type: 'prophecies',
  270. list: msg.data.prophecies || []
  271. });
  272. this.characters[name] = simple;
  273. this.characterList.push(name);
  274. io.set({
  275. ent: this.username,
  276. field: 'characterList',
  277. value: JSON.stringify(this.characterList),
  278. callback: this.onAppendList.bind(this, msg)
  279. });
  280. },
  281. deleteCharacter: function(msg) {
  282. var data = msg.data;
  283. if ((!data.name) || (!this.username))
  284. return;
  285. if (this.characterList.indexOf(data.name) == -1) {
  286. msg.callback([]);
  287. return;
  288. }
  289. io.delete({
  290. ent: data.name,
  291. field: 'character',
  292. callback: this.onDeleteCharacter.bind(this, msg)
  293. });
  294. },
  295. onDeleteCharacter: function(msg, result) {
  296. this.characterList.spliceWhere(c => c == msg.data.name);
  297. var characterList = this.characterList
  298. .map(c => ({
  299. name: c.name ? c.name : c,
  300. level: leaderboard.getLevel(c.name ? c.name : c)
  301. }));
  302. io.set({
  303. ent: this.username,
  304. field: 'characterList',
  305. value: JSON.stringify(characterList),
  306. callback: this.onRemoveFromList.bind(this, msg)
  307. });
  308. leaderboard.deleteCharacter(msg.data.name);
  309. },
  310. onRemoveFromList: function(msg, result) {
  311. msg.callback(this.characterList);
  312. },
  313. onAppendList: function(msg, result) {
  314. this.play({
  315. data: {
  316. name: msg.data.name
  317. },
  318. callback: msg.callback
  319. });
  320. },
  321. permadie: function() {
  322. this.obj.permadead = true;
  323. this.doSave({
  324. permadead: true,
  325. callback: this.onPermadie.bind(this)
  326. });
  327. },
  328. onPermadie: function() {
  329. process.send({
  330. method: 'object',
  331. serverId: this.obj.serverId,
  332. obj: {
  333. dead: true
  334. }
  335. });
  336. }
  337. };
  338. });