diff --git a/.env.development b/.env.development new file mode 100644 index 0000000..e862c4e --- /dev/null +++ b/.env.development @@ -0,0 +1 @@ +HOST=smaga-wiktor-ludo.herokuapp.com \ No newline at end of file diff --git a/backend/routes/game.js b/backend/routes/game.js index f141723..28d8cbd 100644 --- a/backend/routes/game.js +++ b/backend/routes/game.js @@ -1,5 +1,6 @@ var express = require('express'); var router = express.Router(); +const RoomModel = require('../schemas/room'); function setNextPlayer(req, res){ RoomModel.findOne({_id: req.session.roomId}, function (err, doc){ @@ -20,11 +21,58 @@ function setNextPlayer(req, res){ } router.get('/roll', function (req, res){ - res.send({number: Math.ceil(Math.random() * 6)}); + const rolledNumber = Math.ceil(Math.random() * 6); + req.session.rolledNumber = rolledNumber; + res.send({number: rolledNumber}); }); -router.post('/move', function (req, res){ - +router.post('/move', async function (req, res){ + const rolledNumber = req.session.rolledNumber; + req.session.rolledNumber = null; + const room = await RoomModel.findOne({_id: req.session.roomId}); + console.log(room); + const indexOfMovedPawn = room.pawns.findIndex( pawn => pawn._id === req.body.pawnId); + let { position } = room.pawns[indexOfMovedPawn]; + switch (req.session.color){ + case 'red': + if(position >= 0 && position <= 3){ + position = 4; + }else if(position > 15 && position + rolledNumber <= 66){ + position = position + rolledNumber; + }else{ + position = 67 + (position + rolledNumber - 66); + } + break; + case 'blue': + if(position >= 4 && position <= 7){ + position = 55; + }else if(position+rolledNumber>55 || position+rolledNumber <= 53){ + position = position + rolledNumber; + }else{ + position = 71 + (position + rolledNumber - 53); + } + break; + case 'green': + if(pawn.position >= 8 && pawn.position <= 11){ + position = 42; + }else if(position + rolledNumber > 42 || position+rolledNumber <= 40){ + position = position + rolledNumber; + }else{ + position = 76 - (position + rolledNumber - 40) + } + break; + case 'yellow': + if(pawn.position >= 12 && pawn.position <= 15){ + return 29; + }else if(position + rolledNumber > 29 || position+rolledNumber <= 27){ + position = position + rolledNumber; + }else{ + position = 82 + (position + rolledNumber - 27) + } + break; + } + RoomModel.findOneAndUpdate({_id: req.session.roomId, 'pawnw._id': req.body.pawnId}, room); + res.send("Correctly Moved!"); }); diff --git a/backend/routes/player.js b/backend/routes/player.js index 0302592..b38713c 100644 --- a/backend/routes/player.js +++ b/backend/routes/player.js @@ -44,7 +44,7 @@ router.post('/ready', function (req, res){ //deleting user in case he left before game started router.post('/exit', function(req,res){ - changeReadyState(req,res, true) + // changeReadyState(req,res, true) }); //return session data diff --git a/backend/server.js b/backend/server.js index c57629b..d97192c 100644 --- a/backend/server.js +++ b/backend/server.js @@ -26,7 +26,7 @@ const mongoose = require("mongoose"); mongoose.set('useFindAndModify', false); const CONNECTION_URI = require("./credentials.js").MONGODB_URL; -mongoose.connect(CONNECTION_URI, { +mongoose.connect(process.env.MONGODB_URL || CONNECTION_URI, { useNewUrlParser: true, useUnifiedTopology: true }) @@ -51,6 +51,10 @@ app.use(session({ secure: false,}, })); +if (process.env.NODE_ENV === 'production') { + app.use(express.static('../build')) +} + //ROUTES CONFIG const roomRoutes = require("./routes/room"); const playerRoutes = require("./routes/player"); diff --git a/package.json b/package.json index 1388dbf..c52177b 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "scripts": { "start": "react-scripts start", "build": "react-scripts build", + "heroku-postbuild": "cd backend && npm install && cd .. && npm install && npm run build", "test": "react-scripts test", "eject": "react-scripts eject" }, @@ -39,5 +40,5 @@ "last 1 safari version" ] }, - "proxy": "http://localhost:3000" + "proxy": "http://localhost:5000" } diff --git a/src/App.js b/src/App.js index ca8ff7e..8b5b0b3 100644 --- a/src/App.js +++ b/src/App.js @@ -57,11 +57,12 @@ function App() { + {playerData ? - + : null} diff --git a/src/components/Gameboard.jsx b/src/components/Gameboard.jsx index 0325426..0de8a35 100644 --- a/src/components/Gameboard.jsx +++ b/src/components/Gameboard.jsx @@ -37,6 +37,7 @@ const Gameboard = () => { setNowMoving(false); } } + console.log(nowMoving); setPlayers(response.data.players); setPawns(response.data.pawns); setTime(response.data.nextMoveTime); diff --git a/src/components/game-board-components/Map.jsx b/src/components/game-board-components/Map.jsx index 8a5e2c1..9eb7b47 100644 --- a/src/components/game-board-components/Map.jsx +++ b/src/components/game-board-components/Map.jsx @@ -34,7 +34,7 @@ const Map = ({ pawns, nowMoving, rolledNumber }) => { axios.post('http://localhost:3000/game/move', { withCredentials: true, mode: 'cors', - data: {_id: pawn._id} + data: {pawnId: pawn._id} }); setHintPawn(null); } @@ -52,6 +52,8 @@ const Map = ({ pawns, nowMoving, rolledNumber }) => { case 'red': if(pawn.position >= 0 && pawn.position <= 3){ return 16; + }else if(pawn.position>15 && pawn.position<67){ + return pawn.position + rolledNumber } break; case 'blue': @@ -80,6 +82,7 @@ const Map = ({ pawns, nowMoving, rolledNumber }) => { x = event.clientX - rect.left, y = event.clientY - rect.top; canvas.style.cursor = "default"; + console.log("Ruszam siÄ™") for (const pawn of pawns){ if(pawn.circle){ /* diff --git a/src/components/game-board-components/positions.js b/src/components/game-board-components/positions.js index 534fbae..b6b78f3 100644 --- a/src/components/game-board-components/positions.js +++ b/src/components/game-board-components/positions.js @@ -33,7 +33,7 @@ const positions = [ {x: 200, y: 45}, {x: 200, y: 14}, // Top - {x: 230, y: 14}, + {x: 230, y: 14}, //25 {x: 261, y: 14}, {x: 261, y: 45}, {x: 261, y: 76}, @@ -55,14 +55,14 @@ const positions = [ {x: 383, y: 261}, {x: 352, y: 261}, {x: 321, y: 261}, - {x: 291, y: 261}, + {x: 291, y: 261}, //44 {x: 261, y: 291}, {x: 261, y: 322}, {x: 261, y: 353}, {x: 261, y: 384}, {x: 261, y: 414}, - {x: 261, y: 445}, + {x: 261, y: 445}, // Bottom {x: 230, y: 445}, @@ -74,42 +74,42 @@ const positions = [ {x: 200, y: 291}, {x: 169, y: 261}, - {x: 138, y: 261}, + {x: 138, y: 261}, //59 {x: 107, y: 261}, {x: 76, y: 261}, {x: 45, y: 261}, {x: 15, y: 261}, // Left - {x: 15, y: 231}, + {x: 15, y: 231}, //64 // One behind red base - {x: 15, y: 200}, + {x: 15, y: 200}, //65 // Red end - {x: 45, y: 231}, + {x: 45, y: 231}, //66 {x: 76, y: 231}, {x: 107, y: 231}, {x: 138, y: 231}, {x: 169, y: 231}, // Blue end - {x: 231, y: 445}, + {x: 231, y: 445}, //71 {x: 231, y: 414}, {x: 231, y: 384}, - {x: 231, y: 353}, + {x: 231, y: 353}, //74 {x: 231, y: 322}, {x: 231, y: 291}, // Green end - {x: 414, y: 231}, + {x: 414, y: 231}, // 77 {x: 383, y: 231}, {x: 352, y: 231}, {x: 321, y: 231}, {x: 290, y: 231}, // Yellow base - {x: 230, y: 15}, + {x: 230, y: 15}, //82 {x: 230, y: 45}, {x: 230, y: 76}, {x: 230, y: 107}, {x: 230, y: 138}, - {x: 230, y: 169}, + {x: 230, y: 169}, //87 ]; export default positions; \ No newline at end of file