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.
 
 
 

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