barker 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (91) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +3 -0
  3. data/.ruby-gemset +1 -0
  4. data/.ruby-version +1 -0
  5. data/.simplecov +7 -0
  6. data/Gemfile +6 -0
  7. data/Gemfile.lock +50 -0
  8. data/LICENSE +22 -0
  9. data/README.md +204 -0
  10. data/Rakefile +12 -0
  11. data/barker.gemspec +29 -0
  12. data/ci/run +20 -0
  13. data/lib/barker/answer.rb +11 -0
  14. data/lib/barker/api/base.rb +145 -0
  15. data/lib/barker/api/response/base.rb +25 -0
  16. data/lib/barker/api/response/candidate.rb +38 -0
  17. data/lib/barker/api/response/candidate_answer.rb +23 -0
  18. data/lib/barker/api/response/candidate_joker.rb +21 -0
  19. data/lib/barker/api/response/candidate_question.rb +51 -0
  20. data/lib/barker/api/response/error.rb +17 -0
  21. data/lib/barker/api/response/game_show.rb +43 -0
  22. data/lib/barker/api/response/round.rb +21 -0
  23. data/lib/barker/api/response.rb +15 -0
  24. data/lib/barker/api.rb +7 -0
  25. data/lib/barker/candidate.rb +52 -0
  26. data/lib/barker/candidate_answer.rb +42 -0
  27. data/lib/barker/candidate_joker.rb +30 -0
  28. data/lib/barker/candidate_question.rb +116 -0
  29. data/lib/barker/candidate_round.rb +32 -0
  30. data/lib/barker/entity.rb +15 -0
  31. data/lib/barker/game_show.rb +176 -0
  32. data/lib/barker/guideline/base.rb +33 -0
  33. data/lib/barker/guideline/llmcquiz/questions.yaml +300 -0
  34. data/lib/barker/guideline/llmcquiz.rb +28 -0
  35. data/lib/barker/guideline/mock.rb +17 -0
  36. data/lib/barker/guideline.rb +8 -0
  37. data/lib/barker/import/yaml.rb +22 -0
  38. data/lib/barker/import.rb +6 -0
  39. data/lib/barker/joker.rb +28 -0
  40. data/lib/barker/question.rb +12 -0
  41. data/lib/barker/quiz.rb +19 -0
  42. data/lib/barker/repo/memory/game_show.rb +38 -0
  43. data/lib/barker/repo/memory.rb +8 -0
  44. data/lib/barker/repo.rb +6 -0
  45. data/lib/barker/round.rb +68 -0
  46. data/lib/barker/stage.rb +38 -0
  47. data/lib/barker/store/memory.rb +158 -0
  48. data/lib/barker/store.rb +6 -0
  49. data/lib/barker/validations.rb +14 -0
  50. data/lib/barker/version.rb +3 -0
  51. data/lib/barker.rb +45 -0
  52. data/test/factories/answer.rb +21 -0
  53. data/test/factories/candidate.rb +23 -0
  54. data/test/factories/candidate_answer.rb +12 -0
  55. data/test/factories/candidate_joker.rb +8 -0
  56. data/test/factories/candidate_question.rb +16 -0
  57. data/test/factories/candidate_round.rb +8 -0
  58. data/test/factories/game_show.rb +12 -0
  59. data/test/factories/joker.rb +5 -0
  60. data/test/factories/question.rb +13 -0
  61. data/test/factories/quiz.rb +11 -0
  62. data/test/factories/round.rb +12 -0
  63. data/test/factories/stage.rb +12 -0
  64. data/test/fixtures/questions.yaml +20 -0
  65. data/test/test_helper.rb +72 -0
  66. data/test/test_mocks.rb +21 -0
  67. data/test/unit/answer_test.rb +49 -0
  68. data/test/unit/api/base_test.rb +207 -0
  69. data/test/unit/api/response/base_test.rb +35 -0
  70. data/test/unit/api/response/candidate_answer_test.rb +64 -0
  71. data/test/unit/api/response/candidate_joker_test.rb +60 -0
  72. data/test/unit/api/response/candidate_question_test.rb +99 -0
  73. data/test/unit/api/response/candidate_test.rb +82 -0
  74. data/test/unit/api/response/game_show_test.rb +105 -0
  75. data/test/unit/api/response/round_test.rb +49 -0
  76. data/test/unit/candidate_answer_test.rb +66 -0
  77. data/test/unit/candidate_question_test.rb +277 -0
  78. data/test/unit/candidate_round_test.rb +64 -0
  79. data/test/unit/candidate_test.rb +76 -0
  80. data/test/unit/game_show_test.rb +372 -0
  81. data/test/unit/guideline/base_test.rb +34 -0
  82. data/test/unit/guideline/llmcquiz_test.rb +35 -0
  83. data/test/unit/guideline/mock_test.rb +33 -0
  84. data/test/unit/import/yaml_test.rb +41 -0
  85. data/test/unit/joker_test.rb +32 -0
  86. data/test/unit/question_test.rb +55 -0
  87. data/test/unit/quiz_test.rb +37 -0
  88. data/test/unit/repo/memory/game_show_test.rb +54 -0
  89. data/test/unit/round_test.rb +167 -0
  90. data/test/unit/stage_test.rb +53 -0
  91. metadata +283 -0
