@@ -9,9 +9,6 @@ module HqTrivia
9
9
class Hq
10
10
include Interface
11
11
12
- class HttpException < Exception
13
- end
14
-
15
12
# Yields a `Model::WebSocketMessage`
16
13
def on_message (& block : HqTrivia ::Model ::WebSocketMessage - > )
17
14
@on_message_callback = block
@@ -21,34 +18,24 @@ module HqTrivia
21
18
@on_raw_message_callback = block
22
19
end
23
20
24
- # Yields a `Model::Show` if a show is active, otherwise nil
25
- def on_show (& block : HqTrivia ::Model ::Show ? - > )
26
- @on_show = block
27
- end
28
-
29
21
# Connects to the HQ websocket
30
- def connect (blocking = true , record_network = false )
31
- while show = current_show
32
- @on_show .try & .call show
33
-
34
- break if show.active || ! blocking
35
-
36
- HqTrivia .logger.debug(" No active show" )
37
- sleep 5
22
+ def connect (show : Model ::Show , coordinator : Coordinator )
23
+ if coordinator.current_show.active
24
+ open_socket(show, coordinator)
25
+ else
26
+ HqTrivia .logger.info " Not connecting show (#{ coordinator.country } ) is no longer active"
38
27
end
39
-
40
- open_socket(show, record_network) if show.active
41
28
end
42
29
43
- private def open_socket (show , record_network )
44
- HqTrivia .logger.debug(" Connecting... " )
30
+ private def open_socket (show , coordinator )
31
+ HqTrivia .logger.debug(" Connecting: #{ coordinator.country } " )
45
32
46
- socket = HTTP ::WebSocket .new(show.socket_url.not_nil!, headers: websocket_headers)
33
+ socket = HTTP ::WebSocket .new(show.socket_url.not_nil!, headers: websocket_headers(coordinator) )
47
34
48
- HqTrivia .logger.debug(" Connected... " )
35
+ HqTrivia .logger.debug(" Connected: #{ coordinator.country } " )
49
36
50
37
socket.on_close do
51
- connect(false ) # don't block, either the game is still active or it is not
38
+ connect(show, coordinator)
52
39
end
53
40
54
41
socket.on_message do |json |
@@ -59,43 +46,15 @@ module HqTrivia
59
46
socket.run
60
47
end
61
48
62
- private def current_show
63
- connection_failed = - > (ex : Exception ) do
64
- HqTrivia .logger.debug(" Connection to HQ server failed...retrying. #{ ex } " )
65
- end
66
-
67
- retryable(on: HttpException | Socket ::Error , tries: 5 , wait: 1 , callback: connection_failed) do
68
- resp = HTTP ::Client .get(current_show_url, headers: authorization_header)
69
-
70
- if (200 ..299).includes?(resp.status_code)
71
- Model ::Show .from_json(resp.body)
72
- else
73
- raise HttpException .new(" #{ resp.body } (#{ resp.status_code } )" )
74
- end
75
- end
76
- end
77
-
78
- private def current_show_url
79
- " https://api-quiz.hype.space/shows/now?type=hq"
80
- end
81
-
82
- private def authorization_header
83
- HTTP ::Headers {" Authorization" => " Bearer #{ authorization_token } " }
84
- end
85
-
86
- private def authorization_token
87
- ENV [" AUTHORIZATION_TOKEN" ]?
88
- end
89
-
90
- private def websocket_headers
49
+ private def websocket_headers (coordinator )
91
50
HTTP ::Headers {
92
51
" x-hq-client" => " iOS/1.2.17" ,
93
52
" x-hq-stk" => " MQ==" ,
94
53
" Host" => " api-quiz.hype.space" ,
95
54
" Connection" => " Keep-Alive" ,
96
55
" Accept-Encoding" => " gzip" ,
97
56
" User-Agent" => " okhttp/3.8.0" ,
98
- }.merge!(authorization_header )
57
+ }.merge!(HqTrivia .auth.header(coordinator.country) )
99
58
end
100
59
end
101
60
end
0 commit comments