Skip to content

Commit

Permalink
FIX: dropInterval logic in back
Browse files Browse the repository at this point in the history
  • Loading branch information
bauhaas committed Nov 17, 2022
1 parent 1615e1a commit dd820f7
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 31 deletions.
6 changes: 6 additions & 0 deletions client/src/components/PlayArea.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,14 @@ const PlayArea = ({ me, soloGameMode, setGameEnd, setGameStarted, gameStarted })
})

socket.on('playerMoved', (data) => {
console.log('playerMoved data received');
setGames(data);
})

socket.on('test', (data) => {
console.log(data);
})

socket.on('gameStart', (data) => {
console.log('gameStart event received');
setResults({});
Expand All @@ -58,6 +63,7 @@ const PlayArea = ({ me, soloGameMode, setGameEnd, setGameStarted, gameStarted })
socket.off('gamesInRoom');
socket.off('playerMoved');
socket.off('gameStart');
socket.off('test');
};
}, [games, location.state.roomId, soloGameMode, counter, setGameStarted,setCounter])

Expand Down
38 changes: 19 additions & 19 deletions client/src/components/Tetris.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,29 @@ import { socket } from './Menu';

const Tetris = ({start, name, game, me}) => {
const location = useLocation();
const [dropTime, setDropTime] = useState(null);
// const [dropTime, setDropTime] = useState(null);

useEffect(() => {
if (start === true)
setDropTime(500);
}, [start])
// useEffect(() => {
// if (start === true)
// setDropTime(500);
// }, [start])

useEffect(() => {
setDropTime(500 / (game.level + 1) + 400);
}, [game.level])
// useEffect(() => {
// setDropTime(500 / (game.level + 1) + 400);
// }, [game.level])

const drop = () => {
if (start === true && me === name && !game.gameOver)
{
console.log("emit drop");
socket.emit("drop", { id: location.state.roomId });
}
};
// const drop = () => {
// if (start === true && me === name && !game.gameOver)
// {
// console.log("emit drop");
// socket.emit("drop", { id: location.state.roomId });
// }
// };

useInterval(() => {
if(!game.gameOver)
drop();
}, dropTime);
// useInterval(() => {
// if(!game.gameOver)
// drop();
// }, dropTime);

const move = ({ keyCode }) => {
if (me === name && start === true && !game.gameOver)
Expand Down
48 changes: 48 additions & 0 deletions server/common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const Room = require('./Room');
const Client = require('./Client');
const { io } = require('./index')

function drop(room) {
const gamesToDrop = [...room.games]
for (const game of gamesToDrop) {
game.drop();
}
if (room.allPlayersHaveLost()) {
finishGame(room);
return;
}
io.in(room.id).emit("playerMoved", room.games);
}

const emitPlayersInRoom = (room) => {
const playersInRoom = [...room.clients].map(x => ({ name: x.name, status: x.readyToPlay, admin: x.admin }));
io.in(room.id).emit("playersInRoom", playersInRoom);
}

const emitResults = (room) => {
const highestScore = Math.max.apply(Math, room.games.map(function (o) { return o.score; }));
const winnerGame = room.games.find(function (o) { return o.score == highestScore; });
io.in(room.id).emit("results", { name: winnerGame.playerName, score: winnerGame.score });
clearInterval(dropInterval);
}

const unsetReadyToPlayStatus = (room) => {
room.hasStarted = false;
room.clients.forEach(client => {
client.unsetReady();
})
}

const finishGame = (room) => {
emitResults(room);
unsetReadyToPlayStatus(room);
emitPlayersInRoom(room);
}

let dropInterval;

const dropLoop = (room) => {
dropInterval = setInterval(drop, 1000, room);
}

module.exports = { dropLoop, emitResults, unsetReadyToPlayStatus, finishGame, drop}
18 changes: 9 additions & 9 deletions server/gameActionHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ module.exports = function (socket, rooms, client, io) {
}

socket.on('drop', (data) => {
console.log('receive drop', data, 'from', client.name);
const room = rooms.get(data.id);
const gameToUpdate = room.games.find(({ playerName }) => playerName === client.name);
gameToUpdate.drop();
if (room.allPlayersHaveLost()) {
finishGame(room);
return ;
}
io.in(room.id).emit("playerMoved", room.games);
// console.log('receive drop', data, 'from', client.name);
// const room = rooms.get(data.id);
// const gameToUpdate = room.games.find(({ playerName }) => playerName === client.name);
// gameToUpdate.drop();
// if (room.allPlayersHaveLost()) {
// finishGame(room);
// return ;
// }
// io.in(room.id).emit("playerMoved", room.games);
});

socket.on('move', (data) => {
Expand Down
10 changes: 7 additions & 3 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const helpers = require("./helpers");
const PORT = 4000;

const httpServer = require("http").createServer();
const io = require("socket.io")(httpServer, {
let io = require("socket.io")(httpServer, {
cors: {
origin: "http://localhost:3000"
}
Expand All @@ -17,8 +17,9 @@ app.use(cors());
const rooms = new Map;

io.on('connection', (socket) => {
console.log(`⚡: ${socket.id} user just connected!`);
const client = new Client(socket, helpers.createName())

console.log(`⚡: ${socket.id} user just connected!`);
const client = new Client(socket, helpers.createName())

socket.on('disconnect', () => {
console.log('🔥: A user disconnected');
Expand All @@ -36,9 +37,12 @@ io.on('connection', (socket) => {
require('./chatHandler')(socket, io);
require('./gameActionHandler')(socket, rooms, client, io);
require('./getRoomDataHandler')(socket, rooms, client, io);

return io;
});

exports.io = io;

httpServer.listen(PORT, () => {
console.log(`Server listening on ${PORT}`);
});
40 changes: 40 additions & 0 deletions server/roomHandler.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const Room = require('./Room');
const helpers = require("./helpers");
const common = require("./common");

module.exports = function (socket, rooms, client, io) {

Expand Down Expand Up @@ -94,11 +95,15 @@ module.exports = function (socket, rooms, client, io) {
client.unsetReady();
}

// let drop;

const startGame = (room) => {
if (room.allPlayersAreReady()) {
room.startGame();
console.log('emit gameStart');
io.in(room.id).emit("gameStart");
common.dropLoop(room);
// drop = setInterval(lol, 1000, room);
}
}

Expand All @@ -109,4 +114,39 @@ module.exports = function (socket, rooms, client, io) {
emitPlayersInRoom(room);
startGame(room);
});

// const emitResults = (room) => {
// const highestScore = Math.max.apply(Math, room.games.map(function (o) { return o.score; }));
// const winnerGame = room.games.find(function (o) { return o.score == highestScore; });
// io.in(room.id).emit("results", { name: winnerGame.playerName, score: winnerGame.score });
// clearInterval(drop);
// }

// const unsetReadyToPlayStatus = (room) => {
// room.hasStarted = false;
// room.clients.forEach(client => {
// client.unsetReady();
// })
// }

// const finishGame = (room) => {
// emitResults(room);
// unsetReadyToPlayStatus(room);
// emitPlayersInRoom(room);
// }

// function lol(room)
// {
// const gamesToDrop = [...room.games]
// for (const game of gamesToDrop) {
// game.drop();
// }
// if (room.allPlayersHaveLost()) {
// common.finishGame(room);
// return;
// }
// io.in(room.id).emit("playerMoved", room.games);
// }


};

0 comments on commit dd820f7

Please sign in to comment.