iT邦幫忙

2018 iT 邦幫忙鐵人賽
DAY 26
1
自我挑戰組

GAME30天系列 第 26

創建 (Game Manager

  • 分享至 

  • xImage
  •  

game_manager.js

必要變數

const generateQuestionList = require("../request/game_question"); //引入題目
const Manager = {}; //所有建立的遊戲房間都存在這

架構

//gamaManer funtion為接口, 從外部帶入user端傳來的資料
function gameManager ( connection, data ){
  //ROUTE_HANDLER 管理每種"type"要處理的流程, 例如connect
  const ROUTE_HANDLER = {
    connect (data) {
      ...
    },
    join (data) {
      ...
    },
    getQuestion (data) {
      ...
    },
    ...
  };
  
  _init();
  //在這裏初始化gameManager
  function _init() {
    try {
      (data && data.type) && ROUTE_HANDLER[data.type](data);
    }
    catch (error) {
      throw error;
    }
  }
}
module.exports = gameManager; //gameManager為接口

ROUTE_HANDLER 中各種狀態的處理
connect

connect (data) {
  try {
    const {userName, date, key} = data;
    console.log(`user: ${ userName },connect on: ${ new Date(date) }`)
    const res = {
      type : 'create',
      msg : '獲得房間id'
    }
    
    if ( key ) { //已經拿過key
      if ( Manager[key].type && Manager[key].type === 'wait' ) { //還在等, 再回傳一樣的key
        res.key = key
      }else { //房間已經不在, 
        res.key = _getKey()
      }
    }else {
      res.key = _getKey()
    }
    connection.send( JSON.stringify(res) )

    function _getKey() {
      let waitingRoomKey;
      for (const [index, key] of Object.keys(Manager).entries()) {
        if ( Manager[key].type === 'wait' ) {
          waitingRoomKey = key
          break;
        }
      }
      if( waitingRoomKey ) {
        return waitingRoomKey
      }else {
        Manager[date] = new Game(connection, date)
        Manager[date].type = 'wait'
        return date
      }
    }
  } catch (error) {
    connection.close()
    throw error
  }
}

join

join (data) {
  try {
    const {userName, roomId} = data; //roomId === key
    const GAME = Manager[roomId];
    console.log(`user: ${ userName },join in room: ${ roomId }`)

    GAME.join(userName).then(roomInfo => {
      const {id, playerA, playerB} = roomInfo;
      const res = {
        type : 'isMatch',
        id,
        playerA,
        playerB
      }
      GAME.isPlaying = true
      GAME.type = 'play'

      connection.send(JSON.stringify(res))
    }).catch(e => {
      console.log(e)
    })
  } catch (error) {
    connection.close()
    throw error
  }
},

getQuestion

getQuestion (data) {
  try {
    const {id} = data;
    const GAME = Manager[id];
    const question = GAME.getQuestion();
    let res = {
      id : id
    };
    console.log(`room: ${ id } asking question.`)

    if ( question ) {
      res = Object.assign(res, question)
      res.type = 'question'
      res.questionStep = GAME.questionStep
    }else {
      res.type = 'gameOver'
      res.score = GAME.score
      console.log(`GAME.score:${GAME.score}`)
    }
    connection.send(JSON.stringify(res))

  } catch (error) {
    connection.close()
    throw error
  }
},

answer

answer (data) {
  console.log('user is answer now')
  try {
    const GAME = Manager[data.id];
    GAME.setAnswerHistory(data)
    GAME.waitOtherPlayer().then((isAllReady, time) => {
      const res = GAME.answerList[GAME.questionStep]
      res.id = GAME.id
      res.type = 'result'
      connection.send(JSON.stringify(res))

      if (isAllReady) { //when player2 ready, isAllReady is true
        const score = GAME.getScore(time);
        GAME.countScore(score)
        GAME.questionStep += 1
      }
    })

  } catch (error) {
    connection.close()
    throw error
  }
}

上一篇
創建 (main
下一篇
創建 (Game物件
系列文
GAME30天30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言