Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clinet/src/pages/ChooseWords.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ function ChooseWords(props) {
function moveToDraw(word) {
let obj = {word : word.innerText , points : word.value}
let answer = Utils.setNextTurn(params.UserId , params.GameId , obj)
navigate('/'+params.UserId+'/'+params.GameId+'/Play')
}


Expand Down
2 changes: 1 addition & 1 deletion clinet/src/pages/Guess.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function Guess() {
},[])

useEffect(async () => {
if(snapshot.Word.toUpperCase() == input.toUpperCase()){
if(snapshot.Word !== "" && snapshot.Word !== null && snapshot.Word.toUpperCase() === input.toUpperCase()){
await Utils.updateLastStep(params.UserId , params.GameId , {...snapshot , GuessState : "Done"})
}
} ,[input])
Expand Down
29 changes: 16 additions & 13 deletions clinet/src/pages/Play.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,32 @@ function Play() {
})

useEffect(async () => {
let answer = await Utils.getSnapshotLastStep(params.GameId)
setSnapshot(answer)
// let answer = await Utils.getSnapshotLastStep(params.GameId)
// setSnapshot(answer)
} ,[])

useEffect(async () => {
const interval = setInterval(async () => {
let answer = await Utils.getSnapshotLastStep(params.GameId)
setSnapshot(answer)
} , 5000);
return () => clearInterval(interval);

// const interval = setInterval(async () => {
// let answer = await Utils.getSnapshotLastStep(params.GameId)
// setSnapshot(answer)
// } , 5000);

// return () => clearInterval(interval);
},[])

useEffect(async () => {
await Utils.updateLastStep(params.UserId , params.GameId , snapshot)
if(snapshot.PaintingState == "Done" && snapshot.GuessState == "Done")
{
navigate('/'+params.UserId+'/'+params.GameId+'/WaitingRoom')
}
} ,[snapshot])

const finish = async () => {
await Utils.updateLastStep(params.UserId , params.GameId , {...snapshot , PaintingState : "Done"})
}

if(snapshot.PaintingState == "Done" && snapshot.GuessState == "Done")
{
navigate('/'+params.UserId+'/'+params.GameId+'/WaitingRoom')
}

return (
<div>
<h2>Draw</h2>
Expand All @@ -55,7 +56,9 @@ function Play() {
<div>
<CanvasDraw
style={{ boxShadow: "0 13px 27px -5px rgba(50, 50, 93, 0.25), 0 8px 16px -8px rgba(0, 0, 0, 0.3)"}}
onChange={(e) => {setSnapshot({...snapshot , Canvas : e.getDataURL()})} } />
onChange={(e) => {
setSnapshot({...snapshot , Canvas : e.getDataURL()});
} } />

<h2>You need to draw :</h2>
<h4> {snapshot.Word} </h4>
Expand Down
23 changes: 17 additions & 6 deletions clinet/src/pages/Utils/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,28 @@ const newGame = async (UserId) => {
return resp.data
}

const updateLastStep = async (UserId , GameId , snapshot) => {
let resp = await axios.put("http://localhost:8000/games/"+UserId+"/"+GameId+"/setLastStep" , snapshot)
const updateGame = async (UserId , GameMongoId , Game) => {
let resp = await axios.put("http://localhost:8000/games/"+UserId+"/"+GameMongoId , Game)
return resp.data
}

const updateGame = async (UserId , GameMongoId) => {
let resp = await axios.put("http://localhost:8000/games/"+UserId+"/"+GameMongoId)
return resp.data
const updateLastStep = async (UserId , GameId , snapshot) => {

// let resp = await axios.get("http://localhost:8000/games/"+GameId);
// let game = resp.data[0]
// let index = game.Steps.length-1;
// game.Steps[index] = snapshot;

// let resp2 = await axios.put("http://localhost:8000/games/"+UserId+"/"+game._id , game)
// return resp2.data

// let resp = await axios.put("http://localhost:8000/games/"+UserId+"/"+GameId+"/updateLastStep" , game)
// return resp.data
}


const setNextTurn = async (UserId , GameId , word) => {

let resp = await axios.get("http://localhost:8000/games/"+GameId);
let game = {...resp.data[0]}
let newStep = {
Expand All @@ -40,7 +51,7 @@ const setNextTurn = async (UserId , GameId , word) => {
}
game.Steps.push(newStep)

let resp2 = await axios.put("http://localhost:8000/games/"+UserId+"/"+game._id , game)
let resp2 = await axios.put("http://localhost:8000/games/"+UserId+"/"+game._id+"/setNextTurn" , game)
return resp2.data
}

Expand Down
24 changes: 20 additions & 4 deletions clinet/src/pages/WaitingRoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,26 @@ function WaitingRoom() {
const params = useParams();
const navigate = useNavigate();

const goToDrawComp = () => {
navigate('/'+params.UserId+'/'+params.GameId+'/Play')
const [snapshot , setSnapshot] = useState({
ActingUser: "" ,
Canvas: "" ,
GuessState: "" ,
PaintingState: "" ,
Points: 0 ,
Word: "" ,
_id: "" ,
})

useEffect(async () => {
const interval = setInterval(async () => {
let answer = await Utils.getSnapshotLastStep(params.GameId)
setSnapshot(answer)
} , 5000);
return () => clearInterval(interval);
},[])

if(snapshot.GuessState == "In_Progress"){
navigate('/'+params.UserId+'/'+params.GameId+'/Guess')
}


Expand All @@ -23,8 +41,6 @@ function WaitingRoom() {
<span className="visually-hidden">Loading...</span>
</div>

<br/><br/>
<button type="button" className="btn btn-outline-success" onClick={() => goToDrawComp()}>Change Link</button>

</div>
);
Expand Down
30 changes: 21 additions & 9 deletions server/routes/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,28 @@ router.route('/:UserId/:GameMongoId').put(async (req, resp) => {
})

//Update lastStep
router.route('/:UserId/:GameId'+'/setLastStep').put(async (req, resp) => {
// router.route('/:UserId/:GameId'+'/updateLastStep').put(async (req, resp) => {

// console.log(req.body)

// // let answer = await gamesBL.gameByGameId(req.params.GameId);
// // let game = answer[0]
// // let steps = [...game.Steps];
// // let indexLastStep = steps.length - 1;
// // steps[indexLastStep] = updatedLastStep;
// // game.Steps = steps;
// // const GameMongoId = game._id;


// // const answer2 = await gamesBL.updateGame(GameMongoId , game);
// // return resp.json(answer2);
// })

// set Next Turn
router.route('/:UserId/:GameId'+'/setNextTurn').put(async (req, resp) => {
const updatedLastStep = req.body;
let answer = await gamesBL.gameByGameId(req.params.GameId);
let game = answer[0]
let steps = [...game.Steps];
let indexLastStep = steps.length - 1;
steps[indexLastStep] = updatedLastStep;
game.Steps = steps;
const GameMongoId = game._id;
const answer2 = await gamesBL.updateGame(GameMongoId , game);
const GameMongoId = updatedLastStep._id;
const answer2 = await gamesBL.updateGame(GameMongoId , updatedLastStep);
return resp.json(answer2);
})

Expand Down