gambosite/app/models/player.rb

78 lines
1.5 KiB
Ruby
Raw Normal View History

2025-01-24 10:22:13 -08:00
class Player < ApplicationRecord
2025-01-25 17:37:01 -08:00
# 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"
2025-03-11 09:52:32 -07:00
# A Player can have many loots
has_many :loots
2025-01-25 17:37:01 -08:00
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
2025-03-11 09:52:32 -07:00
def has_alts?
self.alternate_players.count > 0
end
2025-02-03 15:07:16 -08:00
def main_name
if alt?
self.main_player.name
end
2025-02-03 14:42:41 -08:00
end
2025-01-25 17:37:01 -08:00
def no_circular_references
if main_player_id.present? && (main_player.main_player== self)
errors.add(:main_account, "circular reference")
end
end
2025-03-11 09:52:32 -07:00
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
2025-01-24 10:22:13 -08:00
end