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
-
#description ⇒ String
A more detailed description, used to give further information about the campaign in general.
-
#files ⇒ Array<Arkaan::Campaigns::Files::Document>
The list of files that were 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 it’s publicly displayed and accessible.
-
#messages ⇒ Array<Arkaan::Campaigns::Messages::Base>
The messages sent in the chatroom of the campaign.
-
#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
-
#characters ⇒ Array<Arkaan::Campaigns::Character>
A flattened list of characters for this campaign.
-
#creator ⇒ Arkaan::Account
Getter for the creator account of this campaign.
-
#creator=(account) ⇒ Object
Sets the creator of the campaign.
-
#documents ⇒ Array<Arkaan::Campaigns::Files::Document>
The document of this campaign as a flattened array.
-
#max_players_minimum ⇒ Object
Validation for the max number of players for a campaign.
-
#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
#description ⇒ String
Returns a more detailed description, used to give further information about the campaign in general.
13 |
# File 'lib/arkaan/campaign.rb', line 13 field :description, type: String |
#files ⇒ Array<Arkaan::Campaigns::Files::Document>
Returns the list of files that were uploaded in this campaign.
29 |
# File 'lib/arkaan/campaign.rb', line 29 has_many :files, class_name: 'Arkaan::Campaigns::Files::Document', inverse_of: :campaign |
#invitations ⇒ Array<Arkaan::Campaigns::Invitation>
Returns the invitations to players that have been made for this campaign.
26 |
# File 'lib/arkaan/campaign.rb', line 26 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 it’s publicly displayed and accessible.
16 |
# File 'lib/arkaan/campaign.rb', line 16 field :is_private, type: Boolean, default: true |
#messages ⇒ Array<Arkaan::Campaigns::Messages::Base>
Returns the messages sent in the chatroom of the campaign.
33 |
# File 'lib/arkaan/campaign.rb', line 33 :messages, class_name: 'Arkaan::Campaigns::Message', inverse_of: :campaign |
#ruleset ⇒ Arkaan::Ruleset
Returns the set of rules this campaign is based upon.
37 |
# File 'lib/arkaan/campaign.rb', line 37 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.
19 |
# File 'lib/arkaan/campaign.rb', line 19 field :tags, type: Array, default: [] |
#title ⇒ String
Returns the title, or name, of the campaign, used to identify it in the list.
10 |
# File 'lib/arkaan/campaign.rb', line 10 field :title, type: String |
Instance Method Details
#characters ⇒ Array<Arkaan::Campaigns::Character>
Returns a flattened list of characters for this campaign.
95 96 97 |
# File 'lib/arkaan/campaign.rb', line 95 def characters players.map(&:characters).flatten end |
#creator ⇒ Arkaan::Account
Getter for the creator account of this campaign.
60 61 62 |
# File 'lib/arkaan/campaign.rb', line 60 def creator return invitations.where(enum_status: :creator).first.account end |
#creator=(account) ⇒ Object
Sets the creator of the campaign. This method is mainly used for backward-compatibility needs.
52 53 54 55 56 |
# File 'lib/arkaan/campaign.rb', line 52 def creator=(account) if !invitations.where(account: account).exists? Arkaan::Campaigns::Invitation.create(campaign: self, account: account, enum_status: :creator) end end |
#documents ⇒ Array<Arkaan::Campaigns::Files::Document>
Returns the document of this campaign as a flattened array.
100 101 102 |
# File 'lib/arkaan/campaign.rb', line 100 def documents invitations.map(&:documents).flatten 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.
78 79 80 81 82 |
# File 'lib/arkaan/campaign.rb', line 78 def max_players_minimum if max_players? && (max_players < players_count || max_players < 1) errors.add(:max_players, 'minimum') end end |
#players ⇒ Array<Arkaan::Campaigns::Invitation>
Returns the players in this campaign.
85 86 87 |
# File 'lib/arkaan/campaign.rb', line 85 def players invitations.where(enum_status: :accepted) end |
#players_count ⇒ Integer
Returns the number of players in this campaign.
90 91 92 |
# File 'lib/arkaan/campaign.rb', line 90 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.
65 66 67 68 69 70 71 72 73 |
# File 'lib/arkaan/campaign.rb', line 65 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) if !creator.nil? && title? && same_title_campaign.exists? errors.add(:title, 'uniq') end end |