Class: RuneRb::Database::PlayerStats

Inherits:
Object
  • Object
show all
Defined in:
app/database/models/player/stats.rb

Overview

Collection of information related to an individual player's skills/stats.

Models a row of the `player_stats` taable

Since:

  • 0.9.3

Defined Under Namespace

Classes: LevelUpInfo

Constant Summary collapse

MAXIMUM_EXPERIENCE =

Returns the maximum amount of experience that can be gained.

Returns:

  • (Integer)

    the maximum amount of experience that can be gained.

Since:

  • 0.9.3

200_000_000
SKILLS =

Returns a map of skills and their level and exp column labels.

Returns:

  • (Hash)

    a map of skills and their level and exp column labels.

Since:

  • 0.9.3

{
  ATTACK: %i[attack_level attack_exp],
  DEFENCE: %i[defence_level defence_exp],
  STRENGTH: %i[strength_level strength_exp],
  HIT_POINTS: %i[hit_points_level hit_points_exp],
  RANGE: %i[range_level range_exp],
  PRAYER: %i[prayer_level prayer_exp],
  MAGIC: %i[magic_level magic_exp],
  COOKING: %i[cooking_level cooking_exp],
  WOODCUTTING: %i[woodcutting_level woodcutting_exp],
  FLETCHING: %i[fletching_level fletching_exp],
  FISHING: %i[fishing_level fishing_exp],
  FIREMAKING: %i[firemaking_level firemaking_exp],
  CRAFTING: %i[crafting_level crafting_exp],
  SMITHING: %i[smithing_level smithing_exp],
  MINING: %i[mining_level mining_exp],
  HERBLORE: %i[herblore_level herblore_exp],
  AGILITY: %i[agility_level agility_exp],
  THIEVING: %i[thieving_level thieving_exp],
  SLAYER: %i[slayer_level slayer_exp],
  FARMING: %i[farming_level farming_exp],
  RUNECRAFTING: %i[runecrafting_level runecrafting_exp]
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#level_upObject (readonly)

Since:

  • 0.9.3


6
7
8
# File 'app/database/models/player/stats.rb', line 6

def level_up
  @level_up
end

Class Method Details

.level_for_experience(xp) ⇒ Integer

Calculates the appropriate level for the amount of passed experience

Parameters:

  • xp (Integer)

    the experience to fetch the level for.

Returns:

  • (Integer)

    the skill level for the given amount of experience

Since:

  • 0.9.3


119
120
121
122
123
124
125
126
127
128
129
130
# File 'app/database/models/player/stats.rb', line 119

def level_for_experience(xp)
  if xp > 13_034_430
    99
  else
    points = 0
    lvl = (1..99).detect do |level|
      points += (level + 300.0 * (2**(level / 7.0))).floor
      (points / 4).floor >= xp
    end
    lvl >= 99 ? 99 : lvl
  end
end

Instance Method Details

#add_experience(skill, amount) ⇒ Object

Adds a specified amount to the corresponding <_experience> row of the passed skill

Parameters:

  • skill (Symbol)

    the skill to add experience to

  • amount (Integer)

    the amount of experience to add

Since:

  • 0.9.3


95
96
97
98
# File 'app/database/models/player/stats.rb', line 95

def add_experience(skill, amount)
  eventual = self[SKILLS[skill].last] + amount
  update_exp(skill, eventual)
end

#combatInteger

Calculates the virtual combat level with the dataset's combat stats.

Returns:

  • (Integer)

    the calculated combat level

Since:

  • 0.9.3


44
45
46
47
48
49
50
51
52
53
54
# File 'app/database/models/player/stats.rb', line 44

def combat
  combat = ((self[:defence_level] + self[:hit_points_level] + (self[:prayer_level] / 2).floor) * 0.2535).to_i + 1
  melee = (self[:attack_level] + self[:strength_level]) * 0.325
  ranger = (self[:range_level] * 1.5).to_i.floor * 0.325
  magic = (self[:magic_level] * 1.5).to_i.floor * 0.325

  combat += melee if melee >= ranger && melee >= magic
  combat += ranger if ranger >= melee && ranger >= magic
  combat += magic if magic >= melee && magic >= ranger
  combat <= 126 ? combat : 126
end

#level_up?Boolean

Is a level up pending?

Returns:

  • (Boolean)

    is a level up required?

Since:

  • 0.9.3


110
111
112
# File 'app/database/models/player/stats.rb', line 110

def level_up?
  true unless @level_up.nil?
end

#maxObject

Updates all skills with a level of 99 and an experience of 13,034,430

Since:

  • 0.9.3


101
102
103
104
105
106
# File 'app/database/models/player/stats.rb', line 101

def max
  SKILLS.each do |_label, columns|
    update(columns.first => 99)
    update(columns.last => 13_034_430)
  end
end

#normalize(skill) ⇒ Object

Attempts to normalize the level of the skill

Parameters:

  • skill (Symbol)

    the skill to normalize

Since:

  • 0.9.3


88
89
90
# File 'app/database/models/player/stats.rb', line 88

def normalize(skill)
  update_level(skill, RuneRb::System::Database::Stats.level_for_experience(self[SKILLS[skill].last]))
end

#totalInteger

Calculates the total level of all stat levels added.

Returns:

  • (Integer)

    the sum of all levels added.

Since:

  • 0.9.3


58
59
60
61
62
63
64
# File 'app/database/models/player/stats.rb', line 58

def total
  self[:attack_level] + self[:defence_level] + self[:strength_level] + self[:hit_points_level] +
    self[:range_level] + self[:prayer_level] + self[:magic_level] + self[:cooking_level] +
    self[:woodcutting_level] + self[:fletching_level] + self[:fishing_level] + self[:firemaking_level] +
    self[:crafting_level] + self[:smithing_level] + self[:mining_level] + self[:herblore_level] +
    self[:agility_level] + self[:thieving_level] + self[:slayer_level] + self[:farming_level] + self[:runecrafting_level]
end

#update_exp(skill, to) ⇒ Object

Updates a skill's experience to that of the passed parameter

Parameters:

  • skill (Symbol)

    the skill to update

  • to (Integer)

    the experience to update to

Since:

  • 0.9.3


77
78
79
80
81
82
83
84
# File 'app/database/models/player/stats.rb', line 77

def update_exp(skill, to)
  if to >= MAXIMUM_EXPERIENCE
    update(SKILLS[skill].last => MAXIMUM_EXPERIENCE)
  else
    update(SKILLS[skill].last => to)
    normalize(skill)
  end
end

#update_level(skill, to) ⇒ Object

Updates a skill's level to that of the passed parameter

Parameters:

  • skill (Symbol)

    the skill to update

  • to (Integer)

    the level to update to

Since:

  • 0.9.3


69
70
71
72
# File 'app/database/models/player/stats.rb', line 69

def update_level(skill, to)
  @level_up = LevelUpInfo.new(skill, to) if self[SKILLS[skill].first] > to
  update(SKILLS[skill].first => to)
end