added turns
This commit is contained in:
parent
3f07e71ce0
commit
148fa85601
@ -28,7 +28,7 @@ router.post('/add', function (req, res) {
|
|||||||
req.session.roomId = newRoom._id;
|
req.session.roomId = newRoom._id;
|
||||||
req.session.playerId = newRoom.players[0]._id;
|
req.session.playerId = newRoom.players[0]._id;
|
||||||
req.session.name = req.body.name;
|
req.session.name = req.body.name;
|
||||||
res.status(200).send('Joined!');
|
res.status(200).send(req.session.playerId);
|
||||||
})
|
})
|
||||||
.catch(err => res.status(400).json('Error: ' + err))
|
.catch(err => res.status(400).json('Error: ' + err))
|
||||||
}else {
|
}else {
|
||||||
@ -54,7 +54,7 @@ router.post('/add', function (req, res) {
|
|||||||
req.session.roomId = results._id;
|
req.session.roomId = results._id;
|
||||||
req.session.playerId = updateObj.players[updateObj.players.length-1]._id;
|
req.session.playerId = updateObj.players[updateObj.players.length-1]._id;
|
||||||
req.session.name = req.body.name;
|
req.session.name = req.body.name;
|
||||||
res.status(200).send('Joined!');
|
res.status(200).send(req.session.playerId);
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
15
src/App.js
15
src/App.js
@ -14,14 +14,17 @@ function App() {
|
|||||||
const [redirect, setRedirect] = useState()
|
const [redirect, setRedirect] = useState()
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
console.log("id", id);
|
||||||
axios.get('http://localhost:3000/player', {
|
axios.get('http://localhost:3000/player', {
|
||||||
withCredentials:true,
|
withCredentials:true,
|
||||||
mode: 'cors'
|
mode: 'cors'
|
||||||
})
|
})
|
||||||
.then( response => {
|
.then( response => {
|
||||||
|
console.log(response);
|
||||||
|
setId(response.data.playerId);
|
||||||
response.data.roomId!=null ? setRedirect(true) : setRedirect(false);
|
response.data.roomId!=null ? setRedirect(true) : setRedirect(false);
|
||||||
});
|
});
|
||||||
})
|
},[id])
|
||||||
|
|
||||||
const handleExit = e => {
|
const handleExit = e => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
@ -31,14 +34,17 @@ function App() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const idCallback = (id)=>{
|
const idCallback = (id)=>{
|
||||||
setId(id);
|
console.log(id);
|
||||||
|
|
||||||
axios.get('http://localhost:3000/player/', {
|
axios.get('http://localhost:3000/player/', {
|
||||||
withCredentials:true,
|
withCredentials:true,
|
||||||
mode: 'cors',
|
mode: 'cors',
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
})
|
})
|
||||||
.then(()=> setRedirect(true))
|
.then(response => {
|
||||||
|
setId(response.data.playerId);
|
||||||
|
setRedirect(true)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@ -53,8 +59,7 @@ function App() {
|
|||||||
</Route>
|
</Route>
|
||||||
<Route path="/game">
|
<Route path="/game">
|
||||||
<Beforeunload onBeforeunload={handleExit}>
|
<Beforeunload onBeforeunload={handleExit}>
|
||||||
<Navbar/>
|
<Gameboard id={id}/>
|
||||||
<Gameboard/>
|
|
||||||
</Beforeunload>
|
</Beforeunload>
|
||||||
</Route>
|
</Route>
|
||||||
</Switch>
|
</Switch>
|
||||||
|
|||||||
@ -1,13 +1,40 @@
|
|||||||
import React from 'react';
|
import React, { useState, useEffect } from 'react';
|
||||||
|
import axios from 'axios';
|
||||||
import Map from './game-board-components/Map'
|
import Map from './game-board-components/Map'
|
||||||
import Dice from './game-board-components/Dice'
|
import Dice from './game-board-components/Dice'
|
||||||
|
import Navbar from './Navbar'
|
||||||
|
|
||||||
const Gameboard = () => {
|
const Gameboard = ({id}) => {
|
||||||
|
|
||||||
|
const [players, setPlayers] = useState([]);
|
||||||
|
const [nowMoving, setNowMoving] = useState(false);
|
||||||
|
//fetching players data to display them in navbar
|
||||||
|
const fetchData = () => {
|
||||||
|
axios.get('http://localhost:3000/room/',{
|
||||||
|
withCredentials:true,
|
||||||
|
mode: 'cors',
|
||||||
|
}).then((response)=>{
|
||||||
|
while(response.data.players.length !== 4){
|
||||||
|
response.data.players.push({
|
||||||
|
name: "...",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
setPlayers(response.data.players);
|
||||||
|
if(id===response.data.players.find(player => player.nowMoving === true)?._id){
|
||||||
|
setNowMoving(true);
|
||||||
|
}else{
|
||||||
|
setNowMoving(false);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
useEffect(()=>{
|
||||||
|
//sending ajax every 1 sec
|
||||||
|
setInterval(fetchData, 1000);
|
||||||
|
},[]);
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Dice/>
|
<Navbar players={players}/>
|
||||||
|
{nowMoving ? <Dice nowMoving={nowMoving}/> : null}
|
||||||
<Map/>
|
<Map/>
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
|
|||||||
@ -15,7 +15,8 @@ const NameInput = ({ idCallback }) => {
|
|||||||
"Content-Type": "application/json"
|
"Content-Type": "application/json"
|
||||||
})
|
})
|
||||||
.then(response => {
|
.then(response => {
|
||||||
idCallback(response.data.id);
|
console.log(response.data);
|
||||||
|
idCallback(response.data);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return(
|
return(
|
||||||
|
|||||||
@ -1,30 +1,8 @@
|
|||||||
import React, { useEffect, useState } from 'react';
|
import React from 'react';
|
||||||
import NameContainer from './navbar-components/NameContainer'
|
import NameContainer from './navbar-components/NameContainer'
|
||||||
import ReadyButton from './navbar-components/ReadyButton'
|
import ReadyButton from './navbar-components/ReadyButton'
|
||||||
import './Navbar.css';
|
import './Navbar.css';
|
||||||
import axios from 'axios';
|
const Navbar = ( { players }) => {
|
||||||
const Navbar = () => {
|
|
||||||
|
|
||||||
const [players, setPlayers] = useState([]);
|
|
||||||
//fetching players data to display them in navbar
|
|
||||||
const fetchData = () => {
|
|
||||||
axios.get('http://localhost:3000/room/',{
|
|
||||||
withCredentials:true,
|
|
||||||
mode: 'cors',
|
|
||||||
}).then((response)=>{
|
|
||||||
while(response.data.players.length !== 4){
|
|
||||||
response.data.players.push({
|
|
||||||
name: "...",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
setPlayers(response.data.players);
|
|
||||||
})
|
|
||||||
}
|
|
||||||
useEffect(()=>{
|
|
||||||
//sending ajax every 3 sec
|
|
||||||
setInterval(fetchData, 1000);
|
|
||||||
},[]);
|
|
||||||
|
|
||||||
return(
|
return(
|
||||||
<div className="navbar-container">
|
<div className="navbar-container">
|
||||||
{players.map( (player, index) =>
|
{players.map( (player, index) =>
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user