ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Game end event
    Project using node.js/Cloning Catch-Mind 2020. 12. 18. 14:08

    Client에게 game이 끝났다는 것을 알리기

    // globalController.js
    
    let painter = null;
    let word = null;
    let timeout = null;
    
    export const socketController = (socket, io) => {
      const startGame = () => {
        if (sockets.length > 1) {
          superBroadcast(events.gameStarted);
          painter = choosePainter();
          word = chooseWord();
          io.to(painter.id).emit(events.painterNotif, { word });
          timeout = setTimeout(endGame, 30000); // timeout 
        }
      };
      
      const endGame = () => {
        if (timeout !== null) clearTimeout(timeout);
        superBroadcast(events.gameEnded);
        setTimeout(startGame, 2000);
      };
    };
    // events.js
    
    gameEnded: "gameEnded",

       30초가 지나면 game이 끝난다. 즉, 30초 안에 문제를 맞춰야 한다. endGame() 함수가 동작하면 events.gameEnded 이벤트를 보내고 다시 game을 재시작한다.

    Game이 끝났을 때 canvas reset

    // sockets.js
    
    import {
      handleGameEnded,
    } from "./player";
    
    export const initSocket = (aSocket) => {
      const { events } = window;
      socket = aSocket;
      socket.on(events.gameEnded, handleGameEnded);
    };
    // player.js
    
    import {
      resetCanvas
    } from "./paint";
    
    export const handleGameEnded = () => {
      resetCanvas();
    };

       game이 끝나면 canvas에 그려져 있던 모든 것을 지운다.

    // paint.js
    
    export const resetCanvas = () => fill("#fff");

     

    소스 코드

    github.com/zpskek/guessMind-v3/commit/e96314d1d3c47aa36c60d1b207d6a5f1d125020a

    'Project using node.js > Cloning Catch-Mind' 카테고리의 다른 글

    Game count down clock  (0) 2020.12.18
    Add points  (0) 2020.12.18
    Game Start event  (0) 2020.12.18
    Log out event with socketIO  (0) 2020.12.17
    Player update  (0) 2020.12.17

    댓글

Designed by Tistory.