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.
 
 
 

609 lines
12 KiB

  1. //Imports
  2. const bcrypt = require('bcrypt-nodejs');
  3. const messages = require('../misc/messages');
  4. const skins = require('../config/skins');
  5. const profanities = require('../misc/profanities');
  6. const fixes = require('../fixes/fixes');
  7. const spirits = require('../config/spirits');
  8. const ga = require('../security/ga');
  9. const eventEmitter = require('../misc/events');
  10. const checkLoginRewards = require('./auth/checkLoginRewards');
  11. //This section of code is in charge of ensuring that we only ever create one account at a time,
  12. // since we don't have a read/write lock on the characters table, we have to address it in code
  13. const createLockBuffer = [];
  14. const getCreateLock = async () => {
  15. const releaseLock = lockEntry => {
  16. createLockBuffer.spliceWhere(c => c === lockEntry);
  17. const nextEntry = createLockBuffer[0];
  18. if (!nextEntry)
  19. return;
  20. nextEntry.takeLock();
  21. };
  22. const promise = new Promise(async res => {
  23. let lockEntry = {};
  24. lockEntry.takeLock = res.bind(null, releaseLock.bind(null, lockEntry));
  25. if (!createLockBuffer.length) {
  26. createLockBuffer.push(lockEntry);
  27. lockEntry.takeLock();
  28. return;
  29. }
  30. createLockBuffer.push(lockEntry);
  31. });
  32. return promise;
  33. };
  34. //Component Definition
  35. module.exports = {
  36. type: 'auth',
  37. username: null,
  38. charname: null,
  39. characters: {},
  40. characterList: [],
  41. stash: null,
  42. accountInfo: null,
  43. customChannels: [],
  44. play: async function (data) {
  45. if (!this.username || this.charname)
  46. return;
  47. let character = this.characters[data.data.name];
  48. if (!character)
  49. return;
  50. else if (character.permadead)
  51. return;
  52. character.stash = this.stash;
  53. character.account = this.username;
  54. this.charname = character.name;
  55. checkLoginRewards(this, data, character, this.onSendRewards.bind(this, data, character));
  56. cons.modifyPlayerCount(1);
  57. },
  58. onSendRewards: async function (data, character) {
  59. await io.setAsync({
  60. key: this.username,
  61. table: 'accountInfo',
  62. value: this.accountInfo,
  63. serialize: true
  64. });
  65. this.obj.player.sessionStart = +new Date();
  66. this.obj.player.spawn(character, data.callback);
  67. let prophecies = this.obj.prophecies ? this.obj.prophecies.simplify().list : [];
  68. await leaderboard.setLevel(character.name, this.obj.stats.values.level, prophecies);
  69. },
  70. doSave: async function (callback, saveStash = true) {
  71. const simple = this.obj.getSimple(true, true);
  72. delete simple.destroyed;
  73. delete simple.forceDestroy;
  74. simple.components.spliceWhere(f => (f.type === 'stash'));
  75. await io.setAsync({
  76. key: this.charname,
  77. table: 'character',
  78. value: simple,
  79. clean: true,
  80. serialize: true
  81. });
  82. if (saveStash)
  83. await this.doSaveStash();
  84. if (callback)
  85. callback();
  86. },
  87. //This function is called from the 'forceSave' command. Because of this, the first argument is the action data
  88. // instead of (callback, saveStash)
  89. doSaveManual: async function (msg) {
  90. await this.doSave(null, true);
  91. process.send({
  92. module: 'atlas',
  93. method: 'resolveCallback',
  94. msg: {
  95. id: msg.callbackId
  96. }
  97. });
  98. },
  99. doSaveStash: async function () {
  100. const { username, obj: { stash } } = this;
  101. if (!stash.changed)
  102. return;
  103. await io.setAsync({
  104. key: username,
  105. table: 'stash',
  106. value: stash.serialize(),
  107. clean: true,
  108. serialize: true
  109. });
  110. },
  111. simplify: function (self) {
  112. if (!self)
  113. return;
  114. return {
  115. type: 'auth',
  116. username: this.username,
  117. charname: this.charname,
  118. accountInfo: this.accountInfo
  119. };
  120. },
  121. getCharacterList: async function (data) {
  122. if (!this.username)
  123. return;
  124. this.characterList = await io.getAsync({
  125. key: this.username,
  126. table: 'characterList',
  127. isArray: true
  128. });
  129. let res = this.characterList.map(c => ({
  130. name: c.name ? c.name : c,
  131. level: leaderboard.getLevel(c.name ? c.name : c)
  132. }));
  133. data.callback(res);
  134. },
  135. getCharacter: async function (data) {
  136. let charName = data.data.name;
  137. if (!this.characterList.some(c => (c.name === charName || c === charName)))
  138. return;
  139. let character = await io.getAsync({
  140. key: charName,
  141. table: 'character',
  142. clean: true
  143. });
  144. eventEmitter.emit('onAfterGetCharacter', {
  145. obj: this.obj,
  146. character
  147. });
  148. fixes.fixCharacter(character);
  149. character.cell = skins.getCell(character.skinId);
  150. character.sheetName = skins.getSpritesheet(character.skinId);
  151. this.characters[charName] = character;
  152. await this.getCustomChannels(character);
  153. await this.verifySkin(character);
  154. data.callback(character);
  155. },
  156. getCustomChannels: async function (character) {
  157. this.customChannels = await io.getAsync({
  158. key: character.name,
  159. table: 'customChannels',
  160. isArray: true
  161. });
  162. let social = character.components.find(c => (c.type === 'social'));
  163. this.customChannels = fixes.fixCustomChannels(this.customChannels);
  164. if (social)
  165. social.customChannels = this.customChannels;
  166. },
  167. getStash: async function (data, character) {
  168. this.stash = await io.getAsync({
  169. key: this.username,
  170. table: 'stash',
  171. isArray: true,
  172. clean: true
  173. });
  174. fixes.fixStash(this.stash);
  175. await eventEmitter.emit('onAfterGetStash', {
  176. obj: this.obj,
  177. stash: this.stash
  178. });
  179. },
  180. verifySkin: async function (character) {
  181. const doesOwn = await this.doesOwnSkin(character.skinId);
  182. if (doesOwn)
  183. return;
  184. const defaultTo = 'wizard';
  185. character.skinId = defaultTo;
  186. character.cell = skins.getCell(defaultTo);
  187. character.sheetName = skins.getSpritesheet(defaultTo);
  188. },
  189. doesOwnSkin: async function (skinId) {
  190. const allSkins = skins.getList();
  191. const filteredSkins = allSkins.filter(({ default: isDefaultSkin }) => isDefaultSkin);
  192. const msgSkinList = {
  193. obj: this,
  194. allSkins,
  195. filteredSkins
  196. };
  197. await eventEmitter.emit('onBeforeGetAccountSkins', msgSkinList);
  198. const result = filteredSkins.some(f => f.id === skinId);
  199. return result;
  200. },
  201. getSkinList: async function ({ callback }) {
  202. const allSkins = skins.getList();
  203. const filteredSkins = allSkins.filter(({ default: isDefaultSkin }) => isDefaultSkin);
  204. const msgSkinList = {
  205. obj: this,
  206. allSkins,
  207. filteredSkins
  208. };
  209. await eventEmitter.emit('onBeforeGetAccountSkins', msgSkinList);
  210. callback(filteredSkins);
  211. },
  212. login: async function (msg) {
  213. let credentials = msg.data;
  214. if (credentials.username === '' || credentials.password === '') {
  215. msg.callback(messages.login.allFields);
  216. return;
  217. } else if (credentials.username.length > 32) {
  218. msg.callback(messages.login.maxUsernameLength);
  219. return;
  220. }
  221. let storedPassword = await io.getAsync({
  222. key: credentials.username,
  223. table: 'login',
  224. noParse: true
  225. });
  226. bcrypt.compare(credentials.password, storedPassword, this.onLogin.bind(this, msg, storedPassword));
  227. },
  228. onLogin: async function (msg, storedPassword, err, compareResult) {
  229. const { data: { username } } = msg;
  230. if (!compareResult) {
  231. msg.callback(messages.login.incorrect);
  232. return;
  233. }
  234. const emBeforeLogin = {
  235. obj: this.obj,
  236. success: true,
  237. msg: null
  238. };
  239. await eventEmitter.emit('onBeforeLogin', emBeforeLogin);
  240. if (!emBeforeLogin.success) {
  241. msg.callback(emBeforeLogin.msg);
  242. return;
  243. }
  244. this.username = username;
  245. await cons.logOut(this.obj);
  246. this.initTracker();
  247. const accountInfo = await io.getAsync({
  248. key: username,
  249. table: 'accountInfo',
  250. noDefault: true
  251. }) || {
  252. loginStreak: 0,
  253. level: 0
  254. };
  255. const msgAccountInfo = {
  256. username,
  257. accountInfo
  258. };
  259. await eventEmitter.emit('onBeforeGetAccountInfo', msgAccountInfo);
  260. await eventEmitter.emit('onAfterLogin', { username });
  261. this.accountInfo = msgAccountInfo.accountInfo;
  262. msg.callback();
  263. },
  264. initTracker: function () {
  265. this.gaTracker = ga.connect(this.username);
  266. },
  267. track: function (category, action, label, value = 1) {
  268. process.send({
  269. method: 'track',
  270. serverId: this.obj.serverId,
  271. obj: {
  272. category,
  273. action,
  274. label,
  275. value
  276. }
  277. });
  278. },
  279. register: async function (msg) {
  280. let credentials = msg.data;
  281. if (credentials.username === '' || credentials.password === '') {
  282. msg.callback(messages.login.allFields);
  283. return;
  284. } else if (credentials.username.length > 32) {
  285. msg.callback(messages.login.maxUsernameLength);
  286. return;
  287. }
  288. let illegal = ["'", '"', '/', '\\', '(', ')', '[', ']', '{', '}', ':', ';', '<', '>', '+', '?', '*'];
  289. for (let i = 0; i < illegal.length; i++) {
  290. if (credentials.username.indexOf(illegal[i]) > -1) {
  291. msg.callback(messages.login.illegal);
  292. return;
  293. }
  294. }
  295. const emBeforeRegisterAccount = {
  296. obj: this.obj,
  297. success: true,
  298. msg: null
  299. };
  300. await eventEmitter.emit('onBeforeRegisterAccount', emBeforeRegisterAccount);
  301. if (!emBeforeRegisterAccount.success) {
  302. msg.callback(emBeforeRegisterAccount.msg);
  303. return;
  304. }
  305. let exists = await io.getAsync({
  306. key: credentials.username,
  307. ignoreCase: true,
  308. table: 'login',
  309. noDefault: true,
  310. noParse: true
  311. });
  312. if (exists) {
  313. msg.callback(messages.login.exists);
  314. return;
  315. }
  316. bcrypt.hash(credentials.password, null, null, this.onHashGenerated.bind(this, msg));
  317. },
  318. onHashGenerated: async function (msg, err, hashedPassword) {
  319. await io.setAsync({
  320. key: msg.data.username,
  321. table: 'login',
  322. value: hashedPassword
  323. });
  324. this.accountInfo = {
  325. loginStreak: 0,
  326. level: 0
  327. };
  328. await io.setAsync({
  329. key: msg.data.username,
  330. table: 'characterList',
  331. value: [],
  332. serialize: true
  333. });
  334. this.username = msg.data.username;
  335. cons.logOut(this.obj);
  336. msg.callback();
  337. },
  338. createCharacter: async function (msg) {
  339. let data = msg.data;
  340. let name = data.name;
  341. let error = null;
  342. if (name.length < 3 || name.length > 12)
  343. error = messages.createCharacter.nameLength;
  344. else if (!profanities.isClean(name))
  345. error = messages.login.invalid;
  346. else if (name.indexOf(' ') > -1)
  347. msg.callback(messages.login.invalid);
  348. else if (!spirits.list.includes(data.class))
  349. return;
  350. let nLen = name.length;
  351. for (let i = 0; i < nLen; i++) {
  352. let char = name[i].toLowerCase();
  353. let valid = [
  354. 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
  355. ];
  356. if (!valid.includes(char)) {
  357. error = messages.login.invalid;
  358. break;
  359. }
  360. }
  361. if (error) {
  362. msg.callback(error);
  363. return;
  364. }
  365. const releaseCreateLock = await getCreateLock();
  366. let exists = await io.getAsync({
  367. key: name,
  368. ignoreCase: true,
  369. table: 'character',
  370. noDefault: true
  371. });
  372. if (exists) {
  373. releaseCreateLock();
  374. msg.callback(messages.login.charExists);
  375. return;
  376. }
  377. let obj = this.obj;
  378. extend(obj, {
  379. name: name,
  380. skinId: data.skinId,
  381. class: data.class,
  382. cell: skins.getCell(data.skinId),
  383. sheetName: skins.getSpritesheet(data.skinId),
  384. x: null,
  385. y: null
  386. });
  387. let simple = this.obj.getSimple(true);
  388. await this.verifySkin(simple);
  389. let prophecies = (data.prophecies || []).filter(p => p);
  390. simple.components.push({
  391. type: 'prophecies',
  392. list: prophecies
  393. }, {
  394. type: 'social',
  395. customChannels: this.customChannels
  396. });
  397. await io.setAsync({
  398. key: name,
  399. table: 'character',
  400. value: simple,
  401. serialize: true
  402. });
  403. this.characters[name] = simple;
  404. this.characterList.push(name);
  405. await io.setAsync({
  406. key: this.username,
  407. table: 'characterList',
  408. value: this.characterList,
  409. serialize: true
  410. });
  411. releaseCreateLock();
  412. this.initTracker();
  413. this.play({
  414. data: {
  415. name: name
  416. },
  417. callback: msg.callback
  418. });
  419. },
  420. deleteCharacter: async function (msg) {
  421. let data = msg.data;
  422. if ((!data.name) || (!this.username))
  423. return;
  424. if (!this.characterList.some(c => ((c.name === data.name) || (c === data.name)))) {
  425. msg.callback([]);
  426. return;
  427. }
  428. await io.deleteAsync({
  429. key: data.name,
  430. table: 'character'
  431. });
  432. let name = data.name;
  433. this.characterList.spliceWhere(c => (c.name === name || c === name));
  434. let characterList = this.characterList
  435. .map(c => ({
  436. name: c.name ? c.name : c,
  437. level: leaderboard.getLevel(c.name ? c.name : c)
  438. }));
  439. await io.setAsync({
  440. key: this.username,
  441. table: 'characterList',
  442. value: characterList,
  443. serialize: true
  444. });
  445. await leaderboard.deleteCharacter(name);
  446. let result = this.characterList
  447. .map(c => ({
  448. name: c.name ? c.name : c,
  449. level: leaderboard.getLevel(c.name ? c.name : c)
  450. }));
  451. msg.callback(result);
  452. },
  453. permadie: function () {
  454. this.obj.permadead = true;
  455. this.doSave(this.onPermadie.bind(this));
  456. },
  457. onPermadie: function () {
  458. process.send({
  459. method: 'object',
  460. serverId: this.obj.serverId,
  461. obj: {
  462. dead: true
  463. }
  464. });
  465. },
  466. getAccountLevel: function () {
  467. return this.accountInfo.level;
  468. }
  469. };