Class: Barker::GameShow
- Inherits:
-
Object
show all
- Includes:
- Errors
- Defined in:
- lib/barker/game_show.rb
Constant Summary
Constants included
from Errors
Errors::AlreadyAnsweredError, Errors::AlreadyStartedError, Errors::AnswerNotFoundError, Errors::BarkerError, Errors::FileNotExistError, Errors::HasToRespondToIdError, Errors::HasToRespondToLocaleError, Errors::JokerAlreadyUsedError, Errors::NoCurrentStageError, Errors::NotStartedError, Errors::RecordNotFound, Errors::UnknownJokerError
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(quizzes, candidates, options = {}) ⇒ GameShow
Returns a new instance of GameShow.
9
10
11
12
13
14
15
16
17
18
19
20
|
# File 'lib/barker/game_show.rb', line 9
def initialize(quizzes, candidates, options = {})
@started_at = nil
@finished_at = nil
@aborted_at = nil
@created_at = Time.now
@stage = -1
@guideline = build_guideline(options[:guideline] || :base)
@quizzes = build_quizzes(quizzes)
@candidates = build_candidates(candidates)
@jokers = build_jokers
@stages = []
end
|
Instance Attribute Details
#aborted_at ⇒ Object
Returns the value of attribute aborted_at.
7
8
9
|
# File 'lib/barker/game_show.rb', line 7
def aborted_at
@aborted_at
end
|
#candidates ⇒ Object
Returns the value of attribute candidates.
7
8
9
|
# File 'lib/barker/game_show.rb', line 7
def candidates
@candidates
end
|
#created_at ⇒ Object
Returns the value of attribute created_at.
7
8
9
|
# File 'lib/barker/game_show.rb', line 7
def created_at
@created_at
end
|
#finished_at ⇒ Object
Returns the value of attribute finished_at.
7
8
9
|
# File 'lib/barker/game_show.rb', line 7
def finished_at
@finished_at
end
|
#guideline ⇒ Object
Returns the value of attribute guideline.
7
8
9
|
# File 'lib/barker/game_show.rb', line 7
def guideline
@guideline
end
|
#id ⇒ Object
Returns the value of attribute id.
6
7
8
|
# File 'lib/barker/game_show.rb', line 6
def id
@id
end
|
#jokers ⇒ Object
Returns the value of attribute jokers.
7
8
9
|
# File 'lib/barker/game_show.rb', line 7
def jokers
@jokers
end
|
#quizzes ⇒ Object
Returns the value of attribute quizzes.
7
8
9
|
# File 'lib/barker/game_show.rb', line 7
def quizzes
@quizzes
end
|
#started_at ⇒ Object
Returns the value of attribute started_at.
7
8
9
|
# File 'lib/barker/game_show.rb', line 7
def started_at
@started_at
end
|
Class Method Details
.load(serialized) ⇒ Object
137
138
139
|
# File 'lib/barker/game_show.rb', line 137
def self.load(serialized)
Marshal::load(Base64.decode64(serialized))
end
|
Instance Method Details
#==(other) ⇒ Object
22
23
24
|
# File 'lib/barker/game_show.rb', line 22
def ==(other)
created_at == other.created_at
end
|
#abort ⇒ Object
44
45
46
|
# File 'lib/barker/game_show.rb', line 44
def abort
@aborted_at = Time.now unless aborted_at
end
|
#aborted? ⇒ Boolean
56
57
58
|
# File 'lib/barker/game_show.rb', line 56
def aborted?
!!aborted_at
end
|
#answer(candidate_id, answer_id) ⇒ Object
113
114
115
116
|
# File 'lib/barker/game_show.rb', line 113
def answer(candidate_id, answer_id)
guideline.answer(current_round.candidate_round(candidate_id), candidate_id, answer_id)
current_round.answer(candidate_id, answer_id)
end
|
#ask(candidate_id, options = {}) ⇒ Object
109
110
111
|
# File 'lib/barker/game_show.rb', line 109
def ask(candidate_id, options = {})
current_round.ask(candidate_id, options)
end
|
#current_round ⇒ Object
83
84
85
|
# File 'lib/barker/game_show.rb', line 83
def current_round
current_stage && current_stage.current_round || nil
end
|
#current_stage ⇒ Object
70
71
72
|
# File 'lib/barker/game_show.rb', line 70
def current_stage
stage != -1 && @stages[stage]
end
|
#finish ⇒ Object
40
41
42
|
# File 'lib/barker/game_show.rb', line 40
def finish
@finished_at = Time.now unless finished_at
end
|
#finished? ⇒ Boolean
52
53
54
|
# File 'lib/barker/game_show.rb', line 52
def finished?
!!finished_at
end
|
#game_show_id ⇒ Object
30
31
32
|
# File 'lib/barker/game_show.rb', line 30
def game_show_id
id
end
|
#given_answer(candidate_id) ⇒ Object
118
119
120
|
# File 'lib/barker/game_show.rb', line 118
def given_answer(candidate_id)
current_round.given_answer(candidate_id)
end
|
#join(candidate) ⇒ Object
95
96
97
98
99
100
101
|
# File 'lib/barker/game_show.rb', line 95
def join(candidate)
raise AlreadyStartedError if started?
candidate.join
@candidates ||= []
@candidates << candidate
candidate
end
|
#joker(candidate_id, joker_id) ⇒ Object
122
123
124
125
126
|
# File 'lib/barker/game_show.rb', line 122
def joker(candidate_id, joker_id)
joker = jokers.find{|joker| joker.id == joker_id }
candidate = candidates.find{|candidate| candidate.id == candidate.id }
current_round.joker(candidate, joker)
end
|
#joker_ids ⇒ Object
128
129
130
|
# File 'lib/barker/game_show.rb', line 128
def joker_ids
jokers.map(&:id)
end
|
#leave(candidate_id) ⇒ Object
103
104
105
106
107
|
# File 'lib/barker/game_show.rb', line 103
def leave(candidate_id)
candidate = candidates.find {|candidate| candidate.id == candidate_id }
candidate.leave
candidate
end
|
#next_round ⇒ Object
87
88
89
|
# File 'lib/barker/game_show.rb', line 87
def next_round
current_stage && current_stage.next_round || nil
end
|
#next_round? ⇒ Boolean
91
92
93
|
# File 'lib/barker/game_show.rb', line 91
def next_round?
current_stage && current_stage.next_round?
end
|
#next_stage ⇒ Object
74
75
76
|
# File 'lib/barker/game_show.rb', line 74
def next_stage
@stage += 1
end
|
#next_stage? ⇒ Boolean
78
79
80
|
# File 'lib/barker/game_show.rb', line 78
def next_stage?
!!@stages[stage + 1]
end
|
#round ⇒ Object
149
150
151
|
# File 'lib/barker/game_show.rb', line 149
def round
current_stage && current_stage.try(:round_counter) || -1
end
|
#round_time ⇒ Object
153
154
155
|
# File 'lib/barker/game_show.rb', line 153
def round_time
@guideline.round_time
end
|
#rounds ⇒ Object
NOTE: ignore stages for the moment - merge rounds from all stages together
133
134
135
|
# File 'lib/barker/game_show.rb', line 133
def rounds
@stages.map{|stage| stage.rounds }.flatten
end
|
#running? ⇒ Boolean
60
61
62
|
# File 'lib/barker/game_show.rb', line 60
def running?
started? && !finished? && !aborted?
end
|
#serialize ⇒ Object
141
142
143
|
# File 'lib/barker/game_show.rb', line 141
def serialize
Base64.encode64(Marshal::dump(self))
end
|
#stage ⇒ Object
145
146
147
|
# File 'lib/barker/game_show.rb', line 145
def stage
@stage
end
|
#stages ⇒ Object
65
66
67
68
|
# File 'lib/barker/game_show.rb', line 65
def stages
raise NotStartedError unless started?
@stages
end
|
#start ⇒ Object
35
36
37
38
|
# File 'lib/barker/game_show.rb', line 35
def start
@stages += quizzes.map { |quiz| Stage.new(quiz, candidates, :guideline => guideline) }
@started_at = Time.now unless started_at
end
|
#started? ⇒ Boolean
48
49
50
|
# File 'lib/barker/game_show.rb', line 48
def started?
!!started_at
end
|
#valid? ⇒ Boolean
26
27
28
|
# File 'lib/barker/game_show.rb', line 26
def valid?
started? && stages.any? && candidates.any?
end
|