Class: Barker::GameShow

Inherits:
Object
  • 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_atObject (readonly)

Returns the value of attribute aborted_at.



7
8
9
# File 'lib/barker/game_show.rb', line 7

def aborted_at
  @aborted_at
end

#candidatesObject (readonly)

Returns the value of attribute candidates.



7
8
9
# File 'lib/barker/game_show.rb', line 7

def candidates
  @candidates
end

#created_atObject (readonly)

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_atObject (readonly)

Returns the value of attribute finished_at.



7
8
9
# File 'lib/barker/game_show.rb', line 7

def finished_at
  @finished_at
end

#guidelineObject (readonly)

Returns the value of attribute guideline.



7
8
9
# File 'lib/barker/game_show.rb', line 7

def guideline
  @guideline
end

#idObject

Returns the value of attribute id.



6
7
8
# File 'lib/barker/game_show.rb', line 6

def id
  @id
end

#jokersObject (readonly)

Returns the value of attribute jokers.



7
8
9
# File 'lib/barker/game_show.rb', line 7

def jokers
  @jokers
end

#quizzesObject (readonly)

Returns the value of attribute quizzes.



7
8
9
# File 'lib/barker/game_show.rb', line 7

def quizzes
  @quizzes
end

#started_atObject (readonly)

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

#abortObject



44
45
46
# File 'lib/barker/game_show.rb', line 44

def abort
  @aborted_at = Time.now unless aborted_at
end

#aborted?Boolean

Returns:

  • (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_roundObject

round



83
84
85
# File 'lib/barker/game_show.rb', line 83

def current_round
  current_stage && current_stage.current_round || nil
end

#current_stageObject



70
71
72
# File 'lib/barker/game_show.rb', line 70

def current_stage
  stage != -1 && @stages[stage]
end

#finishObject



40
41
42
# File 'lib/barker/game_show.rb', line 40

def finish
  @finished_at = Time.now unless finished_at
end

#finished?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/barker/game_show.rb', line 52

def finished?
  !!finished_at
end

#game_show_idObject



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_idsObject



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_roundObject



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

Returns:

  • (Boolean)


91
92
93
# File 'lib/barker/game_show.rb', line 91

def next_round?
  current_stage && current_stage.next_round?
end

#next_stageObject



74
75
76
# File 'lib/barker/game_show.rb', line 74

def next_stage
  @stage += 1
end

#next_stage?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/barker/game_show.rb', line 78

def next_stage?
  !!@stages[stage + 1]
end

#roundObject



149
150
151
# File 'lib/barker/game_show.rb', line 149

def round
  current_stage && current_stage.try(:round_counter) || -1
end

#round_timeObject



153
154
155
# File 'lib/barker/game_show.rb', line 153

def round_time
  @guideline.round_time
end

#roundsObject

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

Returns:

  • (Boolean)


60
61
62
# File 'lib/barker/game_show.rb', line 60

def running?
  started? && !finished? && !aborted?
end

#serializeObject



141
142
143
# File 'lib/barker/game_show.rb', line 141

def serialize
  Base64.encode64(Marshal::dump(self))
end

#stageObject



145
146
147
# File 'lib/barker/game_show.rb', line 145

def stage
  @stage
end

#stagesObject

stage

Raises:



65
66
67
68
# File 'lib/barker/game_show.rb', line 65

def stages
  raise NotStartedError unless started?
  @stages
end

#startObject

state



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

Returns:

  • (Boolean)


48
49
50
# File 'lib/barker/game_show.rb', line 48

def started?
  !!started_at
end

#valid?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/barker/game_show.rb', line 26

def valid?
  started? && stages.any? && candidates.any?
end