class Arkaan::Campaign

A campaign is a gathering of accounts playing on the same interface, and interacting in a common game. @author Vincent Courtois <courtois.vincent@outlook.com>

Public Instance Methods

creator() click to toggle source

Getter for the creator account of this campaign. @return [Arkaan::Account] the account of the player creating this campaign.

# File lib/arkaan/campaign.rb, line 66
def creator
  invitations.to_a.find(&:status_creator?).account
end
creator=(account) click to toggle source

Sets the creator of the campaign. This method is mainly used for backward-compatibility needs. @param account [Arkaan::Account] the account of the creator for this campaign.

# File lib/arkaan/campaign.rb, line 54
def creator=(account)
  return if invitations.where(account: account).exists?

  Arkaan::Campaigns::Invitation.create(
    campaign: self,
    account: account,
    status: :creator
  )
end
max_players_minimum() click to toggle source

Validation for the max number of players for a campaign. If there is a max number of players, and the current number of players is above it, or the max number of players is 0, raises an error.

# File lib/arkaan/campaign.rb, line 84
def max_players_minimum
  return unless max_players? && (max_players < players_count || max_players < 1)

  errors.add(:max_players, 'minimum')
end
messages() click to toggle source
# File lib/arkaan/campaign.rb, line 102
def messages
  chatroom.messages
end
players() click to toggle source

@return [Array<Arkaan::Campaigns::Invitation>] the players in this campaign.

# File lib/arkaan/campaign.rb, line 91
def players
  invitations.to_a.select do |invitation|
    %i[creator accepted].include? invitation.enum_status
  end
end
players_count() click to toggle source

@return [Integer] the number of players in this campaign.

# File lib/arkaan/campaign.rb, line 98
def players_count
  players.count
end
title_unicity() click to toggle source

Adds an error message if the account creating this campaign already has a campaign with the very same name.

# File lib/arkaan/campaign.rb, line 71
def title_unicity
  # First we take all the other campaign ids of the user.
  campaign_ids = creator.invitations.where(:campaign_id.ne => _id).pluck(:campaign_id)
  # With this list of campaign IDs, we look for a campaign with the same title.
  same_title_campaign = Arkaan::Campaign.where(:_id.in => campaign_ids, title: title)
  return unless !creator.nil? && title? && same_title_campaign.exists?

  errors.add(:title, 'uniq')
end