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.
 
 
 

622 lines
13 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) {
  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. await this.doSaveStash();
  83. if (callback)
  84. callback();
  85. },
  86. //This function is called from the 'forceSave' command. Because of this, the first argument is the action data
  87. // instead of (callback, saveStash)
  88. doSaveManual: async function (msg) {
  89. await this.doSave();
  90. process.send({
  91. module: 'atlas',
  92. method: 'resolveCallback',
  93. msg: {
  94. id: msg.callbackId
  95. }
  96. });
  97. },
  98. doSaveStash: async function () {
  99. const { username, obj: { stash } } = this;
  100. if (!stash.changed)
  101. return;
  102. await io.setAsync({
  103. key: username,
  104. table: 'stash',
  105. value: stash.serialize(),
  106. clean: true,
  107. serialize: true
  108. });
  109. },
  110. simplify: function (self) {
  111. if (!self)
  112. return;
  113. return {
  114. type: 'auth',
  115. username: this.username,
  116. charname: this.charname,
  117. accountInfo: this.accountInfo
  118. };
  119. },
  120. getCharacterList: async function (data) {
  121. if (!this.username)
  122. return;
  123. this.characterList = await io.getAsync({
  124. key: this.username,
  125. table: 'characterList',
  126. isArray: true
  127. });
  128. let res = this.characterList.map(c => ({
  129. name: c.name ? c.name : c,
  130. level: leaderboard.getLevel(c.name ? c.name : c)
  131. }));
  132. data.callback(res);
  133. },
  134. getCharacter: async function (data) {
  135. let charName = data.data.name;
  136. if (!this.characterList.some(c => (c.name === charName || c === charName)))
  137. return;
  138. let character = await io.getAsync({
  139. key: charName,
  140. table: 'character',
  141. clean: true
  142. });
  143. await eventEmitter.emit('onAfterGetCharacter', {
  144. obj: this.obj,
  145. character
  146. });
  147. fixes.fixCharacter(character);
  148. character.cell = skins.getCell(character.skinId);
  149. character.sheetName = skins.getSpritesheet(character.skinId);
  150. this.characters[charName] = character;
  151. await this.getCustomChannels(character);
  152. await this.verifySkin(character);
  153. data.callback(character);
  154. },
  155. getCustomChannels: async function (character) {
  156. this.customChannels = await io.getAsync({
  157. key: character.name,
  158. table: 'customChannels',
  159. isArray: true
  160. });
  161. let social = character.components.find(c => (c.type === 'social'));
  162. this.customChannels = fixes.fixCustomChannels(this.customChannels);
  163. if (social)
  164. social.customChannels = this.customChannels;
  165. },
  166. verifySkin: async function (character) {
  167. const doesOwn = await this.doesOwnSkin(character.skinId);
  168. if (doesOwn)
  169. return;
  170. const defaultTo = 'wizard';
  171. character.skinId = defaultTo;
  172. character.cell = skins.getCell(defaultTo);
  173. character.sheetName = skins.getSpritesheet(defaultTo);
  174. },
  175. doesOwnSkin: async function (skinId) {
  176. const allSkins = skins.getList();
  177. const filteredSkins = allSkins.filter(({ default: isDefaultSkin }) => isDefaultSkin);
  178. const msgSkinList = {
  179. obj: this,
  180. allSkins,
  181. filteredSkins
  182. };
  183. await eventEmitter.emit('onBeforeGetAccountSkins', msgSkinList);
  184. const result = filteredSkins.some(f => f.id === skinId);
  185. return result;
  186. },
  187. getSkinList: async function ({ callback }) {
  188. const allSkins = skins.getList();
  189. const filteredSkins = allSkins.filter(({ default: isDefaultSkin }) => isDefaultSkin);
  190. const msgSkinList = {
  191. obj: this,
  192. allSkins,
  193. filteredSkins
  194. };
  195. await eventEmitter.emit('onBeforeGetAccountSkins', msgSkinList);
  196. callback(filteredSkins);
  197. },
  198. login: async function (msg) {
  199. let credentials = msg.data;
  200. if (credentials.username === '' || credentials.password === '') {
  201. msg.callback(messages.login.allFields);
  202. return;
  203. } else if (credentials.username.length > 32) {
  204. msg.callback(messages.login.maxUsernameLength);
  205. return;
  206. }
  207. let storedPassword = await io.getAsync({
  208. key: credentials.username,
  209. table: 'login',
  210. noParse: true
  211. });
  212. bcrypt.compare(credentials.password, storedPassword, this.onLogin.bind(this, msg, storedPassword));
  213. },
  214. onLogin: async function (msg, storedPassword, err, compareResult) {
  215. const { data: { username } } = msg;
  216. if (!compareResult) {
  217. msg.callback(messages.login.incorrect);
  218. return;
  219. }
  220. const emBeforeLogin = {
  221. obj: this.obj,
  222. success: true,
  223. msg: null,
  224. username
  225. };
  226. await eventEmitter.emit('onBeforeLogin', emBeforeLogin);
  227. if (!emBeforeLogin.success) {
  228. msg.callback(emBeforeLogin.msg);
  229. return;
  230. }
  231. this.username = username;
  232. await cons.logOut(this.obj);
  233. this.initTracker();
  234. const accountInfo = await io.getAsync({
  235. key: username,
  236. table: 'accountInfo',
  237. noDefault: true
  238. }) || {
  239. loginStreak: 0,
  240. level: 0
  241. };
  242. const msgAccountInfo = {
  243. username,
  244. accountInfo
  245. };
  246. await eventEmitter.emit('onBeforeGetAccountInfo', msgAccountInfo);
  247. await eventEmitter.emit('onAfterLogin', { username });
  248. this.accountInfo = msgAccountInfo.accountInfo;
  249. msg.callback();
  250. },
  251. initTracker: function () {
  252. this.gaTracker = ga.connect(this.username);
  253. },
  254. track: function (category, action, label, value = 1) {
  255. process.send({
  256. method: 'track',
  257. serverId: this.obj.serverId,
  258. obj: {
  259. category,
  260. action,
  261. label,
  262. value
  263. }
  264. });
  265. },
  266. register: async function (msg) {
  267. let credentials = msg.data;
  268. if (credentials.username === '' || credentials.password === '') {
  269. msg.callback(messages.login.allFields);
  270. return;
  271. } else if (credentials.username.length > 32) {
  272. msg.callback(messages.login.maxUsernameLength);
  273. return;
  274. }
  275. let illegal = ["'", '"', '/', '\\', '(', ')', '[', ']', '{', '}', ':', ';', '<', '>', '+', '?', '*'];
  276. for (let i = 0; i < illegal.length; i++) {
  277. if (credentials.username.indexOf(illegal[i]) > -1) {
  278. msg.callback(messages.login.illegal);
  279. return;
  280. }
  281. }
  282. const emBeforeRegisterAccount = {
  283. obj: this.obj,
  284. success: true,
  285. msg: null,
  286. username: msg.data.username
  287. };
  288. await eventEmitter.emit('onBeforeRegisterAccount', emBeforeRegisterAccount);
  289. if (!emBeforeRegisterAccount.success) {
  290. msg.callback(emBeforeRegisterAccount.msg);
  291. return;
  292. }
  293. let exists = await io.getAsync({
  294. key: credentials.username,
  295. ignoreCase: true,
  296. table: 'login',
  297. noDefault: true,
  298. noParse: true
  299. });
  300. if (exists) {
  301. msg.callback(messages.login.exists);
  302. return;
  303. }
  304. bcrypt.hash(credentials.password, null, null, this.onHashGenerated.bind(this, msg));
  305. },
  306. onHashGenerated: async function (msg, err, hashedPassword) {
  307. await io.setAsync({
  308. key: msg.data.username,
  309. table: 'login',
  310. value: hashedPassword
  311. });
  312. this.accountInfo = {
  313. loginStreak: 0,
  314. level: 0
  315. };
  316. await io.setAsync({
  317. key: msg.data.username,
  318. table: 'characterList',
  319. value: [],
  320. serialize: true
  321. });
  322. this.username = msg.data.username;
  323. cons.logOut(this.obj);
  324. msg.callback();
  325. },
  326. createCharacter: async function (msg) {
  327. let data = msg.data;
  328. let name = data.name;
  329. let error = null;
  330. if (name.length < 3 || name.length > 12)
  331. error = messages.createCharacter.nameLength;
  332. else if (!profanities.isClean(name))
  333. error = messages.login.invalid;
  334. else if (name.indexOf(' ') > -1)
  335. msg.callback(messages.login.invalid);
  336. else if (!spirits.list.includes(data.class))
  337. return;
  338. let nLen = name.length;
  339. for (let i = 0; i < nLen; i++) {
  340. let char = name[i].toLowerCase();
  341. let valid = [
  342. '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'
  343. ];
  344. if (!valid.includes(char)) {
  345. error = messages.login.invalid;
  346. break;
  347. }
  348. }
  349. if (error) {
  350. msg.callback(error);
  351. return;
  352. }
  353. const releaseCreateLock = await getCreateLock();
  354. let exists = await io.getAsync({
  355. key: name,
  356. ignoreCase: true,
  357. table: 'character',
  358. noDefault: true
  359. });
  360. if (exists) {
  361. releaseCreateLock();
  362. msg.callback(messages.login.charExists);
  363. return;
  364. }
  365. let obj = this.obj;
  366. extend(obj, {
  367. name: name,
  368. skinId: data.skinId,
  369. class: data.class,
  370. cell: skins.getCell(data.skinId),
  371. sheetName: skins.getSpritesheet(data.skinId),
  372. x: null,
  373. y: null
  374. });
  375. let simple = this.obj.getSimple(true);
  376. await this.verifySkin(simple);
  377. let prophecies = (data.prophecies || []).filter(p => p);
  378. simple.components.push({
  379. type: 'prophecies',
  380. list: prophecies
  381. }, {
  382. type: 'social',
  383. customChannels: this.customChannels
  384. });
  385. const eBeforeSaveCharacter = {
  386. obj: simple,
  387. config: data
  388. };
  389. eventEmitter.emit('beforeSaveCharacter', eBeforeSaveCharacter);
  390. await io.setAsync({
  391. key: name,
  392. table: 'character',
  393. value: eBeforeSaveCharacter.obj,
  394. serialize: true
  395. });
  396. this.characters[name] = simple;
  397. this.characterList.push(name);
  398. await io.setAsync({
  399. key: this.username,
  400. table: 'characterList',
  401. value: this.characterList,
  402. serialize: true
  403. });
  404. releaseCreateLock();
  405. this.initTracker();
  406. this.play({
  407. data: {
  408. name: name
  409. },
  410. callback: msg.callback
  411. });
  412. },
  413. deleteCharacter: async function (msg) {
  414. let data = msg.data;
  415. if ((!data.name) || (!this.username))
  416. return;
  417. if (!this.characterList.some(c => ((c.name === data.name) || (c === data.name)))) {
  418. msg.callback([]);
  419. return;
  420. }
  421. const msgBeforeDeleteCharacter = {
  422. obj: this,
  423. name: data.name,
  424. success: true,
  425. msg: null
  426. };
  427. await eventEmitter.emit('beforeDeleteCharacter', msgBeforeDeleteCharacter);
  428. if (!msgBeforeDeleteCharacter.success) {
  429. msg.callback({
  430. success: false,
  431. msg: msgBeforeDeleteCharacter.msg
  432. });
  433. return;
  434. }
  435. await io.deleteAsync({
  436. key: data.name,
  437. table: 'character'
  438. });
  439. let name = data.name;
  440. this.characterList.spliceWhere(c => (c.name === name || c === name));
  441. let characterList = this.characterList
  442. .map(c => ({
  443. name: c.name ? c.name : c,
  444. level: leaderboard.getLevel(c.name ? c.name : c)
  445. }));
  446. await io.setAsync({
  447. key: this.username,
  448. table: 'characterList',
  449. value: characterList,
  450. serialize: true
  451. });
  452. await leaderboard.deleteCharacter(name);
  453. let result = this.characterList
  454. .map(c => ({
  455. name: c.name ? c.name : c,
  456. level: leaderboard.getLevel(c.name ? c.name : c)
  457. }));
  458. msg.callback({
  459. success: true,
  460. characterList: result
  461. });
  462. },
  463. permadie: function () {
  464. this.obj.permadead = true;
  465. this.doSave(this.onPermadie.bind(this));
  466. },
  467. onPermadie: function () {
  468. process.send({
  469. method: 'object',
  470. serverId: this.obj.serverId,
  471. obj: {
  472. dead: true
  473. }
  474. });
  475. },
  476. getAccountLevel: function () {
  477. return this.accountInfo.level;
  478. }
  479. };