diff --git a/README.txt b/README.md similarity index 84% rename from README.txt rename to README.md index 1f12615..97f9d3e 100644 --- a/README.txt +++ b/README.md @@ -9,14 +9,10 @@ Any movement, including initial locations, is represented by pushing or popping A victory state can be stored by taking a snapshot of the current stack. -## Credit +## Credits Icons from [https://game-icons.net](https://game-icons.net) ## TODO -- chat box - walls and winstate algorithm - tutorial - -# Final install -- move websocket server to /core -- dynamic socket server resolution +- restore name prompt1 diff --git a/assets/avatar.png b/assets/avatar.png new file mode 100644 index 0000000..b4a67c8 Binary files /dev/null and b/assets/avatar.png differ diff --git a/assets/gas-giant.png b/assets/gas-giant.png deleted file mode 100644 index f6a4551..0000000 Binary files a/assets/gas-giant.png and /dev/null differ diff --git a/client/connection.js b/client/connection.js index ed96543..2b62829 100644 --- a/client/connection.js +++ b/client/connection.js @@ -30,7 +30,8 @@ const Connection = function() { }; Connection.prototype.connect = function() { - this.ws = new WebSocket('ws://localhost:8080/ricochet?name=' + this.name); + const hostname = window.location.hostname; + this.ws = new WebSocket(`ws://${hostname}:8080/ricochet?name=${this.name}`); this.ws.addEventListener('open', this.socketListeners.open); this.ws.addEventListener('error', this.socketListeners.error); diff --git a/server/ricochet.js b/server/ricochet.js index 48220c3..e70685d 100644 --- a/server/ricochet.js +++ b/server/ricochet.js @@ -189,7 +189,7 @@ Ricochet.prototype.freshWalls = function() { Ricochet.prototype.freshObjective = function() { const rand = Math.floor(Math.random() * this.robotIds.length); - const id = this.robotIds[0]; //this.robotIds[rand]; + const id = this.robotIds[rand]; const { i, j } = this.randomUnoccupiedSquare(); return { diff --git a/socket/messenger.js b/socket/messenger.js deleted file mode 100644 index a1fa392..0000000 --- a/socket/messenger.js +++ /dev/null @@ -1,48 +0,0 @@ -const WebSocket = require('ws'); -const DEBUG = (process.env.NODE_ENV !== "production"); - -const Messenger = function() { - this.clients = {}; -}; - -Messenger.prototype.messageOne = function(ws, message) { - DEBUG && console.log(`Sending to only ${ws.id}:`); - DEBUG && console.log(message); - - ws.send(JSON.stringify(message)); -}; - -Messenger.prototype.messageOthers = function(ws, message) { - DEBUG && console.log(`Sending to other client(s):`); - DEBUG && console.log(message); - - Object.values(this.clients).forEach((client) => { - if (client !== ws && client.readyState === WebSocket.OPEN) { - client.send(JSON.stringify(message)); - } - }); -}; - -Messenger.prototype.messageAll = function(message) { - const clients = Object.values(this.clients); - - DEBUG && console.log(`Sending to all ${clients.length} client(s):`); - DEBUG && console.log(message); - - clients.forEach((client) => { - if (client.readyState === WebSocket.OPEN) { - client.send(JSON.stringify(message)); - } - }); -}; - -Messenger.prototype.subscribe = function(ws) { - this.clients[ws.id] = ws; -}; - -Messenger.prototype.unsubscribe = function(ws) { - delete this.clients[ws.id]; -}; - - -module.exports = Messenger; \ No newline at end of file diff --git a/socket/server.js b/socket/server.js deleted file mode 100644 index 726fdbf..0000000 --- a/socket/server.js +++ /dev/null @@ -1,52 +0,0 @@ -const WebSocket = require('ws'); -const UrlParser = require('url'); -const uuid = require('node-uuid'); -const Messenger = require('./messenger'); - -const RicochetApp = require(`${__dirname}/../server/ricochet.js`); - -const apps = { - '/ricochet': new RicochetApp({ messenger: new Messenger() }) -}; - -// Make sure all apps follow the messaging contract. -Object.keys(apps).forEach((key) => { - const app = apps[key]; - - const valid = app && app.messenger && app.onConnect && app.onMessage && app.onDisconnect; - - if (!valid) { - delete apps[key]; - console.log(`==================== \n\nCRITICAL ERROR! \n\nApplication at ${key} is missing one or more: messenger, onConnect, onMessage, onDisconnect.\n\n====================\n`); - } -}); - -const onConnect = (ws, req) => { - // Store an ID on the socket connection. - ws.id = uuid.v4(); - - const url = UrlParser.parse(req.url, true); - const app = apps[url.pathname]; - - if (app) { - app.messenger.subscribe(ws); - - app.onConnect(ws, req); - - ws.on('message', (rawBody) => { - app.onMessage(ws, rawBody); - }); - - ws.on('close', () => { - app.onDisconnect(ws); - app.messenger.unsubscribe(ws); - }); - } -}; - -//===== Generic socket server for any application instantiated above. -const wss = new WebSocket.Server({ port: 8080 }); -wss.on('connection', onConnect); - -console.log("Websocket server listening on :8080"); -