diff --git a/.gitignore b/.gitignore index dd1ffe6..a805f8d 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /server/node_modules +/server/config.js diff --git a/game/index.html b/game/index.html deleted file mode 100644 index add5b41..0000000 --- a/game/index.html +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/game/index.js b/game/index.js index d907d41..811b9bd 100644 --- a/game/index.js +++ b/game/index.js @@ -7,9 +7,10 @@ import Camera from './Camera.js' import Point from './Point.js' import EventAdapter from './EventAdapter.js' import { choice } from './util.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 // final resized version of the puzzle image const TARGET_TILES = 1000 // desired number of tiles @@ -48,11 +49,11 @@ function fillBitmapCapped(bitmap, rgba, rect_cap) { if (!rect_cap) { return fillBitmap(bitmap, rgba) } - let startX = Math.floor(Math.max(rect_cap.x0)) - let startY = Math.floor(Math.max(rect_cap.y0)) + let startX = Math.floor(rect_cap.x0) + let startY = Math.floor(rect_cap.y0) - let endX = Math.ceil(Math.min(rect_cap.x1)) - let endY = Math.ceil(Math.min(rect_cap.y1)) + let endX = Math.ceil(rect_cap.x1) + let endY = Math.ceil(rect_cap.y1) for (let x = startX; x < endX; x++) { for (let y = startY; y < endY; y++) { @@ -550,7 +551,7 @@ function initme() { } function setupNetwork(me) { - const wsc = new WsClient('ws://localhost:1338/ws', me) + const wsc = new WsClient(WS_ADDRESS, me) wsc.connect() return wsc } @@ -566,6 +567,7 @@ async function main () { conn.onSocket('message', async ({data}) => { const d = JSON.parse(data) let puzzle + console.log(d) if (d.type === 'init') { if (d.puzzle) { puzzle = d.puzzle @@ -574,6 +576,7 @@ async function main () { // The game doesnt exist yet on the server, so load puzzle // and then give the server some info about the puzzle // Load puzzle and determine information about it + console.log(puzzle) puzzle = await loadPuzzle(TARGET_TILES, IMAGE_URL) conn.send(JSON.stringify({ type: 'init_puzzle', diff --git a/scripts/install b/scripts/install index 7b001d5..c2a2f09 100755 --- a/scripts/install +++ b/scripts/install @@ -3,3 +3,7 @@ cd "$RUN_DIR/server" npm install + +if [ ! -e "$RUN_DIR/server/config.js" ]; then + cp "$RUN_DIR/server/config.example.js" "$RUN_DIR/server/config.js" +fi diff --git a/server/config.example.js b/server/config.example.js new file mode 100644 index 0000000..4b008aa --- /dev/null +++ b/server/config.example.js @@ -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`, + }, +} diff --git a/server/index.js b/server/index.js index 8f754bf..98a3812 100644 --- a/server/index.js +++ b/server/index.js @@ -2,15 +2,29 @@ import WebSocketServer from './WebSocketServer.js' import express from 'express' -const httpConfig = { - hostname: 'localhost', - port: 1337, -} -const port = httpConfig.port -const hostname = httpConfig.hostname +import config from './config.js' + +const port = config.http.port +const hostname = config.http.hostname const app = express() -app.use('/', express.static('./../game/')) -app.listen(port, hostname, () => console.log(`server running on ${hostname}:${port}`)) +const statics = express.static('./../game/') +app.use('/', (req, res, next) => { + if (req.path === '/') { + res.send(` + + + + + + `) + } else { + statics(req, res, next) + } +}) +app.listen(port, hostname, () => console.log(`server running on http://${hostname}:${port}`)) const players = { @@ -18,13 +32,7 @@ const players = { } const games = {} - -const wssConfig = { - hostname: 'localhost', - port: 1338, - connectstring: `ws://localhost:1338/ws`, -} -const wss = new WebSocketServer(wssConfig); +const wss = new WebSocketServer(config.ws); const notify = (data) => {