heroku deploy and added moving pawns

This commit is contained in:
Wenszel 2021-04-29 16:34:50 +02:00
parent 981b5fd032
commit 57358b0105
9 changed files with 79 additions and 20 deletions

1
.env.development Normal file
View File

@ -0,0 +1 @@
HOST=smaga-wiktor-ludo.herokuapp.com

View File

@ -1,5 +1,6 @@
var express = require('express'); var express = require('express');
var router = express.Router(); var router = express.Router();
const RoomModel = require('../schemas/room');
function setNextPlayer(req, res){ function setNextPlayer(req, res){
RoomModel.findOne({_id: req.session.roomId}, function (err, doc){ RoomModel.findOne({_id: req.session.roomId}, function (err, doc){
@ -20,11 +21,58 @@ function setNextPlayer(req, res){
} }
router.get('/roll', function (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!");
}); });

View File

@ -44,7 +44,7 @@ router.post('/ready', function (req, res){
//deleting user in case he left before game started //deleting user in case he left before game started
router.post('/exit', function(req,res){ router.post('/exit', function(req,res){
changeReadyState(req,res, true) // changeReadyState(req,res, true)
}); });
//return session data //return session data

View File

@ -26,7 +26,7 @@ const mongoose = require("mongoose");
mongoose.set('useFindAndModify', false); mongoose.set('useFindAndModify', false);
const CONNECTION_URI = require("./credentials.js").MONGODB_URL; const CONNECTION_URI = require("./credentials.js").MONGODB_URL;
mongoose.connect(CONNECTION_URI, { mongoose.connect(process.env.MONGODB_URL || CONNECTION_URI, {
useNewUrlParser: true, useNewUrlParser: true,
useUnifiedTopology: true useUnifiedTopology: true
}) })
@ -51,6 +51,10 @@ app.use(session({
secure: false,}, secure: false,},
})); }));
if (process.env.NODE_ENV === 'production') {
app.use(express.static('../build'))
}
//ROUTES CONFIG //ROUTES CONFIG
const roomRoutes = require("./routes/room"); const roomRoutes = require("./routes/room");
const playerRoutes = require("./routes/player"); const playerRoutes = require("./routes/player");

View File

@ -18,6 +18,7 @@
"scripts": { "scripts": {
"start": "react-scripts start", "start": "react-scripts start",
"build": "react-scripts build", "build": "react-scripts build",
"heroku-postbuild": "cd backend && npm install && cd .. && npm install && npm run build",
"test": "react-scripts test", "test": "react-scripts test",
"eject": "react-scripts eject" "eject": "react-scripts eject"
}, },
@ -39,5 +40,5 @@
"last 1 safari version" "last 1 safari version"
] ]
}, },
"proxy": "http://localhost:3000" "proxy": "http://localhost:5000"
} }

View File

@ -57,11 +57,12 @@ function App() {
<NameInput idCallback = {idCallback}/> <NameInput idCallback = {idCallback}/>
</Route> </Route>
<Route path="/game"> <Route path="/game">
{playerData ?
<Beforeunload onBeforeunload={handleExit}> <Beforeunload onBeforeunload={handleExit}>
<PlayerDataContext.Provider value={playerData}> <PlayerDataContext.Provider value={playerData}>
<Gameboard/> <Gameboard/>
</PlayerDataContext.Provider> </PlayerDataContext.Provider>
</Beforeunload> </Beforeunload> : null}
</Route> </Route>
</Switch> </Switch>
</Router> </Router>

View File

@ -37,6 +37,7 @@ const Gameboard = () => {
setNowMoving(false); setNowMoving(false);
} }
} }
console.log(nowMoving);
setPlayers(response.data.players); setPlayers(response.data.players);
setPawns(response.data.pawns); setPawns(response.data.pawns);
setTime(response.data.nextMoveTime); setTime(response.data.nextMoveTime);

View File

