server config

This commit is contained in:
Zutatensuppe 2020-11-07 12:21:38 +01:00
parent 9c1f7b9b2f
commit 2cdbdd484f
6 changed files with 50 additions and 32 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
/server/node_modules /server/node_modules
/server/config.js

View file

@ -1,11 +0,0 @@
<html>
<head>
<style>
html,body {margin: 0; overflow: hidden;}
html, body, #main { background: #222 }
</style>
</head>
<body>
<script src="index.js" type="module"></script>
</body>
</html>

View file

@ -7,9 +7,10 @@ import Camera from './Camera.js'
import Point from './Point.js' import Point from './Point.js'
import EventAdapter from './EventAdapter.js' import EventAdapter from './EventAdapter.js'
import { choice } from './util.js' import { choice } from './util.js'
import WsClient from './WsClient.js' import WsClient from './WsClient.js'
if (!WS_ADDRESS) throw '[ WS_ADDRESS not set ]'
const TILE_SIZE = 64 // cut size of each puzzle tile in the const TILE_SIZE = 64 // cut size of each puzzle tile in the
// final resized version of the puzzle image // final resized version of the puzzle image
const TARGET_TILES = 1000 // desired number of tiles const TARGET_TILES = 1000 // desired number of tiles
@ -48,11 +49,11 @@ function fillBitmapCapped(bitmap, rgba, rect_cap) {
if (!rect_cap) { if (!rect_cap) {
return fillBitmap(bitmap, rgba) return fillBitmap(bitmap, rgba)
} }
let startX = Math.floor(Math.max(rect_cap.x0)) let startX = Math.floor(rect_cap.x0)
let startY = Math.floor(Math.max(rect_cap.y0)) let startY = Math.floor(rect_cap.y0)
let endX = Math.ceil(Math.min(rect_cap.x1)) let endX = Math.ceil(rect_cap.x1)
let endY = Math.ceil(Math.min(rect_cap.y1)) let endY = Math.ceil(rect_cap.y1)
for (let x = startX; x < endX; x++) { for (let x = startX; x < endX; x++) {
for (let y = startY; y < endY; y++) { for (let y = startY; y < endY; y++) {
@ -550,7 +551,7 @@ function initme() {
} }
function setupNetwork(me) { function setupNetwork(me) {
const wsc = new WsClient('ws://localhost:1338/ws', me) const wsc = new WsClient(WS_ADDRESS, me)
wsc.connect() wsc.connect()
return wsc return wsc
} }
@ -566,6 +567,7 @@ async function main () {
conn.onSocket('message', async ({data}) => { conn.onSocket('message', async ({data}) => {
const d = JSON.parse(data) const d = JSON.parse(data)
let puzzle let puzzle
console.log(d)
if (d.type === 'init') { if (d.type === 'init') {
if (d.puzzle) { if (d.puzzle) {
puzzle = d.puzzle puzzle = d.puzzle
@ -574,6 +576,7 @@ async function main () {
// The game doesnt exist yet on the server, so load puzzle // The game doesnt exist yet on the server, so load puzzle
// and then give the server some info about the puzzle // and then give the server some info about the puzzle
// Load puzzle and determine information about it // Load puzzle and determine information about it
console.log(puzzle)
puzzle = await loadPuzzle(TARGET_TILES, IMAGE_URL) puzzle = await loadPuzzle(TARGET_TILES, IMAGE_URL)
conn.send(JSON.stringify({ conn.send(JSON.stringify({
type: 'init_puzzle', type: 'init_puzzle',

View file

@ -3,3 +3,7 @@
cd "$RUN_DIR/server" cd "$RUN_DIR/server"
npm install npm install
if [ ! -e "$RUN_DIR/server/config.js" ]; then
cp "$RUN_DIR/server/config.example.js" "$RUN_DIR/server/config.js"
fi

13
server/config.example.js Normal file
View file

@ -0,0 +1,13 @@
const hostname = '127.0.0.1'
export default {
http: {
hostname: hostname,
port: 1337,
},
ws: {
hostname: hostname,
port: 1338,
connectstring: `ws://localhost:1338/ws`,
},
}

View file

@ -2,15 +2,29 @@ import WebSocketServer from './WebSocketServer.js'
import express from 'express' import express from 'express'
const httpConfig = { import config from './config.js'
hostname: 'localhost',
port: 1337, const port = config.http.port
} const hostname = config.http.hostname
const port = httpConfig.port
const hostname = httpConfig.hostname
const app = express() const app = express()
app.use('/', express.static('./../game/')) const statics = express.static('./../game/')
app.listen(port, hostname, () => console.log(`server running on ${hostname}:${port}`)) app.use('/', (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>
<script>window.WS_ADDRESS = '${config.ws.connectstring}'</script>
<script src="index.js" type="module"></script>
</body>
</html>
`)
} else {
statics(req, res, next)
}
})
app.listen(port, hostname, () => console.log(`server running on http://${hostname}:${port}`))
const players = { const players = {
@ -18,13 +32,7 @@ const players = {
} }
const games = {} const games = {}
const wss = new WebSocketServer(config.ws);
const wssConfig = {
hostname: 'localhost',
port: 1338,
connectstring: `ws://localhost:1338/ws`,
}
const wss = new WebSocketServer(wssConfig);
const notify = (data) => { const notify = (data) => {