Fixed server, dockerfile draft

This commit is contained in:
Thomas
2020-11-02 15:15:48 +01:00
parent c1764a7d89
commit 41676a09bc
8 changed files with 68 additions and 35 deletions

View File

@@ -1,6 +1,19 @@
FROM alpine:edge
MAINTAINER gltron
RUN apk --update add openjdk11-jre
COPY tronio.jar /home/tronio.jar
CMD ["java","-jar","/home/tronio.jar"]
EXPOSE 8181
# https://medium.com/bb-tutorials-and-thoughts/packaging-your-vue-js-app-with-nodejs-backend-for-production-83abe213532c
FROM node:10 AS ui-build
WORKDIR /usr/src/app
COPY client/ ./client/
RUN cd client && npm install && npm run build
FROM node:10 AS server-build
WORKDIR /root/
COPY --from=ui-build /usr/src/app/client/dist ./server/dist
COPY server/package*.json ./server/
RUN cd server && npm install
COPY server/src ./server/
EXPOSE 3000
CMD ["npm", "start"]

View File

@@ -1,6 +1,6 @@
import { ToastProgrammatic as Toast } from 'buefy'
const connection = new WebSocket('ws://localhost:8181/socket')
const connection = new WebSocket('ws://localhost:3000/socket')
// const connection = new WebSocket('wss://tronio.gltronic.ovh/socket')
export default function createSocketPlugin () {

View File

@@ -1860,6 +1860,11 @@
"resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz",
"integrity": "sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM="
},
"uuid": {
"version": "8.3.1",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.1.tgz",
"integrity": "sha512-FOmRr+FmWEIG8uhZv6C2bTgEVXsHk08kE7mPlrBbEe+c3r9pjceVPgupIfNIhc4yx55H69OXANrUaSuu9eInKg=="
},
"v8-compile-cache": {
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/v8-compile-cache/-/v8-compile-cache-2.2.0.tgz",

View File

@@ -15,6 +15,7 @@
"license": "ISC",
"dependencies": {
"express": "^4.17.1",
"uuid": "^8.3.1",
"ws": "^7.3.1"
},
"devDependencies": {

View File

@@ -1,39 +1,51 @@
const { v4: uuidv4 } = require('uuid')
const Player = require('./models/player')
const gameSettings = require('./models/gameSettings')
const players = new Map()
const sockets = new Map()
const players = {}
const sockets = {}
let updateInterval = -1
let lastUpdateTime = Date.now()
let doUpdate = true
function login (connection, name) {
sockets[connection.id] = connection
players[connection.id] = new Player(connection.id, name)
const id = uuidv4()
connection.id = id
sockets[id] = connection
players[id] = new Player(id, name)
connection.send({
connection.send(JSON.stringify({
type: 'login',
player: players[connection.id]
})
player: players[id]
}))
connection.send({
connection.send(JSON.stringify({
type: 'gameSettings',
gameSettings: gameSettings
})
}))
if (updateInterval === -1) updateInterval = setInterval(() => step(), 1000)
}
function logout (connection) {
sockets.delete(connection.id)
players.delete(connection.id)
delete sockets[connection.id]
delete players[connection.id]
if (Object.keys(players).length === 0) {
clearInterval(updateInterval)
updateInterval = -1
}
}
function respawn (connection) {
players[connection.id].reset()
connection.send({
connection.send(JSON.stringify({
type: 'gamePlayerSpawn',
player: players[connection.id]
})
}))
}
function update (connection, player) {
@@ -42,10 +54,10 @@ function update (connection, player) {
function kill (player) {
player.kill()
sockets[player.id].send({
sockets[player.id].send(JSON.stringify({
type: 'gamePlayerDead',
player: player
})
}))
}
function step () {
@@ -54,22 +66,24 @@ function step () {
const tickToSimulate = (durationSinceLastUpdate * 120) / 1000
lastUpdateTime = currentTime
players.forEach((player, id) => {
// console.log('UPDATE ' + currentTime + ' doUpdate ' + doUpdate)
Object.values(players).forEach((player) => {
if (player.isOutOfBorders()) kill(player)
players.forEach((player2, id2) => {
Object.values(players).forEach((player2) => {
if (player.state === 'DEAD') return
for (let i = 0; i < player2.walls.length - 2; i++) {
// Prevent self destroy on last wall
if (id === id2 && i >= player2.walls.length - 1) break
if (player === player2 && i >= player2.walls.length - 1) break
const wallA = player2.walls[i]
const wallB = player2.walls[i + 1]
if (player.isCloseToWall(wallA, wallB)) {
if (player.isCrossingLine(wallA, wallB)) {
if (id !== id2) player2.score += 300
if (player !== player2) player2.score += 300
kill(player)
return
}
@@ -78,7 +92,7 @@ function step () {
})
})
players.forEach((player, id) => {
Object.values(players).forEach((player) => {
player.step(tickToSimulate)
})
@@ -89,16 +103,16 @@ function step () {
function broadcastUpdate () {
const update = {
type: 'update',
players: players,
players: Object.values(players),
time: lastUpdateTime
}
sockets.forEach((connection, id) => {
connection.send(update)
console.log('Broadcast ' + JSON.stringify(update))
Object.values(sockets).forEach((connection) => {
connection.send(JSON.stringify(update))
})
}
setInterval(() => step(), 1000 / 60)
module.exports = {
login,
logout,

View File

@@ -71,4 +71,4 @@ class Player {
}
}
module.exports = { Player }
module.exports = Player

View File

@@ -5,4 +5,4 @@ class Wall {
}
}
module.exports = { Wall }
module.exports = Wall

View File

@@ -15,7 +15,7 @@ const server = app.listen(port, () => {
const wss = new WebSocketServer({ server })
wss.on('connection', function (connection) {
connection.on('close', game.logout(connection))
connection.on('close', () => game.logout(connection))
connection.on('message', (message) => {
let data