Beispiel #1
0
 def on_close(self):
     print("WebSocket closed")
     # 在socket关掉的时候,把用户从房间里删除
     if self.isValid:
         core.socketPool[self.roomNo].pop(self.sid)
         key = redisKey % self.roomNo
         yield execRedis('hincrby', key, 'cnt', -1)
Beispiel #2
0
 def processDigAction(self, msg):
     result = yield AnswerHandler.digMime(msg, self.sid)
     if type(result) == int:
         print('digging')
         # 推送到房间内的所有人
         data = {
             'type': 'dig',
             'answer': result,
             'isMe': False, # 下面赋值
             'x': msg.get('x'),
             'y': msg.get('y')
         }
         sockets = core.socketPool[self.roomNo]
         # print(self.roomNo, sockets)
         key = redisKey % self.roomNo
         lefts = yield execRedis('hget', key, 'lefts')
         lefts = int(lefts)
         for sid in sockets:
             info = sockets[sid]
             ws = info.get('sock')
             data['isMe'] = (self==ws)
             ws.write_message(rspSuccess(data))
             # 判断是否是最后一个
             if result < 0 and lefts <= 0: # 结束了
                 score = info.get('score')
                 endData = {
                     'type': 'over',
                     'score': info['score']
                 }
                 ws.write_message(rspSuccess(endData))
         print('push done')
Beispiel #3
0
 def createRoom(self, data):
     # data = self.request.body
     roomNo = None
     if data:
         # data = JSON.loads(data)
         roomNo = data.get('no')
     sid = data.get('sid')
     key = redisKey % roomNo
     room = yield execRedis('hgetall', key)
     if room:  # 房间已经开了
         userCnt = int(room.get('cnt', 0))
         mimeData = room.get('data', 0)
         if userCnt < maxUserCnt:
             userCnt += 1  # TODO: 这里可以记录另一个玩家的信息
             yield execRedis('hset', key, 'cnt', userCnt)
             lefts = room.get('lefts', 0)
             rspData = {'map': JSON.loads(mimeData), 'count': lefts}
             # self.write(rspSuccess(rspData))
             raise gen.Return(rspData)
         else:
             # self.write(rspError(u'房间%s已经满员' % roomNo))
             raise gen.Return(u'房间%s已经满员' % roomNo)
             # self.finish()
             # return
     else:  # 新房间
         mimeData = genMimeArr()
         initData = getInitArr(9)
         room = {  # TODO: 这里可以记录用户的信息
             'createTime': int(time() * 1000),
             'cnt': 1,
             'online': True,
             'lefts': mimeCnt,
             'answer': JSON.dumps(mimeData),
             'data': JSON.dumps(initData),
             'curId': sid,
             'lastId': 0
         }
         yield execRedis('hmset', key, data=room,
                         expire=60 * 60 * 12)  # 12小时
         rspData = {'map': initData, 'count': mimeCnt}
         print('room number: ', roomNo)
         # self.write(rspSuccess(rspData))
         raise gen.Return(rspData)
 def digMime(self, data, sid):
     x = y = -1
     roomNo = 0
     if data:
         # data = JSON.loads(data)
         x = int(data.get('x'))
         y = int(data.get('y'))
         roomNo = int(data.get('no'))
     if x < 0 or y < 0 or not roomNo:
         raise gen.Return('x or y or roomNo is invalid')
     key = redisKey % roomNo
     room = yield execRedis('hgetall', key)
     lastPlayer = int(room.get('lastId'))
     isValid = True
     if lastPlayer:
         print(lastPlayer, sid)
         if sid == lastPlayer:
             isValid = False
     else:
         curPlayer = int(room.get('curId'))
         print(curPlayer, sid)
         if sid != curPlayer:  # 不用管curId不存在的bug,随便谁点谁开始
             isValid = False
     if not isValid:
         raise gen.Return(u'请等待下轮再操作')
     answer = JSON.loads(room.get('answer'))
     data = JSON.loads(room.get('data'))
     print answer, data
     result = answer[y][x]
     data[y][x] = result
     # update redis
     updateData = {'data': JSON.dumps(data), 'lastId': sid}
     yield execRedis('hmset', key, data=updateData)
     if result < 0:  # 挖到金子了,要把剩余的个数减1
         yield execRedis('hincrby', key, 'lefts', -1)
         info = core.socketPool[roomNo][sid]
         info['score'] += 1
     raise gen.Return(result)