Class: Core::Models::Campaign
- Inherits:
-
Object
- Object
- Core::Models::Campaign
- Includes:
- Mongoid::Document, Mongoid::Timestamps
- Defined in:
- lib/core/models/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 ⇒ Core::Models::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<Core::Models::Files::Document>
The files uploaded in this campaign.
-
#invitations ⇒ Array<Core::Models::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.
-
#ruleset ⇒ Core::Models::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.
-
#tokens ⇒ Array<Core::Models::Campaigns::Token>
The tokens declared in this campaign.
Instance Method Summary collapse
-
#creator ⇒ Core::Models::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<Core::Models::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 ⇒ Core::Models::Chatrooms::Campaign
Returns the chatroom linked to this campaign.
36 |
# File 'lib/core/models/campaign.rb', line 36 :chatroom, class_name: 'Core::Models::Chatrooms::Campaign', inverse_of: :campaign |
#description ⇒ String
Returns a more detailed description, used to give further information about the campaign in general.
16 |
# File 'lib/core/models/campaign.rb', line 16 field :description, type: String |
#files ⇒ Array<Core::Models::Files::Document>
Returns the files uploaded in this campaign.
32 |
# File 'lib/core/models/campaign.rb', line 32 has_many :files, class_name: 'Core::Models::Files::Document' |
#invitations ⇒ Array<Core::Models::Campaigns::Invitation>
Returns the invitations to players that have been made for this campaign.
29 |
# File 'lib/core/models/campaign.rb', line 29 has_many :invitations, class_name: 'Core::Models::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.
19 |
# File 'lib/core/models/campaign.rb', line 19 field :is_private, type: Mongoid::Boolean, default: true |
#ruleset ⇒ Core::Models::Ruleset
Returns the set of rules this campaign is based upon.
44 |
# File 'lib/core/models/campaign.rb', line 44 belongs_to :ruleset, class_name: 'Core::Models::Ruleset', inverse_of: :campaigns, optional: true |
#tags ⇒ Array<String>
Returns an array of tags describing characteristics of this campaign.
22 |
# File 'lib/core/models/campaign.rb', line 22 field :tags, type: Array, default: [] |
#title ⇒ String
Returns the title, or name, of the campaign, used to identify it in the list.
13 |
# File 'lib/core/models/campaign.rb', line 13 field :title, type: String |
#tokens ⇒ Array<Core::Models::Campaigns::Token>
Returns the tokens declared in this campaign.
40 |
# File 'lib/core/models/campaign.rb', line 40 :tokens, class_name: 'Core::Models::Campaigns::Token', inverse_of: :campaign |
Instance Method Details
#creator ⇒ Core::Models::Account
Getter for the creator account of this campaign.
71 72 73 |
# File 'lib/core/models/campaign.rb', line 71 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.
59 60 61 62 63 64 65 66 67 |
# File 'lib/core/models/campaign.rb', line 59 def creator=(account) if !invitations.where(account: account).exists? invitation = Core::Models::Campaigns::Invitation.create( campaign: self, account: account, status: :creator ) end 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.
89 90 91 92 93 |
# File 'lib/core/models/campaign.rb', line 89 def max_players_minimum if max_players? && (max_players < players_count || max_players < 1) errors.add(:max_players, 'minimum') end end |
#messages ⇒ Object
107 108 109 |
# File 'lib/core/models/campaign.rb', line 107 def chatroom. end |
#players ⇒ Array<Core::Models::Campaigns::Invitation>
Returns the players in this campaign.
96 97 98 99 100 |
# File 'lib/core/models/campaign.rb', line 96 def players invitations.to_a.select do |invitation| [:creator, :accepted].include? invitation.enum_status end end |
#players_count ⇒ Integer
Returns the number of players in this campaign.
103 104 105 |
# File 'lib/core/models/campaign.rb', line 103 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.
76 77 78 79 80 81 82 83 84 |
# File 'lib/core/models/campaign.rb', line 76 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 = Core::Models::Campaign.where(:_id.in => campaign_ids, title: title) if !creator.nil? && title? && same_title_campaign.exists? errors.add(:title, 'uniq') end end |