Skip to content

Commit 6fc7e73

Browse files
Merge pull request #5 from taylorfinnell/support-show-types
support game types
2 parents 2aaf83a + ba2004a commit 6fc7e73

File tree

9 files changed

+77
-15
lines changed

9 files changed

+77
-15
lines changed

shard.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
name: hqtrivia
2-
version: 0.2.5
2+
version: 0.2.6
33

44
authors:
55
- Taylor Finnell <[email protected]>
66

7-
crystal: 0.26.0
7+
crystal: 0.27.0
88

99
license: MIT
1010

spec/hqtrivia/show_coordinator_spec.cr

+53-3
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,66 @@ require "../../spec_helper"
22

33
module HqTrivia
44
describe ShowCoordinator do
5-
it "yields the show if it is active" do
6-
coordinator = LocalCoordinator.new("us", active: true)
5+
it "yields if show is active and no game type is set" do
6+
coordinator = LocalCoordinator.new("us")
7+
coordinator.show = Model::Show.new(active: true, show_id: 123,
8+
start_time: Time.utc_now, prize: 666, show_type: "hq-us")
9+
10+
show_coord = ShowCoordinator.new(coordinator)
11+
show_coord.on_show do |show|
12+
show.should be_a(Model::Show)
13+
end
14+
end
15+
16+
it "yields if show is active and game type matches trivia" do
17+
coordinator = LocalCoordinator.new("us", game_type: "trivia")
18+
coordinator.show = Model::Show.new(active: true, show_id: 123,
19+
start_time: Time.utc_now, prize: 666, show_type: "hq-us", game_type: "trivia")
20+
21+
show_coord = ShowCoordinator.new(coordinator)
22+
show_coord.on_show do |show|
23+
show.should be_a(Model::Show)
24+
end
25+
end
26+
27+
it "yields if show is active and game type matches words" do
28+
coordinator = LocalCoordinator.new("us", game_type: "words")
29+
coordinator.show = Model::Show.new(active: true, show_id: 123,
30+
start_time: Time.utc_now, prize: 666, show_type: "hq-us", game_type: "words")
31+
732
show_coord = ShowCoordinator.new(coordinator)
833
show_coord.on_show do |show|
934
show.should be_a(Model::Show)
1035
end
1136
end
1237

38+
it "does not yield if game_type does not match" do
39+
coordinator = LocalCoordinator.new("us", game_type: "trivia")
40+
coordinator.show = Model::Show.new(active: true, show_id: 123,
41+
start_time: Time.utc_now, prize: 666, show_type: "hq-us", game_type: "words")
42+
43+
show_coord = ShowCoordinator.new(coordinator)
44+
show_coord.on_show(blocking: false) do |show|
45+
raise "bad"
46+
end
47+
end
48+
49+
it "yields the show if it is active" do
50+
coordinator = LocalCoordinator.new("us")
51+
coordinator.show = Model::Show.new(active: true, show_id: 123,
52+
start_time: Time.utc_now, prize: 666, show_type: "hq-us", game_type: "words")
53+
54+
show_coord = ShowCoordinator.new(coordinator)
55+
show_coord.on_show(blocking: false) do |show|
56+
show.should be_a(Model::Show)
57+
end
58+
end
59+
1360
it "does not yield the show if it is inactive" do
14-
coordinator = LocalCoordinator.new("us", active: false)
61+
coordinator = LocalCoordinator.new("us")
62+
coordinator.show = Model::Show.new(active: false, show_id: 123,
63+
start_time: Time.utc_now, prize: 666, show_type: "hq-us", game_type: "words")
64+
1565
show_coord = ShowCoordinator.new(coordinator)
1666
show_coord.on_show(blocking: false) do |show|
1767
raise "bad"

spec/hqtrivia_spec.cr

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@ require "./spec_helper"
22

33
describe HqTrivia do
44
it "yields active shows" do
5-
coordinator = HqTrivia::LocalCoordinator.new("us", active: true)
5+
coordinator = HqTrivia::LocalCoordinator.new("us")
6+
coordinator.show = HqTrivia::Model::Show.new(active: true, show_id: 123,
7+
start_time: Time.utc_now, prize: 666, show_type: "hq-us", game_type: "trivia")
68

79
HqTrivia.on_show(coordinator) do |show|
810
show.should be_a(HqTrivia::Model::Show)
911
end
1012
end
1113

1214
it "does not yield inactive shows" do
13-
coordinator = HqTrivia::LocalCoordinator.new("us", active: false)
15+
coordinator = HqTrivia::LocalCoordinator.new("us")
16+
coordinator.show = HqTrivia::Model::Show.new(active: false, show_id: 123,
17+
start_time: Time.utc_now, prize: 666, show_type: "hq-us", game_type: "trivia")
1418

1519
HqTrivia.on_show(coordinator, blocking: false) do |show|
1620
raise "should not have yielded"

src/hqtrivia/bot.cr

+6
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ module HqTrivia
3535
@show.prize
3636
end
3737

38+
# The type of game, currently "trivia" or "words"
39+
def game_type
40+
@coordinator.game_type
41+
end
42+
43+
# The country that is playing this game
3844
def country
3945
@coordinator.country
4046
end

src/hqtrivia/model/show.cr

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ module HqTrivia
1111
broadcast: Broadcast?,
1212
prize: Int32?,
1313
show_type: {type: String?, key: "showType"},
14+
game_type: {type: String?, key: "gameType"},
1415
})
1516

1617
def initialize(@active : Bool, @show_id : Int32?, @start_time : Time?,
17-
@prize : Int32, @show_type : String?)
18+
@prize : Int32, @show_type : String?, @game_type : String? = nil)
1819
end
1920

2021
def socket_url

src/hqtrivia/show_coordinator.cr

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module HqTrivia
1010
while show = @coordinator.current_show
1111
HqTrivia.logger.debug("Show: #{show.to_json}")
1212

13-
if show.active
13+
if show.active && (@coordinator.game_type.nil? || @coordinator.game_type == show.game_type)
1414
return yield show
1515
elsif !blocking
1616
break

src/hqtrivia/show_coordinator/coordinator.cr

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
module HqTrivia
22
abstract class Coordinator
33
getter country
4+
getter game_type
45

5-
def initialize(@country : String)
6+
def initialize(@country : String, @game_type : String? = nil)
67
end
78

89
abstract def current_show
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
module HqTrivia
22
class LocalCoordinator < Coordinator
3-
def initialize(@country : String, @active = false)
4-
end
3+
@show : Model::Show?
4+
5+
property show
56

67
def current_show
7-
Model::Show.new(active: @active, show_id: 123,
8-
start_time: Time.utc_now, prize: 666, show_type: "hq-us")
8+
@show
99
end
1010
end
1111
end

src/hqtrivia/version.cr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
module HqTrivia
22
# Current HqTrivia version
3-
VERSION = "0.2.5"
3+
VERSION = "0.2.6"
44
end

0 commit comments

Comments
 (0)