Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

429 linhas
9.1 KiB

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