-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
207 lines (179 loc) · 5 KB
/
index.js
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
207
const express = require('express')
const app = express()
const http = require('http').createServer(app)
require('dotenv').config()
const ioOpts = process.env.dev
? { cors: { origin: 'http://localhost:8080' } }
: {}
const io = require('socket.io')(http, ioOpts)
require('./db/index')
const { initGame, startGame, getGame, markGameComplete } = require('./lib/game')
const { getPlayers, createPlayer, updatePlayer } = require('./lib/player')
const { getLastLine, addLine, getLines } = require('./lib/line')
const { getSheets, getFullSheets } = require('./lib/sheet')
// placeholder for socket io namespaces
const ns = {}
app.use(express.json())
app.use(express.static('client'))
if (process.env.dev) {
const cors = require('cors')
app.use(cors())
}
// make an initialized game active
app.post('/api/game/start', async (req, res) => {
try {
const id = req.body.id
if (!id) return res.send(400, ('Request must include a game id'))
// TODO: allow passing params
const data = await startGame(id)
ns[id].emit('game:start', data)
res.send(200, data)
} catch (e) {
res.send(500, e)
}
})
// join a game
app.get('/api/game', async (req, res) => {
try {
const id = req.query.id
if (!id) res.send(400, ('Request must include a game id'))
const game = await getGame(id)
const players = await getPlayers(id)
const data = { game, players }
res.json(200, data)
} catch (e) {
res.send(500, e)
}
})
// initialize a game
app.post('/api/game', async (req, res) => {
try {
const opts = req.body
const data = await initGame(opts)
const gameId = data.game.uuid
const namespace = '/' + gameId
ns[gameId] = io.of(namespace)
res.send(200, data)
ns[gameId].on('connection', () => {
console.log('a user connected to ' + namespace)
})
} catch (e) {
res.send(400, e)
}
})
app.get('/api/player', async (req, res) => {
const gameId = req.query.id
try {
const data = await getPlayers(gameId)
res.send(200, data)
} catch (e) {
res.send(400, e)
}
})
app.post('/api/player', async (req, res) => {
try {
if (!req.body || !req.body.gameId) {
res.send(400, 'You must pass a gameId')
}
const { gameId, params } = req.body
const player = await createPlayer(gameId, params)
res.send(200, player)
ns[gameId].emit('player:add', player)
} catch (e) {
res.send(400, e)
}
})
app.patch('/api/player', async (req, res) => {
try {
if (!req.body || !req.body.id) {
return res.send(400, 'You must pass the id of the player to update')
}
if (!req.body.params) {
return res.send(400, 'You must pass a params object with the params to update')
}
const player = await updatePlayer(req.body.id, req.body.params)
const gameId = player.game_id
ns[gameId].emit('player:update', player)
if (!player.active){
const sheets = await getSheets(gameId)
if (sheets.length) ns[gameId].emit('sheet:pass', sheets)
}
res.send(200, player)
} catch (e) {
res.send(500, e)
}
})
app.get('/api/line/last', async (req, res) => {
try {
if (!req.query || !req.query.sheetId) {
return res.send(400, 'You must pass a sheetId')
}
const line = await getLastLine(req.query.sheetId)
return res.send(200, line)
} catch (e) {
res.send(500, e)
}
})
app.post('/api/line', async (req, res) => {
try {
if (!req.body) {
res.send(400, 'Request body missing')
}
const { sheetId, gameId, playerId, text } = req.body
if (!sheetId || !gameId || !playerId || !text) {
res.send(400,
'You must include sheetId, gameId, playerId, and text in the request body')
}
const line = await addLine({ sheetId, playerId, text })
const sheets = await getSheets(gameId)
// check if game is complete...
const gameIsComplete = sheets.every(sheet => {
return !sheet.active_player_id
})
if (gameIsComplete) {
markGameComplete(gameId)
ns[gameId].emit('game:complete', sheets)
res.send(200, line)
return
}
ns[gameId].emit('sheet:pass', sheets)
res.send(200, line)
} catch (e) {
res.send(500, e)
}
})
app.get('/api/line', async (req, res) => {
const sheetId = req.query.id
try {
const data = await getLines(sheetId)
res.send(200, data)
} catch (e) {
res.send(400, e)
}
})
app.get('/api/sheet/full', async (req, res) => {
const gameId = req.query.gameId
try {
const data = await getFullSheets(gameId)
res.send(200, data)
} catch (e) {
res.send(400, e)
}
})
// for re-joining an in-progress game
app.get('/api/sheet', async (req, res) => {
const gameId = req.query.gameId
// if game
try {
const data = await getSheets(gameId)
res.send(200, data)
} catch (e) {
res.send(400, e)
}
})
app.get('/', function (req, res) {
// res.sendFile(resolve(__dirname, 'client', 'index.html'));
});
http.listen(process.env.PORT || 3000, function () {
console.log(`listening on ${process.env.PORT}`);
});