Exemple #1
0
    def route(self,conn, addr, data):
        action = data['action']

        if action == 'disconnect':
            player = self.playersMap[data["playerID"]]
            if player.roomID:
                del self.roomsMap[player.roomID]
            del self.playersMap[data["playerID"]]
            self.send(conn,True)
            conn.close()
            return False

        elif action == 'register':
            player = Player(data["name"], addr, conn)
            print(f"[NEW PLAYER] {player}")
            self.playersMap[player.playerID] = player
            self.send(conn, player.playerID)

        elif action == "createRoom":
            room = Room(data["name"])
            self.roomsMap[room.roomID] = room
            room.addPlayer(self.playersMap[data["playerID"]])
            print(f"[NEW ROOM] {room}")
            self.send(conn, room.roomID)

        elif action == "joinToRoom":
            room = self.roomsMap[data['roomID']]
            if room:
                player = self.playersMap[data["playerID"]]
                result = room.addPlayer(player)
                if result:
                    player.roomID = room.roomID
                    self.send(room.playerList[0].conn, {'action' : 'startGame'})
            else:
                result = False
            self.send(conn, result)

        elif action == "getRoomList":
            listRoom = []
            for room in self.roomsMap.values():
                listRoom.append([room.roomName, room.roomID, room.numberOfPlayers])
            self.send(conn, listRoom)

        elif action == 'currentPlayer':
            room = self.roomsMap[data['roomID']]
            self.send(conn, [room.currentPlayer.playerName, room.currentPlayer.playerID])

        elif action == 'changePlayer':
            room = self.roomsMap[data['roomID']]
            room.changePlayer()
            self.send(room.currentPlayer.conn, {'action' : 'changePlayer'})

        return True
Exemple #2
0
    async def handler(self, socket, path):
        self.clients.append(socket)
        print('Client connected')
        # 返回房间列表
        await socket.send(
            get_mesg({
                'mesg':
                'room_list',
                'room_list': [{
                    'id': i.roomId,
                    'type': i.type
                } for i in self.rooms]
            }))

        # 接收前端消息并转换成json对象
        data = await socket.recv()
        data = json.loads(data)
        print(data)

        # 判断是添加房间还是加入房间
        if data['mesg'] == 'add_room':
            room = Room(data['type'], data['args'], data['time'],
                        len(self.rooms) + 1)
            self.rooms.append(room)
            order = data['order']
            for i, c in enumerate(self.clients):
                if c != socket:
                    try:
                        await c.send(
                            get_mesg({
                                'mesg':
                                'room_list',
                                'room_list': [{
                                    'id': i.roomId,
                                    'type': i.type
                                } for i in self.rooms]
                            }))
                    except websockets.exceptions.ConnectionClosedOK:
                        self.clients.remove(c)

        elif data['mesg'] == 'join':
            room = self.rooms[int(data['room_id']) - 1]
            order = room.getOrder()
        playerId = room.addPlayer(socket, order)

        # 返回房间和玩家信息
        await socket.send(
            get_mesg({
                'mesg': data['mesg'],
                'playerId': playerId,
                'type': room.type,
                'order': order
            }))

        # 等待进一步操作(开始 / 移动 / 悔棋)
        while True:
            data = await socket.recv()
            data = json.loads(data)
            print(data)

            if (data['mesg'] == 'start'):
                await room.start(playerId)
            elif (data['mesg'] == 'move'):
                location = data['location']
                if data.get('kw') != None:
                    await room.move(playerId, location, data['kw'])
                else:
                    await room.move(playerId, location)
            elif (data['mesg'] == 'rollback'):
                await room.rollback()