diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7f37482 --- /dev/null +++ b/.gitignore @@ -0,0 +1,108 @@ +*.sw* +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +lerna-debug.log* + +# Diagnostic reports (https://nodejs.org/api/report.html) +report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage +*.lcov + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ +jspm_packages/ + +# TypeScript v1 declaration files +typings/ + +# TypeScript cache +*.tsbuildinfo + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Microbundle cache +.rpt2_cache/ +.rts2_cache_cjs/ +.rts2_cache_es/ +.rts2_cache_umd/ + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# dotenv environment variables file +.env +.env.test + +# parcel-bundler cache (https://parceljs.org/) +.cache + +# Next.js build output +.next + +# Nuxt.js build / generate output +.nuxt +dist + +# Gatsby files +.cache/ +# Comment in the public line in if your project uses Gatsby and not Next.js +# https://nextjs.org/blog/next-9-1#public-directory-support +# public + +# vuepress build output +.vuepress/dist + +# Serverless directories +.serverless/ + +# FuseBox cache +.fusebox/ + +# DynamoDB Local files +.dynamodb/ + +# TernJS port file +.tern-port + +# Stores VSCode versions used for testing VSCode extensions +.vscode-test diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7f37482 --- /dev/null +++ b/.gitignore @@ -0,0 +1,108 @@ +*.sw* +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +lerna-debug.log* + +# Diagnostic reports (https://nodejs.org/api/report.html) +report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage +*.lcov + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ +jspm_packages/ + +# TypeScript v1 declaration files +typings/ + +# TypeScript cache +*.tsbuildinfo + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Microbundle cache +.rpt2_cache/ +.rts2_cache_cjs/ +.rts2_cache_es/ +.rts2_cache_umd/ + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# dotenv environment variables file +.env +.env.test + +# parcel-bundler cache (https://parceljs.org/) +.cache + +# Next.js build output +.next + +# Nuxt.js build / generate output +.nuxt +dist + +# Gatsby files +.cache/ +# Comment in the public line in if your project uses Gatsby and not Next.js +# https://nextjs.org/blog/next-9-1#public-directory-support +# public + +# vuepress build output +.vuepress/dist + +# Serverless directories +.serverless/ + +# FuseBox cache +.fusebox/ + +# DynamoDB Local files +.dynamodb/ + +# TernJS port file +.tern-port + +# Stores VSCode versions used for testing VSCode extensions +.vscode-test diff --git a/index.js b/index.js new file mode 100644 index 0000000..e8220d4 --- /dev/null +++ b/index.js @@ -0,0 +1,57 @@ +const WebSocket = require('ws'); +const Player = require('./player'); +const Room = require('./room'); + +var lobby = new Room("Lobby"); +var playersBySocket = {}; + +const ws = new WebSocket.Server({port: 9997}); + +ws.on('listening', function() { + console.log('Listening!'); +}); + +ws.on('connection', function(conn) + { + console.log('Connection!'); + + conn.on('message', function(message) + { + if (playersBySocket[conn] !== undefined) + { + var p = playersBySocket[conn]; + if (p.room != null) + { + p.room.playerMessage(p, message); + } + } + console.log('Message: ' + message); + } + ); + conn.on('close', function(code, reason) + { + console.log('Socket closed: ' . reason); + if (playersBySocket[conn] !== undefined) + { + const p = playersBySocket[conn]; + delete playersBySocket[conn]; + + p.room.removePlayer(p); + } + } + ); + + var p = new Player(conn); + playersBySocket[conn] = p; + + p.sendMessage("connected", {player_id:p.id}); + + lobby.addPlayer(p); + } +); + +ws.on('error', function(err) + { + console.log('Error: ' + err); + } +); diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7f37482 --- /dev/null +++ b/.gitignore @@ -0,0 +1,108 @@ +*.sw* +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +lerna-debug.log* + +# Diagnostic reports (https://nodejs.org/api/report.html) +report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage +*.lcov + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ +jspm_packages/ + +# TypeScript v1 declaration files +typings/ + +# TypeScript cache +*.tsbuildinfo + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Microbundle cache +.rpt2_cache/ +.rts2_cache_cjs/ +.rts2_cache_es/ +.rts2_cache_umd/ + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# dotenv environment variables file +.env +.env.test + +# parcel-bundler cache (https://parceljs.org/) +.cache + +# Next.js build output +.next + +# Nuxt.js build / generate output +.nuxt +dist + +# Gatsby files +.cache/ +# Comment in the public line in if your project uses Gatsby and not Next.js +# https://nextjs.org/blog/next-9-1#public-directory-support +# public + +# vuepress build output +.vuepress/dist + +# Serverless directories +.serverless/ + +# FuseBox cache +.fusebox/ + +# DynamoDB Local files +.dynamodb/ + +# TernJS port file +.tern-port + +# Stores VSCode versions used for testing VSCode extensions +.vscode-test diff --git a/index.js b/index.js new file mode 100644 index 0000000..e8220d4 --- /dev/null +++ b/index.js @@ -0,0 +1,57 @@ +const WebSocket = require('ws'); +const Player = require('./player'); +const Room = require('./room'); + +var lobby = new Room("Lobby"); +var playersBySocket = {}; + +const ws = new WebSocket.Server({port: 9997}); + +ws.on('listening', function() { + console.log('Listening!'); +}); + +ws.on('connection', function(conn) + { + console.log('Connection!'); + + conn.on('message', function(message) + { + if (playersBySocket[conn] !== undefined) + { + var p = playersBySocket[conn]; + if (p.room != null) + { + p.room.playerMessage(p, message); + } + } + console.log('Message: ' + message); + } + ); + conn.on('close', function(code, reason) + { + console.log('Socket closed: ' . reason); + if (playersBySocket[conn] !== undefined) + { + const p = playersBySocket[conn]; + delete playersBySocket[conn]; + + p.room.removePlayer(p); + } + } + ); + + var p = new Player(conn); + playersBySocket[conn] = p; + + p.sendMessage("connected", {player_id:p.id}); + + lobby.addPlayer(p); + } +); + +ws.on('error', function(err) + { + console.log('Error: ' + err); + } +); diff --git a/item.js b/item.js new file mode 100644 index 0000000..6a4a64d --- /dev/null +++ b/item.js @@ -0,0 +1,18 @@ +class Item +{ + constructor(playerId, itemId) + { + this.playerId = playerId; + this.itemId = itemId; + this.key = playerId + "_" + itemId; + this.details = {}; + } + + getJSON() + { + return {...this.details, player_id:this.playerId, item_id:this.itemId, key:this.key}; + } +} + + +module.exports = Item; diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7f37482 --- /dev/null +++ b/.gitignore @@ -0,0 +1,108 @@ +*.sw* +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +lerna-debug.log* + +# Diagnostic reports (https://nodejs.org/api/report.html) +report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage +*.lcov + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ +jspm_packages/ + +# TypeScript v1 declaration files +typings/ + +# TypeScript cache +*.tsbuildinfo + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Microbundle cache +.rpt2_cache/ +.rts2_cache_cjs/ +.rts2_cache_es/ +.rts2_cache_umd/ + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# dotenv environment variables file +.env +.env.test + +# parcel-bundler cache (https://parceljs.org/) +.cache + +# Next.js build output +.next + +# Nuxt.js build / generate output +.nuxt +dist + +# Gatsby files +.cache/ +# Comment in the public line in if your project uses Gatsby and not Next.js +# https://nextjs.org/blog/next-9-1#public-directory-support +# public + +# vuepress build output +.vuepress/dist + +# Serverless directories +.serverless/ + +# FuseBox cache +.fusebox/ + +# DynamoDB Local files +.dynamodb/ + +# TernJS port file +.tern-port + +# Stores VSCode versions used for testing VSCode extensions +.vscode-test diff --git a/index.js b/index.js new file mode 100644 index 0000000..e8220d4 --- /dev/null +++ b/index.js @@ -0,0 +1,57 @@ +const WebSocket = require('ws'); +const Player = require('./player'); +const Room = require('./room'); + +var lobby = new Room("Lobby"); +var playersBySocket = {}; + +const ws = new WebSocket.Server({port: 9997}); + +ws.on('listening', function() { + console.log('Listening!'); +}); + +ws.on('connection', function(conn) + { + console.log('Connection!'); + + conn.on('message', function(message) + { + if (playersBySocket[conn] !== undefined) + { + var p = playersBySocket[conn]; + if (p.room != null) + { + p.room.playerMessage(p, message); + } + } + console.log('Message: ' + message); + } + ); + conn.on('close', function(code, reason) + { + console.log('Socket closed: ' . reason); + if (playersBySocket[conn] !== undefined) + { + const p = playersBySocket[conn]; + delete playersBySocket[conn]; + + p.room.removePlayer(p); + } + } + ); + + var p = new Player(conn); + playersBySocket[conn] = p; + + p.sendMessage("connected", {player_id:p.id}); + + lobby.addPlayer(p); + } +); + +ws.on('error', function(err) + { + console.log('Error: ' + err); + } +); diff --git a/item.js b/item.js new file mode 100644 index 0000000..6a4a64d --- /dev/null +++ b/item.js @@ -0,0 +1,18 @@ +class Item +{ + constructor(playerId, itemId) + { + this.playerId = playerId; + this.itemId = itemId; + this.key = playerId + "_" + itemId; + this.details = {}; + } + + getJSON() + { + return {...this.details, player_id:this.playerId, item_id:this.itemId, key:this.key}; + } +} + + +module.exports = Item; diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..a321d8f --- /dev/null +++ b/package-lock.json @@ -0,0 +1,13 @@ +{ + "name": "blackout_server", + "version": "0.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "ws": { + "version": "7.2.1", + "resolved": "https://registry.npmjs.org/ws/-/ws-7.2.1.tgz", + "integrity": "sha512-sucePNSafamSKoOqoNfBd8V0StlkzJKL2ZAhGQinCfNQ+oacw+Pk7lcdAElecBF2VkLNZRiIb5Oi1Q5lVUVt2A==" + } + } +} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7f37482 --- /dev/null +++ b/.gitignore @@ -0,0 +1,108 @@ +*.sw* +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +lerna-debug.log* + +# Diagnostic reports (https://nodejs.org/api/report.html) +report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage +*.lcov + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ +jspm_packages/ + +# TypeScript v1 declaration files +typings/ + +# TypeScript cache +*.tsbuildinfo + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Microbundle cache +.rpt2_cache/ +.rts2_cache_cjs/ +.rts2_cache_es/ +.rts2_cache_umd/ + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# dotenv environment variables file +.env +.env.test + +# parcel-bundler cache (https://parceljs.org/) +.cache + +# Next.js build output +.next + +# Nuxt.js build / generate output +.nuxt +dist + +# Gatsby files +.cache/ +# Comment in the public line in if your project uses Gatsby and not Next.js +# https://nextjs.org/blog/next-9-1#public-directory-support +# public + +# vuepress build output +.vuepress/dist + +# Serverless directories +.serverless/ + +# FuseBox cache +.fusebox/ + +# DynamoDB Local files +.dynamodb/ + +# TernJS port file +.tern-port + +# Stores VSCode versions used for testing VSCode extensions +.vscode-test diff --git a/index.js b/index.js new file mode 100644 index 0000000..e8220d4 --- /dev/null +++ b/index.js @@ -0,0 +1,57 @@ +const WebSocket = require('ws'); +const Player = require('./player'); +const Room = require('./room'); + +var lobby = new Room("Lobby"); +var playersBySocket = {}; + +const ws = new WebSocket.Server({port: 9997}); + +ws.on('listening', function() { + console.log('Listening!'); +}); + +ws.on('connection', function(conn) + { + console.log('Connection!'); + + conn.on('message', function(message) + { + if (playersBySocket[conn] !== undefined) + { + var p = playersBySocket[conn]; + if (p.room != null) + { + p.room.playerMessage(p, message); + } + } + console.log('Message: ' + message); + } + ); + conn.on('close', function(code, reason) + { + console.log('Socket closed: ' . reason); + if (playersBySocket[conn] !== undefined) + { + const p = playersBySocket[conn]; + delete playersBySocket[conn]; + + p.room.removePlayer(p); + } + } + ); + + var p = new Player(conn); + playersBySocket[conn] = p; + + p.sendMessage("connected", {player_id:p.id}); + + lobby.addPlayer(p); + } +); + +ws.on('error', function(err) + { + console.log('Error: ' + err); + } +); diff --git a/item.js b/item.js new file mode 100644 index 0000000..6a4a64d --- /dev/null +++ b/item.js @@ -0,0 +1,18 @@ +class Item +{ + constructor(playerId, itemId) + { + this.playerId = playerId; + this.itemId = itemId; + this.key = playerId + "_" + itemId; + this.details = {}; + } + + getJSON() + { + return {...this.details, player_id:this.playerId, item_id:this.itemId, key:this.key}; + } +} + + +module.exports = Item; diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..a321d8f --- /dev/null +++ b/package-lock.json @@ -0,0 +1,13 @@ +{ + "name": "blackout_server", + "version": "0.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "ws": { + "version": "7.2.1", + "resolved": "https://registry.npmjs.org/ws/-/ws-7.2.1.tgz", + "integrity": "sha512-sucePNSafamSKoOqoNfBd8V0StlkzJKL2ZAhGQinCfNQ+oacw+Pk7lcdAElecBF2VkLNZRiIb5Oi1Q5lVUVt2A==" + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..14e614e --- /dev/null +++ b/package.json @@ -0,0 +1,18 @@ +{ + "name": "blackout_server", + "version": "0.0.0", + "description": "Blackout websocket server", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "https://git.mhack.io/git/mark/BlackoutServerNode.git" + }, + "author": "", + "license": "ISC", + "dependencies": { + "ws": "^7.2.1" + } +} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7f37482 --- /dev/null +++ b/.gitignore @@ -0,0 +1,108 @@ +*.sw* +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +lerna-debug.log* + +# Diagnostic reports (https://nodejs.org/api/report.html) +report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage +*.lcov + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ +jspm_packages/ + +# TypeScript v1 declaration files +typings/ + +# TypeScript cache +*.tsbuildinfo + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Microbundle cache +.rpt2_cache/ +.rts2_cache_cjs/ +.rts2_cache_es/ +.rts2_cache_umd/ + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# dotenv environment variables file +.env +.env.test + +# parcel-bundler cache (https://parceljs.org/) +.cache + +# Next.js build output +.next + +# Nuxt.js build / generate output +.nuxt +dist + +# Gatsby files +.cache/ +# Comment in the public line in if your project uses Gatsby and not Next.js +# https://nextjs.org/blog/next-9-1#public-directory-support +# public + +# vuepress build output +.vuepress/dist + +# Serverless directories +.serverless/ + +# FuseBox cache +.fusebox/ + +# DynamoDB Local files +.dynamodb/ + +# TernJS port file +.tern-port + +# Stores VSCode versions used for testing VSCode extensions +.vscode-test diff --git a/index.js b/index.js new file mode 100644 index 0000000..e8220d4 --- /dev/null +++ b/index.js @@ -0,0 +1,57 @@ +const WebSocket = require('ws'); +const Player = require('./player'); +const Room = require('./room'); + +var lobby = new Room("Lobby"); +var playersBySocket = {}; + +const ws = new WebSocket.Server({port: 9997}); + +ws.on('listening', function() { + console.log('Listening!'); +}); + +ws.on('connection', function(conn) + { + console.log('Connection!'); + + conn.on('message', function(message) + { + if (playersBySocket[conn] !== undefined) + { + var p = playersBySocket[conn]; + if (p.room != null) + { + p.room.playerMessage(p, message); + } + } + console.log('Message: ' + message); + } + ); + conn.on('close', function(code, reason) + { + console.log('Socket closed: ' . reason); + if (playersBySocket[conn] !== undefined) + { + const p = playersBySocket[conn]; + delete playersBySocket[conn]; + + p.room.removePlayer(p); + } + } + ); + + var p = new Player(conn); + playersBySocket[conn] = p; + + p.sendMessage("connected", {player_id:p.id}); + + lobby.addPlayer(p); + } +); + +ws.on('error', function(err) + { + console.log('Error: ' + err); + } +); diff --git a/item.js b/item.js new file mode 100644 index 0000000..6a4a64d --- /dev/null +++ b/item.js @@ -0,0 +1,18 @@ +class Item +{ + constructor(playerId, itemId) + { + this.playerId = playerId; + this.itemId = itemId; + this.key = playerId + "_" + itemId; + this.details = {}; + } + + getJSON() + { + return {...this.details, player_id:this.playerId, item_id:this.itemId, key:this.key}; + } +} + + +module.exports = Item; diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..a321d8f --- /dev/null +++ b/package-lock.json @@ -0,0 +1,13 @@ +{ + "name": "blackout_server", + "version": "0.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "ws": { + "version": "7.2.1", + "resolved": "https://registry.npmjs.org/ws/-/ws-7.2.1.tgz", + "integrity": "sha512-sucePNSafamSKoOqoNfBd8V0StlkzJKL2ZAhGQinCfNQ+oacw+Pk7lcdAElecBF2VkLNZRiIb5Oi1Q5lVUVt2A==" + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..14e614e --- /dev/null +++ b/package.json @@ -0,0 +1,18 @@ +{ + "name": "blackout_server", + "version": "0.0.0", + "description": "Blackout websocket server", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "https://git.mhack.io/git/mark/BlackoutServerNode.git" + }, + "author": "", + "license": "ISC", + "dependencies": { + "ws": "^7.2.1" + } +} diff --git a/player.js b/player.js new file mode 100644 index 0000000..ebd6b9a --- /dev/null +++ b/player.js @@ -0,0 +1,16 @@ +var nextId = 0; +class Player +{ + constructor(socket) + { + this.socket = socket; + this.room = null; + this.id = nextId++; + } + sendMessage(type, details) + { + this.socket.send(JSON.stringify({...details, type:type})); + } +} + +module.exports = Player; diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7f37482 --- /dev/null +++ b/.gitignore @@ -0,0 +1,108 @@ +*.sw* +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +lerna-debug.log* + +# Diagnostic reports (https://nodejs.org/api/report.html) +report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage +*.lcov + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ +jspm_packages/ + +# TypeScript v1 declaration files +typings/ + +# TypeScript cache +*.tsbuildinfo + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Microbundle cache +.rpt2_cache/ +.rts2_cache_cjs/ +.rts2_cache_es/ +.rts2_cache_umd/ + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# dotenv environment variables file +.env +.env.test + +# parcel-bundler cache (https://parceljs.org/) +.cache + +# Next.js build output +.next + +# Nuxt.js build / generate output +.nuxt +dist + +# Gatsby files +.cache/ +# Comment in the public line in if your project uses Gatsby and not Next.js +# https://nextjs.org/blog/next-9-1#public-directory-support +# public + +# vuepress build output +.vuepress/dist + +# Serverless directories +.serverless/ + +# FuseBox cache +.fusebox/ + +# DynamoDB Local files +.dynamodb/ + +# TernJS port file +.tern-port + +# Stores VSCode versions used for testing VSCode extensions +.vscode-test diff --git a/index.js b/index.js new file mode 100644 index 0000000..e8220d4 --- /dev/null +++ b/index.js @@ -0,0 +1,57 @@ +const WebSocket = require('ws'); +const Player = require('./player'); +const Room = require('./room'); + +var lobby = new Room("Lobby"); +var playersBySocket = {}; + +const ws = new WebSocket.Server({port: 9997}); + +ws.on('listening', function() { + console.log('Listening!'); +}); + +ws.on('connection', function(conn) + { + console.log('Connection!'); + + conn.on('message', function(message) + { + if (playersBySocket[conn] !== undefined) + { + var p = playersBySocket[conn]; + if (p.room != null) + { + p.room.playerMessage(p, message); + } + } + console.log('Message: ' + message); + } + ); + conn.on('close', function(code, reason) + { + console.log('Socket closed: ' . reason); + if (playersBySocket[conn] !== undefined) + { + const p = playersBySocket[conn]; + delete playersBySocket[conn]; + + p.room.removePlayer(p); + } + } + ); + + var p = new Player(conn); + playersBySocket[conn] = p; + + p.sendMessage("connected", {player_id:p.id}); + + lobby.addPlayer(p); + } +); + +ws.on('error', function(err) + { + console.log('Error: ' + err); + } +); diff --git a/item.js b/item.js new file mode 100644 index 0000000..6a4a64d --- /dev/null +++ b/item.js @@ -0,0 +1,18 @@ +class Item +{ + constructor(playerId, itemId) + { + this.playerId = playerId; + this.itemId = itemId; + this.key = playerId + "_" + itemId; + this.details = {}; + } + + getJSON() + { + return {...this.details, player_id:this.playerId, item_id:this.itemId, key:this.key}; + } +} + + +module.exports = Item; diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..a321d8f --- /dev/null +++ b/package-lock.json @@ -0,0 +1,13 @@ +{ + "name": "blackout_server", + "version": "0.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "ws": { + "version": "7.2.1", + "resolved": "https://registry.npmjs.org/ws/-/ws-7.2.1.tgz", + "integrity": "sha512-sucePNSafamSKoOqoNfBd8V0StlkzJKL2ZAhGQinCfNQ+oacw+Pk7lcdAElecBF2VkLNZRiIb5Oi1Q5lVUVt2A==" + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..14e614e --- /dev/null +++ b/package.json @@ -0,0 +1,18 @@ +{ + "name": "blackout_server", + "version": "0.0.0", + "description": "Blackout websocket server", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "https://git.mhack.io/git/mark/BlackoutServerNode.git" + }, + "author": "", + "license": "ISC", + "dependencies": { + "ws": "^7.2.1" + } +} diff --git a/player.js b/player.js new file mode 100644 index 0000000..ebd6b9a --- /dev/null +++ b/player.js @@ -0,0 +1,16 @@ +var nextId = 0; +class Player +{ + constructor(socket) + { + this.socket = socket; + this.room = null; + this.id = nextId++; + } + sendMessage(type, details) + { + this.socket.send(JSON.stringify({...details, type:type})); + } +} + +module.exports = Player; diff --git a/room.js b/room.js new file mode 100644 index 0000000..1f9643e --- /dev/null +++ b/room.js @@ -0,0 +1,97 @@ +const Item = require("./item"); +var nextId = 0; + +class Room +{ + constructor(map) + { + this.id = nextId++; + this.players = {}; // id => Player + this.items = {}; // key => Item + this.map = map; + } + addPlayer(p) + { + if (this.players[p] !== undefined) return false; + + this.players[p.id] = p; + + p.room = this; + p.sendMessage("enter_room", {room_id:this.id, map:this.map, items:Object.values(this.items).map(i => i.getJSON())}); + + this.broadcastMessage("new_player", {player_id:p.id}); + + return true; + } + removePlayer(p) + { + if (this.players[p] === undefined) return false; + + delete this.players[p]; + + this.broadcastMessage("remove_player", {player_id:p.id}); + + return true; + } + getItem(playerId, itemId) + { + const key = playerId + "_" + itemId; + if (this.items[key] === undefined) return null; + + return items[key]; + } + playerMessage(player, messageStr) + { + const message = JSON.parse(messageStr); + switch (message.type) + { + case 'update_items': + if (message.items !== undefined) + { + let toSend = []; + for (var itemData of message.items) + { + if (itemData.item_id !== undefined) + { + const itemId = parseInt(itemData.item_id); + let item = this.getItem(player.ID, itemId); + if (item == null) + { + item = new Item(player.id, itemId); + this.items[item.key] = item; + } + const { item_id, player_id, key, ...cleanDetails } = itemData; + item.details = cleanDetails; + + toSend.push(item.getJSON()); + } + } + console.log("ITEMS: "); + console.log(this.items); + console.log("TOSEND: "); + console.log(toSend); + this.broadcastMessage('update_items', {items:toSend}, player); + } + break; + } + } + broadcastString(msg, skipPlayer = null) + { + for (var p of Object.values(this.playeres)) + { + if (p == skipPlayer) continue; + p.socket.send(msg); + } + } + broadcastMessage(type, details, skipPlayer = null) + { + const msg = JSON.stringify({...details, type:type}); + for (var p of Object.values(this.players)) + { + if (p == skipPlayer) continue; + p.socket.send(msg); + } + } +} + +module.exports = Room;