From fd86244bef390ed1bc91f4b9670809954525d6ac Mon Sep 17 00:00:00 2001 From: Mizanali Panjwani Date: Mon, 22 Feb 2021 05:02:50 +0530 Subject: [PATCH 1/6] Changed folder structure --- .gitignore | 21 +----------------- package-lock.json => client/package-lock.json | 0 package.json => client/package.json | 0 {public => client/public}/favicon.ico | Bin {public => client/public}/index.html | 0 {public => client/public}/logo192.png | Bin {public => client/public}/logo512.png | Bin {public => client/public}/manifest.json | 0 {public => client/public}/robots.txt | 0 {src => client/src}/App.css | 0 {src => client/src}/App.js | 0 {src => client/src}/components/Game.js | 0 {src => client/src}/components/Homepage.js | 0 {src => client/src}/index.css | 0 {src => client/src}/index.js | 0 {src => client/src}/utils/packOfCards.js | 0 {src => client/src}/utils/shuffleArray.js | 0 17 files changed, 1 insertion(+), 20 deletions(-) rename package-lock.json => client/package-lock.json (100%) rename package.json => client/package.json (100%) rename {public => client/public}/favicon.ico (100%) rename {public => client/public}/index.html (100%) rename {public => client/public}/logo192.png (100%) rename {public => client/public}/logo512.png (100%) rename {public => client/public}/manifest.json (100%) rename {public => client/public}/robots.txt (100%) rename {src => client/src}/App.css (100%) rename {src => client/src}/App.js (100%) rename {src => client/src}/components/Game.js (100%) rename {src => client/src}/components/Homepage.js (100%) rename {src => client/src}/index.css (100%) rename {src => client/src}/index.js (100%) rename {src => client/src}/utils/packOfCards.js (100%) rename {src => client/src}/utils/shuffleArray.js (100%) diff --git a/.gitignore b/.gitignore index 4d29575..af59483 100644 --- a/.gitignore +++ b/.gitignore @@ -1,23 +1,4 @@ # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. # dependencies -/node_modules -/.pnp -.pnp.js - -# testing -/coverage - -# production -/build - -# misc -.DS_Store -.env.local -.env.development.local -.env.test.local -.env.production.local - -npm-debug.log* -yarn-debug.log* -yarn-error.log* +node_modules/ \ No newline at end of file diff --git a/package-lock.json b/client/package-lock.json similarity index 100% rename from package-lock.json rename to client/package-lock.json diff --git a/package.json b/client/package.json similarity index 100% rename from package.json rename to client/package.json diff --git a/public/favicon.ico b/client/public/favicon.ico similarity index 100% rename from public/favicon.ico rename to client/public/favicon.ico diff --git a/public/index.html b/client/public/index.html similarity index 100% rename from public/index.html rename to client/public/index.html diff --git a/public/logo192.png b/client/public/logo192.png similarity index 100% rename from public/logo192.png rename to client/public/logo192.png diff --git a/public/logo512.png b/client/public/logo512.png similarity index 100% rename from public/logo512.png rename to client/public/logo512.png diff --git a/public/manifest.json b/client/public/manifest.json similarity index 100% rename from public/manifest.json rename to client/public/manifest.json diff --git a/public/robots.txt b/client/public/robots.txt similarity index 100% rename from public/robots.txt rename to client/public/robots.txt diff --git a/src/App.css b/client/src/App.css similarity index 100% rename from src/App.css rename to client/src/App.css diff --git a/src/App.js b/client/src/App.js similarity index 100% rename from src/App.js rename to client/src/App.js diff --git a/src/components/Game.js b/client/src/components/Game.js similarity index 100% rename from src/components/Game.js rename to client/src/components/Game.js diff --git a/src/components/Homepage.js b/client/src/components/Homepage.js similarity index 100% rename from src/components/Homepage.js rename to client/src/components/Homepage.js diff --git a/src/index.css b/client/src/index.css similarity index 100% rename from src/index.css rename to client/src/index.css diff --git a/src/index.js b/client/src/index.js similarity index 100% rename from src/index.js rename to client/src/index.js diff --git a/src/utils/packOfCards.js b/client/src/utils/packOfCards.js similarity index 100% rename from src/utils/packOfCards.js rename to client/src/utils/packOfCards.js diff --git a/src/utils/shuffleArray.js b/client/src/utils/shuffleArray.js similarity index 100% rename from src/utils/shuffleArray.js rename to client/src/utils/shuffleArray.js From 157aa5f94b3f77c17eeb99baf18fa6730cee5671 Mon Sep 17 00:00:00 2001 From: Mizanali Panjwani Date: Mon, 22 Feb 2021 05:20:20 +0530 Subject: [PATCH 2/6] Set up socketio server-client connection --- client/package-lock.json | 120 +++++++++++++++++++++++++--------- client/package.json | 3 +- client/src/components/Game.js | 16 +++++ 3 files changed, 107 insertions(+), 32 deletions(-) diff --git a/client/package-lock.json b/client/package-lock.json index f96194b..aced44e 100644 --- a/client/package-lock.json +++ b/client/package-lock.json @@ -2168,6 +2168,11 @@ "@babel/types": "^7.3.0" } }, + "@types/component-emitter": { + "version": "1.2.10", + "resolved": "https://registry.npmjs.org/@types/component-emitter/-/component-emitter-1.2.10.tgz", + "integrity": "sha512-bsjleuRKWmGqajMerkzox19aGbscQX5rmmvvXl3wlIp5gMG1HgkiwPxsN5p070fBDKTNSPgojVbuY1+HWMbFhg==" + }, "@types/eslint": { "version": "7.2.6", "resolved": "https://registry.npmjs.org/@types/eslint/-/eslint-7.2.6.tgz", @@ -3411,6 +3416,11 @@ "resolved": "https://registry.npmjs.org/babylon/-/babylon-6.18.0.tgz", "integrity": "sha512-q/UEjfGJ2Cm3oKV71DJz9d25TPnq5rhBVL2Q4fA5wcC3jcrdn7+SssEybFIxwAvvP+YCsCYNKughoF33GxgycQ==" }, + "backo2": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/backo2/-/backo2-1.0.2.tgz", + "integrity": "sha1-MasayLEpNjRj41s+u2n038+6eUc=" + }, "balanced-match": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", @@ -3466,6 +3476,11 @@ } } }, + "base64-arraybuffer": { + "version": "0.1.4", + "resolved": "https://registry.npmjs.org/base64-arraybuffer/-/base64-arraybuffer-0.1.4.tgz", + "integrity": "sha1-mBjHngWbE1X5fgQooBfIOOkLqBI=" + }, "base64-js": { "version": "1.5.1", "resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.5.1.tgz", @@ -5218,6 +5233,31 @@ "once": "^1.4.0" } }, + "engine.io-client": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/engine.io-client/-/engine.io-client-4.1.1.tgz", + "integrity": "sha512-iYasV/EttP/2pLrdowe9G3zwlNIFhwny8VSIh+vPlMnYZqSzLsTzSLa9hFy015OrH1s4fzoYxeHjVkO8hSFKwg==", + "requires": { + "base64-arraybuffer": "0.1.4", + "component-emitter": "~1.3.0", + "debug": "~4.3.1", + "engine.io-parser": "~4.0.1", + "has-cors": "1.1.0", + "parseqs": "0.0.6", + "parseuri": "0.0.6", + "ws": "~7.4.2", + "xmlhttprequest-ssl": "~1.5.4", + "yeast": "0.1.2" + } + }, + "engine.io-parser": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/engine.io-parser/-/engine.io-parser-4.0.2.tgz", + "integrity": "sha512-sHfEQv6nmtJrq6TKuIz5kyEKH/qSdK56H/A+7DnAuUPWosnIZAS2NHNcPLmyjtY3cGS/MqJdZbUjW97JU72iYg==", + "requires": { + "base64-arraybuffer": "0.1.4" + } + }, "enhanced-resolve": { "version": "4.5.0", "resolved": "https://registry.npmjs.org/enhanced-resolve/-/enhanced-resolve-4.5.0.tgz", @@ -6895,6 +6935,11 @@ "function-bind": "^1.1.1" } }, + "has-cors": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/has-cors/-/has-cors-1.1.0.tgz", + "integrity": "sha1-XkdHk/fqmEPRu5nCPu9J/xJv/zk=" + }, "has-flag": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz", @@ -10655,6 +10700,16 @@ "resolved": "https://registry.npmjs.org/parse5/-/parse5-5.1.1.tgz", "integrity": "sha512-ugq4DFI0Ptb+WWjAdOK16+u/nHfiIrcE+sh8kZMaM0WllQKLI9rOUq6c2b7cwPkXdzfQESqvoqK6ug7U/Yyzug==" }, + "parseqs": { + "version": "0.0.6", + "resolved": "https://registry.npmjs.org/parseqs/-/parseqs-0.0.6.tgz", + "integrity": "sha512-jeAGzMDbfSHHA091hr0r31eYfTig+29g3GKKE/PPbEQ65X0lmMwlEoqmhzu0iztID5uJpZsFlUPDP8ThPL7M8w==" + }, + "parseuri": { + "version": "0.0.6", + "resolved": "https://registry.npmjs.org/parseuri/-/parseuri-0.0.6.tgz", + "integrity": "sha512-AUjen8sAkGgao7UyCX6Ahv0gIK2fABKmYjvP4xmy5JaKvcbTRueIqIPHLAfq30xJddqSE033IOMUSOMCcK3Sow==" + }, "parseurl": { "version": "1.3.3", "resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz", @@ -12303,18 +12358,6 @@ "resolved": "https://registry.npmjs.org/react-is/-/react-is-16.13.1.tgz", "integrity": "sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ==" }, - "react-redux": { - "version": "7.2.2", - "resolved": "https://registry.npmjs.org/react-redux/-/react-redux-7.2.2.tgz", - "integrity": "sha512-8+CQ1EvIVFkYL/vu6Olo7JFLWop1qRUeb46sGtIMDCSpgwPQq8fPLpirIB0iTqFe9XYEFPHssdX8/UwN6pAkEA==", - "requires": { - "@babel/runtime": "^7.12.1", - "hoist-non-react-statics": "^3.3.2", - "loose-envify": "^1.4.0", - "prop-types": "^15.7.2", - "react-is": "^16.13.1" - } - }, "react-refresh": { "version": "0.8.3", "resolved": "https://registry.npmjs.org/react-refresh/-/react-refresh-0.8.3.tgz", @@ -12547,15 +12590,6 @@ "strip-indent": "^3.0.0" } }, - "redux": { - "version": "4.0.5", - "resolved": "https://registry.npmjs.org/redux/-/redux-4.0.5.tgz", - "integrity": "sha512-VSz1uMAH24DM6MF72vcojpYPtrTUu3ByVWfPL1nPfVRb5mZVTve5GnNCUV53QM/BZ66xfWrm0CTWoM+Xlz8V1w==", - "requires": { - "loose-envify": "^1.4.0", - "symbol-observable": "^1.2.0" - } - }, "regenerate": { "version": "1.4.2", "resolved": "https://registry.npmjs.org/regenerate/-/regenerate-1.4.2.tgz", @@ -13638,6 +13672,30 @@ } } }, + "socket.io-client": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/socket.io-client/-/socket.io-client-3.1.1.tgz", + "integrity": "sha512-BLgIuCjI7Sf3mDHunKddX9zKR/pbkP7IACM3sJS3jha+zJ6/pGKRV6Fz5XSBHCfUs9YzT8kYIqNwOOuFNLtnYA==", + "requires": { + "@types/component-emitter": "^1.2.10", + "backo2": "~1.0.2", + "component-emitter": "~1.3.0", + "debug": "~4.3.1", + "engine.io-client": "~4.1.0", + "parseuri": "0.0.6", + "socket.io-parser": "~4.0.4" + } + }, + "socket.io-parser": { + "version": "4.0.4", + "resolved": "https://registry.npmjs.org/socket.io-parser/-/socket.io-parser-4.0.4.tgz", + "integrity": "sha512-t+b0SS+IxG7Rxzda2EVvyBZbvFPBCjJoyHuE0P//7OAsN23GItzDRdWa6ALxZI/8R5ygK7jAR6t028/z+7295g==", + "requires": { + "@types/component-emitter": "^1.2.10", + "component-emitter": "~1.3.0", + "debug": "~4.3.1" + } + }, "sockjs": { "version": "0.3.20", "resolved": "https://registry.npmjs.org/sockjs/-/sockjs-0.3.20.tgz", @@ -14195,11 +14253,6 @@ "util.promisify": "~1.0.0" } }, - "symbol-observable": { - "version": "1.2.0", - "resolved": "https://registry.npmjs.org/symbol-observable/-/symbol-observable-1.2.0.tgz", - "integrity": "sha512-e900nM8RRtGhlV36KGEU9k65K3mPb1WV70OdjfxlG2EAuM1noi/E/BaW/uMhL7bPEssK8QV57vN3esixjUvcXQ==" - }, "symbol-tree": { "version": "3.2.4", "resolved": "https://registry.npmjs.org/symbol-tree/-/symbol-tree-3.2.4.tgz", @@ -15262,11 +15315,6 @@ "minimalistic-assert": "^1.0.0" } }, - "web-vitals": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/web-vitals/-/web-vitals-1.1.0.tgz", - "integrity": "sha512-1cx54eRxY/+M0KNKdNpNnuXAXG+vJEvwScV4DiV9rOYDguHoeDIzm09ghBohOPtkqPO5OtPC14FWkNva3SDisg==" - }, "webidl-conversions": { "version": "6.1.0", "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-6.1.0.tgz", @@ -16421,6 +16469,11 @@ "resolved": "https://registry.npmjs.org/xmlchars/-/xmlchars-2.2.0.tgz", "integrity": "sha512-JZnDKK8B0RCDw84FNdDAIpZK+JuJw+s7Lz8nksI7SIuU3UXJJslUthsi+uWBUYOwPFwW7W7PRLRfUKpxjtjFCw==" }, + "xmlhttprequest-ssl": { + "version": "1.5.5", + "resolved": "https://registry.npmjs.org/xmlhttprequest-ssl/-/xmlhttprequest-ssl-1.5.5.tgz", + "integrity": "sha1-wodrBhaKrcQOV9l+gRkayPQ5iz4=" + }, "xtend": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz", @@ -16475,6 +16528,11 @@ } } }, + "yeast": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/yeast/-/yeast-0.1.2.tgz", + "integrity": "sha1-AI4G2AlDIMNy28L47XagymyKxBk=" + }, "yocto-queue": { "version": "0.1.0", "resolved": "https://registry.npmjs.org/yocto-queue/-/yocto-queue-0.1.0.tgz", diff --git a/client/package.json b/client/package.json index 59da2e5..9014a7d 100644 --- a/client/package.json +++ b/client/package.json @@ -9,7 +9,8 @@ "react": "^17.0.1", "react-dom": "^17.0.1", "react-router-dom": "^5.2.0", - "react-scripts": "4.0.2" + "react-scripts": "4.0.2", + "socket.io-client": "^3.1.1" }, "scripts": { "start": "react-scripts start", diff --git a/client/src/components/Game.js b/client/src/components/Game.js index d841b3e..1b6db80 100644 --- a/client/src/components/Game.js +++ b/client/src/components/Game.js @@ -1,6 +1,7 @@ import React, { useEffect, useState } from 'react' import PACK_OF_CARDS from '../utils/packOfCards' import shuffleArray from '../utils/shuffleArray' +import io from 'socket.io-client' //NUMBER CODES FOR ACTION CARDS //SKIP - 404 @@ -8,8 +9,23 @@ import shuffleArray from '../utils/shuffleArray' //WILD - 300 //DRAW 4 WILD - 600 +let socket +const ENDPOINT = 'http://localhost:5000' + const Game = () => { + + useEffect(() => { + const connectionOptions = { + "forceNew" : true, + "reconnectionAttempts": "Infinity", + "timeout" : 10000, + "transports" : ["websocket"] + } + socket = io.connect(ENDPOINT, connectionOptions) + console.log(socket); + }, []) + //initialize game state const [gameOver, setGameOver] = useState(true) const [winner, setWinner] = useState('') From 93d0c9fdde4517aa38ea1396fca759159a9a67a6 Mon Sep 17 00:00:00 2001 From: Mizanali Panjwani Date: Mon, 22 Feb 2021 05:29:23 +0530 Subject: [PATCH 3/6] Set up socketio server-client connection --- README.md | 71 +------ package-lock.json | 531 ++++++++++++++++++++++++++++++++++++++++++++++ package.json | 24 +++ server.js | 24 +++ 4 files changed, 580 insertions(+), 70 deletions(-) create mode 100644 package-lock.json create mode 100644 package.json create mode 100644 server.js diff --git a/README.md b/README.md index 0c83cde..aea5d5e 100644 --- a/README.md +++ b/README.md @@ -1,70 +1 @@ -# Getting Started with Create React App - -This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). - -## Available Scripts - -In the project directory, you can run: - -### `npm start` - -Runs the app in the development mode.\ -Open [http://localhost:3000](http://localhost:3000) to view it in the browser. - -The page will reload if you make edits.\ -You will also see any lint errors in the console. - -### `npm test` - -Launches the test runner in the interactive watch mode.\ -See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. - -### `npm run build` - -Builds the app for production to the `build` folder.\ -It correctly bundles React in production mode and optimizes the build for the best performance. - -The build is minified and the filenames include the hashes.\ -Your app is ready to be deployed! - -See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. - -### `npm run eject` - -**Note: this is a one-way operation. Once you `eject`, you can’t go back!** - -If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. - -Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. - -You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. - -## Learn More - -You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). - -To learn React, check out the [React documentation](https://reactjs.org/). - -### Code Splitting - -This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting) - -### Analyzing the Bundle Size - -This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size) - -### Making a Progressive Web App - -This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app) - -### Advanced Configuration - -This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration) - -### Deployment - -This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment) - -### `npm run build` fails to minify - -This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify) +Online multiplayer card game. \ No newline at end of file diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..1e106d9 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,531 @@ +{ + "name": "uno-online", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "@types/component-emitter": { + "version": "1.2.10", + "resolved": "https://registry.npmjs.org/@types/component-emitter/-/component-emitter-1.2.10.tgz", + "integrity": "sha512-bsjleuRKWmGqajMerkzox19aGbscQX5rmmvvXl3wlIp5gMG1HgkiwPxsN5p070fBDKTNSPgojVbuY1+HWMbFhg==" + }, + "@types/cookie": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/@types/cookie/-/cookie-0.4.0.tgz", + "integrity": "sha512-y7mImlc/rNkvCRmg8gC3/lj87S7pTUIJ6QGjwHR9WQJcFs+ZMTOaoPrkdFA/YdbuqVEmEbb5RdhVxMkAcgOnpg==" + }, + "@types/cors": { + "version": "2.8.10", + "resolved": "https://registry.npmjs.org/@types/cors/-/cors-2.8.10.tgz", + "integrity": "sha512-C7srjHiVG3Ey1nR6d511dtDkCEjxuN9W1HWAEjGq8kpcwmNM6JJkpC0xvabM7BXTG2wDq8Eu33iH9aQKa7IvLQ==" + }, + "@types/node": { + "version": "14.14.31", + "resolved": "https://registry.npmjs.org/@types/node/-/node-14.14.31.tgz", + "integrity": "sha512-vFHy/ezP5qI0rFgJ7aQnjDXwAMrG0KqqIH7tQG5PPv3BWBayOPIQNBjVc/P6hhdZfMx51REc6tfDNXHUio893g==" + }, + "accepts": { + "version": "1.3.7", + "resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.7.tgz", + "integrity": "sha512-Il80Qs2WjYlJIBNzNkK6KYqlVMTbZLXgHx2oT0pU/fjRHyEp+PEfEPY0R3WCwAGVOtauxh1hOxNgIf5bv7dQpA==", + "requires": { + "mime-types": "~2.1.24", + "negotiator": "0.6.2" + } + }, + "array-flatten": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", + "integrity": "sha1-ml9pkFGx5wczKPKgCJaLZOopVdI=" + }, + "base64-arraybuffer": { + "version": "0.1.4", + "resolved": "https://registry.npmjs.org/base64-arraybuffer/-/base64-arraybuffer-0.1.4.tgz", + "integrity": "sha1-mBjHngWbE1X5fgQooBfIOOkLqBI=" + }, + "base64id": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/base64id/-/base64id-2.0.0.tgz", + "integrity": "sha512-lGe34o6EHj9y3Kts9R4ZYs/Gr+6N7MCaMlIFA3F1R2O5/m7K06AxfSeO5530PEERE6/WyEg3lsuyw4GHlPZHog==" + }, + "body-parser": { + "version": "1.19.0", + "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.19.0.tgz", + "integrity": "sha512-dhEPs72UPbDnAQJ9ZKMNTP6ptJaionhP5cBb541nXPlW60Jepo9RV/a4fX4XWW9CuFNK22krhrj1+rgzifNCsw==", + "requires": { + "bytes": "3.1.0", + "content-type": "~1.0.4", + "debug": "2.6.9", + "depd": "~1.1.2", + "http-errors": "1.7.2", + "iconv-lite": "0.4.24", + "on-finished": "~2.3.0", + "qs": "6.7.0", + "raw-body": "2.4.0", + "type-is": "~1.6.17" + } + }, + "bytes": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.0.tgz", + "integrity": "sha512-zauLjrfCG+xvoyaqLoV8bLVXXNGC4JqlxFCutSDWA6fJrTo2ZuvLYTqZ7aHBLZSMOopbzwv8f+wZcVzfVTI2Dg==" + }, + "component-emitter": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/component-emitter/-/component-emitter-1.3.0.tgz", + "integrity": "sha512-Rd3se6QB+sO1TwqZjscQrurpEPIfO0/yYnSin6Q/rD3mOutHvUrCAhJub3r90uNb+SESBuE0QYoB90YdfatsRg==" + }, + "content-disposition": { + "version": "0.5.3", + "resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.3.tgz", + "integrity": "sha512-ExO0774ikEObIAEV9kDo50o+79VCUdEB6n6lzKgGwupcVeRlhrj3qGAfwq8G6uBJjkqLrhT0qEYFcWng8z1z0g==", + "requires": { + "safe-buffer": "5.1.2" + } + }, + "content-type": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/content-type/-/content-type-1.0.4.tgz", + "integrity": "sha512-hIP3EEPs8tB9AT1L+NUqtwOAps4mk2Zob89MWXMHjHWg9milF/j4osnnQLXBCBFBk/tvIG/tUc9mOUJiPBhPXA==" + }, + "cookie": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.4.0.tgz", + "integrity": "sha512-+Hp8fLp57wnUSt0tY0tHEXh4voZRDnoIrZPqlo3DPiI4y9lwg/jqx+1Om94/W6ZaPDOUbnjOt/99w66zk+l1Xg==" + }, + "cookie-signature": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz", + "integrity": "sha1-4wOogrNCzD7oylE6eZmXNNqzriw=" + }, + "cors": { + "version": "2.8.5", + "resolved": "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz", + "integrity": "sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==", + "requires": { + "object-assign": "^4", + "vary": "^1" + } + }, + "debug": { + "version": "2.6.9", + "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", + "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==", + "requires": { + "ms": "2.0.0" + } + }, + "depd": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/depd/-/depd-1.1.2.tgz", + "integrity": "sha1-m81S4UwJd2PnSbJ0xDRu0uVgtak=" + }, + "destroy": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/destroy/-/destroy-1.0.4.tgz", + "integrity": "sha1-l4hXRCxEdJ5CBmE+N5RiBYJqvYA=" + }, + "ee-first": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", + "integrity": "sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0=" + }, + "encodeurl": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/encodeurl/-/encodeurl-1.0.2.tgz", + "integrity": "sha1-rT/0yG7C0CkyL1oCw6mmBslbP1k=" + }, + "engine.io": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/engine.io/-/engine.io-4.1.1.tgz", + "integrity": "sha512-t2E9wLlssQjGw0nluF6aYyfX8LwYU8Jj0xct+pAhfWfv/YrBn6TSNtEYsgxHIfaMqfrLx07czcMg9bMN6di+3w==", + "requires": { + "accepts": "~1.3.4", + "base64id": "2.0.0", + "cookie": "~0.4.1", + "cors": "~2.8.5", + "debug": "~4.3.1", + "engine.io-parser": "~4.0.0", + "ws": "~7.4.2" + }, + "dependencies": { + "cookie": { + "version": "0.4.1", + "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.4.1.tgz", + "integrity": "sha512-ZwrFkGJxUR3EIoXtO+yVE69Eb7KlixbaeAWfBQB9vVsNn/o+Yw69gBWSSDK825hQNdN+wF8zELf3dFNl/kxkUA==" + }, + "debug": { + "version": "4.3.1", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.1.tgz", + "integrity": "sha512-doEwdvm4PCeK4K3RQN2ZC2BYUBaxwLARCqZmMjtF8a51J2Rb0xpVloFRnCODwqjpwnAoao4pelN8l3RJdv3gRQ==", + "requires": { + "ms": "2.1.2" + } + }, + "ms": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" + } + } + }, + "engine.io-parser": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/engine.io-parser/-/engine.io-parser-4.0.2.tgz", + "integrity": "sha512-sHfEQv6nmtJrq6TKuIz5kyEKH/qSdK56H/A+7DnAuUPWosnIZAS2NHNcPLmyjtY3cGS/MqJdZbUjW97JU72iYg==", + "requires": { + "base64-arraybuffer": "0.1.4" + } + }, + "escape-html": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/escape-html/-/escape-html-1.0.3.tgz", + "integrity": "sha1-Aljq5NPQwJdN4cFpGI7wBR0dGYg=" + }, + "etag": { + "version": "1.8.1", + "resolved": "https://registry.npmjs.org/etag/-/etag-1.8.1.tgz", + "integrity": "sha1-Qa4u62XvpiJorr/qg6x9eSmbCIc=" + }, + "express": { + "version": "4.17.1", + "resolved": "https://registry.npmjs.org/express/-/express-4.17.1.tgz", + "integrity": "sha512-mHJ9O79RqluphRrcw2X/GTh3k9tVv8YcoyY4Kkh4WDMUYKRZUq0h1o0w2rrrxBqM7VoeUVqgb27xlEMXTnYt4g==", + "requires": { + "accepts": "~1.3.7", + "array-flatten": "1.1.1", + "body-parser": "1.19.0", + "content-disposition": "0.5.3", + "content-type": "~1.0.4", + "cookie": "0.4.0", + "cookie-signature": "1.0.6", + "debug": "2.6.9", + "depd": "~1.1.2", + "encodeurl": "~1.0.2", + "escape-html": "~1.0.3", + "etag": "~1.8.1", + "finalhandler": "~1.1.2", + "fresh": "0.5.2", + "merge-descriptors": "1.0.1", + "methods": "~1.1.2", + "on-finished": "~2.3.0", + "parseurl": "~1.3.3", + "path-to-regexp": "0.1.7", + "proxy-addr": "~2.0.5", + "qs": "6.7.0", + "range-parser": "~1.2.1", + "safe-buffer": "5.1.2", + "send": "0.17.1", + "serve-static": "1.14.1", + "setprototypeof": "1.1.1", + "statuses": "~1.5.0", + "type-is": "~1.6.18", + "utils-merge": "1.0.1", + "vary": "~1.1.2" + } + }, + "finalhandler": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-1.1.2.tgz", + "integrity": "sha512-aAWcW57uxVNrQZqFXjITpW3sIUQmHGG3qSb9mUah9MgMC4NeWhNOlNjXEYq3HjRAvL6arUviZGGJsBg6z0zsWA==", + "requires": { + "debug": "2.6.9", + "encodeurl": "~1.0.2", + "escape-html": "~1.0.3", + "on-finished": "~2.3.0", + "parseurl": "~1.3.3", + "statuses": "~1.5.0", + "unpipe": "~1.0.0" + } + }, + "forwarded": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.1.2.tgz", + "integrity": "sha1-mMI9qxF1ZXuMBXPozszZGw/xjIQ=" + }, + "fresh": { + "version": "0.5.2", + "resolved": "https://registry.npmjs.org/fresh/-/fresh-0.5.2.tgz", + "integrity": "sha1-PYyt2Q2XZWn6g1qx+OSyOhBWBac=" + }, + "http-errors": { + "version": "1.7.2", + "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-1.7.2.tgz", + "integrity": "sha512-uUQBt3H/cSIVfch6i1EuPNy/YsRSOUBXTVfZ+yR7Zjez3qjBz6i9+i4zjNaoqcoFVI4lQJ5plg63TvGfRSDCRg==", + "requires": { + "depd": "~1.1.2", + "inherits": "2.0.3", + "setprototypeof": "1.1.1", + "statuses": ">= 1.5.0 < 2", + "toidentifier": "1.0.0" + } + }, + "iconv-lite": { + "version": "0.4.24", + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz", + "integrity": "sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==", + "requires": { + "safer-buffer": ">= 2.1.2 < 3" + } + }, + "inherits": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz", + "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=" + }, + "ipaddr.js": { + "version": "1.9.1", + "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz", + "integrity": "sha512-0KI/607xoxSToH7GjN1FfSbLoU0+btTicjsQSWQlh/hZykN8KpmMf7uYwPW3R+akZ6R/w18ZlXSHBYXiYUPO3g==" + }, + "media-typer": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", + "integrity": "sha1-hxDXrwqmJvj/+hzgAWhUUmMlV0g=" + }, + "merge-descriptors": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-1.0.1.tgz", + "integrity": "sha1-sAqqVW3YtEVoFQ7J0blT8/kMu2E=" + }, + "methods": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/methods/-/methods-1.1.2.tgz", + "integrity": "sha1-VSmk1nZUE07cxSZmVoNbD4Ua/O4=" + }, + "mime": { + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/mime/-/mime-1.6.0.tgz", + "integrity": "sha512-x0Vn8spI+wuJ1O6S7gnbaQg8Pxh4NNHb7KSINmEWKiPE4RKOplvijn+NkmYmmRgP68mc70j2EbeTFRsrswaQeg==" + }, + "mime-db": { + "version": "1.46.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.46.0.tgz", + "integrity": "sha512-svXaP8UQRZ5K7or+ZmfNhg2xX3yKDMUzqadsSqi4NCH/KomcH75MAMYAGVlvXn4+b/xOPhS3I2uHKRUzvjY7BQ==" + }, + "mime-types": { + "version": "2.1.29", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.29.tgz", + "integrity": "sha512-Y/jMt/S5sR9OaqteJtslsFZKWOIIqMACsJSiHghlCAyhf7jfVYjKBmLiX8OgpWeW+fjJ2b+Az69aPFPkUOY6xQ==", + "requires": { + "mime-db": "1.46.0" + } + }, + "ms": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", + "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=" + }, + "negotiator": { + "version": "0.6.2", + "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.2.tgz", + "integrity": "sha512-hZXc7K2e+PgeI1eDBe/10Ard4ekbfrrqG8Ep+8Jmf4JID2bNg7NvCPOZN+kfF574pFQI7mum2AUqDidoKqcTOw==" + }, + "object-assign": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz", + "integrity": "sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM=" + }, + "on-finished": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.3.0.tgz", + "integrity": "sha1-IPEzZIGwg811M3mSoWlxqi2QaUc=", + "requires": { + "ee-first": "1.1.1" + } + }, + "parseurl": { + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz", + "integrity": "sha512-CiyeOxFT/JZyN5m0z9PfXw4SCBJ6Sygz1Dpl0wqjlhDEGGBP1GnsUVEL0p63hoG1fcj3fHynXi9NYO4nWOL+qQ==" + }, + "path-to-regexp": { + "version": "0.1.7", + "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.7.tgz", + "integrity": "sha1-32BBeABfUi8V60SQ5yR6G/qmf4w=" + }, + "proxy-addr": { + "version": "2.0.6", + "resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.6.tgz", + "integrity": "sha512-dh/frvCBVmSsDYzw6n926jv974gddhkFPfiN8hPOi30Wax25QZyZEGveluCgliBnqmuM+UJmBErbAUFIoDbjOw==", + "requires": { + "forwarded": "~0.1.2", + "ipaddr.js": "1.9.1" + } + }, + "qs": { + "version": "6.7.0", + "resolved": "https://registry.npmjs.org/qs/-/qs-6.7.0.tgz", + "integrity": "sha512-VCdBRNFTX1fyE7Nb6FYoURo/SPe62QCaAyzJvUjwRaIsc+NePBEniHlvxFmmX56+HZphIGtV0XeCirBtpDrTyQ==" + }, + "range-parser": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.1.tgz", + "integrity": "sha512-Hrgsx+orqoygnmhFbKaHE6c296J+HTAQXoxEF6gNupROmmGJRoyzfG3ccAveqCBrwr/2yxQ5BVd/GTl5agOwSg==" + }, + "raw-body": { + "version": "2.4.0", + "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.4.0.tgz", + "integrity": "sha512-4Oz8DUIwdvoa5qMJelxipzi/iJIi40O5cGV1wNYp5hvZP8ZN0T+jiNkL0QepXs+EsQ9XJ8ipEDoiH70ySUJP3Q==", + "requires": { + "bytes": "3.1.0", + "http-errors": "1.7.2", + "iconv-lite": "0.4.24", + "unpipe": "1.0.0" + } + }, + "safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" + }, + "safer-buffer": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", + "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" + }, + "send": { + "version": "0.17.1", + "resolved": "https://registry.npmjs.org/send/-/send-0.17.1.tgz", + "integrity": "sha512-BsVKsiGcQMFwT8UxypobUKyv7irCNRHk1T0G680vk88yf6LBByGcZJOTJCrTP2xVN6yI+XjPJcNuE3V4fT9sAg==", + "requires": { + "debug": "2.6.9", + "depd": "~1.1.2", + "destroy": "~1.0.4", + "encodeurl": "~1.0.2", + "escape-html": "~1.0.3", + "etag": "~1.8.1", + "fresh": "0.5.2", + "http-errors": "~1.7.2", + "mime": "1.6.0", + "ms": "2.1.1", + "on-finished": "~2.3.0", + "range-parser": "~1.2.1", + "statuses": "~1.5.0" + }, + "dependencies": { + "ms": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.1.tgz", + "integrity": "sha512-tgp+dl5cGk28utYktBsrFqA7HKgrhgPsg6Z/EfhWI4gl1Hwq8B/GmY/0oXZ6nF8hDVesS/FpnYaD/kOWhYQvyg==" + } + } + }, + "serve-static": { + "version": "1.14.1", + "resolved": "https://registry.npmjs.org/serve-static/-/serve-static-1.14.1.tgz", + "integrity": "sha512-JMrvUwE54emCYWlTI+hGrGv5I8dEwmco/00EvkzIIsR7MqrHonbD9pO2MOfFnpFntl7ecpZs+3mW+XbQZu9QCg==", + "requires": { + "encodeurl": "~1.0.2", + "escape-html": "~1.0.3", + "parseurl": "~1.3.3", + "send": "0.17.1" + } + }, + "setprototypeof": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.1.1.tgz", + "integrity": "sha512-JvdAWfbXeIGaZ9cILp38HntZSFSo3mWg6xGcJJsd+d4aRMOqauag1C63dJfDw7OaMYwEbHMOxEZ1lqVRYP2OAw==" + }, + "socket.io": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/socket.io/-/socket.io-3.1.1.tgz", + "integrity": "sha512-7cBWdsDC7bbyEF6WbBqffjizc/H4YF1wLdZoOzuYfo2uMNSFjJKuQ36t0H40o9B20DO6p+mSytEd92oP4S15bA==", + "requires": { + "@types/cookie": "^0.4.0", + "@types/cors": "^2.8.8", + "@types/node": "^14.14.10", + "accepts": "~1.3.4", + "base64id": "~2.0.0", + "debug": "~4.3.1", + "engine.io": "~4.1.0", + "socket.io-adapter": "~2.1.0", + "socket.io-parser": "~4.0.3" + }, + "dependencies": { + "debug": { + "version": "4.3.1", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.1.tgz", + "integrity": "sha512-doEwdvm4PCeK4K3RQN2ZC2BYUBaxwLARCqZmMjtF8a51J2Rb0xpVloFRnCODwqjpwnAoao4pelN8l3RJdv3gRQ==", + "requires": { + "ms": "2.1.2" + } + }, + "ms": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" + } + } + }, + "socket.io-adapter": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/socket.io-adapter/-/socket.io-adapter-2.1.0.tgz", + "integrity": "sha512-+vDov/aTsLjViYTwS9fPy5pEtTkrbEKsw2M+oVSoFGw6OD1IpvlV1VPhUzNbofCQ8oyMbdYJqDtGdmHQK6TdPg==" + }, + "socket.io-parser": { + "version": "4.0.4", + "resolved": "https://registry.npmjs.org/socket.io-parser/-/socket.io-parser-4.0.4.tgz", + "integrity": "sha512-t+b0SS+IxG7Rxzda2EVvyBZbvFPBCjJoyHuE0P//7OAsN23GItzDRdWa6ALxZI/8R5ygK7jAR6t028/z+7295g==", + "requires": { + "@types/component-emitter": "^1.2.10", + "component-emitter": "~1.3.0", + "debug": "~4.3.1" + }, + "dependencies": { + "debug": { + "version": "4.3.1", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.1.tgz", + "integrity": "sha512-doEwdvm4PCeK4K3RQN2ZC2BYUBaxwLARCqZmMjtF8a51J2Rb0xpVloFRnCODwqjpwnAoao4pelN8l3RJdv3gRQ==", + "requires": { + "ms": "2.1.2" + } + }, + "ms": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" + } + } + }, + "statuses": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/statuses/-/statuses-1.5.0.tgz", + "integrity": "sha1-Fhx9rBd2Wf2YEfQ3cfqZOBR4Yow=" + }, + "toidentifier": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.0.tgz", + "integrity": "sha512-yaOH/Pk/VEhBWWTlhI+qXxDFXlejDGcQipMlyxda9nthulaxLZUNcUqFxokp0vcYnvteJln5FNQDRrxj3YcbVw==" + }, + "type-is": { + "version": "1.6.18", + "resolved": "https://registry.npmjs.org/type-is/-/type-is-1.6.18.tgz", + "integrity": "sha512-TkRKr9sUTxEH8MdfuCSP7VizJyzRNMjj2J2do2Jr3Kym598JVdEksuzPQCnlFPW4ky9Q+iA+ma9BGm06XQBy8g==", + "requires": { + "media-typer": "0.3.0", + "mime-types": "~2.1.24" + } + }, + "unpipe": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz", + "integrity": "sha1-sr9O6FFKrmFltIF4KdIbLvSZBOw=" + }, + "utils-merge": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz", + "integrity": "sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM=" + }, + "vary": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz", + "integrity": "sha1-IpnwLG3tMNSllhsLn3RSShj2NPw=" + }, + "ws": { + "version": "7.4.3", + "resolved": "https://registry.npmjs.org/ws/-/ws-7.4.3.tgz", + "integrity": "sha512-hr6vCR76GsossIRsr8OLR9acVVm1jyfEWvhbNjtgPOrfvAlKzvyeg/P6r8RuDjRyrcQoPQT7K0DGEPc7Ae6jzA==" + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..becffdd --- /dev/null +++ b/package.json @@ -0,0 +1,24 @@ +{ + "name": "uno-online", + "version": "1.0.0", + "description": "Online multiplayer card game", + "main": "server.js", + "scripts": { + "start": "node server" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/mizanxali/uno-online.git" + }, + "author": "Mizan Ali", + "license": "ISC", + "bugs": { + "url": "https://github.com/mizanxali/uno-online/issues" + }, + "homepage": "https://github.com/mizanxali/uno-online#readme", + "dependencies": { + "cors": "^2.8.5", + "express": "^4.17.1", + "socket.io": "^3.1.1" + } +} diff --git a/server.js b/server.js new file mode 100644 index 0000000..e107c69 --- /dev/null +++ b/server.js @@ -0,0 +1,24 @@ +const express = require('express') +const socketio = require('socket.io') +const http = require('http') +const cors = require('cors') + +const PORT = process.env.PORT || 5000 + +const app = express() +const server = http.createServer(app) +const io = socketio(server) + +app.use(cors()) + +io.on('connection', socket => { + console.log('connection made') + + socket.on('disconnect', () => { + console.log('connection lost') + }) +}) + +server.listen(PORT, () => { + console.log(`Server running on port ${PORT}`) +}) \ No newline at end of file From 42648100322082793c5d4780d4b395a603fd72c3 Mon Sep 17 00:00:00 2001 From: Mizanali Panjwani Date: Mon, 22 Feb 2021 06:32:57 +0530 Subject: [PATCH 4/6] Communicated game logic over socketio server --- client/src/components/Game.js | 597 +++++++++++++++++++++++++--------- 1 file changed, 445 insertions(+), 152 deletions(-) diff --git a/client/src/components/Game.js b/client/src/components/Game.js index 1b6db80..75fa80c 100644 --- a/client/src/components/Game.js +++ b/client/src/components/Game.js @@ -70,14 +70,51 @@ const Game = () => { const drawCardPile = shuffledCards //set initial state - setGameOver(false) - setTurn('Player 1') - setPlayer1Deck([...player1Deck]) - setPlayer2Deck([...player2Deck]) - setCurrentColor(playedCardsPile[0].charAt(1)) - setCurrentNumber(playedCardsPile[0].charAt(0)) - setPlayedCardsPile([...playedCardsPile]) - setDrawCardPile([...drawCardPile]) + // setGameOver(false) + // setTurn('Player 1') + // setPlayer1Deck([...player1Deck]) + // setPlayer2Deck([...player2Deck]) + // setCurrentColor(playedCardsPile[0].charAt(1)) + // setCurrentNumber(playedCardsPile[0].charAt(0)) + // setPlayedCardsPile([...playedCardsPile]) + // setDrawCardPile([...drawCardPile]) + //send this state to server + socket.emit('initGameState', { + gameOver: false, + turn: 'Player 1', + player1Deck: [...player1Deck], + player2Deck: [...player2Deck], + currentColor: playedCardsPile[0].charAt(1), + currentNumber: playedCardsPile[0].charAt(0), + playedCardsPile: [...playedCardsPile], + drawCardPile: [...drawCardPile] + }) + }, []) + + useEffect(() => { + socket.on('initGameState', ({ gameOver, turn, player1Deck, player2Deck, currentColor, currentNumber, playedCardsPile, drawCardPile }) => { + setGameOver(gameOver) + setTurn(turn) + setPlayer1Deck(player1Deck) + setPlayer2Deck(player2Deck) + setCurrentColor(currentColor) + setCurrentNumber(currentNumber) + setPlayedCardsPile(playedCardsPile) + setDrawCardPile(drawCardPile) + }) + + socket.on('updateGameState', ({ gameOver, winner, turn, player1Deck, player2Deck, currentColor, currentNumber, playedCardsPile, drawCardPile }) => { + gameOver && setGameOver(gameOver) + winner && setWinner(winner) + turn && setTurn(turn) + player1Deck && setPlayer1Deck(player1Deck) + player2Deck && setPlayer2Deck(player2Deck) + currentColor && setCurrentColor(currentColor) + currentNumber && setCurrentNumber(currentNumber) + playedCardsPile && setPlayedCardsPile(playedCardsPile) + drawCardPile && setDrawCardPile(drawCardPile) + }) + }, []) //some util functions @@ -107,26 +144,46 @@ const Game = () => { //then update turn, currentColor and currentNumber const removeIndex = player1Deck.indexOf(played_card) //set new state - setGameOver(checkGameOver(player1Deck)) - setWinner(checkWinner(player1Deck, 'Player 1')) - setTurn('Player 2') - setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)]) - setPlayer1Deck([...player1Deck.slice(0, removeIndex), ...player1Deck.slice(removeIndex + 1)]) - setCurrentColor(colorOfPlayedCard) - setCurrentNumber(numberOfPlayedCard) + // setGameOver(checkGameOver(player1Deck)) + // setWinner(checkWinner(player1Deck, 'Player 1')) + // setTurn('Player 2') + // setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)]) + // setPlayer1Deck([...player1Deck.slice(0, removeIndex), ...player1Deck.slice(removeIndex + 1)]) + // setCurrentColor(colorOfPlayedCard) + // setCurrentNumber(numberOfPlayedCard) + //send this state to server + socket.emit('updateGameState', { + gameOver: checkGameOver(player1Deck), + winner: checkWinner(player1Deck, 'Player 1'), + turn: 'Player 2', + playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)], + player1Deck: [...player1Deck.slice(0, removeIndex), ...player1Deck.slice(removeIndex + 1)], + currentColor: colorOfPlayedCard, + currentNumber: numberOfPlayedCard + }) } else { //remove the played card from player2's deck and add it to playedCardsPile (immutably) //then update turn, currentColor and currentNumber const removeIndex = player2Deck.indexOf(played_card) //set new state - setGameOver(checkGameOver(player2Deck)) - setWinner(checkWinner(player1Deck, 'Player 2')) - setTurn('Player 1') - setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)]) - setPlayer2Deck([...player2Deck.slice(0, removeIndex), ...player2Deck.slice(removeIndex + 1)]) - setCurrentColor(colorOfPlayedCard) - setCurrentNumber(numberOfPlayedCard) + // setGameOver(checkGameOver(player2Deck)) + // setWinner(checkWinner(player2Deck, 'Player 2')) + // setTurn('Player 1') + // setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)]) + // setPlayer2Deck([...player2Deck.slice(0, removeIndex), ...player2Deck.slice(removeIndex + 1)]) + // setCurrentColor(colorOfPlayedCard) + // setCurrentNumber(numberOfPlayedCard) + //send this state to server + socket.emit('updateGameState', { + gameOver: checkGameOver(player2Deck), + winner: checkWinner(player2Deck, 'Player 2'), + turn: 'Player 1', + playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)], + player2Deck: [...player2Deck.slice(0, removeIndex), ...player2Deck.slice(removeIndex + 1)], + currentColor: colorOfPlayedCard, + currentNumber: numberOfPlayedCard + }) } } //check for number match @@ -138,13 +195,23 @@ const Game = () => { //then update turn, currentColor and currentNumber const removeIndex = player1Deck.indexOf(played_card) //set new state - setGameOver(checkGameOver(player1Deck)) - setWinner(checkWinner(player1Deck, 'Player 1')) - setTurn('Player 2') - setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)]) - setPlayer1Deck([...player1Deck.slice(0, removeIndex), ...player1Deck.slice(removeIndex + 1)]) - setCurrentColor(colorOfPlayedCard) - setCurrentNumber(numberOfPlayedCard) + // setGameOver(checkGameOver(player1Deck)) + // setWinner(checkWinner(player1Deck, 'Player 1')) + // setTurn('Player 2') + // setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)]) + // setPlayer1Deck([...player1Deck.slice(0, removeIndex), ...player1Deck.slice(removeIndex + 1)]) + // setCurrentColor(colorOfPlayedCard) + // setCurrentNumber(numberOfPlayedCard) + //send this state to server + socket.emit('updateGameState', { + gameOver: checkGameOver(player1Deck), + winner: checkWinner(player1Deck, 'Player 1'), + turn: 'Player 2', + playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)], + player1Deck: [...player1Deck.slice(0, removeIndex), ...player1Deck.slice(removeIndex + 1)], + currentColor: colorOfPlayedCard, + currentNumber: numberOfPlayedCard + }) } else { //remove the played card from player2's deck and add it to playedCardsPile (immutably) @@ -152,12 +219,22 @@ const Game = () => { const removeIndex = player2Deck.indexOf(played_card) //set new state setGameOver(checkGameOver(player2Deck)) - setWinner(checkWinner(player1Deck, 'Player 2')) + setWinner(checkWinner(player2Deck, 'Player 2')) setTurn('Player 1') setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)]) setPlayer2Deck([...player2Deck.slice(0, removeIndex), ...player2Deck.slice(removeIndex + 1)]) setCurrentColor(colorOfPlayedCard) setCurrentNumber(numberOfPlayedCard) + //send this state to server + socket.emit('updateGameState', { + gameOver: checkGameOver(player2Deck), + winner: checkWinner(player2Deck, 'Player 2'), + turn: 'Player 1', + playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)], + player2Deck: [...player2Deck.slice(0, removeIndex), ...player2Deck.slice(removeIndex + 1)], + currentColor: colorOfPlayedCard, + currentNumber: numberOfPlayedCard + }) } } @@ -180,24 +257,42 @@ const Game = () => { //then update currentColor and currentNumber const removeIndex = player1Deck.indexOf(played_card) //set new state - setGameOver(checkGameOver(player1Deck)) - setWinner(checkWinner(player1Deck, 'Player 1')) - setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)]) - setPlayer1Deck([...player1Deck.slice(0, removeIndex), ...player1Deck.slice(removeIndex + 1)]) - setCurrentColor(colorOfPlayedCard) - setCurrentNumber(404) + // setGameOver(checkGameOver(player1Deck)) + // setWinner(checkWinner(player1Deck, 'Player 1')) + // setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)]) + // setPlayer1Deck([...player1Deck.slice(0, removeIndex), ...player1Deck.slice(removeIndex + 1)]) + // setCurrentColor(colorOfPlayedCard) + // setCurrentNumber(404) + //send this state to server + socket.emit('updateGameState', { + gameOver: checkGameOver(player1Deck), + winner: checkWinner(player1Deck, 'Player 1'), + playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)], + player1Deck: [...player1Deck.slice(0, removeIndex), ...player1Deck.slice(removeIndex + 1)], + currentColor: colorOfPlayedCard, + currentNumber: 404 + }) } else { //remove the played card from player2's deck and add it to playedCardsPile (immutably) //then update currentColor and currentNumber const removeIndex = player2Deck.indexOf(played_card) //set new state - setGameOver(checkGameOver(player2Deck)) - setWinner(checkWinner(player1Deck, 'Player 2')) - setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)]) - setPlayer2Deck([...player2Deck.slice(0, removeIndex), ...player2Deck.slice(removeIndex + 1)]) - setCurrentColor(colorOfPlayedCard) - setCurrentNumber(404) + // setGameOver(checkGameOver(player2Deck)) + // setWinner(checkWinner(player2Deck, 'Player 2')) + // setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)]) + // setPlayer2Deck([...player2Deck.slice(0, removeIndex), ...player2Deck.slice(removeIndex + 1)]) + // setCurrentColor(colorOfPlayedCard) + // setCurrentNumber(404) + //send this state to server + socket.emit('updateGameState', { + gameOver: checkGameOver(player2Deck), + winner: checkWinner(player2Deck, 'Player 2'), + playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)], + player2Deck: [...player2Deck.slice(0, removeIndex), ...player2Deck.slice(removeIndex + 1)], + currentColor: colorOfPlayedCard, + currentNumber: 404 + }) } } //check for number match - if skip card played on skip card @@ -209,24 +304,42 @@ const Game = () => { //then update currentColor and currentNumber - turn will remain same const removeIndex = player1Deck.indexOf(played_card) //set new state - setGameOver(checkGameOver(player1Deck)) - setWinner(checkWinner(player1Deck, 'Player 1')) - setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)]) - setPlayer1Deck([...player1Deck.slice(0, removeIndex), ...player1Deck.slice(removeIndex + 1)]) - setCurrentColor(colorOfPlayedCard) - setCurrentNumber(404) + // setGameOver(checkGameOver(player1Deck)) + // setWinner(checkWinner(player1Deck, 'Player 1')) + // setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)]) + // setPlayer1Deck([...player1Deck.slice(0, removeIndex), ...player1Deck.slice(removeIndex + 1)]) + // setCurrentColor(colorOfPlayedCard) + // setCurrentNumber(404) + //send this state to server + socket.emit('updateGameState', { + gameOver: checkGameOver(player1Deck), + winner: checkWinner(player1Deck, 'Player 1'), + playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)], + player1Deck: [...player1Deck.slice(0, removeIndex), ...player1Deck.slice(removeIndex + 1)], + currentColor: colorOfPlayedCard, + currentNumber: 404 + }) } else { //remove the played card from player2's deck and add it to playedCardsPile (immutably) //then update currentColor and currentNumber - turn will remain same const removeIndex = player2Deck.indexOf(played_card) //set new state - setGameOver(checkGameOver(player2Deck)) - setWinner(checkWinner(player1Deck, 'Player 2')) - setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)]) - setPlayer2Deck([...player2Deck.slice(0, removeIndex), ...player2Deck.slice(removeIndex + 1)]) - setCurrentColor(colorOfPlayedCard) - setCurrentNumber(404) + // setGameOver(checkGameOver(player2Deck)) + // setWinner(checkWinner(player2Deck, 'Player 2')) + // setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)]) + // setPlayer2Deck([...player2Deck.slice(0, removeIndex), ...player2Deck.slice(removeIndex + 1)]) + // setCurrentColor(colorOfPlayedCard) + // setCurrentNumber(404) + //send this state to server + socket.emit('updateGameState', { + gameOver: checkGameOver(player2Deck), + winner: checkWinner(player2Deck, 'Player 2'), + playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)], + player2Deck: [...player2Deck.slice(0, removeIndex), ...player2Deck.slice(removeIndex + 1)], + currentColor: colorOfPlayedCard, + currentNumber: 404 + }) } } //if no color or number match, invalid move - do not update state @@ -262,6 +375,17 @@ const Game = () => { setCurrentColor(colorOfPlayedCard) setCurrentNumber(252) setDrawCardPile([...copiedDrawCardPileArray]) + //send this state to server + socket.emit('updateGameState', { + gameOver: checkGameOver(player1Deck), + winner: checkWinner(player1Deck, 'Player 1'), + playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)], + player1Deck: [...player1Deck.slice(0, removeIndex), ...player1Deck.slice(removeIndex + 1)], + player2Deck: [...player2Deck.slice(0, player2Deck.length), drawCard1, drawCard2, ...player2Deck.slice(player2Deck.length)], + currentColor: colorOfPlayedCard, + currentNumber: 252, + drawCardPile: [...copiedDrawCardPileArray] + }) } else { //remove the played card from player2's deck and add it to playedCardsPile (immutably) @@ -274,14 +398,25 @@ const Game = () => { const drawCard1 = copiedDrawCardPileArray.pop() const drawCard2 = copiedDrawCardPileArray.pop() //set new state - setGameOver(checkGameOver(player2Deck)) - setWinner(checkWinner(player1Deck, 'Player 2')) - setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)]) - setPlayer2Deck([...player2Deck.slice(0, removeIndex), ...player2Deck.slice(removeIndex + 1)]) - setPlayer1Deck([...player1Deck.slice(0, player1Deck.length), drawCard1, drawCard2, ...player1Deck.slice(player1Deck.length)]) - setCurrentColor(colorOfPlayedCard) - setCurrentNumber(252) - setDrawCardPile([...copiedDrawCardPileArray]) + // setGameOver(checkGameOver(player2Deck)) + // setWinner(checkWinner(player2Deck, 'Player 2')) + // setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)]) + // setPlayer2Deck([...player2Deck.slice(0, removeIndex), ...player2Deck.slice(removeIndex + 1)]) + // setPlayer1Deck([...player1Deck.slice(0, player1Deck.length), drawCard1, drawCard2, ...player1Deck.slice(player1Deck.length)]) + // setCurrentColor(colorOfPlayedCard) + // setCurrentNumber(252) + // setDrawCardPile([...copiedDrawCardPileArray]) + //send this state to server + socket.emit('updateGameState', { + gameOver: checkGameOver(player2Deck), + winner: checkWinner(player2Deck, 'Player 2'), + playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)], + player2Deck: [...player2Deck.slice(0, removeIndex), ...player2Deck.slice(removeIndex + 1)], + player1Deck: [...player1Deck.slice(0, player1Deck.length), drawCard1, drawCard2, ...player1Deck.slice(player1Deck.length)], + currentColor: colorOfPlayedCard, + currentNumber: 252, + drawCardPile: [...copiedDrawCardPileArray] + }) } } //check for number match - if draw 2 card played on draw 2 card @@ -299,14 +434,25 @@ const Game = () => { const drawCard1 = copiedDrawCardPileArray.pop() const drawCard2 = copiedDrawCardPileArray.pop() //set new state - setGameOver(checkGameOver(player1Deck)) - setWinner(checkWinner(player1Deck, 'Player 1')) - setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)]) - setPlayer1Deck([...player1Deck.slice(0, removeIndex), ...player1Deck.slice(removeIndex + 1)]) - setPlayer2Deck([...player2Deck.slice(0, player2Deck.length), drawCard1, drawCard2, ...player2Deck.slice(player2Deck.length)]) - setCurrentColor(colorOfPlayedCard) - setCurrentNumber(252) - setDrawCardPile([...copiedDrawCardPileArray]) + // setGameOver(checkGameOver(player1Deck)) + // setWinner(checkWinner(player1Deck, 'Player 1')) + // setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)]) + // setPlayer1Deck([...player1Deck.slice(0, removeIndex), ...player1Deck.slice(removeIndex + 1)]) + // setPlayer2Deck([...player2Deck.slice(0, player2Deck.length), drawCard1, drawCard2, ...player2Deck.slice(player2Deck.length)]) + // setCurrentColor(colorOfPlayedCard) + // setCurrentNumber(252) + // setDrawCardPile([...copiedDrawCardPileArray]) + //send this state to server + socket.emit('updateGameState', { + gameOver: checkGameOver(player1Deck), + winner: checkWinner(player1Deck, 'Player 1'), + playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)], + player1Deck: [...player1Deck.slice(0, removeIndex), ...player1Deck.slice(removeIndex + 1)], + player2Deck: [...player2Deck.slice(0, player2Deck.length), drawCard1, drawCard2, ...player2Deck.slice(player2Deck.length)], + currentColor: colorOfPlayedCard, + currentNumber: 252, + drawCardPile: [...copiedDrawCardPileArray] + }) } else { //remove the played card from player2's deck and add it to playedCardsPile (immutably) @@ -319,14 +465,25 @@ const Game = () => { const drawCard1 = copiedDrawCardPileArray.pop() const drawCard2 = copiedDrawCardPileArray.pop() //set new state - setGameOver(checkGameOver(player2Deck)) - setWinner(checkWinner(player1Deck, 'Player 2')) - setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)]) - setPlayer2Deck([...player2Deck.slice(0, removeIndex), ...player2Deck.slice(removeIndex + 1)]) - setPlayer1Deck([...player1Deck.slice(0, player1Deck.length), drawCard1, drawCard2, ...player1Deck.slice(player1Deck.length)]) - setCurrentColor(colorOfPlayedCard) - setCurrentNumber(252) - setDrawCardPile([...copiedDrawCardPileArray]) + // setGameOver(checkGameOver(player2Deck)) + // setWinner(checkWinner(player2Deck, 'Player 2')) + // setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)]) + // setPlayer2Deck([...player2Deck.slice(0, removeIndex), ...player2Deck.slice(removeIndex + 1)]) + // setPlayer1Deck([...player1Deck.slice(0, player1Deck.length), drawCard1, drawCard2, ...player1Deck.slice(player1Deck.length)]) + // setCurrentColor(colorOfPlayedCard) + // setCurrentNumber(252) + // setDrawCardPile([...copiedDrawCardPileArray]) + //send this state to server + socket.emit('updateGameState', { + gameOver: checkGameOver(player2Deck), + winner: checkWinner(player2Deck, 'Player 2'), + playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)], + player2Deck: [...player2Deck.slice(0, removeIndex), ...player2Deck.slice(removeIndex + 1)], + player1Deck: [...player1Deck.slice(0, player1Deck.length), drawCard1, drawCard2, ...player1Deck.slice(player1Deck.length)], + currentColor: colorOfPlayedCard, + currentNumber: 252, + drawCardPile: [...copiedDrawCardPileArray] + }) } } //if no color or number match, invalid move - do not update state @@ -352,6 +509,16 @@ const Game = () => { setPlayer1Deck([...player1Deck.slice(0, removeIndex), ...player1Deck.slice(removeIndex + 1)]) setCurrentColor(newColor) setCurrentNumber(300) + //send this state to server + socket.emit('updateGameState', { + gameOver: checkGameOver(player1Deck), + winner: checkWinner(player1Deck, 'Player 1'), + turn: 'Player 2', + playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)], + player1Deck: [...player1Deck.slice(0, removeIndex), ...player1Deck.slice(removeIndex + 1)], + currentColor: newColor, + currentNumber: 300 + }) } else { //ask for new color @@ -360,13 +527,23 @@ const Game = () => { const removeIndex = player2Deck.indexOf(played_card) //then update turn, currentColor and currentNumber //set new state - setGameOver(checkGameOver(player2Deck)) - setWinner(checkWinner(player1Deck, 'Player 1')) - setTurn('Player 1') - setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)]) - setPlayer2Deck([...player2Deck.slice(0, removeIndex), ...player2Deck.slice(removeIndex + 1)]) - setCurrentColor(newColor) - setCurrentNumber(300) + // setGameOver(checkGameOver(player2Deck)) + // setWinner(checkWinner(player2Deck, 'Player 2')) + // setTurn('Player 1') + // setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)]) + // setPlayer2Deck([...player2Deck.slice(0, removeIndex), ...player2Deck.slice(removeIndex + 1)]) + // setCurrentColor(newColor) + // setCurrentNumber(300) + //send this state to server + socket.emit('updateGameState', { + gameOver: checkGameOver(player2Deck), + winner: checkWinner(player2Deck, 'Player 2'), + turn: 'Player 2', + playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)], + player2Deck: [...player2Deck.slice(0, removeIndex), ...player2Deck.slice(removeIndex + 1)], + currentColor: newColor, + currentNumber: 300 + }) } break; } @@ -388,14 +565,25 @@ const Game = () => { const drawCard4 = copiedDrawCardPileArray.pop() //then update currentColor and currentNumber - turn will remain same //set new state - setGameOver(checkGameOver(player1Deck)) - setWinner(checkWinner(player1Deck, 'Player 1')) - setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)]) - setPlayer1Deck([...player1Deck.slice(0, removeIndex), ...player1Deck.slice(removeIndex + 1)]) - setPlayer2Deck([...player2Deck.slice(0, player2Deck.length), drawCard1, drawCard2, drawCard3, drawCard4, ...player2Deck.slice(player2Deck.length)]) - setCurrentColor(newColor) - setCurrentNumber(600) - setDrawCardPile([...copiedDrawCardPileArray]) + // setGameOver(checkGameOver(player1Deck)) + // setWinner(checkWinner(player1Deck, 'Player 1')) + // setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)]) + // setPlayer1Deck([...player1Deck.slice(0, removeIndex), ...player1Deck.slice(removeIndex + 1)]) + // setPlayer2Deck([...player2Deck.slice(0, player2Deck.length), drawCard1, drawCard2, drawCard3, drawCard4, ...player2Deck.slice(player2Deck.length)]) + // setCurrentColor(newColor) + // setCurrentNumber(600) + // setDrawCardPile([...copiedDrawCardPileArray]) + //send this state to server + socket.emit('updateGameState', { + gameOver: checkGameOver(player1Deck), + winner: checkWinner(player1Deck, 'Player 1'), + playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)], + player1Deck: [...player1Deck.slice(0, removeIndex), ...player1Deck.slice(removeIndex + 1)], + player2Deck: [...player2Deck.slice(0, player2Deck.length), drawCard1, drawCard2, drawCard3, drawCard4, ...player2Deck.slice(player2Deck.length)], + currentColor: newColor, + currentNumber: 600, + drawCardPile: [...copiedDrawCardPileArray] + }) } else { //ask for new color @@ -412,14 +600,25 @@ const Game = () => { const drawCard4 = copiedDrawCardPileArray.pop() //then update currentColor and currentNumber - turn will remain same //set new state - setGameOver(checkGameOver(player2Deck)) - setWinner(checkWinner(player1Deck, 'Player 1')) - setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)]) - setPlayer2Deck([...player2Deck.slice(0, removeIndex), ...player2Deck.slice(removeIndex + 1)]) - setPlayer1Deck([...player1Deck.slice(0, player1Deck.length), drawCard1, drawCard2, drawCard3, drawCard4, ...player1Deck.slice(player1Deck.length)]) - setCurrentColor(newColor) - setCurrentNumber(600) - setDrawCardPile([...copiedDrawCardPileArray]) + // setGameOver(checkGameOver(player2Deck)) + // setWinner(checkWinner(player2Deck, 'Player 2')) + // setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)]) + // setPlayer2Deck([...player2Deck.slice(0, removeIndex), ...player2Deck.slice(removeIndex + 1)]) + // setPlayer1Deck([...player1Deck.slice(0, player1Deck.length), drawCard1, drawCard2, drawCard3, drawCard4, ...player1Deck.slice(player1Deck.length)]) + // setCurrentColor(newColor) + // setCurrentNumber(600) + // setDrawCardPile([...copiedDrawCardPileArray]) + //send this state to server + socket.emit('updateGameState', { + gameOver: checkGameOver(player2Deck), + winner: checkWinner(player2Deck, 'Player 2'), + playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)], + player2Deck: [...player2Deck.slice(0, removeIndex), ...player2Deck.slice(removeIndex + 1)], + player1Deck: [...player1Deck.slice(0, player1Deck.length), drawCard1, drawCard2, drawCard3, drawCard4, ...player1Deck.slice(player1Deck.length)], + currentColor: newColor, + currentNumber: 600, + drawCardPile: [...copiedDrawCardPileArray] + }) } } break; @@ -442,9 +641,17 @@ const Game = () => { if(colorOfDrawnCard === currentColor && (drawCard === 'skipR' || drawCard === 'skipG' || drawCard === 'skipB' || drawCard === 'skipY')) { alert(`You drew ${drawCard}. It was played for you.`) //set new state - setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), drawCard, ...playedCardsPile.slice(playedCardsPile.length)]) - setCurrentColor(colorOfDrawnCard) - setCurrentNumber(404) + // setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), drawCard, ...playedCardsPile.slice(playedCardsPile.length)]) + // setCurrentColor(colorOfDrawnCard) + // setCurrentNumber(404) + // setDrawCardPile([...copiedDrawCardPileArray]) + //send this state to server + socket.emit('updateGameState', { + playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), drawCard, ...playedCardsPile.slice(playedCardsPile.length)], + currentColor: colorOfDrawnCard, + currentNumber: 404, + drawCardPile: [...copiedDrawCardPileArray] + }) } else if(colorOfDrawnCard === currentColor && (drawCard === 'D2R' || drawCard === 'D2G' || drawCard === 'D2B' || drawCard === 'D2Y')) { alert(`You drew ${drawCard}. It was played for you.`) @@ -455,21 +662,38 @@ const Game = () => { const drawCard1 = copiedDrawCardPileArray.pop() const drawCard2 = copiedDrawCardPileArray.pop() //set new state - setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), drawCard, ...playedCardsPile.slice(playedCardsPile.length)]) - setPlayer2Deck([...player2Deck.slice(0, player2Deck.length), drawCard1, drawCard2, ...player2Deck.slice(player2Deck.length)]) - setCurrentColor(colorOfDrawnCard) - setCurrentNumber(252) - setDrawCardPile([...copiedDrawCardPileArray]) + // setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), drawCard, ...playedCardsPile.slice(playedCardsPile.length)]) + // setPlayer2Deck([...player2Deck.slice(0, player2Deck.length), drawCard1, drawCard2, ...player2Deck.slice(player2Deck.length)]) + // setCurrentColor(colorOfDrawnCard) + // setCurrentNumber(252) + // setDrawCardPile([...copiedDrawCardPileArray]) + //send this state to server + socket.emit('updateGameState', { + playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), drawCard, ...playedCardsPile.slice(playedCardsPile.length)], + player2Deck: [...player2Deck.slice(0, player2Deck.length), drawCard1, drawCard2, ...player2Deck.slice(player2Deck.length)], + currentColor: colorOfDrawnCard, + currentNumber: 252, + drawCardPile: [...copiedDrawCardPileArray] + }) } else if(drawCard === 'W') { alert(`You drew ${drawCard}. It was played for you.`) //ask for new color const newColor = prompt('Enter first letter of new color in uppercase (R/G/B/Y)') //set new state - setTurn('Player 2') - setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), drawCard, ...playedCardsPile.slice(playedCardsPile.length)]) - setCurrentColor(newColor) - setCurrentNumber(300) + // setTurn('Player 2') + // setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), drawCard, ...playedCardsPile.slice(playedCardsPile.length)]) + // setCurrentColor(newColor) + // setCurrentNumber(300) + // setDrawCardPile([...copiedDrawCardPileArray]) + //send this state to server + socket.emit('updateGameState', { + turn: 'Player 2', + playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), drawCard, ...playedCardsPile.slice(playedCardsPile.length)], + currentColor: newColor, + currentNumber: 300, + drawCardPile: [...copiedDrawCardPileArray] + }) } else if(drawCard === 'D4W') { alert(`You drew ${drawCard}. It was played for you.`) @@ -484,29 +708,51 @@ const Game = () => { const drawCard3 = copiedDrawCardPileArray.pop() const drawCard4 = copiedDrawCardPileArray.pop() //set new state - setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), drawCard, ...playedCardsPile.slice(playedCardsPile.length)]) - setPlayer1Deck([...player1Deck.slice(0, player1Deck.length), drawCard1, drawCard2, drawCard3, drawCard4, ...player1Deck.slice(player1Deck.length)]) - setCurrentColor(newColor) - setCurrentNumber(600) - setDrawCardPile([...copiedDrawCardPileArray]) + // setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), drawCard, ...playedCardsPile.slice(playedCardsPile.length)]) + // setPlayer1Deck([...player1Deck.slice(0, player1Deck.length), drawCard1, drawCard2, drawCard3, drawCard4, ...player1Deck.slice(player1Deck.length)]) + // setCurrentColor(newColor) + // setCurrentNumber(600) + // setDrawCardPile([...copiedDrawCardPileArray]) + //send this state to server + socket.emit('updateGameState', { + playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), drawCard, ...playedCardsPile.slice(playedCardsPile.length)], + player1Deck: [...player1Deck.slice(0, player1Deck.length), drawCard1, drawCard2, drawCard3, drawCard4, ...player1Deck.slice(player1Deck.length)], + currentColor: newColor, + currentNumber: 600, + drawCardPile: [...copiedDrawCardPileArray] + }) } //if not action card - check if drawn card is playable else if(numberOfDrawnCard === currentNumber || colorOfDrawnCard === currentColor) { alert(`You drew ${drawCard}. It was played for you.`) //set new state - setTurn('Player 2') - setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), drawCard, ...playedCardsPile.slice(playedCardsPile.length)]) - setCurrentColor(colorOfDrawnCard) - setCurrentNumber(numberOfDrawnCard) - setDrawCardPile([...copiedDrawCardPileArray]) + // setTurn('Player 2') + // setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), drawCard, ...playedCardsPile.slice(playedCardsPile.length)]) + // setCurrentColor(colorOfDrawnCard) + // setCurrentNumber(numberOfDrawnCard) + // setDrawCardPile([...copiedDrawCardPileArray]) + //send this state to server + socket.emit('updateGameState', { + turn: 'Player 2', + playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), drawCard, ...playedCardsPile.slice(playedCardsPile.length)], + currentColor: colorOfDrawnCard, + currentNumber: numberOfDrawnCard, + drawCardPile: [...copiedDrawCardPileArray] + }) } //else add the drawn card to player1's deck else { alert(`You drew ${drawCard}.`) //set new state - setTurn('Player 2') - setPlayer1Deck([...player1Deck.slice(0, player1Deck.length), drawCard, ...player1Deck.slice(player1Deck.length)]) - setDrawCardPile([...copiedDrawCardPileArray]) + // setTurn('Player 2') + // setPlayer1Deck([...player1Deck.slice(0, player1Deck.length), drawCard, ...player1Deck.slice(player1Deck.length)]) + // setDrawCardPile([...copiedDrawCardPileArray]) + //send this state to server + socket.emit('updateGameState', { + turn: 'Player 2', + player1Deck: [...player1Deck.slice(0, player1Deck.length), drawCard, ...player1Deck.slice(player1Deck.length)], + drawCardPile: [...copiedDrawCardPileArray] + }) } } else { @@ -521,9 +767,17 @@ const Game = () => { if(colorOfDrawnCard === currentColor && (drawCard === 'skipR' || drawCard === 'skipG' || drawCard === 'skipB' || drawCard === 'skipY')) { alert(`You drew ${drawCard}. It was played for you.`) //set new state - setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), drawCard, ...playedCardsPile.slice(playedCardsPile.length)]) - setCurrentColor(colorOfDrawnCard) - setCurrentNumber(404) + // setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), drawCard, ...playedCardsPile.slice(playedCardsPile.length)]) + // setCurrentColor(colorOfDrawnCard) + // setCurrentNumber(404) + // setDrawCardPile([...copiedDrawCardPileArray]) + //send this state to server + socket.emit('updateGameState', { + playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), drawCard, ...playedCardsPile.slice(playedCardsPile.length)], + currentColor: colorOfDrawnCard, + currentNumber: 404, + drawCardPile: [...copiedDrawCardPileArray] + }) } else if(colorOfDrawnCard === currentColor && (drawCard === 'D2R' || drawCard === 'D2G' || drawCard === 'D2B' || drawCard === 'D2Y')) { alert(`You drew ${drawCard}. It was played for you.`) @@ -534,21 +788,38 @@ const Game = () => { const drawCard1 = copiedDrawCardPileArray.pop() const drawCard2 = copiedDrawCardPileArray.pop() //set new state - setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), drawCard, ...playedCardsPile.slice(playedCardsPile.length)]) - setPlayer2Deck([...player2Deck.slice(0, player2Deck.length), drawCard1, drawCard2, ...player2Deck.slice(player2Deck.length)]) - setCurrentColor(colorOfDrawnCard) - setCurrentNumber(252) - setDrawCardPile([...copiedDrawCardPileArray]) + // setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), drawCard, ...playedCardsPile.slice(playedCardsPile.length)]) + // setPlayer2Deck([...player2Deck.slice(0, player2Deck.length), drawCard1, drawCard2, ...player2Deck.slice(player2Deck.length)]) + // setCurrentColor(colorOfDrawnCard) + // setCurrentNumber(252) + // setDrawCardPile([...copiedDrawCardPileArray]) + //send this state to server + socket.emit('updateGameState', { + playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), drawCard, ...playedCardsPile.slice(playedCardsPile.length)], + player2Deck: [...player2Deck.slice(0, player2Deck.length), drawCard1, drawCard2, ...player2Deck.slice(player2Deck.length)], + currentColor: colorOfDrawnCard, + currentNumber: 252, + drawCardPile: [...copiedDrawCardPileArray] + }) } else if(drawCard === 'W') { alert(`You drew ${drawCard}. It was played for you.`) //ask for new color const newColor = prompt('Enter first letter of new color in uppercase (R/G/B/Y)') //set new state - setTurn('Player 1') - setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), drawCard, ...playedCardsPile.slice(playedCardsPile.length)]) - setCurrentColor(newColor) - setCurrentNumber(300) + // setTurn('Player 1') + // setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), drawCard, ...playedCardsPile.slice(playedCardsPile.length)]) + // setCurrentColor(newColor) + // setCurrentNumber(300) + // setDrawCardPile([...copiedDrawCardPileArray]) + //send this state to server + socket.emit('updateGameState', { + turn: 'Player 1', + playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), drawCard, ...playedCardsPile.slice(playedCardsPile.length)], + currentColor: newColor, + currentNumber: 300, + drawCardPile: [...copiedDrawCardPileArray] + }) } else if(drawCard === 'D4W') { alert(`You drew ${drawCard}. It was played for you.`) @@ -563,35 +834,57 @@ const Game = () => { const drawCard3 = copiedDrawCardPileArray.pop() const drawCard4 = copiedDrawCardPileArray.pop() //set new state - setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), drawCard, ...playedCardsPile.slice(playedCardsPile.length)]) - setPlayer1Deck([...player1Deck.slice(0, player1Deck.length), drawCard1, drawCard2, drawCard3, drawCard4, ...player1Deck.slice(player1Deck.length)]) - setCurrentColor(newColor) - setCurrentNumber(600) - setDrawCardPile([...copiedDrawCardPileArray]) + // setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), drawCard, ...playedCardsPile.slice(playedCardsPile.length)]) + // setPlayer1Deck([...player1Deck.slice(0, player1Deck.length), drawCard1, drawCard2, drawCard3, drawCard4, ...player1Deck.slice(player1Deck.length)]) + // setCurrentColor(newColor) + // setCurrentNumber(600) + // setDrawCardPile([...copiedDrawCardPileArray]) + //send this state to server + socket.emit('updateGameState', { + playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), drawCard, ...playedCardsPile.slice(playedCardsPile.length)], + player1Deck: [...player1Deck.slice(0, player1Deck.length), drawCard1, drawCard2, drawCard3, drawCard4, ...player1Deck.slice(player1Deck.length)], + currentColor: newColor, + currentNumber: 600, + drawCardPile: [...copiedDrawCardPileArray] + }) } //if not action card - check if drawn card is playable else if(numberOfDrawnCard === currentNumber || colorOfDrawnCard === currentColor) { alert(`You drew ${drawCard}. It was played for you.`) //set new state - setTurn('Player 1') - setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), drawCard, ...playedCardsPile.slice(playedCardsPile.length)]) - setCurrentColor(colorOfDrawnCard) - setCurrentNumber(numberOfDrawnCard) - setDrawCardPile([...copiedDrawCardPileArray]) + // setTurn('Player 1') + // setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), drawCard, ...playedCardsPile.slice(playedCardsPile.length)]) + // setCurrentColor(colorOfDrawnCard) + // setCurrentNumber(numberOfDrawnCard) + // setDrawCardPile([...copiedDrawCardPileArray]) + //send this state to server + socket.emit('updateGameState', { + turn: 'Player 1', + playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), drawCard, ...playedCardsPile.slice(playedCardsPile.length)], + currentColor: colorOfDrawnCard, + currentNumber: numberOfDrawnCard, + drawCardPile: [...copiedDrawCardPileArray] + }) } //else add the drawn card to player2's deck else { alert(`You drew ${drawCard}.`) //set new state - setTurn('Player 1') - setPlayer2Deck([...player2Deck.slice(0, player2Deck.length), drawCard, ...player2Deck.slice(player2Deck.length)]) - setDrawCardPile([...copiedDrawCardPileArray]) + // setTurn('Player 1') + // setPlayer2Deck([...player2Deck.slice(0, player2Deck.length), drawCard, ...player2Deck.slice(player2Deck.length)]) + // setDrawCardPile([...copiedDrawCardPileArray]) + //send this state to server + socket.emit('updateGameState', { + turn: 'Player 1', + player2Deck: [...player2Deck.slice(0, player2Deck.length), drawCard, ...player2Deck.slice(player2Deck.length)], + drawCardPile: [...copiedDrawCardPileArray] + }) } } } return ( - gameOver ?