@ -34,7 +34,7 @@ const Map = ({ pawns, nowMoving, rolledNumber }) => {
axios.post('http://localhost:3000/game/move', { axios.post('http://localhost:3000/game/move', {
withCredentials: true, withCredentials: true,
mode: 'cors', mode: 'cors',
data: {_id: pawn._id} data: {pawnId: pawn._id}
}); });
setHintPawn(null); setHintPawn(null);
} }
@ -52,6 +52,8 @@ const Map = ({ pawns, nowMoving, rolledNumber }) => {
case 'red': case 'red':
if(pawn.position >= 0 && pawn.position <= 3){ if(pawn.position >= 0 && pawn.position <= 3){
return 16; return 16;
}else if(pawn.position>15 && pawn.position<67){
return pawn.position + rolledNumber
} }
break; break;
case 'blue': case 'blue':
@ -80,6 +82,7 @@ const Map = ({ pawns, nowMoving, rolledNumber }) => {
x = event.clientX - rect.left, x = event.clientX - rect.left,
y = event.clientY - rect.top; y = event.clientY - rect.top;
canvas.style.cursor = "default"; canvas.style.cursor = "default";
console.log("Ruszam się")
for (const pawn of pawns){ for (const pawn of pawns){
if(pawn.circle){ if(pawn.circle){
/* /*

View File

@ -33,7 +33,7 @@ const positions = [
{x: 200, y: 45}, {x: 200, y: 45},
{x: 200, y: 14}, {x: 200, y: 14},
// Top // Top
{x: 230, y: 14}, {x: 230, y: 14}, //25
{x: 261, y: 14}, {x: 261, y: 14},
{x: 261, y: 45}, {x: 261, y: 45},
{x: 261, y: 76}, {x: 261, y: 76},
@ -55,14 +55,14 @@ const positions = [
{x: 383, y: 261}, {x: 383, y: 261},
{x: 352, y: 261}, {x: 352, y: 261},
{x: 321, y: 261}, {x: 321, y: 261},
{x: 291, y: 261}, {x: 291, y: 261}, //44
{x: 261, y: 291}, {x: 261, y: 291},
{x: 261, y: 322}, {x: 261, y: 322},
{x: 261, y: 353}, {x: 261, y: 353},
{x: 261, y: 384}, {x: 261, y: 384},
{x: 261, y: 414}, {x: 261, y: 414},
{x: 261, y: 445}, {x: 261, y: 445},
// Bottom // Bottom
{x: 230, y: 445}, {x: 230, y: 445},
@ -74,42 +74,42 @@ const positions = [
{x: 200, y: 291}, {x: 200, y: 291},
{x: 169, y: 261}, {x: 169, y: 261},
{x: 138, y: 261}, {x: 138, y: 261}, //59
{x: 107, y: 261}, {x: 107, y: 261},
{x: 76, y: 261}, {x: 76, y: 261},
{x: 45, y: 261}, {x: 45, y: 261},
{x: 15, y: 261}, {x: 15, y: 261},
// Left // Left
{x: 15, y: 231}, {x: 15, y: 231}, //64
// One behind red base // One behind red base
{x: 15, y: 200}, {x: 15, y: 200}, //65
// Red end // Red end
{x: 45, y: 231}, {x: 45, y: 231}, //66
{x: 76, y: 231}, {x: 76, y: 231},
{x: 107, y: 231}, {x: 107, y: 231},
{x: 138, y: 231}, {x: 138, y: 231},
{x: 169, y: 231}, {x: 169, y: 231},
// Blue end // Blue end
{x: 231, y: 445}, {x: 231, y: 445}, //71
{x: 231, y: 414}, {x: 231, y: 414},
{x: 231, y: 384}, {x: 231, y: 384},
{x: 231, y: 353}, {x: 231, y: 353}, //74
{x: 231, y: 322}, {x: 231, y: 322},
{x: 231, y: 291}, {x: 231, y: 291},
// Green end // Green end
{x: 414, y: 231}, {x: 414, y: 231}, // 77
{x: 383, y: 231}, {x: 383, y: 231},
{x: 352, y: 231}, {x: 352, y: 231},
{x: 321, y: 231}, {x: 321, y: 231},
{x: 290, y: 231}, {x: 290, y: 231},
// Yellow base // Yellow base
{x: 230, y: 15}, {x: 230, y: 15}, //82
{x: 230, y: 45}, {x: 230, y: 45},
{x: 230, y: 76}, {x: 230, y: 76},
{x: 230, y: 107}, {x: 230, y: 107},
{x: 230, y: 138}, {x: 230, y: 138},
{x: 230, y: 169}, {x: 230, y: 169}, //87
]; ];
export default positions; export default positions;