game stuff
This commit is contained in:
parent
07da40dce7
commit
e75cd7406f
20 changed files with 1527 additions and 387 deletions
125
server/index.js
125
server/index.js
|
|
@ -1,58 +1,91 @@
|
|||
import WebSocketServer from './WebSocketServer.js'
|
||||
|
||||
import fs from 'fs'
|
||||
import express from 'express'
|
||||
import multer from 'multer'
|
||||
import config from './config.js'
|
||||
import Protocol from './../common/Protocol.js'
|
||||
import Util from './../common/Util.js'
|
||||
import Game from './Game.js'
|
||||
import twing from 'twing'
|
||||
import bodyParser from 'body-parser'
|
||||
|
||||
// desired number of tiles
|
||||
// actual calculated number can be higher
|
||||
const TARGET_TILES = 1000
|
||||
|
||||
const IMAGES = [
|
||||
'/example-images/ima_86ec3fa.jpeg',
|
||||
'/example-images/bleu.png',
|
||||
'/example-images/saechsische_schweiz.jpg',
|
||||
'/example-images/132-2048x1365.jpg',
|
||||
const allImages = () => [
|
||||
...fs.readdirSync('./../game/example-images/').map(f => ({
|
||||
file: `./../game/example-images/${f}`,
|
||||
url: `/example-images/${f}`,
|
||||
})),
|
||||
...fs.readdirSync('./../data/uploads/').map(f => ({
|
||||
file: `./../data/uploads/${f}`,
|
||||
url: `/uploads/${f}`,
|
||||
})),
|
||||
]
|
||||
|
||||
const games = {}
|
||||
|
||||
const port = config.http.port
|
||||
const hostname = config.http.hostname
|
||||
const app = express()
|
||||
|
||||
const uploadDir = './../data/uploads'
|
||||
const storage = multer.diskStorage({
|
||||
destination: uploadDir,
|
||||
filename: function (req, file, cb) {
|
||||
cb(null , file.originalname);
|
||||
}
|
||||
})
|
||||
const upload = multer({storage}).single('file');
|
||||
|
||||
const statics = express.static('./../game/')
|
||||
app.use('/g/:gid', (req, res, next) => {
|
||||
res.send(`
|
||||
<html><head><style>
|
||||
html,body {margin: 0; overflow: hidden;}
|
||||
html, body, #main { background: #222 }
|
||||
canvas {cursor: none;}
|
||||
</style></head><body>
|
||||
<script>window.GAME_ID = '${req.params.gid}'</script>
|
||||
<script>window.WS_ADDRESS = '${config.ws.connectstring}'</script>
|
||||
<script src="/index.js" type="module"></script>
|
||||
</body>
|
||||
</html>
|
||||
`)
|
||||
|
||||
const render = async (template, data) => {
|
||||
const loader = new twing.TwingLoaderFilesystem('./../game/templates')
|
||||
const env = new twing.TwingEnvironment(loader)
|
||||
return env.render(template, data)
|
||||
}
|
||||
|
||||
app.use('/g/:gid', async (req, res, next) => {
|
||||
res.send(await render('game.html.twig', {
|
||||
GAME_ID: req.params.gid,
|
||||
WS_ADDRESS: config.ws.connectstring,
|
||||
}))
|
||||
})
|
||||
app.post('/upload', (req, res) => {
|
||||
upload(req, res, (err) => {
|
||||
if (err) {
|
||||
console.log(err)
|
||||
res.status(400).send("Something went wrong!");
|
||||
}
|
||||
res.send({
|
||||
image: {
|
||||
file: './../data/uploads/' + req.file.filename,
|
||||
url: '/uploads/' + req.file.filename,
|
||||
},
|
||||
})
|
||||
})
|
||||
})
|
||||
app.post('/newgame', bodyParser.json(), async (req, res) => {
|
||||
console.log(req.body.tiles, req.body.image)
|
||||
const gameId = Util.uniqId()
|
||||
if (!Game.exists(gameId)) {
|
||||
await Game.createGame(gameId, req.body.tiles, req.body.image)
|
||||
}
|
||||
res.send({ url: `/g/${gameId}` })
|
||||
})
|
||||
|
||||
app.use('/common/', express.static('./../common/'))
|
||||
app.use('/', (req, res, next) => {
|
||||
app.use('/uploads/', express.static('./../data/uploads/'))
|
||||
app.use('/', async (req, res, next) => {
|
||||
if (req.path === '/') {
|
||||
res.send(`
|
||||
<html><head><style>
|
||||
html,body {margin: 0; overflow: hidden;}
|
||||
html, body, #main { background: #222 }
|
||||
</style></head><body>
|
||||
<a href="/g/${Util.uniqId()}">New game :P</a>
|
||||
${Object.keys(games).map(k => {
|
||||
return `<a href="/g/${k}">Game ${k}</a>`
|
||||
})}
|
||||
</body>
|
||||
</html>
|
||||
`)
|
||||
const games = [
|
||||
...Object.keys(Game.getAllGames()).map(id => ({
|
||||
id: id,
|
||||
title: id,
|
||||
})),
|
||||
]
|
||||
|
||||
res.send(await render('index.html.twig', {
|
||||
games,
|
||||
images: allImages(),
|
||||
}))
|
||||
} else {
|
||||
statics(req, res, next)
|
||||
}
|
||||
|
|
@ -67,6 +100,15 @@ const notify = (data, sockets) => {
|
|||
}
|
||||
}
|
||||
|
||||
wss.on('close', async ({socket}) => {
|
||||
const proto = socket.protocol.split('|')
|
||||
const clientId = proto[0]
|
||||
const gameId = proto[1]
|
||||
if (Game.exists(gameId)) {
|
||||
Game.removeSocket(gameId, socket)
|
||||
}
|
||||
})
|
||||
|
||||
wss.on('message', async ({socket, data}) => {
|
||||
try {
|
||||
const proto = socket.protocol.split('|')
|
||||
|
|
@ -76,13 +118,12 @@ wss.on('message', async ({socket, data}) => {
|
|||
const msgType = msg[0]
|
||||
switch (msgType) {
|
||||
case Protocol.EV_CLIENT_INIT: {
|
||||
const name = msg[1]
|
||||
if (!Game.exists(gameId)) {
|
||||
await Game.createGame(gameId, TARGET_TILES, Util.choice(IMAGES))
|
||||
throw `[game ${gameId} does not exist... ]`
|
||||
}
|
||||
Game.addPlayer(gameId, clientId, name)
|
||||
Game.addPlayer(gameId, clientId)
|
||||
Game.addSocket(gameId, socket)
|
||||
|
||||
Game.store(gameId)
|
||||
notify(
|
||||
[Protocol.EV_SERVER_INIT, Game.get(gameId)],
|
||||
[socket]
|
||||
|
|
@ -93,6 +134,7 @@ wss.on('message', async ({socket, data}) => {
|
|||
const clientSeq = msg[1]
|
||||
const clientEvtData = msg[2]
|
||||
const changes = Game.handleInput(gameId, clientId, clientEvtData)
|
||||
Game.store(gameId)
|
||||
notify(
|
||||
[Protocol.EV_SERVER_EVENT, clientId, clientSeq, changes],
|
||||
Game.getSockets(gameId)
|
||||
|
|
@ -104,5 +146,6 @@ wss.on('message', async ({socket, data}) => {
|
|||
}
|
||||
})
|
||||
|
||||
Game.loadAllGames()
|
||||
app.listen(port, hostname, () => console.log(`server running on http://${hostname}:${port}`))
|
||||
wss.listen()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue