player.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. 'use strict'
  2. const SpeedDivider = [120, 100, 110, 90, 110, 120, 105, 105, 100, 100, 100, 100, 100];
  3. const NOCTAN = new Set([1206, 1210, 1230, 1300, 1301, 1302, 1303, 81212, 201225]),
  4. HOOK_LAST = { order: 100, filter: { fake: null } }
  5. class SPPlayer {
  6. reset() {
  7. Object.assign(this, {
  8. // Session
  9. gameId: -1n,
  10. templateId: -1,
  11. race: -1,
  12. job: -1,
  13. kdId: 0,
  14. // Status
  15. mounted: false,
  16. // Combat stats
  17. attackSpeed: 1,
  18. stamina: 0,
  19. // Passivity stuff
  20. crests: new Set(), // Crests
  21. epPerks: new Set(), // EP
  22. skillPolishing: new Set(), // Skill Polishing (Talyphs)
  23. skillList: new Set(), // for passive skilleffects
  24. // Equipment / Inventory
  25. hasWeapon: false,
  26. hasNocTan: false,
  27. itemPassives: [],
  28. itemPassivityGroups: new Set()
  29. })
  30. }
  31. constructor(mod) {
  32. this.reset()
  33. mod.hook('S_LOGIN', mod.majorPatchVersion >= 86 ? 14 : 13, HOOK_LAST, event => {
  34. this.reset()
  35. Object.assign(this, {
  36. gameId: event.gameId,
  37. templateId: event.templateId,
  38. race: Math.floor(event.templateId / 100) % 100 - 1,
  39. job: event.templateId % 100 - 1,
  40. })
  41. this.kdId = this.templateId === 10911 ? 1101102 : (this.templateId * 100 + 2)
  42. })
  43. mod.hook('S_RETURN_TO_LOBBY', 'raw', { order: 100, filter: { fake: false } }, () => { this.reset() })
  44. // Status
  45. mod.hook('S_MOUNT_VEHICLE', 2, event => { if (event.gameId === this.gameId) this.mounted = true })
  46. mod.hook('S_UNMOUNT_VEHICLE', 2, event => { if (event.gameId === this.gameId) this.mounted = false })
  47. // Combat stats
  48. mod.hook('S_PLAYER_STAT_UPDATE', mod.majorPatchVersion >= 93 ? 14 : 13, HOOK_LAST, event => {
  49. Object.assign(this, {
  50. // Newer classes use a different speed algorithm
  51. attackSpeed: (event.attackSpeed + event.attackSpeedBonus) / SpeedDivider[this.job],
  52. stamina: event.stamina
  53. })
  54. })
  55. mod.hook('S_PLAYER_CHANGE_STAMINA', 1, HOOK_LAST, event => { this.stamina = event.current })
  56. // Crests
  57. mod.game.initialize('glyphs');
  58. mod.game.glyphs.on('change', () => {
  59. this.crests = new Set(mod.game.glyphs.enabled)
  60. })
  61. // EP
  62. mod.game.initialize('talents');
  63. mod.game.talents.on('change_perks', () => {
  64. this.epPerks.clear()
  65. for (let [id, level] of Object.entries(mod.game.talents.perks)) this.epPerks.add(`${id},${level}`)
  66. })
  67. // Skill Polishing
  68. mod.hook('S_RP_SKILL_POLISHING_LIST', 1, event => {
  69. this.skillPolishing.clear()
  70. // Skill Advancement
  71. for (let sp of event.levelEffects)
  72. this.skillPolishing.add(sp.id)
  73. // Skill Optimization
  74. for (let sp of event.optionEffects)
  75. if (sp.active)
  76. this.skillPolishing.add(sp.id)
  77. })
  78. // Skill List
  79. mod.hook('S_SKILL_LIST', 2, event => {
  80. this.skillList = new Set()
  81. // Passive Skills
  82. for (let skill of event.skills)
  83. if (!skill.active) this.skillList.add(skill.id)
  84. })
  85. // Equipment / Inventory
  86. mod.game.initialize('inventory');
  87. mod.game.initialize("contract");
  88. mod.game.inventory.on('update', () => {
  89. this.hasWeapon = mod.game.inventory.weaponEquipped
  90. this.itemPassives = mod.game.inventory.equipmentPassivities
  91. //this.hasNocTan = !!mod.game.inventory.findInBagOrPockets(NOCTAN)
  92. this.itemPassivityGroups.clear()
  93. const equipmentSlots = mod.game.inventory.equipment.slots
  94. for(let slot in equipmentSlots)
  95. for (let stat of equipmentSlots[slot].equipmentStats)
  96. for (let group of stat.passivityGroups)
  97. this.itemPassivityGroups.add(group.id)
  98. });
  99. }
  100. }
  101. module.exports = SPPlayer