78 lines
1.5 KiB
Ruby
78 lines
1.5 KiB
Ruby
class Player < ApplicationRecord
|
|
# A Player can be an alt of another player (the main)
|
|
belongs_to :main_player, class_name: "Player", optional: true, foreign_key: "main_player_id"
|
|
|
|
# A Player can have multiple alts
|
|
has_many :alternate_players, class_name: "Player", foreign_key: "main_player_id"
|
|
|
|
# A Player can have many loots
|
|
has_many :loots
|
|
|
|
validate :no_circular_references
|
|
|
|
def total_wins
|
|
w = self.wins
|
|
alternate_players.each { |alt|
|
|
w += alt.wins
|
|
}
|
|
w
|
|
end
|
|
|
|
def total_losses
|
|
l = self.losses
|
|
alternate_players.each { |alt|
|
|
l += alt.losses
|
|
}
|
|
l
|
|
end
|
|
|
|
def total_purse
|
|
p = self.purse
|
|
alternate_players.each { |alt|
|
|
p += alt.purse
|
|
}
|
|
p
|
|
end
|
|
|
|
def alt?
|
|
self.main_player
|
|
end
|
|
|
|
def has_alts?
|
|
self.alternate_players.count > 0
|
|
end
|
|
|
|
def main_name
|
|
if alt?
|
|
self.main_player.name
|
|
end
|
|
end
|
|
|
|
def no_circular_references
|
|
if main_player_id.present? && (main_player.main_player== self)
|
|
errors.add(:main_account, "circular reference")
|
|
end
|
|
end
|
|
|
|
def get_loot
|
|
loot = Loot.where(player_id: self.id)
|
|
self.alternate_players.each do |alt|
|
|
loot << Loot.where(player_id: alt.id)
|
|
end
|
|
loot
|
|
end
|
|
|
|
def get_loot_with_roll(roll)
|
|
ids = [self.id] + self.alternate_players.pluck(:id)
|
|
Loot.where(player_id: ids, roll_type: roll)
|
|
end
|
|
|
|
def get_loot_count
|
|
get_loot.size
|
|
end
|
|
|
|
def get_loot_count_with_roll(roll)
|
|
get_loot_with_roll(roll).size
|
|
end
|
|
end
|