Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
195 lines (157 loc) · 4.94 KB
/
Copy pathapp.js
File metadata and controls
195 lines (157 loc) · 4.94 KB
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
const express = require('express')
var cors = require('cors')
var bodyParser = require('body-parser')
const NodeCache = require('node-cache')
const moment = require('moment')
const app = express()
const PORT = process.env.PORT || 3000
app.use(cors()) // allow CORS requests
app.use(bodyParser.json()) // parse application/json
app.get('/available', (req, res) => {
res.send({
available: true
})
})
/** Game Finder cache */
const gameFinderAppliedCache = new NodeCache({ stdTTL: 10 })
/** Blackbox caches, state and config */
let blackboxDrawId = 1
let blackboxLastDrawDate = moment()
const blackboxDrawFrequencyInMinutes = 1
const blackboxAppliedCache = new NodeCache({ stdTTL: 3600 }) // only hold long enough till the draw is made
const blackboxDrawCache = new NodeCache({ stdTTL: 7200 }) // hold for a couple of hours
function getRandomInteger(max) {
return Math.floor(Math.random() * max)
}
function getRandomArrayElement(array) {
return array[getRandomInteger(array.length)]
}
/**
* Run the Blackbox draw every blackboxDrawFrequencyInMinutes
*/
setInterval(function () {
const blackboxDrawTeams = []
for (const coachName of blackboxAppliedCache.keys()) {
const blackboxApplication = blackboxAppliedCache.get(coachName)
if (blackboxApplication.teams.length > 0) {
blackboxDrawTeams.push({
coachName: coachName,
team: getRandomArrayElement(blackboxApplication.teams)
})
}
}
let newPair = true
let team1;
let team2;
const blackboxMatches = []
for (const coachSettings of blackboxDrawTeams) {
if (newPair) {
team1 = coachSettings
newPair = false
} else {
team2 = coachSettings
if (team1 && team2) {
blackboxMatches.push({
home: team1,
away: team2
})
}
team1 = null
team2 = null
newPair = true
}
}
// only create a draw entry if a match up was made
if (blackboxMatches.length > 0) {
const drawKey = moment().format('YYYY-MM-DD-HH-mm')
blackboxDrawCache.set(drawKey, {drawKey: drawKey, date: new Date(), matches: blackboxMatches})
}
// increment the draw id so the UI knows its not on the active one anymore
blackboxDrawId++
// reset the applied teams
blackboxAppliedCache.flushAll()
// record when the last draw took place, even if no-one was matched up
blackboxLastDrawDate = moment()
}, blackboxDrawFrequencyInMinutes * 60000)
app.post('/blackbox/activate', (req, res) => {
const coach = req.body.coach
const teams = req.body.teams
blackboxAppliedCache.set(
coach.name,
{
coach: coach,
teams: teams
}
)
res.send({drawId: blackboxDrawId, teamCount: teams.length})
})
app.post('/blackbox/deactivate', (req, res) => {
const coach = req.body.coach
const deleteResult = blackboxAppliedCache.del(coach.name)
res.send({coachRemoved: deleteResult})
})
function getBlackboxCurrentInfo() {
let coachCount = 0
let teamCount = 0
const timeOfNextDraw = blackboxLastDrawDate.clone().add(blackboxDrawFrequencyInMinutes, 'minutes');
for (const coachName of blackboxAppliedCache.keys()) {
const blackboxApplication = blackboxAppliedCache.get(coachName)
if (blackboxApplication.teams.length > 0) {
coachCount++
}
teamCount += blackboxApplication.teams.length
}
return {
drawId: blackboxDrawId,
coachCount: coachCount,
teamCount: teamCount,
timeOfNextDraw: timeOfNextDraw
}
}
function getBlackboxDrawResults() {
const blackboxDrawCacheKeys = blackboxDrawCache.keys()
blackboxDrawCacheKeys.sort(function (a, b) {
if (a == b) {
return 0
} else if (a < b) {
return 1
} else {
return -1;
}
})
const results = []
for (const blackboxDrawCacheKey of blackboxDrawCacheKeys) {
const drawResult = blackboxDrawCache.get(blackboxDrawCacheKey)
if (drawResult !== undefined) {
results.push(drawResult)
}
}
return results
}
app.get('/blackbox/latest', (req, res) => {
res.send({
currentInfo: getBlackboxCurrentInfo(),
drawResults: getBlackboxDrawResults()
})
})
app.post('/game-finder/apply', (req, res) => {
const gameFinderCoachRequest = req.body
gameFinderAppliedCache.set(
gameFinderCoachRequest.coach.name,
gameFinderCoachRequest
)
const opponentGameFinderCoachRequests = []
for (const oppCoachName of gameFinderAppliedCache.keys()) {
if (oppCoachName === gameFinderCoachRequest.coach.name) {
continue
}
let opponentGameFinderCoachRequest = gameFinderAppliedCache.get(oppCoachName)
if (opponentGameFinderCoachRequest !== undefined) {
opponentGameFinderCoachRequests.push(opponentGameFinderCoachRequest)
}
}
res.send(opponentGameFinderCoachRequests)
})
app.listen(PORT, () => {
console.log(`App listening at http://localhost:${PORT}`)
})