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.
 
 
 

595 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. 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. } else if (credentials.username.length > 32) {
  206. msg.callback(messages.login.maxUsernameLength);
  207. return;
  208. }
  209. let storedPassword = await io.getAsync({
  210. key: credentials.username,
  211. table: 'login',
  212. noParse: true
  213. });
  214. bcrypt.compare(credentials.password, storedPassword, this.onLogin.bind(this, msg, storedPassword));
  215. },
  216. onLogin: async function (msg, storedPassword, err, compareResult) {
  217. const { data: { username } } = msg;
  218. if (!compareResult) {
  219. msg.callback(messages.login.incorrect);
  220. return;
  221. }
  222. const emBeforeLogin = {
  223. obj: this.obj,
  224. success: true,
  225. msg: null
  226. };
  227. await eventEmitter.emit('onBeforeLogin', emBeforeLogin);
  228. if (!emBeforeLogin.success) {
  229. msg.callback(emBeforeLogin.msg);
  230. return;
  231. }
  232. this.username = username;
  233. await cons.logOut(this.obj);
  234. this.initTracker();
  235. const accountInfo = await io.getAsync({
  236. key: username,
  237. table: 'accountInfo',
  238. noDefault: true
  239. }) || {
  240. loginStreak: 0,
  241. level: 0
  242. };
  243. const msgAccountInfo = {
  244. username,
  245. accountInfo
  246. };
  247. await eventEmitter.emit('onBeforeGetAccountInfo', msgAccountInfo);
  248. await eventEmitter.emit('onAfterLogin', { username });
  249. this.accountInfo = msgAccountInfo.accountInfo;
  250. msg.callback();
  251. },
  252. initTracker: function () {
  253. this.gaTracker = ga.connect(this.username);
  254. },
  255. track: function (category, action, label, value = 1) {
  256. process.send({
  257. method: 'track',
  258. serverId: this.obj.serverId,
  259. obj: {
  260. category,
  261. action,
  262. label,
  263. value
  264. }
  265. });
  266. },
  267. register: async function (msg) {
  268. let credentials = msg.data;
  269. if (credentials.username === '' || credentials.password === '') {
  270. msg.callback(messages.login.allFields);
  271. return;
  272. } else if (credentials.username.length > 32) {
  273. msg.callback(messages.login.maxUsernameLength);
  274. return;
  275. }
  276. let illegal = ["'", '"', '/', '\\', '(', ')', '[', ']', '{', '}', ':', ';', '<', '>', '+', '?', '*'];
  277. for (let i = 0; i < illegal.length; i++) {
  278. if (credentials.username.indexOf(illegal[i]) > -1) {
  279. msg.callback(messages.login.illegal);
  280. return;
  281. }
  282. }
  283. const emBeforeRegisterAccount = {
  284. obj: this.obj,
  285. success: true,
  286. msg: null
  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. await io.setAsync({
  386. key: name,
  387. table: 'character',
  388. value: simple,
  389. serialize: true
  390. });
  391. this.characters[name] = simple;
  392. this.characterList.push(name);
  393. await io.setAsync({
  394. key: this.username,
  395. table: 'characterList',
  396. value: this.characterList,
  397. serialize: true
  398. });
  399. releaseCreateLock();
  400. this.initTracker();
  401. this.play({
  402. data: {
  403. name: name
  404. },
  405. callback: msg.callback
  406. });
  407. },
  408. deleteCharacter: async function (msg) {
  409. let data = msg.data;
  410. if ((!data.name) || (!this.username))
  411. return;
  412. if (!this.characterList.some(c => ((c.name === data.name) || (c === data.name)))) {
  413. msg.callback([]);
  414. return;
  415. }
  416. await io.deleteAsync({
  417. key: data.name,
  418. table: 'character'
  419. });
  420. let name = data.name;
  421. this.characterList.spliceWhere(c => (c.name === name || c === name));
  422. let characterList = this.characterList
  423. .map(c => ({
  424. name: c.name ? c.name : c,
  425. level: leaderboard.getLevel(c.name ? c.name : c)
  426. }));
  427. await io.setAsync({
  428. key: this.username,
  429. table: 'characterList',
  430. value: characterList,
  431. serialize: true
  432. });
  433. await leaderboard.deleteCharacter(name);
  434. let result = this.characterList
  435. .map(c => ({
  436. name: c.name ? c.name : c,
  437. level: leaderboard.getLevel(c.name ? c.name : c)
  438. }));
  439. msg.callback(result);
  440. },
  441. permadie: function () {
  442. this.obj.permadead = true;
  443. this.doSave(this.onPermadie.bind(this));
  444. },
  445. onPermadie: function () {
  446. process.send({
  447. method: 'object',
  448. serverId: this.obj.serverId,
  449. obj: {
  450. dead: true
  451. }
  452. });
  453. },
  454. getAccountLevel: function () {
  455. return this.accountInfo.level;
  456. }
  457. };