index.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. const { Document } = require('camo')
  2. const battleAIs = require('../battle/ai')
  3. const log = require('../util/log')
  4. const gm = require('gm')
  5. const request = require('request')
  6. const tinygradient = require('tinygradient')
  7. const tinycolor = require('tinycolor2')
  8. const healthGradient = tinygradient(['#BA0020', '#ffbf00', '#77dd77'])
  9. const colorDead = '#36454f'
  10. const BACKUP_AVATAR_URL = 'https://discordapp.com/assets/6debd47ed13483642cf09e832ed0bc1b.png'
  11. // A Character refers to both a player and an enemy. The difference is in
  12. // their `ai` that controls their actions (for players, this is DiscordAI).
  13. class Character extends Document {
  14. constructor() {
  15. super()
  16. this.schema({
  17. discordID: {type: String, required: false},
  18. battleAI: {type: String, required: true, choices: Object.keys(battleAIs)},
  19. partyID: {type: String, required: false}, // Automatically generated
  20. health: {type: Number, default: 5, min: 0},
  21. maxHealth: {type: Number, default: 5, min: 1},
  22. moveIDs: {type: [String]},
  23. })
  24. }
  25. // Rather than a string (this.battleAI), returns the class itself.
  26. get BattleAI() {
  27. return battleAIs[this.battleAI]
  28. }
  29. async modHealth(by, guild) {
  30. this.health += by
  31. if (this.health > this.maxHealth) this.health = this.maxHealth
  32. if (this.health < 0) this.health = 0
  33. if (guild) await this.healthUpdate(guild)
  34. return this.healthState
  35. }
  36. async healthUpdate(guild) {
  37. await this.save()
  38. if (!this.discordID) return
  39. const member = guild.members.get(this.discordID)
  40. // Remove existing health role, if any
  41. const roleExisting = member.roles.find(role => role.name.endsWith(' health'))
  42. if (roleExisting) await roleExisting.delete()
  43. // Reuse/create a health role and grant it
  44. const roleName = `${this.health}/${this.maxHealth} health`
  45. const role = member.roles.find(role => role.name === roleName)
  46. || await guild.createRole({
  47. name: roleName,
  48. color: this.health === 0
  49. ? colorDead
  50. : tinycolor(healthGradient.rgbAt(this.health / this.maxHealth))
  51. .toHexString(),
  52. })
  53. await member.addRole(role)
  54. }
  55. get healthState() {
  56. if (this.health === this.maxHealth) return 'healthy'
  57. else if (this.health === 1) return 'peril'
  58. else if (this.health === 0) return 'dead'
  59. else return 'injured'
  60. }
  61. getName(guild) {
  62. // FIXME: names for characters without accociated discord users
  63. return guild.members.get(this.discordID).displayName
  64. }
  65. // "Label" of a character, to be displayed in messages.
  66. async getLabel(game) {
  67. const emote = await this.getEmote(game)
  68. const role = await this.getRole(game)
  69. return `${emote} ${role}`
  70. }
  71. // Finds/creates a Discord role with the same name as the character and with
  72. // the same color as the character's party.
  73. async getRole(game, forceUpdate = false) {
  74. if (this._discordRole && !forceUpdate) {
  75. return this._discordRole
  76. }
  77. // Create the role
  78. const name = this.getName(game.guild)
  79. const party = await this.getParty()
  80. const role = await game.guild.createRole({name, color: party.colorHex})
  81. // Give this member the role
  82. const member = await game.guild.members.get(this.discordID)
  83. await member.addRole(role)
  84. return this._discordRole = role
  85. }
  86. // Returns a little icon emote based on the character's avatar.
  87. // TODO: implement this for characters that aren't discord-backed
  88. async getEmote(game) {
  89. if (!this.discordID) throw new Error() // FIXME
  90. try {
  91. const member = game.guild.members.get(this.discordID)
  92. const graphic = gm(request(member.user.avatarURL))
  93. .resize(48, 48)
  94. .stream('PNG')
  95. const emote = await game.emoteStore.get(`char${this._id}`, graphic)
  96. return emote
  97. } catch (err) {
  98. log.warning(`Error while generating character emote:\n${err.message}`)
  99. const graphic = gm(request(BACKUP_AVATAR_URL))
  100. .resize(48, 48)
  101. .stream('PNG')
  102. const emote = await game.emoteStore.get(`char${this._id}`, graphic)
  103. return emote
  104. //return _.sample(['❤️', '💛', '💙', '💜', '💚'])
  105. }
  106. }
  107. async getParty() {
  108. const Party = require('./party') // I don't like this any more than you do, but recursive requires DESTROY Node.js. Sorry.
  109. let party
  110. if (!this.partyID) {
  111. // Create a one-man party
  112. party = await Party.of([this])
  113. }
  114. return party || Party.findOne({_id: this.partyID})
  115. }
  116. }
  117. module.exports = Character