Class: Arkaan::Campaign
- Inherits:
-
Object
- Object
- Arkaan::Campaign
- Includes:
- Mongoid::Document, Mongoid::Timestamps
- Defined in:
- lib/arkaan/campaign.rb
Overview
A campaign is a gathering of accounts playing on the same interface, and interacting in a common game.
Instance Attribute Summary collapse
-
#chatroom ⇒ Arkaan::Chatrooms::Campaign
The chatroom linked to this campaign.
-
#description ⇒ String
A more detailed description, used to give further information about the campaign in general.
-
#files ⇒ Array<Arkaan::Files::Document>
The files uploaded in this campaign.
-
#invitations ⇒ Array<Arkaan::Campaigns::Invitation>
The invitations to players that have been made for this campaign.
-
#is_private ⇒ Boolean
TRUE if the campaign can be joined only by being invited by the creator, FALSE if public.
-
#ruleset ⇒ Arkaan::Ruleset
The set of rules this campaign is based upon.
-
#tags ⇒ Array<String>
An array of tags describing characteristics of this campaign.
-
#title ⇒ String
The title, or name, of the campaign, used to identify it in the list.
Instance Method Summary collapse
-
#creator ⇒ Arkaan::Account
Getter for the creator account of this campaign.
-
#creator=(account) ⇒ Object
Sets the creator of the campaign.
-
#max_players_minimum ⇒ Object
Validation for the max number of players for a campaign.
- #messages ⇒ Object
-
#players ⇒ Array<Arkaan::Campaigns::Invitation>
The players in this campaign.
-
#players_count ⇒ Integer
The number of players in this campaign.
-
#title_unicity ⇒ Object
Adds an error message if the account creating this campaign already has a campaign with the very same name.
Instance Attribute Details
#chatroom ⇒ Arkaan::Chatrooms::Campaign
Returns the chatroom linked to this campaign.
35 |
# File 'lib/arkaan/campaign.rb', line 35 :chatroom, class_name: 'Arkaan::Chatrooms::Campaign', inverse_of: :campaign |
#description ⇒ String
Returns a more detailed description, used to give further information about the campaign in general.
15 |
# File 'lib/arkaan/campaign.rb', line 15 field :description, type: String |
#files ⇒ Array<Arkaan::Files::Document>
Returns the files uploaded in this campaign.
31 |
# File 'lib/arkaan/campaign.rb', line 31 has_many :files, class_name: 'Arkaan::Files::Document' |
#invitations ⇒ Array<Arkaan::Campaigns::Invitation>
Returns the invitations to players that have been made for this campaign.
28 |
# File 'lib/arkaan/campaign.rb', line 28 has_many :invitations, class_name: 'Arkaan::Campaigns::Invitation', inverse_of: :campaign |
#is_private ⇒ Boolean
Returns TRUE if the campaign can be joined only by being invited by the creator, FALSE if public.
18 |
# File 'lib/arkaan/campaign.rb', line 18 field :is_private, type: Boolean, default: true |
#ruleset ⇒ Arkaan::Ruleset
Returns the set of rules this campaign is based upon.
39 |
# File 'lib/arkaan/campaign.rb', line 39 belongs_to :ruleset, class_name: 'Arkaan::Ruleset', inverse_of: :campaigns, optional: true |
#tags ⇒ Array<String>
Returns an array of tags describing characteristics of this campaign.
21 |
# File 'lib/arkaan/campaign.rb', line 21 field :tags, type: Array, default: [] |
#title ⇒ String
Returns the title, or name, of the campaign, used to identify it in the list.
12 |
# File 'lib/arkaan/campaign.rb', line 12 field :title, type: String |
Instance Method Details
#creator ⇒ Arkaan::Account
Getter for the creator account of this campaign.
66 67 68 |
# File 'lib/arkaan/campaign.rb', line 66 def creator invitations.to_a.find(&:status_creator?).account end |
#creator=(account) ⇒ Object
Sets the creator of the campaign. This method is mainly used for backward-compatibility needs.
54 55 56 57 58 59 60 61 62 |
# 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 ⇒ Object
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.
84 85 86 87 88 |
# 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 ⇒ Object
102 103 104 |
# File 'lib/arkaan/campaign.rb', line 102 def chatroom. end |
#players ⇒ Array<Arkaan::Campaigns::Invitation>
Returns the players in this campaign.
91 92 93 94 95 |
# 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 ⇒ Integer
Returns the number of players in this campaign.
98 99 100 |
# File 'lib/arkaan/campaign.rb', line 98 def players_count players.count end |
#title_unicity ⇒ Object
Adds an error message if the account creating this campaign already has a campaign with the very same name.
71 72 73 74 75 76 77 78 79 |
# 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 |