You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

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