@@ -0,0 +1,51 @@
1
+ module Barker
2
+ module Api
3
+ module Response
4
+ class CandidateQuestion < Base
5
+
6
+ extend Forwardable
7
+ def_delegators :candidate_question, :id, :question_id, :candidate_id, :label, :state, :asked_at, :answered_at, :asked?, :answered?, :open?, :time_left, :timeout?, :correct?, :wrong?
8
+
9
+ def answers
10
+ candidate_question.answers.map { |candidate_answer| build_candidate_answer(candidate_answer) }
11
+ end
12
+
13
+ def given_answer
14
+ build_candidate_answer(candidate_question.given_answer)
15
+ end
16
+
17
+ def to_hash
18
+ {
19
+ 'id' => id,
20
+ 'question_id' => question_id,
21
+ 'candidate_id' => candidate_id,
22
+ 'label' => label,
23
+ 'state' => state,
24
+ 'asked_at' => to_i(asked_at),
25
+ 'answered_at' => to_i(answered_at),
26
+ 'asked?' => asked?,
27
+ 'answered?' => answered?,
28
+ 'open?' => open?,
29
+ 'time_left' => time_left,
30
+ 'timeout?' => timeout?,
31
+ 'correct?' => correct?,
32
+ 'wrong?' => wrong?,
33
+ 'answers' => answers.map(&:to_hash),
34
+ 'given_answer' => given_answer? && given_answer.to_hash
35
+ }
36
+ end
37
+
38
+ private
39
+
40
+ def build_candidate_answer(candidate_answer)
41
+ Response::CandidateAnswer.new(:candidate_answer => candidate_answer)
42
+ end
43
+
44
+ def given_answer?
45
+ given_answer.candidate_answer
46
+ end
47
+
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,17 @@
1
+ module Barker
2
+ module Api
3
+ module Response
4
+ class Error < Base
5
+
6
+ extend Forwardable
7
+
8
+ def to_hash
9
+ {
10
+ 'error' => error
11
+ }
12
+ end
13
+
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,43 @@
1
+ module Barker
2
+ module Api
3
+ module Response
4
+ class GameShow < Base
5
+
6
+ extend Forwardable
7
+ def_delegators :game_show, :id, :game_show_id, :joker_ids, :jokers, :started_at, :finished_at, :aborted_at, :started?, :finished?, :aborted?, :running?, :stage, :round, :next_stage?, :next_round?
8
+
9
+ def candidates
10
+ game_show.candidates.map {|candidate| Response::Candidate.new(:candidate => candidate)}
11
+ end
12
+
13
+ def rounds
14
+ game_show.rounds.map {|round| Response::Round.new(:round => round)}
15
+ end
16
+
17
+ def to_hash
18
+ {
19
+ 'id' => id,
20
+ 'game_show_id' => game_show_id,
21
+ 'joker_ids' => joker_ids,
22
+ 'jokers' => jokers.map(&:to_hash),
23
+ 'started_at' => to_i(started_at),
24
+ 'finished_at' => to_i(finished_at),
25
+ 'aborted_at' => to_i(aborted_at),
26
+ 'started?' => started?,
27
+ 'finished?' => finished?,
28
+ 'aborted?' => aborted?,
29
+ 'running?' => running?,
30
+ 'stage' => stage,
31
+ 'round' => round,
32
+ 'next_stage?' => next_stage?,
33
+ 'next_round?' => next_round?,
34
+ 'candidates' => candidates.map(&:to_hash),
35
+ 'rounds' => rounds.map(&:to_hash),
36
+ 'round_time' => round_time
37
+ }
38
+ end
39
+
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,21 @@
1
+ module Barker
2
+ module Api
3
+ module Response
4
+ class Round < Base
5
+
6
+ extend Forwardable
7
+
8
+ def questions
9
+ round.candidate_questions.map{|candidate_question| Response::CandidateQuestion.new(:candidate_question => candidate_question) }
10
+ end
11
+
12
+ def to_hash
13
+ {
14
+ 'questions' => questions.map(&:to_hash)
15
+ }
16
+ end
17
+
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,15 @@
1
+ require 'barker/api/response/base'
2
+ require 'barker/api/response/error'
3
+ require 'barker/api/response/game_show'
4
+ require 'barker/api/response/round'
5
+ require 'barker/api/response/candidate'
6
+ require 'barker/api/response/candidate_question'
7
+ require 'barker/api/response/candidate_answer'
8
+ require 'barker/api/response/candidate_joker'
9
+
10
+ module Barker
11
+ module Api
12
+ module Response
13
+ end
14
+ end
15
+ end
data/lib/barker/api.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'barker/api/base'
2
+ require 'barker/api/response'
3
+
4
+ module Barker
5
+ module Api
6
+ end
7
+ end
@@ -0,0 +1,52 @@
1
+ module Barker
2
+ class Candidate
3
+
4
+ include Errors
5
+
6
+ attr_accessor :id, :locale, :joined_at, :leaved_at, :aborted_at
7
+
8
+ def initialize(object)
9
+ raise HasToRespondToIdError unless object.respond_to?(:id)
10
+ raise HasToRespondToLocaleError unless object.respond_to?(:locale)
11
+
12
+ @id = object.id
13
+ @locale = object.locale.to_s
14
+ @joined_at = nil
15
+ @leaved_at = nil
16
+ @aborted_at = nil
17
+ end
18
+
19
+ def join
20
+ @joined_at = Time.now
21
+ end
22
+
23
+ def leave
24
+ @leaved_at = Time.now
25
+ end
26
+
27
+ def abort
28
+ @aborted_at = Time.now
29
+ end
30
+
31
+ def joined?
32
+ !!joined_at
33
+ end
34
+
35
+ def leaved?
36
+ !!leaved_at
37
+ end
38
+
39
+ def aborted?
40
+ !!aborted_at
41
+ end
42
+
43
+ def jokers
44
+ @jokers ||= []
45
+ end
46
+
47
+ def candidate_id
48
+ id
49
+ end
50
+
51
+ end
52
+ end
@@ -0,0 +1,42 @@
1
+ module Barker
2
+ class CandidateAnswer
3
+
4
+ attr_accessor :candidate, :answer, :reveal_answer
5
+
6
+ def initialize(candidate, answer)
7
+ @candidate = candidate
8
+ @answer = answer
9
+ @revealed = false
10
+ end
11
+
12
+ def valid?
13
+ answer.valid?
14
+ end
15
+
16
+ def label
17
+ answer.label[candidate.locale] || answer.label["en"]
18
+ end
19
+
20
+ def revealed?
21
+ @revealed
22
+ end
23
+
24
+ def reveal
25
+ @revealed = true
26
+ end
27
+
28
+ def correct?
29
+ answer.correct?
30
+ end
31
+
32
+ def answer_id
33
+ answer.id
34
+ end
35
+ alias :id :answer_id
36
+
37
+ def candidate_id
38
+ candidate.id
39
+ end
40
+
41
+ end
42
+ end
@@ -0,0 +1,30 @@
1
+ module Barker
2
+ class CandidateJoker
3
+
4
+ include Errors
5
+
6
+ attr_reader :used_at, :used_in
7
+
8
+ def initialize(candidate_question, joker)
9
+ @candidate_question = candidate_question
10
+ @joker = joker
11
+ end
12
+
13
+ def joker_id
14
+ @joker.id
15
+ end
16
+ alias :id :joker_id
17
+
18
+ def candidate_id
19
+ @candidate_question.candidate_id
20
+ end
21
+
22
+ def call
23
+ raise UnknownJokerError unless @joker
24
+ @used_at = Time.now
25
+ @used_in = @candidate_question
26
+ @joker.call(@used_in)
27
+ end
28
+ end
29
+ end
30
+
@@ -0,0 +1,116 @@
1
+ module Barker
2
+ class CandidateQuestion
3
+
4
+ include Errors
5
+
6
+ attr_reader :candidate, :question, :answers, :given_answer
7
+ attr_accessor :answered_at, :asked_at, :timer
8
+
9
+ def initialize(candidate, question, options = {})
10
+ @candidate = candidate
11
+ @question = question
12
+ @answers = question.answers.map { |answer| CandidateAnswer.new(candidate, answer) }
13
+
14
+ @given_answer = nil
15
+ @asked_at = nil
16
+ @answered_at = nil
17
+ @timer = options[:timer] || nil
18
+ end
19
+
20
+ def ask(options = {})
21
+ if !asked? || options[:reset_timer]
22
+ @asked_at = Time.now
23
+ end
24
+ self
25
+ end
26
+
27
+ def answer(answer_id)
28
+ raise AlreadyAnsweredError if answered?
29
+ raise AnswerNotFoundError unless @given_answer = answers.find {|answer| answer.id == answer_id }
30
+ @answered_at = Time.now
31
+ given_answer
32
+ end
33
+
34
+ def joker(joker)
35
+ raise JokerAlreadyUsedError if joker_played?(joker)
36
+ CandidateJoker.new(self, joker).tap do |candidate_joker|
37
+ candidate_joker.call
38
+ candidate.jokers << candidate_joker
39
+ end
40
+ end
41
+
42
+ def valid?
43
+ question.valid? && timestamps? && asked_before_answered?
44
+ end
45
+
46
+ def open?
47
+ !asked? && !answered?
48
+ end
49
+
50
+ def asked?
51
+ !!asked_at
52
+ end
53
+
54
+ def answered?
55
+ !!answered_at
56
+ end
57
+
58
+ def correct?
59
+ !!given_answer.try(:correct?)
60
+ end
61
+
62
+ def wrong?
63
+ !correct?
64
+ end
65
+
66
+ def state
67
+ if open?
68
+ :open
69
+ elsif answered?
70
+ correct?? :correct : :wrong
71
+ elsif asked?
72
+ :asked
73
+ end
74
+ end
75
+
76
+ def question_id
77
+ question.id
78
+ end
79
+ alias :id :question_id
80
+
81
+ def candidate_id
82
+ candidate.id
83
+ end
84
+
85
+ def label
86
+ question.label[candidate.locale] || question.label["en"]
87
+ end
88
+
89
+ def timeout?
90
+ timer.nil?? false : !(time_left.try(:>, 0))
91
+ end
92
+
93
+ def time_left
94
+ left = timer.try(:-, delay)
95
+ (left.try(:>=, 0))? left : 0
96
+ end
97
+
98
+ private
99
+
100
+ def delay
101
+ (asked_at.try(:-, answered_at || Time.now) || 0).abs
102
+ end
103
+
104
+ def timestamps?
105
+ asked_at && answered_at
106
+ end
107
+
108
+ def asked_before_answered?
109
+ answered_at >= asked_at
110
+ end
111
+
112
+ def joker_played?(joker)
113
+ candidate.jokers.any? {|candidate_joker| candidate_joker.id == joker.id}
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,32 @@
1
+ module Barker
2
+ class CandidateRound
3
+ attr_reader :candidate, :round, :guideline, :timer
4
+
5
+ def initialize(candidate, round, options = {})
6
+ @candidate = candidate
7
+ @round = round
8
+ @guideline = options[:guideline]
9
+ @timer = guideline.try(:round_time)
10
+ end
11
+
12
+ def question
13
+ @question ||= CandidateQuestion.new(candidate, round.question, :timer => timer)
14
+ end
15
+
16
+ def ask(options = {})
17
+ question.ask(options)
18
+ end
19
+
20
+ def answer(answer_id)
21
+ question.answer(answer_id)
22
+ end
23
+
24
+ def given_answer
25
+ question.given_answer
26
+ end
27
+
28
+ def joker(joker)
29
+ question.joker(joker)
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,15 @@
1
+ module Barker
2
+ class Entity < Struct
3
+
4
+ include ActiveModel::Validations
5
+
6
+ def self.model_name
7
+ ActiveModel::Name.new(self, nil, self.to_s)
8
+ end
9
+
10
+ def ==(other)
11
+ id == other.id
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,176 @@
1
+ module Barker
2
+ class GameShow
3
+
4
+ include Errors
5
+
6
+ attr_accessor :id
7
+ attr_reader :quizzes, :candidates, :guideline, :jokers, :guideline, :created_at, :started_at, :finished_at, :aborted_at
8
+
9
+ def initialize(quizzes, candidates, options = {})
10
+ @started_at = nil
11
+ @finished_at = nil
12
+ @aborted_at = nil
13
+ @created_at = Time.now
14
+ @stage = -1
15
+ @guideline = build_guideline(options[:guideline] || :base)
16
+ @quizzes = build_quizzes(quizzes)
17
+ @candidates = build_candidates(candidates)
18
+ @jokers = build_jokers
19
+ @stages = []
20
+ end
21
+
22
+ def ==(other)
23
+ created_at == other.created_at
24
+ end
25
+
26
+ def valid?
27
+ started? && stages.any? && candidates.any?
28
+ end
29
+
30
+ def game_show_id
31
+ id
32
+ end
33
+
34
+ # state
35
+ def start
36
+ @stages += quizzes.map { |quiz| Stage.new(quiz, candidates, :guideline => guideline) }
37
+ @started_at = Time.now unless started_at
38
+ end
39
+
40
+ def finish
41
+ @finished_at = Time.now unless finished_at
42
+ end
43
+
44
+ def abort
45
+ @aborted_at = Time.now unless aborted_at
46
+ end
47
+
48
+ def started?
49
+ !!started_at
50
+ end
51
+
52
+ def finished?
53
+ !!finished_at
54
+ end
55
+
56
+ def aborted?
57
+ !!aborted_at
58
+ end
59
+
60
+ def running?
61
+ started? && !finished? && !aborted?
62
+ end
63
+
64
+ # stage
65
+ def stages
66
+ raise NotStartedError unless started?
67
+ @stages
68
+ end
69
+
70
+ def current_stage
71
+ stage != -1 && @stages[stage]
72
+ end
73
+
74
+ def next_stage
75
+ @stage += 1
76
+ end
77
+
78
+ def next_stage?
79
+ !!@stages[stage + 1]
80
+ end
81
+
82
+ # round
83
+ def current_round
84
+ current_stage && current_stage.current_round || nil
85
+ end
86
+
87
+ def next_round
88
+ current_stage && current_stage.next_round || nil
89
+ end
90
+
91
+ def next_round?
92
+ current_stage && current_stage.next_round?
93
+ end
94
+
95
+ def join(candidate)
96
+ raise AlreadyStartedError if started?
97
+ candidate.join
98
+ @candidates ||= []
99
+ @candidates << candidate
100
+ candidate
101
+ end
102
+
103
+ def leave(candidate_id)
104
+ candidate = candidates.find {|candidate| candidate.id == candidate_id }
105
+ candidate.leave
106
+ candidate
107
+ end
108
+
109
+ def ask(candidate_id, options = {})
110
+ current_round.ask(candidate_id, options)
111
+ end
112
+
113
+ def answer(candidate_id, answer_id)
114
+ guideline.answer(current_round.candidate_round(candidate_id), candidate_id, answer_id)
115
+ current_round.answer(candidate_id, answer_id)
116
+ end
117
+
118
+ def given_answer(candidate_id)
119
+ current_round.given_answer(candidate_id)
120
+ end
121
+
122
+ def joker(candidate_id, joker_id)
123
+ joker = jokers.find{|joker| joker.id == joker_id }
124
+ candidate = candidates.find{|candidate| candidate.id == candidate.id }
125
+ current_round.joker(candidate, joker)
126
+ end
127
+
128
+ def joker_ids
129
+ jokers.map(&:id)
130
+ end
131
+
132
+ # NOTE: ignore stages for the moment - merge rounds from all stages together
133
+ def rounds
134
+ @stages.map{|stage| stage.rounds }.flatten
135
+ end
136
+
137
+ def self.load(serialized)
138
+ Marshal::load(Base64.decode64(serialized))
139
+ end
140
+
141
+ def serialize
142
+ Base64.encode64(Marshal::dump(self))
143
+ end
144
+
145
+ def stage
146
+ @stage
147
+ end
148
+
149
+ def round
150
+ current_stage && current_stage.try(:round_counter) || -1
151
+ end
152
+
153
+ def round_time
154
+ @guideline.round_time
155
+ end
156
+
157
+ private
158
+
159
+ def build_guideline(key)
160
+ "Barker::Guideline::#{key.to_s.classify}".constantize.new
161
+ end
162
+
163
+ def build_quizzes(quizzes)
164
+ (quizzes && quizzes.any?)? quizzes : guideline.quizzes
165
+ end
166
+
167
+ def build_candidates(candidates)
168
+ ((candidates && candidates.any?)? candidates : guideline.candidates).map {|candidate| self.join(candidate) }
169
+ end
170
+
171
+ def build_jokers
172
+ guideline.jokers.map {|joker_key| Joker.new(joker_key) }
173
+ end
174
+
175
+ end
176
+ end
@@ -0,0 +1,33 @@
1
+ module Barker
2
+ module Guideline
3
+ class Base
4
+
5
+ include Errors
6
+
7
+ def initialize(options = {})
8
+ end
9
+
10
+ def quizzes
11
+ []
12
+ end
13
+
14
+ def candidates
15
+ []
16
+ end
17
+
18
+ def jokers
19
+ []
20
+ end
21
+
22
+ def round_time
23
+ nil
24
+ end
25
+
26
+ def answer(current_round, candidate_id, answer_id)
27
+ nil
28
+ end
29
+
30
+ end
31
+
32
+ end
33
+ end