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.
 
 
 

242 lines
5.9 KiB

  1. let atlas = require('../world/atlas');
  2. let classes = require('../config/spirits');
  3. let roles = require('../config/roles');
  4. let serverConfig = require('../config/serverConfig');
  5. module.exports = {
  6. type: 'player',
  7. seen: [],
  8. cdSave: 1000,
  9. cdSaveMax: 1000,
  10. update: function () {
  11. if (this.cdSave > 0)
  12. this.cdSave--;
  13. else {
  14. this.cdSave = this.cdSaveMax;
  15. this.obj.auth.doSave();
  16. }
  17. },
  18. spawn: function (character, cb) {
  19. let obj = this.obj;
  20. if (character.dead)
  21. obj.dead = true;
  22. extend(true, obj, {
  23. layerName: 'mobs',
  24. cell: character.cell,
  25. sheetName: character.sheetName,
  26. skinId: character.skinId,
  27. name: character.name,
  28. class: character.class,
  29. zoneName: character.zoneName || serverConfig.defaultZone,
  30. x: character.x,
  31. y: character.y,
  32. hidden: character.dead,
  33. account: character.account,
  34. instanceId: character.instanceId
  35. });
  36. character.components = character.components || [];
  37. roles.onBeforePlayerEnterGame(obj, character);
  38. let blueprintStats = character.components.find(c => c.type === 'stats') || {};
  39. extend(true, blueprintStats, classes.stats[obj.class]);
  40. blueprintStats.values.hpMax = (blueprintStats.values.level || 1) * 32.7;
  41. if (!blueprintStats.values.hp)
  42. blueprintStats.values.hp = blueprintStats.values.hpMax;
  43. let stats = obj.addComponent('stats');
  44. for (let s in blueprintStats.values)
  45. stats.values[s] = blueprintStats.values[s];
  46. for (let s in blueprintStats.stats)
  47. stats.stats[s] = blueprintStats.stats[s];
  48. let gainStats = classes.stats[character.class].gainStats;
  49. for (let s in gainStats)
  50. stats.values[s] += (gainStats[s] * stats.values.level);
  51. obj.portrait = classes.portraits[character.class];
  52. obj.addComponent('spellbook');
  53. obj.addComponent('dialogue');
  54. obj.addComponent('trade', character.components.find(c => c.type === 'trade'));
  55. obj.addComponent('reputation', character.components.find(c => c.type === 'reputation'));
  56. let social = character.components.find(c => c.type === 'social');
  57. if (social)
  58. delete social.party;
  59. obj.addComponent('social', social);
  60. obj.social.init();
  61. obj.social.party = null;
  62. obj.addComponent('aggro', {
  63. faction: 'players'
  64. });
  65. obj.addComponent('gatherer');
  66. obj.addComponent('stash', {
  67. items: character.stash
  68. });
  69. let blueprintEffects = character.components.find(c => c.type === 'effects') || {};
  70. if (blueprintEffects.effects) {
  71. //Calculate ttl of effects
  72. let time = +new Date();
  73. blueprintEffects.effects = blueprintEffects.effects.filter(function (e) {
  74. let remaining = e.expire - time;
  75. if (remaining < 0)
  76. return false;
  77. e.ttl = Math.max(~~(remaining / 350), 1);
  78. return true;
  79. });
  80. }
  81. obj.addComponent('effects', blueprintEffects);
  82. let prophecies = character.components.find(c => c.type === 'prophecies');
  83. if (prophecies)
  84. obj.addComponent('prophecies', prophecies);
  85. obj.addComponent('equipment', character.components.find(c => c.type === 'equipment'));
  86. obj.addComponent('inventory', character.components.find(c => c.type === 'inventory'));
  87. obj.addComponent('passives', character.components.find(c => c.type === 'passives'));
  88. obj.addComponent('quests', character.components.find(c => c.type === 'quests'));
  89. obj.addComponent('events', character.components.find(c => c.type === 'events'));
  90. obj.xp = stats.values.xp;
  91. obj.level = stats.values.level;
  92. stats.stats.logins++;
  93. atlas.addObject(this.obj, true);
  94. io.sockets.emit('events', {
  95. onGetMessages: [{
  96. messages: [{
  97. class: 'color-blueB',
  98. message: this.obj.name + ' has come online'
  99. }]
  100. }],
  101. onGetConnectedPlayer: [cons.getCharacterList()]
  102. });
  103. cb();
  104. },
  105. broadcastSelf: function () {
  106. let obj = this.obj;
  107. let self = {
  108. id: obj.id,
  109. zone: obj.zone,
  110. name: obj.name,
  111. level: obj.level,
  112. class: obj.class
  113. };
  114. io.sockets.emit('events', {
  115. onGetConnectedPlayer: [self]
  116. });
  117. },
  118. hasSeen: function (id) {
  119. return (this.seen.indexOf(id) > -1);
  120. },
  121. see: function (id) {
  122. this.seen.push(id);
  123. },
  124. unsee: function (id) {
  125. this.seen.spliceWhere(s => s === id);
  126. },
  127. die: function (source, permadeath) {
  128. this.obj.clearQueue();
  129. let physics = this.obj.instance.physics;
  130. physics.removeObject(this.obj, this.obj.x, this.obj.y);
  131. this.obj.dead = true;
  132. this.obj.aggro.die();
  133. if (!permadeath) {
  134. let level = this.obj.stats.values.level;
  135. let spawns = this.obj.spawn;
  136. let spawnPos = spawns.filter(s => (((s.maxLevel) && (s.maxLevel >= level)) || (!s.maxLevel)));
  137. if ((spawnPos.length === 0) || (!source.name))
  138. spawnPos = spawns[0];
  139. else if (source.name) {
  140. let sourceSpawnPos = spawnPos.find(s => ((s.source) && (s.source.toLowerCase() === source.name.toLowerCase())));
  141. if (sourceSpawnPos)
  142. spawnPos = sourceSpawnPos;
  143. else
  144. spawnPos = spawnPos[0];
  145. }
  146. this.obj.x = spawnPos.x;
  147. this.obj.y = spawnPos.y;
  148. this.obj.stats.die(source);
  149. process.send({
  150. method: 'object',
  151. serverId: this.obj.serverId,
  152. obj: {
  153. dead: true
  154. }
  155. });
  156. } else {
  157. process.send({
  158. method: 'object',
  159. serverId: this.obj.serverId,
  160. obj: {
  161. dead: true,
  162. permadead: true
  163. }
  164. });
  165. }
  166. this.obj.fireEvent('onAfterDeath', source);
  167. this.obj.spellbook.die();
  168. this.obj.effects.die();
  169. },
  170. respawn: function () {
  171. let syncer = this.obj.syncer;
  172. syncer.o.x = this.obj.x;
  173. syncer.o.y = this.obj.y;
  174. this.obj.aggro.move();
  175. this.obj.instance.physics.addObject(this.obj, this.obj.x, this.obj.y);
  176. },
  177. move: function (msg) {
  178. atlas.queueAction(this.obj, {
  179. action: 'move',
  180. data: msg.data
  181. });
  182. },
  183. moveList: function (msg) {
  184. atlas.queueAction(this.obj, {
  185. action: 'move',
  186. list: true,
  187. data: msg.data
  188. });
  189. },
  190. queueAction: function (msg) {
  191. atlas.queueAction(this.obj, msg.data);
  192. },
  193. performAction: function (msg) {
  194. if (msg.callback)
  195. msg.data.data.callbackId = atlas.registerCallback(msg.callback);
  196. atlas.performAction(this.obj, msg.data);
  197. }
  198. };