iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 6
0
Software Development

線上娃娃機-js開發篇系列 第 7

線上娃娃機-server machine setup

恩昨天忘記發文 今年又GG了
不過還是把它寫完好了

import { ApolloServer } from 'apollo-server-express'

這一篇 主要是 server map 多台機器 的處理

這邊我們會用 new Machine 的方法去實作機器, 然後把每一台 new 出來的塞到 一個陣列去

這裡的比較重要的是與建立與娃娃機的連線

   conn = net.createConnection(configs.SOCKET_PORT, configs.SOCKET_HOST);
    conn.on('connect', () => { console.log('connect socket  server (register)', configs.SOCKET_HOST + ":" + configs.SOCKET_PORT); conn.write(Buffer.from([0])); });

class Machine {
    constructor(mac: any, conn: any, wawajiId: any, pubSub: any, connection:           Connection) {
        this.mac = mac;
        this.socket = conn;   // 建立與給娃娃機的 socket
        this.waitStartTimer = null;
        this.maintainPromise;
        this.currentMember; 
        this.uniqueKey; 
        this.roomInfo;
        this.machineStatus = 0;   
        this.connection = connection;  
        this.pubSub = pubsub;  
        this.wawajiId = wawajiId;  
        this.queue = []
        this.countDown = 10;  
        this.isCreateing = false;
        this.init();
    }
    init() {
        this._initRoomInfo(() => { this._queueUpdate(); });
     }
     _createNewGame(){
        //放置下方說明
     }
    ...略
   }
   

apollo server 的 mutation 這邊開放打入的資料 看是機器操作 或是投幣遊玩 加入排隊 跟移除排隊 等等

   
   
type Mutation{      
   machineCmd(wawajiId:Int,cmd:Int,cameraId:Int):status
   coinAndPlay(machineId:Int,coinWay:Int):CoinAndPlaystatus
   addPlayQueue(machineId:Int):[MachineMemberQueue]
   removePlayQueue(machineId:Int):[MachineMemberQueue]
    
}

以投幣開始遊玩為例 這邊傳入的 graphl資料對應 machine 塞入的陣列 allMachines.get(machine.node)

    coinAndPlay: async (root: any, args: any, { pubsub, connection, id, allMachines, machineMap }: { pubsub: any, connection: any, id: any, allMachines: any, machineMap: any }) => {

      const machine = await connection.getRepository(Machine).findOne(args.machineId)
      const member = await await connection.getRepository(Member).findOne(id)
      let coinWay = args.coinWay
      return allMachines.get(machine.node)._createNewGame(member, coinWay);

 

    }

在 找到陣列中的 machine執行 投幣動作 這一連串有許多判斷
例如 機器有無遊玩中,投幣速度太快有可能是機器人,是否是本人 接關 使用點數貨票卷遊玩或不足等等
大致程式碼如下


export const createNewGame = async (machine: Machine, member: Member, coinWay: number) => {
    const machineStatus = await machine.machineStatus


    //時間遊玩間隔不能連續按開始
    viewerUpdate(machine, member)
    if (member == null) { return { statu: 0, message: '連線超時請重新登入' } };
    if (!coinWay) { return { statu: 0, message: '請選擇付款方式' } };
    if (machine.queue?.length > 0 && machine.currentMember?.uuid! != member?.uuid) { return { status: 0, message: '請重新加入排序' } }

    try {
        if (machineStatus != 0) { return { status: 0, message: '機台有人在使用中無法開局' } }
        //costInfo 消費種類 票或是點數
        return await handlePayment(machine, coinWay, member, machineStatus);
    } catch (err) {
        console.log('startGameWork err', err);
        return { status: 0, message: '開局失敗' };
    }
 
}

這個章節主要說明 apollo 操作 machine 實體之間如何溝通,使用 socket .
以投幣為範例 當一連串的判斷動作 也是操作 machine實體 . 機器間不會相互影響


上一篇
Next.js 9.x
下一篇
線上娃娃機- apollo client init
系列文
線上娃娃機-js開發篇11
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言