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.
 
 
 

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