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.
 
 
 

549 line
11 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. this.username = username;
  216. await cons.logOut(this.obj);
  217. this.initTracker();
  218. const accountInfo = await io.getAsync({
  219. key: username,
  220. table: 'accountInfo',
  221. noDefault: true
  222. }) || {
  223. loginStreak: 0
  224. };
  225. const msgAccountInfo = {
  226. username,
  227. accountInfo
  228. };
  229. await eventEmitter.emit('onBeforeGetAccountInfo', msgAccountInfo);
  230. await eventEmitter.emit('onAfterLogin', { username });
  231. this.accountInfo = msgAccountInfo.accountInfo;
  232. msg.callback();
  233. },
  234. initTracker: function () {
  235. this.gaTracker = ga.connect(this.username);
  236. },
  237. track: function (category, action, label, value = 1) {
  238. process.send({
  239. method: 'track',
  240. serverId: this.obj.serverId,
  241. obj: {
  242. category,
  243. action,
  244. label,
  245. value
  246. }
  247. });
  248. },
  249. register: async function (msg) {
  250. let credentials = msg.data;
  251. if ((credentials.username === '') || (credentials.password === '')) {
  252. msg.callback(messages.login.allFields);
  253. return;
  254. }
  255. let illegal = ["'", '"', '/', '\\', '(', ')', '[', ']', '{', '}', ':', ';', '<', '>', '+', '?', '*'];
  256. for (let i = 0; i < illegal.length; i++) {
  257. if (credentials.username.indexOf(illegal[i]) > -1) {
  258. msg.callback(messages.login.illegal);
  259. return;
  260. }
  261. }
  262. let exists = await io.getAsync({
  263. key: credentials.username,
  264. ignoreCase: true,
  265. table: 'login',
  266. noDefault: true,
  267. noParse: true
  268. });
  269. if (exists) {
  270. msg.callback(messages.login.exists);
  271. return;
  272. }
  273. bcrypt.hash(credentials.password, null, null, this.onHashGenerated.bind(this, msg));
  274. },
  275. onHashGenerated: async function (msg, err, hashedPassword) {
  276. await io.setAsync({
  277. key: msg.data.username,
  278. table: 'login',
  279. value: hashedPassword
  280. });
  281. this.accountInfo = {
  282. loginStreak: 0
  283. };
  284. await io.setAsync({
  285. key: msg.data.username,
  286. table: 'characterList',
  287. value: [],
  288. serialize: true
  289. });
  290. this.username = msg.data.username;
  291. cons.logOut(this.obj);
  292. msg.callback();
  293. },
  294. createCharacter: async function (msg) {
  295. let data = msg.data;
  296. let name = data.name;
  297. let error = null;
  298. if (name.length < 3 || name.length > 12)
  299. error = messages.createCharacter.nameLength;
  300. else if (!profanities.isClean(name))
  301. error = messages.login.invalid;
  302. else if (name.indexOf(' ') > -1)
  303. msg.callback(messages.login.invalid);
  304. else if (!spirits.list.includes(data.class))
  305. return;
  306. let nLen = name.length;
  307. for (let i = 0; i < nLen; i++) {
  308. let char = name[i].toLowerCase();
  309. let valid = [
  310. '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'
  311. ];
  312. if (!valid.includes(char)) {
  313. error = messages.login.invalid;
  314. break;
  315. }
  316. }
  317. if (error) {
  318. msg.callback(error);
  319. return;
  320. }
  321. const releaseCreateLock = await getCreateLock();
  322. let exists = await io.getAsync({
  323. key: name,
  324. ignoreCase: true,
  325. table: 'character',
  326. noDefault: true
  327. });
  328. if (exists) {
  329. releaseCreateLock();
  330. msg.callback(messages.login.charExists);
  331. return;
  332. }
  333. let obj = this.obj;
  334. extend(obj, {
  335. name: name,
  336. skinId: data.skinId,
  337. class: data.class,
  338. cell: skins.getCell(data.skinId),
  339. sheetName: skins.getSpritesheet(data.skinId),
  340. x: null,
  341. y: null
  342. });
  343. let simple = this.obj.getSimple(true);
  344. await this.verifySkin(simple);
  345. let prophecies = (data.prophecies || []).filter(p => p);
  346. simple.components.push({
  347. type: 'prophecies',
  348. list: prophecies
  349. }, {
  350. type: 'social',
  351. customChannels: this.customChannels
  352. });
  353. await io.setAsync({
  354. key: name,
  355. table: 'character',
  356. value: simple,
  357. serialize: true
  358. });
  359. this.characters[name] = simple;
  360. this.characterList.push(name);
  361. await io.setAsync({
  362. key: this.username,
  363. table: 'characterList',
  364. value: this.characterList,
  365. serialize: true
  366. });
  367. releaseCreateLock();
  368. this.initTracker();
  369. this.play({
  370. data: {
  371. name: name
  372. },
  373. callback: msg.callback
  374. });
  375. },
  376. deleteCharacter: async function (msg) {
  377. let data = msg.data;
  378. if ((!data.name) || (!this.username))
  379. return;
  380. if (!this.characterList.some(c => ((c.name === data.name) || (c === data.name)))) {
  381. msg.callback([]);
  382. return;
  383. }
  384. await io.deleteAsync({
  385. key: data.name,
  386. table: 'character'
  387. });
  388. let name = data.name;
  389. this.characterList.spliceWhere(c => (c.name === name || c === name));
  390. let characterList = this.characterList
  391. .map(c => ({
  392. name: c.name ? c.name : c,
  393. level: leaderboard.getLevel(c.name ? c.name : c)
  394. }));
  395. await io.setAsync({
  396. key: this.username,
  397. table: 'characterList',
  398. value: characterList,
  399. serialize: true
  400. });
  401. await leaderboard.deleteCharacter(name);
  402. let result = this.characterList
  403. .map(c => ({
  404. name: c.name ? c.name : c,
  405. level: leaderboard.getLevel(c.name ? c.name : c)
  406. }));
  407. msg.callback(result);
  408. },
  409. permadie: function () {
  410. this.obj.permadead = true;
  411. this.doSave(this.onPermadie.bind(this));
  412. },
  413. onPermadie: function () {
  414. process.send({
  415. method: 'object',
  416. serverId: this.obj.serverId,
  417. obj: {
  418. dead: true
  419. }
  420. });
  421. },
  422. getAccountLevel: function () {
  423. return this.accountInfo.level;
  424. }
  425. };