diff options
author | David Gay <david@davidgay.org> | 2021-05-28 21:45:50 -0400 |
---|---|---|
committer | David Gay <david@davidgay.org> | 2021-05-28 21:45:50 -0400 |
commit | 65752b4f73cf8e1ce83cdaf8227b5d67923ac729 (patch) | |
tree | 5aa91176d33e828a7c70f122f87946948c884026 /app/models | |
parent | ca98987c1a14655aedb4bdcf8fef7311131ca1be (diff) |
Quickly port leaderboard and rankings from old Esoterra
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/character.rb | 36 | ||||
-rw-r--r-- | app/models/character_skill.rb | 9 |
2 files changed, 45 insertions, 0 deletions
diff --git a/app/models/character.rb b/app/models/character.rb index 8913f71..a5c871b 100644 --- a/app/models/character.rb +++ b/app/models/character.rb @@ -25,6 +25,22 @@ class Character < ApplicationRecord after_create :create_skills, :set_combat_styles after_create { Hearth.create(character: self) } + def self.sorted_by_total_xp + all.sort_by(&:total_xp).reverse + end + + def self.sorted_by_total_level + all.sort_by(&:total_level).reverse + end + + def self.top_total_xp + sorted_by_total_xp.first(5) + end + + def self.top_total_level + sorted_by_total_level.first(5) + end + def beastslay_level; skill_level("beastslay"); end def fluxseethe_level; skill_level("fluxseethe"); end def havencast_level; skill_level("havencast"); end @@ -41,6 +57,26 @@ class Character < ApplicationRecord def wildscour_level; skill_level("wildscour"); end def worldcall_level; skill_level("worldcall"); end + def total_xp + character_skills.sum(:xp).to_i + end + + def total_level + count = 0 + character_skills.each do |cs| + count += cs.level + end + count + end + + def total_xp_rank + Character.sorted_by_total_xp.map(&:id).index(self.id) + 1 + end + + def total_level_rank + Character.sorted_by_total_level.map(&:id).index(self.id) + 1 + end + def vestige vestige = self.character_items.find_by(item: Item.find_by_gid("vestige")) vestige ? vestige.quantity : 0 diff --git a/app/models/character_skill.rb b/app/models/character_skill.rb index 20468c3..722a371 100644 --- a/app/models/character_skill.rb +++ b/app/models/character_skill.rb @@ -26,6 +26,11 @@ class CharacterSkill < ApplicationRecord 1459475733, 1680697391, 1935475040, 2228899094 ].freeze + def self.top_xp_for(skill, limit: 10) + skill = Skill.find_by_gid(skill) if skill.is_a? String + where(skill: skill).order(xp: :desc, updated_at: :asc).limit(limit) + end + def self.level_for_xp(xp) XP_TOTALS_PER_LEVEL.each_with_index do |total, index| if total > xp @@ -48,6 +53,10 @@ class CharacterSkill < ApplicationRecord total_xp_for_next_level - self.xp end + def rank + CharacterSkill.top_xp_for(self.skill, limit: nil).map(&:character).map(&:id).index(self.character.id) + 1 + end + private def xp_required_for_level(level) level <= 120 ? XP_TOTALS_PER_LEVEL[level - 1] : nil |