GAME FORFEITED

{winner !== '' && <>

GAME OVER

{winner} wins!

}Home
: + gameOver ?
{winner !== '' && <>

GAME OVER

{winner} wins!

}Home
:

Turn: {turn}

From 6d064cec4b970986263e512fb9230d870a067bea Mon Sep 17 00:00:00 2001 From: Mizanali Panjwani Date: Mon, 22 Feb 2021 06:33:25 +0530 Subject: [PATCH 5/6] Communicated game logic over socketio server --- server.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/server.js b/server.js index e107c69..3f94a02 100644 --- a/server.js +++ b/server.js @@ -14,6 +14,14 @@ app.use(cors()) io.on('connection', socket => { console.log('connection made') + socket.on('initGameState', gameState => { + io.emit('initGameState', gameState) + }) + + socket.on('updateGameState', gameState => { + io.emit('updateGameState', gameState) + }) + socket.on('disconnect', () => { console.log('connection lost') }) From 183229e79134c1989ce252fd9c3c2dc6b12c74ab Mon Sep 17 00:00:00 2001 From: Mizanali Panjwani Date: Mon, 22 Feb 2021 21:04:17 +0530 Subject: [PATCH 6/6] Working two-player game ready! --- client/package-lock.json | 44 +++- client/package.json | 1 + client/src/components/Game.js | 327 ++++++++---------------------- client/src/components/Homepage.js | 7 +- package.json | 3 +- server.js | 32 ++- users.js | 28 +++ 7 files changed, 181 insertions(+), 261 deletions(-) create mode 100644 users.js diff --git a/client/package-lock.json b/client/package-lock.json index aced44e..c21ee54 100644 --- a/client/package-lock.json +++ b/client/package-lock.json @@ -6408,6 +6408,11 @@ "to-regex-range": "^5.0.1" } }, + "filter-obj": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/filter-obj/-/filter-obj-1.1.0.tgz", + "integrity": "sha1-mzERErxsYSehbgFsbF1/GeCAXFs=" + }, "finalhandler": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-1.1.2.tgz", @@ -10314,6 +10319,22 @@ "prepend-http": "^1.0.0", "query-string": "^4.1.0", "sort-keys": "^1.0.0" + }, + "dependencies": { + "query-string": { + "version": "4.3.4", + "resolved": "https://registry.npmjs.org/query-string/-/query-string-4.3.4.tgz", + "integrity": "sha1-u7aTucqRXCMlFbIosaArYJBD2+s=", + "requires": { + "object-assign": "^4.1.0", + "strict-uri-encode": "^1.0.0" + } + }, + "strict-uri-encode": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/strict-uri-encode/-/strict-uri-encode-1.1.0.tgz", + "integrity": "sha1-J5siXfHVgrH1TmWt3UNS4Y+qBxM=" + } } }, "npm-run-path": { @@ -12134,12 +12155,14 @@ "integrity": "sha512-N5ZAX4/LxJmF+7wN74pUD6qAh9/wnvdQcjq9TZjevvXzSUo7bfmw91saqMjzGS2xq91/odN2dW/WOl7qQHNDGA==" }, "query-string": { - "version": "4.3.4", - "resolved": "https://registry.npmjs.org/query-string/-/query-string-4.3.4.tgz", - "integrity": "sha1-u7aTucqRXCMlFbIosaArYJBD2+s=", + "version": "6.14.0", + "resolved": "https://registry.npmjs.org/query-string/-/query-string-6.14.0.tgz", + "integrity": "sha512-In3o+lUxlgejoVJgwEdYtdxrmlL0cQWJXj0+kkI7RWVo7hg5AhFtybeKlC9Dpgbr8eOC4ydpEh8017WwyfzqVQ==", "requires": { - "object-assign": "^4.1.0", - "strict-uri-encode": "^1.0.0" + "decode-uri-component": "^0.2.0", + "filter-obj": "^1.1.0", + "split-on-first": "^1.0.0", + "strict-uri-encode": "^2.0.0" } }, "querystring": { @@ -13853,6 +13876,11 @@ "wbuf": "^1.7.3" } }, + "split-on-first": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/split-on-first/-/split-on-first-1.1.0.tgz", + "integrity": "sha512-43ZssAJaMusuKWL8sKUBQXHWOpq8d6CfN/u1p4gUzfJkM05C8rxTmYrkIPTXapZpORA6LkkzcUulJ8FqA7Uudw==" + }, "split-string": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/split-string/-/split-string-3.1.0.tgz", @@ -14028,9 +14056,9 @@ "integrity": "sha512-AiisoFqQ0vbGcZgQPY1cdP2I76glaVA/RauYR4G4thNFgkTqr90yXTo4LYX60Jl+sIlPNHHdGSwo01AvbKUSVQ==" }, "strict-uri-encode": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/strict-uri-encode/-/strict-uri-encode-1.1.0.tgz", - "integrity": "sha1-J5siXfHVgrH1TmWt3UNS4Y+qBxM=" + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/strict-uri-encode/-/strict-uri-encode-2.0.0.tgz", + "integrity": "sha1-ucczDHBChi9rFC3CdLvMWGbONUY=" }, "string-length": { "version": "4.0.1", diff --git a/client/package.json b/client/package.json index 9014a7d..4b7644d 100644 --- a/client/package.json +++ b/client/package.json @@ -6,6 +6,7 @@ "@testing-library/jest-dom": "^5.11.9", "@testing-library/react": "^11.2.5", "@testing-library/user-event": "^12.7.0", + "query-string": "^6.14.0", "react": "^17.0.1", "react-dom": "^17.0.1", "react-router-dom": "^5.2.0", diff --git a/client/src/components/Game.js b/client/src/components/Game.js index 75fa80c..35cc0e1 100644 --- a/client/src/components/Game.js +++ b/client/src/components/Game.js @@ -2,6 +2,7 @@ import React, { useEffect, useState } from 'react' import PACK_OF_CARDS from '../utils/packOfCards' import shuffleArray from '../utils/shuffleArray' import io from 'socket.io-client' +import queryString from 'query-string' //NUMBER CODES FOR ACTION CARDS //SKIP - 404 @@ -12,8 +13,14 @@ import io from 'socket.io-client' let socket const ENDPOINT = 'http://localhost:5000' -const Game = () => { +const Game = (props) => { + const data = queryString.parse(props.location.search) + //initialize socket state + const [room, setRoom] = useState(data.roomCode) + const [roomFull, setRoomFull] = useState(false) + const [users, setUsers] = useState([]) + const [currentUser, setCurrentUser] = useState('') useEffect(() => { const connectionOptions = { @@ -24,6 +31,18 @@ const Game = () => { } socket = io.connect(ENDPOINT, connectionOptions) console.log(socket); + + socket.emit('join', {room: room}, (error) => { + if(error) + setRoomFull(true) + }) + + //cleanup on component unmount + return function cleanup() { + socket.emit('disconnect') + //shut down connnection instance + socket.off() + } }, []) //initialize game state @@ -69,16 +88,7 @@ const Game = () => { //store all remaining cards into drawCardPile const drawCardPile = shuffledCards - //set initial state - // setGameOver(false) - // setTurn('Player 1') - // setPlayer1Deck([...player1Deck]) - // setPlayer2Deck([...player2Deck]) - // setCurrentColor(playedCardsPile[0].charAt(1)) - // setCurrentNumber(playedCardsPile[0].charAt(0)) - // setPlayedCardsPile([...playedCardsPile]) - // setDrawCardPile([...drawCardPile]) - //send this state to server + //send initial state to server socket.emit('initGameState', { gameOver: false, turn: 'Player 1', @@ -115,6 +125,13 @@ const Game = () => { drawCardPile && setDrawCardPile(drawCardPile) }) + socket.on("roomData", ({ users }) => { + setUsers(users) + }) + + socket.on('currentUserData', ({ name }) => { + setCurrentUser(name) + }) }, []) //some util functions @@ -143,15 +160,7 @@ const Game = () => { //remove the played card from player1's deck and add it to playedCardsPile (immutably) //then update turn, currentColor and currentNumber const removeIndex = player1Deck.indexOf(played_card) - //set new state - // setGameOver(checkGameOver(player1Deck)) - // setWinner(checkWinner(player1Deck, 'Player 1')) - // setTurn('Player 2') - // setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)]) - // setPlayer1Deck([...player1Deck.slice(0, removeIndex), ...player1Deck.slice(removeIndex + 1)]) - // setCurrentColor(colorOfPlayedCard) - // setCurrentNumber(numberOfPlayedCard) - //send this state to server + //send new state to server socket.emit('updateGameState', { gameOver: checkGameOver(player1Deck), winner: checkWinner(player1Deck, 'Player 1'), @@ -166,15 +175,7 @@ const Game = () => { //remove the played card from player2's deck and add it to playedCardsPile (immutably) //then update turn, currentColor and currentNumber const removeIndex = player2Deck.indexOf(played_card) - //set new state - // setGameOver(checkGameOver(player2Deck)) - // setWinner(checkWinner(player2Deck, 'Player 2')) - // setTurn('Player 1') - // setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)]) - // setPlayer2Deck([...player2Deck.slice(0, removeIndex), ...player2Deck.slice(removeIndex + 1)]) - // setCurrentColor(colorOfPlayedCard) - // setCurrentNumber(numberOfPlayedCard) - //send this state to server + //send new state to server socket.emit('updateGameState', { gameOver: checkGameOver(player2Deck), winner: checkWinner(player2Deck, 'Player 2'), @@ -194,15 +195,7 @@ const Game = () => { //remove the played card from player1's deck and add it to playedCardsPile (immutably) //then update turn, currentColor and currentNumber const removeIndex = player1Deck.indexOf(played_card) - //set new state - // setGameOver(checkGameOver(player1Deck)) - // setWinner(checkWinner(player1Deck, 'Player 1')) - // setTurn('Player 2') - // setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)]) - // setPlayer1Deck([...player1Deck.slice(0, removeIndex), ...player1Deck.slice(removeIndex + 1)]) - // setCurrentColor(colorOfPlayedCard) - // setCurrentNumber(numberOfPlayedCard) - //send this state to server + //send new state to server socket.emit('updateGameState', { gameOver: checkGameOver(player1Deck), winner: checkWinner(player1Deck, 'Player 1'), @@ -217,15 +210,7 @@ const Game = () => { //remove the played card from player2's deck and add it to playedCardsPile (immutably) //then update turn, currentColor and currentNumber const removeIndex = player2Deck.indexOf(played_card) - //set new state - setGameOver(checkGameOver(player2Deck)) - setWinner(checkWinner(player2Deck, 'Player 2')) - setTurn('Player 1') - setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)]) - setPlayer2Deck([...player2Deck.slice(0, removeIndex), ...player2Deck.slice(removeIndex + 1)]) - setCurrentColor(colorOfPlayedCard) - setCurrentNumber(numberOfPlayedCard) - //send this state to server + //send new state to server socket.emit('updateGameState', { gameOver: checkGameOver(player2Deck), winner: checkWinner(player2Deck, 'Player 2'), @@ -237,7 +222,6 @@ const Game = () => { }) } } - //if no color or number match, invalid move - do not update state else { alert('Invalid Move!') @@ -256,14 +240,7 @@ const Game = () => { //remove the played card from player1's deck and add it to playedCardsPile (immutably) //then update currentColor and currentNumber const removeIndex = player1Deck.indexOf(played_card) - //set new state - // setGameOver(checkGameOver(player1Deck)) - // setWinner(checkWinner(player1Deck, 'Player 1')) - // setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)]) - // setPlayer1Deck([...player1Deck.slice(0, removeIndex), ...player1Deck.slice(removeIndex + 1)]) - // setCurrentColor(colorOfPlayedCard) - // setCurrentNumber(404) - //send this state to server + //send new state to server socket.emit('updateGameState', { gameOver: checkGameOver(player1Deck), winner: checkWinner(player1Deck, 'Player 1'), @@ -277,14 +254,7 @@ const Game = () => { //remove the played card from player2's deck and add it to playedCardsPile (immutably) //then update currentColor and currentNumber const removeIndex = player2Deck.indexOf(played_card) - //set new state - // setGameOver(checkGameOver(player2Deck)) - // setWinner(checkWinner(player2Deck, 'Player 2')) - // setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)]) - // setPlayer2Deck([...player2Deck.slice(0, removeIndex), ...player2Deck.slice(removeIndex + 1)]) - // setCurrentColor(colorOfPlayedCard) - // setCurrentNumber(404) - //send this state to server + //send new state to server socket.emit('updateGameState', { gameOver: checkGameOver(player2Deck), winner: checkWinner(player2Deck, 'Player 2'), @@ -303,14 +273,7 @@ const Game = () => { //remove the played card from player1's deck and add it to playedCardsPile (immutably) //then update currentColor and currentNumber - turn will remain same const removeIndex = player1Deck.indexOf(played_card) - //set new state - // setGameOver(checkGameOver(player1Deck)) - // setWinner(checkWinner(player1Deck, 'Player 1')) - // setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)]) - // setPlayer1Deck([...player1Deck.slice(0, removeIndex), ...player1Deck.slice(removeIndex + 1)]) - // setCurrentColor(colorOfPlayedCard) - // setCurrentNumber(404) - //send this state to server + //send new state to server socket.emit('updateGameState', { gameOver: checkGameOver(player1Deck), winner: checkWinner(player1Deck, 'Player 1'), @@ -324,14 +287,7 @@ const Game = () => { //remove the played card from player2's deck and add it to playedCardsPile (immutably) //then update currentColor and currentNumber - turn will remain same const removeIndex = player2Deck.indexOf(played_card) - //set new state - // setGameOver(checkGameOver(player2Deck)) - // setWinner(checkWinner(player2Deck, 'Player 2')) - // setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)]) - // setPlayer2Deck([...player2Deck.slice(0, removeIndex), ...player2Deck.slice(removeIndex + 1)]) - // setCurrentColor(colorOfPlayedCard) - // setCurrentNumber(404) - //send this state to server + //send new state to server socket.emit('updateGameState', { gameOver: checkGameOver(player2Deck), winner: checkWinner(player2Deck, 'Player 2'), @@ -366,16 +322,7 @@ const Game = () => { //pull out last two elements from it const drawCard1 = copiedDrawCardPileArray.pop() const drawCard2 = copiedDrawCardPileArray.pop() - //set new state - setGameOver(checkGameOver(player1Deck)) - setWinner(checkWinner(player1Deck, 'Player 1')) - setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)]) - setPlayer1Deck([...player1Deck.slice(0, removeIndex), ...player1Deck.slice(removeIndex + 1)]) - setPlayer2Deck([...player2Deck.slice(0, player2Deck.length), drawCard1, drawCard2, ...player2Deck.slice(player2Deck.length)]) - setCurrentColor(colorOfPlayedCard) - setCurrentNumber(252) - setDrawCardPile([...copiedDrawCardPileArray]) - //send this state to server + //send new state to server socket.emit('updateGameState', { gameOver: checkGameOver(player1Deck), winner: checkWinner(player1Deck, 'Player 1'), @@ -397,16 +344,6 @@ const Game = () => { //pull out last two elements from it const drawCard1 = copiedDrawCardPileArray.pop() const drawCard2 = copiedDrawCardPileArray.pop() - //set new state - // setGameOver(checkGameOver(player2Deck)) - // setWinner(checkWinner(player2Deck, 'Player 2')) - // setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)]) - // setPlayer2Deck([...player2Deck.slice(0, removeIndex), ...player2Deck.slice(removeIndex + 1)]) - // setPlayer1Deck([...player1Deck.slice(0, player1Deck.length), drawCard1, drawCard2, ...player1Deck.slice(player1Deck.length)]) - // setCurrentColor(colorOfPlayedCard) - // setCurrentNumber(252) - // setDrawCardPile([...copiedDrawCardPileArray]) - //send this state to server socket.emit('updateGameState', { gameOver: checkGameOver(player2Deck), winner: checkWinner(player2Deck, 'Player 2'), @@ -433,16 +370,7 @@ const Game = () => { //pull out last two elements from it const drawCard1 = copiedDrawCardPileArray.pop() const drawCard2 = copiedDrawCardPileArray.pop() - //set new state - // setGameOver(checkGameOver(player1Deck)) - // setWinner(checkWinner(player1Deck, 'Player 1')) - // setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)]) - // setPlayer1Deck([...player1Deck.slice(0, removeIndex), ...player1Deck.slice(removeIndex + 1)]) - // setPlayer2Deck([...player2Deck.slice(0, player2Deck.length), drawCard1, drawCard2, ...player2Deck.slice(player2Deck.length)]) - // setCurrentColor(colorOfPlayedCard) - // setCurrentNumber(252) - // setDrawCardPile([...copiedDrawCardPileArray]) - //send this state to server + //send new state to server socket.emit('updateGameState', { gameOver: checkGameOver(player1Deck), winner: checkWinner(player1Deck, 'Player 1'), @@ -464,16 +392,7 @@ const Game = () => { //pull out last two elements from it const drawCard1 = copiedDrawCardPileArray.pop() const drawCard2 = copiedDrawCardPileArray.pop() - //set new state - // setGameOver(checkGameOver(player2Deck)) - // setWinner(checkWinner(player2Deck, 'Player 2')) - // setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)]) - // setPlayer2Deck([...player2Deck.slice(0, removeIndex), ...player2Deck.slice(removeIndex + 1)]) - // setPlayer1Deck([...player1Deck.slice(0, player1Deck.length), drawCard1, drawCard2, ...player1Deck.slice(player1Deck.length)]) - // setCurrentColor(colorOfPlayedCard) - // setCurrentNumber(252) - // setDrawCardPile([...copiedDrawCardPileArray]) - //send this state to server + //send new state to server socket.emit('updateGameState', { gameOver: checkGameOver(player2Deck), winner: checkWinner(player2Deck, 'Player 2'), @@ -501,15 +420,7 @@ const Game = () => { //remove the played card from player1's deck and add it to playedCardsPile (immutably) const removeIndex = player1Deck.indexOf(played_card) //then update turn, currentColor and currentNumber - //set new state - setGameOver(checkGameOver(player1Deck)) - setWinner(checkWinner(player1Deck, 'Player 1')) - setTurn('Player 2') - setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)]) - setPlayer1Deck([...player1Deck.slice(0, removeIndex), ...player1Deck.slice(removeIndex + 1)]) - setCurrentColor(newColor) - setCurrentNumber(300) - //send this state to server + //send new state to server socket.emit('updateGameState', { gameOver: checkGameOver(player1Deck), winner: checkWinner(player1Deck, 'Player 1'), @@ -526,15 +437,6 @@ const Game = () => { //remove the played card from player2's deck and add it to playedCardsPile (immutably) const removeIndex = player2Deck.indexOf(played_card) //then update turn, currentColor and currentNumber - //set new state - // setGameOver(checkGameOver(player2Deck)) - // setWinner(checkWinner(player2Deck, 'Player 2')) - // setTurn('Player 1') - // setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)]) - // setPlayer2Deck([...player2Deck.slice(0, removeIndex), ...player2Deck.slice(removeIndex + 1)]) - // setCurrentColor(newColor) - // setCurrentNumber(300) - //send this state to server socket.emit('updateGameState', { gameOver: checkGameOver(player2Deck), winner: checkWinner(player2Deck, 'Player 2'), @@ -564,16 +466,7 @@ const Game = () => { const drawCard3 = copiedDrawCardPileArray.pop() const drawCard4 = copiedDrawCardPileArray.pop() //then update currentColor and currentNumber - turn will remain same - //set new state - // setGameOver(checkGameOver(player1Deck)) - // setWinner(checkWinner(player1Deck, 'Player 1')) - // setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)]) - // setPlayer1Deck([...player1Deck.slice(0, removeIndex), ...player1Deck.slice(removeIndex + 1)]) - // setPlayer2Deck([...player2Deck.slice(0, player2Deck.length), drawCard1, drawCard2, drawCard3, drawCard4, ...player2Deck.slice(player2Deck.length)]) - // setCurrentColor(newColor) - // setCurrentNumber(600) - // setDrawCardPile([...copiedDrawCardPileArray]) - //send this state to server + //send new state to server socket.emit('updateGameState', { gameOver: checkGameOver(player1Deck), winner: checkWinner(player1Deck, 'Player 1'), @@ -599,16 +492,7 @@ const Game = () => { const drawCard3 = copiedDrawCardPileArray.pop() const drawCard4 = copiedDrawCardPileArray.pop() //then update currentColor and currentNumber - turn will remain same - //set new state - // setGameOver(checkGameOver(player2Deck)) - // setWinner(checkWinner(player2Deck, 'Player 2')) - // setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)]) - // setPlayer2Deck([...player2Deck.slice(0, removeIndex), ...player2Deck.slice(removeIndex + 1)]) - // setPlayer1Deck([...player1Deck.slice(0, player1Deck.length), drawCard1, drawCard2, drawCard3, drawCard4, ...player1Deck.slice(player1Deck.length)]) - // setCurrentColor(newColor) - // setCurrentNumber(600) - // setDrawCardPile([...copiedDrawCardPileArray]) - //send this state to server + //send new state to server socket.emit('updateGameState', { gameOver: checkGameOver(player2Deck), winner: checkWinner(player2Deck, 'Player 2'), @@ -640,12 +524,7 @@ const Game = () => { let numberOfDrawnCard = drawCard.charAt(0) if(colorOfDrawnCard === currentColor && (drawCard === 'skipR' || drawCard === 'skipG' || drawCard === 'skipB' || drawCard === 'skipY')) { alert(`You drew ${drawCard}. It was played for you.`) - //set new state - // setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), drawCard, ...playedCardsPile.slice(playedCardsPile.length)]) - // setCurrentColor(colorOfDrawnCard) - // setCurrentNumber(404) - // setDrawCardPile([...copiedDrawCardPileArray]) - //send this state to server + //send new state to server socket.emit('updateGameState', { playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), drawCard, ...playedCardsPile.slice(playedCardsPile.length)], currentColor: colorOfDrawnCard, @@ -661,13 +540,7 @@ const Game = () => { //pull out last two elements from it const drawCard1 = copiedDrawCardPileArray.pop() const drawCard2 = copiedDrawCardPileArray.pop() - //set new state - // setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), drawCard, ...playedCardsPile.slice(playedCardsPile.length)]) - // setPlayer2Deck([...player2Deck.slice(0, player2Deck.length), drawCard1, drawCard2, ...player2Deck.slice(player2Deck.length)]) - // setCurrentColor(colorOfDrawnCard) - // setCurrentNumber(252) - // setDrawCardPile([...copiedDrawCardPileArray]) - //send this state to server + //send new state to server socket.emit('updateGameState', { playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), drawCard, ...playedCardsPile.slice(playedCardsPile.length)], player2Deck: [...player2Deck.slice(0, player2Deck.length), drawCard1, drawCard2, ...player2Deck.slice(player2Deck.length)], @@ -680,15 +553,8 @@ const Game = () => { alert(`You drew ${drawCard}. It was played for you.`) //ask for new color const newColor = prompt('Enter first letter of new color in uppercase (R/G/B/Y)') - //set new state - // setTurn('Player 2') - // setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), drawCard, ...playedCardsPile.slice(playedCardsPile.length)]) - // setCurrentColor(newColor) - // setCurrentNumber(300) - // setDrawCardPile([...copiedDrawCardPileArray]) - //send this state to server + //send new state to server socket.emit('updateGameState', { - turn: 'Player 2', playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), drawCard, ...playedCardsPile.slice(playedCardsPile.length)], currentColor: newColor, currentNumber: 300, @@ -699,7 +565,7 @@ const Game = () => { alert(`You drew ${drawCard}. It was played for you.`) //ask for new color const newColor = prompt('Enter first letter of new color in uppercase (R/G/B/Y)') - //remove 2 new cards from drawCardPile and add them to player1's deck (immutably) + //remove 2 new cards from drawCardPile and add them to player2's deck (immutably) //make a copy of drawCardPile array const copiedDrawCardPileArray = [...drawCardPile] //pull out last four elements from it @@ -707,16 +573,10 @@ const Game = () => { const drawCard2 = copiedDrawCardPileArray.pop() const drawCard3 = copiedDrawCardPileArray.pop() const drawCard4 = copiedDrawCardPileArray.pop() - //set new state - // setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), drawCard, ...playedCardsPile.slice(playedCardsPile.length)]) - // setPlayer1Deck([...player1Deck.slice(0, player1Deck.length), drawCard1, drawCard2, drawCard3, drawCard4, ...player1Deck.slice(player1Deck.length)]) - // setCurrentColor(newColor) - // setCurrentNumber(600) - // setDrawCardPile([...copiedDrawCardPileArray]) - //send this state to server + //send new state to server socket.emit('updateGameState', { playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), drawCard, ...playedCardsPile.slice(playedCardsPile.length)], - player1Deck: [...player1Deck.slice(0, player1Deck.length), drawCard1, drawCard2, drawCard3, drawCard4, ...player1Deck.slice(player1Deck.length)], + player2Deck: [...player2Deck.slice(0, player2Deck.length), drawCard1, drawCard2, drawCard3, drawCard4, ...player2Deck.slice(player2Deck.length)], currentColor: newColor, currentNumber: 600, drawCardPile: [...copiedDrawCardPileArray] @@ -725,13 +585,7 @@ const Game = () => { //if not action card - check if drawn card is playable else if(numberOfDrawnCard === currentNumber || colorOfDrawnCard === currentColor) { alert(`You drew ${drawCard}. It was played for you.`) - //set new state - // setTurn('Player 2') - // setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), drawCard, ...playedCardsPile.slice(playedCardsPile.length)]) - // setCurrentColor(colorOfDrawnCard) - // setCurrentNumber(numberOfDrawnCard) - // setDrawCardPile([...copiedDrawCardPileArray]) - //send this state to server + //send new state to server socket.emit('updateGameState', { turn: 'Player 2', playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), drawCard, ...playedCardsPile.slice(playedCardsPile.length)], @@ -743,11 +597,7 @@ const Game = () => { //else add the drawn card to player1's deck else { alert(`You drew ${drawCard}.`) - //set new state - // setTurn('Player 2') - // setPlayer1Deck([...player1Deck.slice(0, player1Deck.length), drawCard, ...player1Deck.slice(player1Deck.length)]) - // setDrawCardPile([...copiedDrawCardPileArray]) - //send this state to server + //send new state to server socket.emit('updateGameState', { turn: 'Player 2', player1Deck: [...player1Deck.slice(0, player1Deck.length), drawCard, ...player1Deck.slice(player1Deck.length)], @@ -766,12 +616,7 @@ const Game = () => { let numberOfDrawnCard = drawCard.charAt(0) if(colorOfDrawnCard === currentColor && (drawCard === 'skipR' || drawCard === 'skipG' || drawCard === 'skipB' || drawCard === 'skipY')) { alert(`You drew ${drawCard}. It was played for you.`) - //set new state - // setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), drawCard, ...playedCardsPile.slice(playedCardsPile.length)]) - // setCurrentColor(colorOfDrawnCard) - // setCurrentNumber(404) - // setDrawCardPile([...copiedDrawCardPileArray]) - //send this state to server + //send new state to server socket.emit('updateGameState', { playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), drawCard, ...playedCardsPile.slice(playedCardsPile.length)], currentColor: colorOfDrawnCard, @@ -781,22 +626,16 @@ const Game = () => { } else if(colorOfDrawnCard === currentColor && (drawCard === 'D2R' || drawCard === 'D2G' || drawCard === 'D2B' || drawCard === 'D2Y')) { alert(`You drew ${drawCard}. It was played for you.`) - //remove 2 new cards from drawCardPile and add them to player2's deck (immutably) + //remove 2 new cards from drawCardPile and add them to player1's deck (immutably) //make a copy of drawCardPile array const copiedDrawCardPileArray = [...drawCardPile] //pull out last two elements from it const drawCard1 = copiedDrawCardPileArray.pop() const drawCard2 = copiedDrawCardPileArray.pop() - //set new state - // setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), drawCard, ...playedCardsPile.slice(playedCardsPile.length)]) - // setPlayer2Deck([...player2Deck.slice(0, player2Deck.length), drawCard1, drawCard2, ...player2Deck.slice(player2Deck.length)]) - // setCurrentColor(colorOfDrawnCard) - // setCurrentNumber(252) - // setDrawCardPile([...copiedDrawCardPileArray]) - //send this state to server + //send new state to server socket.emit('updateGameState', { playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), drawCard, ...playedCardsPile.slice(playedCardsPile.length)], - player2Deck: [...player2Deck.slice(0, player2Deck.length), drawCard1, drawCard2, ...player2Deck.slice(player2Deck.length)], + player1Deck: [...player1Deck.slice(0, player1Deck.length), drawCard1, drawCard2, ...player1Deck.slice(player1Deck.length)], currentColor: colorOfDrawnCard, currentNumber: 252, drawCardPile: [...copiedDrawCardPileArray] @@ -806,15 +645,8 @@ const Game = () => { alert(`You drew ${drawCard}. It was played for you.`) //ask for new color const newColor = prompt('Enter first letter of new color in uppercase (R/G/B/Y)') - //set new state - // setTurn('Player 1') - // setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), drawCard, ...playedCardsPile.slice(playedCardsPile.length)]) - // setCurrentColor(newColor) - // setCurrentNumber(300) - // setDrawCardPile([...copiedDrawCardPileArray]) - //send this state to server + //send new state to server socket.emit('updateGameState', { - turn: 'Player 1', playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), drawCard, ...playedCardsPile.slice(playedCardsPile.length)], currentColor: newColor, currentNumber: 300, @@ -833,13 +665,7 @@ const Game = () => { const drawCard2 = copiedDrawCardPileArray.pop() const drawCard3 = copiedDrawCardPileArray.pop() const drawCard4 = copiedDrawCardPileArray.pop() - //set new state - // setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), drawCard, ...playedCardsPile.slice(playedCardsPile.length)]) - // setPlayer1Deck([...player1Deck.slice(0, player1Deck.length), drawCard1, drawCard2, drawCard3, drawCard4, ...player1Deck.slice(player1Deck.length)]) - // setCurrentColor(newColor) - // setCurrentNumber(600) - // setDrawCardPile([...copiedDrawCardPileArray]) - //send this state to server + //send new state to server socket.emit('updateGameState', { playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), drawCard, ...playedCardsPile.slice(playedCardsPile.length)], player1Deck: [...player1Deck.slice(0, player1Deck.length), drawCard1, drawCard2, drawCard3, drawCard4, ...player1Deck.slice(player1Deck.length)], @@ -851,13 +677,7 @@ const Game = () => { //if not action card - check if drawn card is playable else if(numberOfDrawnCard === currentNumber || colorOfDrawnCard === currentColor) { alert(`You drew ${drawCard}. It was played for you.`) - //set new state - // setTurn('Player 1') - // setPlayedCardsPile([...playedCardsPile.slice(0, playedCardsPile.length), drawCard, ...playedCardsPile.slice(playedCardsPile.length)]) - // setCurrentColor(colorOfDrawnCard) - // setCurrentNumber(numberOfDrawnCard) - // setDrawCardPile([...copiedDrawCardPileArray]) - //send this state to server + //send new state to server socket.emit('updateGameState', { turn: 'Player 1', playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), drawCard, ...playedCardsPile.slice(playedCardsPile.length)], @@ -869,11 +689,7 @@ const Game = () => { //else add the drawn card to player2's deck else { alert(`You drew ${drawCard}.`) - //set new state - // setTurn('Player 1') - // setPlayer2Deck([...player2Deck.slice(0, player2Deck.length), drawCard, ...player2Deck.slice(player2Deck.length)]) - // setDrawCardPile([...copiedDrawCardPileArray]) - //send this state to server + //send new state to server socket.emit('updateGameState', { turn: 'Player 1', player2Deck: [...player2Deck.slice(0, player2Deck.length), drawCard, ...player2Deck.slice(player2Deck.length)], @@ -884,9 +700,14 @@ const Game = () => { } return ( - gameOver ?
{winner !== '' && <>

GAME OVER

{winner} wins!

}Home
: + (!roomFull) ? <> + + {users.length===2 ? <> + + {gameOver ?
{winner !== '' && <>

GAME OVER

{winner} wins!

}Home
:

Turn: {turn}

+ {currentUser === 'Player 1' &&
{player1Deck.map((item) => ( { {item} ))} -
+ +
}

Current Card: {playedCardsPile[playedCardsPile.length-1]}

Current Color: {currentColor}

- +

+ {currentUser === 'Player 2' &&
{player2Deck.map((item) => ( { {item} ))} + +
} +
+
+

Users in room:

+ {users.map(user => { + return
{user.name}
+ })}
Home -
+
} + + :

Waiting for other player

} + + :

Room full

) } diff --git a/client/src/components/Homepage.js b/client/src/components/Homepage.js index 0819410..f83605f 100644 --- a/client/src/components/Homepage.js +++ b/client/src/components/Homepage.js @@ -1,11 +1,14 @@ -import React from 'react' +import React, { useState } from 'react' import { Link } from 'react-router-dom' const Homepage = () => { + const [roomCode, setRoomCode] = useState('') + return (

UNO

- +
setRoomCode(event.target.value)} />
+
) } diff --git a/package.json b/package.json index becffdd..16aeb7e 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,8 @@ "description": "Online multiplayer card game", "main": "server.js", "scripts": { - "start": "node server" + "start": "node server", + "client": "npm start --prefix client" }, "repository": { "type": "git", diff --git a/server.js b/server.js index 3f94a02..0d71d1c 100644 --- a/server.js +++ b/server.js @@ -2,6 +2,7 @@ const express = require('express') const socketio = require('socket.io') const http = require('http') const cors = require('cors') +const { addUser, removeUser, getUser, getUsersInRoom } = require('./users') const PORT = process.env.PORT || 5000 @@ -12,18 +13,41 @@ const io = socketio(server) app.use(cors()) io.on('connection', socket => { - console.log('connection made') + socket.on('join', (payload, callback) => { + let numberOfUsersInRoom = getUsersInRoom(payload.room).length + + const { error, newUser} = addUser({ + id: socket.id, + name: numberOfUsersInRoom===0 ? 'Player 1' : 'Player 2', + room: payload.room + }) + + if(error) + return callback(error) + + socket.join(newUser.room) + + io.to(newUser.room).emit('roomData', {room: newUser.room, users: getUsersInRoom(newUser.room)}) + socket.emit('currentUserData', {name: newUser.name}) + callback() + }) socket.on('initGameState', gameState => { - io.emit('initGameState', gameState) + const user = getUser(socket.id) + if(user) + io.to(user.room).emit('initGameState', gameState) }) socket.on('updateGameState', gameState => { - io.emit('updateGameState', gameState) + const user = getUser(socket.id) + if(user) + io.to(user.room).emit('updateGameState', gameState) }) socket.on('disconnect', () => { - console.log('connection lost') + const user = removeUser(socket.id) + if(user) + io.to(user.room).emit('roomData', {room: user.room, users: getUsersInRoom(user.room)}) }) }) diff --git a/users.js b/users.js new file mode 100644 index 0000000..c44c1f6 --- /dev/null +++ b/users.js @@ -0,0 +1,28 @@ + const users = [] + + const addUser = ({id, name, room}) => { + const numberOfUsersInRoom = users.filter(user => user.room === room).length + if(numberOfUsersInRoom === 2) + return { error: 'Room full' } + + const newUser = { id, name, room } + users.push(newUser) + return { newUser } +} + +const removeUser = id => { + const removeIndex = users.findIndex(user => user.id === id) + + if(removeIndex!==-1) + return users.splice(removeIndex, 1)[0] +} + +const getUser = id => { + return users.find(user => user.id === id) +} + +const getUsersInRoom = room => { + return users.filter(user => user.room === room) +} + +module.exports = { addUser, removeUser, getUser, getUsersInRoom } \ No newline at end of file