forked from PeterJCLaw/srcomp-stream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsrcomp.coffee
206 lines (169 loc) · 6.03 KB
/
srcomp.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
rq = require 'request'
_ = require 'underscore'
moment = require 'moment'
Bacon = require 'baconjs'
configuration = require './config'
checkedRequest = (url, cb) ->
rq url, (error, response, body) =>
if error
console.error "Error making request to '#{url}': #{error}"
return
unless response.statusCode is 200
console.error "Invalid status code from '#{url}': #{response.statusCode}"
return
return cb(response, body)
class SRComp
constructor: (@base) ->
@events = new Bacon.Bus()
@teams = {}
@matches = []
@currentDelay = 0
@currentMatch = []
@currentStagingMatches = []
@currentShepherdingMatches = []
@lastScoredMatch = null
@koRounds = null
@tiebreaker = null
do @queryConfig
queryConfig: ->
configInterval = null
loadConfig = () =>
checkedRequest "#{@base}/config", (response, body) =>
# Avoid double configuration
return unless configInterval
clearInterval configInterval
configInterval = null
@config = JSON.parse(body)['config']
console.log @config
do @queryState
setInterval (=> do @sendPing), @config['ping_period'] * 1000
setInterval (=> do @queryState), 500
setInterval (=> do @updateCurrent), 2000
configInterval = setInterval (=> do loadConfig), 300
queryState: ->
checkedRequest "#{@base}/state", (response, body) =>
newState = JSON.parse(body)['state']
if newState isnt @savedState
@savedState = newState
do @reloadData
reloadData: ->
console.log "Reloading data"
do @reloadTeams
do @reloadMatches
do @reloadLastScoredMatch
do @reloadKnockouts
do @reloadTiebreaker
seedRecords: ->
@seedTeamRecords().concat(@seedMatchRecord())
.concat(@seedCurrentStagingMatchesRecord())
.concat(@seedCurrentShepherdingMatchesRecord())
.concat(@seedScoredMatchRecord())
.concat(@seedKnockoutsRecord())
.concat(@seedDelayRecord())
.concat(@seedTiebreakerRecord())
seedTeamRecords: ->
for team, record of @teams
event: 'team'
data: record
seedDelayRecord: ->
[{event: 'current-delay', data: @currentDelay}]
seedMatchRecord: ->
[{event: 'match', data: @currentMatch}]
seedCurrentStagingMatchesRecord: ->
[{event: 'current-staging-matches', data: @currentStagingMatches}]
seedCurrentShepherdingMatchesRecord: ->
[{event: 'current-shepherding-matches', data: @currentShepherdingMatches}]
seedScoredMatchRecord: ->
return [] if not @lastScoredMatch?
[{event: 'last-scored-match', data: @lastScoredMatch}]
seedKnockoutsRecord: ->
return [] if not @koRounds?
[{event: 'knockouts', data: @koRounds}]
seedTiebreakerRecord: ->
return [] if not @tiebreaker?
[{event: 'tiebreaker', data: @tiebreaker}]
txTeamRecord: (tla, record) ->
@events.push
event: 'team'
data: record
reloadTeams: ->
checkedRequest "#{@base}/teams", (response, body) =>
newTeams = JSON.parse(body)['teams']
if not _.isEqual(@teams, newTeams)
# Diffs 1: deleted teams
for key of _.difference(_.keys(@teams), _.keys(newTeams))
@txTeamRecord key, null
# Diffs 2: actual differences
for key, record of newTeams
if not _.isEqual(record, @teams[key])
@txTeamRecord key, record
@teams = newTeams
reloadMatches: ->
checkedRequest "#{@base}/matches", (response, body) =>
matches = JSON.parse(body)['matches']
if not _.isEqual(@matches, matches)
@matches = matches
do @updateCurrent
reloadLastScoredMatch: ->
checkedRequest "#{@base}/matches/last_scored", (response, body) =>
lastScoredMatch = JSON.parse(body)['last_scored']
if not _.isEqual(@lastScoredMatch, lastScoredMatch)
@lastScoredMatch = lastScoredMatch
@events.push
event: 'last-scored-match'
data: @lastScoredMatch
updateCurrent: ->
checkedRequest "#{@base}/current", (response, body) =>
currentInfo = JSON.parse(body)
newCurrentMatch = currentInfo['matches']
if not _.isEqual(newCurrentMatch, @currentMatch)
@currentMatch = newCurrentMatch
@events.push
event: 'match'
data: @currentMatch
newCurrentStagingMatches = currentInfo['staging_matches']
if not _.isEqual(newCurrentStagingMatches, @currentStagingMatches)
@currentStagingMatches = newCurrentStagingMatches
@events.push
event: 'current-staging-matches'
data: @currentStagingMatches
newCurrentShepherdingMatches = currentInfo['shepherding_matches']
if not _.isEqual(newCurrentShepherdingMatches, @currentShepherdingMatches)
@currentShepherdingMatches = newCurrentShepherdingMatches
@events.push
event: 'current-shepherding-matches'
data: @currentShepherdingMatches
newCurrentDelay = currentInfo['delay']
if not _.isEqual(newCurrentDelay, @currentDelay)
@currentDelay = newCurrentDelay
@events.push
event: 'current-delay'
data: @currentDelay
sendPing: ->
@events.push
event: 'ping'
data: @config['ping_period'] * 1000
reloadKnockouts: ->
checkedRequest "#{@base}/knockout", (response, body) =>
newKORounds = JSON.parse(body)['rounds']
if not _.isEqual(@koRounds, newKORounds)
@koRounds = newKORounds
@events.push
event: 'knockouts'
data: @koRounds
reloadTiebreaker: ->
rq "#{@base}/tiebreaker", (error, response, body) =>
if error
console.error error
return
newTiebreaker = null
if response.statusCode is 200
newTiebreaker = JSON.parse(body)['tiebreaker']
console.log(@tiebreaker, newTiebreaker)
if not _.isEqual(@tiebreaker, newTiebreaker)
@tiebreaker = newTiebreaker
@events.push
event: 'tiebreaker'
data: @tiebreaker
module.exports =
SRComp: SRComp