-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
79 lines (68 loc) · 2.62 KB
/
server.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
const fs = require('fs')
const cors = require('cors')
const http = require('http')
const path = require('path')
const helmet = require('helmet')
const WebSocket = require('ws')
const express = require('express')
const compression = require('compression')
const { database } = require('./database')
require('dotenv').config({ path: '.env.local' })
const MODEL_LOCATION = 'AlphaZeroAgent/trained_model'
const app = express()
const server = http.createServer(app)
const websocket_server = new WebSocket.Server({ server, path: process.env.VITE_WEBSOCKET_PATH })
app.use(compression())
app.use(express.json())
app.use(express.static(path.join(__dirname, 'build')))
app.use(helmet({
contentSecurityPolicy: {
directives: {
'defaultSrc': ["'self'"],
'script-src': ["'self'", 'cdn.skypack.dev'],
'style-src': ["'self'", 'fonts.googleapis.com'],
'font-src': ["'self'", 'fonts.gstatic.com'],
'worker-src': ["'self'", 'cdn.skypack.dev'],
}
}
}))
app.post('/save-game', (req, res) => {
const { gameid, game } = req.body
database.set(gameid, game).then(() => res.status(200).send({ result: { success: true } }))
})
app.post('/fetch-game', (req, res) => {
const { gameid } = req.body
database.get(gameid).then(value => res.status(200).send({ result: value }))
})
var active_clients = {}
websocket_server.on('connection', ws => {
ws.on('message', message => {
message = JSON.parse(message)
let { register: reg_id, name, gameid, context } = message
if (reg_id) {
if (!(reg_id in active_clients))
active_clients[reg_id] = []
active_clients[reg_id] = active_clients[reg_id].filter(socket => socket.readyState === WebSocket.OPEN)
active_clients[reg_id].push(ws)
active_clients[reg_id].forEach(client => {
if (client !== ws && client.readyState === WebSocket.OPEN)
client.send(JSON.stringify({ name }))
})
} else if (context) {
let { player, ...game } = context
database.set(gameid, game)
if (gameid in active_clients) {
active_clients[gameid].forEach(client => {
if (client !== ws && client.readyState === WebSocket.OPEN)
client.send(JSON.stringify(context))
})
}
}
})
})
app.get('/check-saved-model', (_, res) => res.status(200).send({ found: fs.existsSync(`./${MODEL_LOCATION}/model.json`) }))
app.get('/model/:file', cors(), (req, res) => res.sendFile(path.join(__dirname, MODEL_LOCATION, req.params.file)))
app.get('*', (_, res) => res.sendFile(path.join(__dirname, 'build', 'index.html')))
server.listen(process.env.PORT, () => {
console.log(`Application is running at ${process.env.PORT}`)
})