Пример #1
0
    def begin_game(clients, room_id, is_first=False):
        users = UserTool.room_users(room_id)
        current_user = None
        index = 0
        flag = False
        next_flag = False

        for client, topic in zip(clients, TopicTool.get_topics()):
            if client:
                if is_first:
                    user = UserTool.update_user(client,
                                                topic=topic,
                                                status=Status.playing)
                else:
                    user = UserTool.current_user(client)
                if is_first and index == 0:
                    user = UserTool.update_user(client, status=Status.action)
                    current_user = user
                elif next_flag:
                    user = UserTool.update_user(client, status=Status.action)
                    current_user = user
                    flag = True
                    next_flag = False
                elif not is_first and not flag and user[
                        'status'] == Status.action:
                    user = UserTool.update_user(client, status=Status.playing)
                    next_flag = True
                p_id = int(user['p_id'])
                users[p_id] = user

            index += 1

        other_clients = []
        for client in clients:
            if client:
                user = UserTool.current_user(client)
                if user['status'] != Status.action:
                    other_clients.append(client)

        over = False
        if not is_first and not flag:  # game over
            over = True
            for client in clients:
                if client:
                    user = UserTool.update_user(client,
                                                topic=None,
                                                status=Status.waiting)
                    p_id = int(user['p_id'])
                    users[p_id] = user
            RoomTool.update_room(room_id, status=RoomStatus.ready)
        return {
            'room': RoomTool.update_room(room_id, users=users),
            'user': current_user,
            'over': over,
            'other_clients': other_clients
        }
Пример #2
0
    def __init__(self):
        handlers = [
            (r'/', GameHandler),
        ]

        self.handler = GameMessageHanlder()

        # init all rooms
        RoomTool.init_rooms()

        super(GameApplication, self).__init__(handlers)
Пример #3
0
    def chess_win(client, clients, i, j, army_type):
        room_id = UserTool.current_user(client)['r_id']
        room_clients = RoomTool.room_clients(clients, room_id)
        other_client = filter(lambda u: id(u) != id(client), room_clients)
        Packet(header='chess.down',
               data={
                   'i': i,
                   'j': j,
                   'army_type': army_type
               },
               msg=None).write_message_to(clients=other_client)
        Packet(header='chess.over', data=None,
               msg=None).write_message_to(clients=room_clients)

        army = u'白色方' if army_type == 'white' else u'黑色方'
        gm_say(u'%s获胜啦 5s后游戏结束' % army, room_clients)

        def game_over():
            room = RoomTool.game_over(room_clients, room_id)
            time.sleep(5)
            Packet(header='rooms.info', data=room,
                   msg=None).write_message_to(clients=clients)

        p = threading.Thread(target=game_over)
        p.start()
Пример #4
0
 def next_begin(self, data):
     room_id = data.get('room_id', None)
     game_type = RoomTool.room_by(room_id)['game_type']
     if game_type == 'draw':
         self.draw_handler.next_begin(self.client, self.clients)
     elif game_type == 'chess':
         self.chess_handler.next_begin(self.client, self.clients)
Пример #5
0
 def draw_clear(client, clients):
     other_clients = filter(
         lambda u: id(u) != id(client),
         RoomTool.room_clients(clients,
                               UserTool.current_user(client)['r_id']))
     Packet(header='draw.clear', data=None,
            msg=None).write_message_to(clients=other_clients)
Пример #6
0
    def leave(self, data):
        room_id, p_id = data.get('room_id', None), data.get('p_id', None)

        room = RoomTool.leave_room(self.client, room_id, p_id)
        Packet(header='rooms.leave', data=None,
               msg=None).write_message_to(self.client)
        Packet(header='rooms.info', data=room,
               msg=None).write_message_to(clients=self.clients)
Пример #7
0
 def change_position(self, data):
     room_id, op_id, p_id = data.get('room_id', None), data.get('op_id', None), data.get('p_id', None)
     try:
         op_id, p_id = int(op_id), int(p_id)
         room = RoomTool.change_position(self.client, room_id, op_id, p_id)
         if room is not None:
             Packet(header='rooms.info', data=room, msg=None).write_message_to(clients=self.clients)
     except TypeError as e:
         log(e)
Пример #8
0
    def draw_start(data, client, clients):
        color, weight = data.get('color', None), data.get('weight', None)

        other_clients = filter(
            lambda u: id(u) != id(client),
            RoomTool.room_clients(clients,
                                  UserTool.current_user(client)['r_id']))

        Packet(header='draw.begin', data={'color': color, 'weight': weight}, msg=None). \
            write_message_to(clients=other_clients)
Пример #9
0
    def next_begin(client, clients):
        r_id = UserTool.current_user(client)['r_id']
        room_clients = RoomTool.room_clients(clients, r_id)
        games = DrawHandler.begin_game(room_clients, r_id)
        room, current_user, over, other_clients = games['room'], games[
            'user'], games['over'], games['other_clients']

        RoomTool.update_room(r_id, cantip=False)

        if over:
            Packet(header='rooms.info', data=room,
                   msg=None).write_message_to(clients=clients)
            gm_say('本局游戏结束啦', room_clients)
        else:
            Packet(header='rooms.next', data=room,
                   msg=None).write_message_to(clients=clients)
            gm_say(u'接下去由 %s 作画' % current_user['rolename'], room_clients)

            DrawHandler.send_tips(current_user, other_clients)
Пример #10
0
    def begin(room, clients):
        r_id = int(room['r_id'])
        room_clients = RoomTool.room_clients(clients, r_id)
        room_user = DrawHandler.begin_game(room_clients, r_id, True)
        room, current_user, other_clients = room_user['room'], room_user[
            'user'], room_user['other_clients']

        Packet(header='rooms.begin', data=room,
               msg=None).write_message_to(clients=clients)
        gm_say(u'游戏开始啦, 由 %s 先开始' % current_user['rolename'], room_clients)
        DrawHandler.send_tips(current_user, other_clients)
Пример #11
0
 def draw_move(data, client, clients):
     x, y, width, height, r_id = (data.get('x', None), data.get('y', None),
                                  data.get('width',
                                           None), data.get('height', None),
                                  data.get('room_id', None))
     other_clients = filter(lambda u: id(u) != id(client),
                            RoomTool.room_clients(clients, r_id))
     x_r, y_r = float(x) / width, float(y) / height
     Packet(header='draw.move', data={
         'x': x_r,
         'y': y_r
     }, msg=None).write_message_to(clients=other_clients)
Пример #12
0
    def say_room(self, data):
        message, r_id = data.get('message', None), data.get('room_id', None)
        clients = RoomTool.room_clients(self.clients, r_id)

        topic = self.draw_handler.current_topic(r_id)

        user = UserTool.current_user(self.client)

        if topic is not None:
            self.draw_handler.judge(self.client, self.clients, topic, user, message)
        else:
            say(self.client, message, clients, 'rooms.message')
Пример #13
0
    def toggle(self):
        result = RoomTool.toggle(self.client)
        room, is_begin = result['room'], result['begin']

        room_type = room['game_type']

        Packet(header='rooms.info', data=room, msg=None).write_message_to(clients=self.clients)
        if is_begin:
            if room_type == 'draw':
                self.draw_handler.begin(room, self.clients)
            elif room_type == 'chess':
                self.chess_handler.begin(room, self.clients)
Пример #14
0
    def begin(room, clients):
        r_id = int(room['r_id'])
        room_clients = RoomTool.room_clients(clients, r_id)
        games = ChessHandler.begin_game(room_clients, r_id, True)
        room, current_user, begin_client = games['room'], games['user'], games[
            'begin_client']

        Packet(header='rooms.begin', data=room,
               msg=None).write_message_to(clients=clients)
        gm_say(u'游戏开始啦, 由黑色方 %s 先出手' % current_user['rolename'], room_clients)
        if begin_client:
            Packet(header='rooms.turned', data=room,
                   msg=None).write_message_to(begin_client)
Пример #15
0
    def chess_down(client, clients, i, j, army_type):
        other_client = filter(
            lambda u: id(u) != id(client),
            RoomTool.room_clients(clients,
                                  UserTool.current_user(client)['r_id']))
        Packet(header='chess.down',
               data={
                   'i': i,
                   'j': j,
                   'army_type': army_type
               },
               msg=None).write_message_to(clients=other_client)

        ChessHandler.next_begin(client, clients)
Пример #16
0
    def current_topic(r_id):

        room = RoomTool.room_by(r_id)

        if room['status'] != RoomStatus.playing:
            return None

        users = UserTool.room_users(r_id)

        for user in users:
            if user:
                if user.get('status', None) == Status.action:
                    return user['topic']

        return None
Пример #17
0
    def next_begin(client, clients):
        r_id = UserTool.current_user(client)['r_id']
        room_clients = RoomTool.room_clients(clients, r_id)
        games = ChessHandler.begin_game(room_clients, r_id)
        room, current_user, army, begin_client = games['room'], games[
            'user'], games['army'], games['begin_client']

        # RoomTool.update_room(r_id, cantip=False)

        Packet(header='rooms.next', data=room,
               msg=None).write_message_to(clients=clients)
        gm_say(u'接下去由%s %s 出手' % (army, current_user['rolename']),
               room_clients)
        if begin_client is not None:
            Packet(header='rooms.turned', data=None,
                   msg=None).write_message_to(begin_client)
Пример #18
0
    def login(self, data):
        username = data.get('username', None)
        password = data.get('password', None)
        results = UserTool.login(self.client, self.clients, username, password)
        current_users, user = UserTool.current_users(self.client, self.clients.values()), UserTool.current_user(self.client)
        result, msg = results['result'], results['msg']

        if result:
            # init room

            Packet(header='login.success', data=user, msg=msg).write_message_to(self.client)
            Packet(header='user.enter', data=user, msg=None).write_message_to(clients=self.clients)
            if len(current_users) > 0:
                Packet(header='user.all', data=current_users, msg=None).write_message_to(self.client)
            Packet(header='hall.init', data=RoomTool.rooms(), msg=None).write_message_to(self.client)
        else:
            Packet(header='login.error', data=None, msg=msg).write_message_to(self.client)
Пример #19
0
    def enter_room(self, data):
        room_id, p_id = data.get('room_id', None), data.get('p_id', None)

        # 1 get room
        room = RoomTool.enter_room(self.client, room_id, p_id)

        room_id = room["r_id"]

        Packet(header='rooms.info', data=room, msg=None).write_message_to(clients=self.clients)
        Packet(header='rooms.enter', data=room_id, msg=None).write_message_to(self.client)

        if room['game_type'] == 'draw':
            gm_say('欢迎来到聚会玩, 该房间将开启的游戏是 - 你画我猜', client=self.client)
        elif room['game_type'] == 'chess':
            gm_say('欢迎来到聚会玩, 该房间将开启的游戏是 - 五子棋', client=self.client)
            gm_say('游戏规则 - 上方为白色方 下方为黑色方, 黑色方先出棋, 黑色方选手下完轮到白色方, 如此交替进行', client=self.client)
        else:
            gm_say('欢迎来到聚会玩, 该房间将开启的游戏是 - UNO棋牌', client=self.client)
            gm_say('游戏规则 - ')
Пример #20
0
    def begin_game(clients, room_id, is_first=False):
        users = UserTool.room_users(room_id)

        current_user = None
        index = 0
        flag = False
        next_flag = False

        # 1 2       =>   2 1
        # 1 2 3 4   =>   3 1 4 2                0  2  4
        # 1 2 3 4 5 6 => 4 1 5 2 6 3        4 1 2 3 5 6    4 1 5 2 3 6
        seq_clients = []

        fil_clients = filter(lambda x: x, clients)

        mid = len(fil_clients) / 2

        is_black = True

        dict_clients = dict()

        for i in range(0, len(fil_clients)):
            dict_clients[i] = fil_clients[i]

        print dict_clients
        while len(filter(lambda x: x, dict_clients.values())) > 0:
            # print filter(lambda x: x, dict_clients.values())

            for key in dict_clients.keys():
                print type(key)
                if is_black and key >= mid and dict_clients[key] is not None:
                    seq_clients.append(dict_clients[key])
                    dict_clients[key] = None
                    print dict_clients
                    is_black = False
                    break
                if not is_black and key < mid and dict_clients[key] is not None:
                    seq_clients.append(dict_clients[key])
                    dict_clients[key] = None
                    is_black = True
                    break
            # print 1111

        print seq_clients
        for client in seq_clients:
            if client:
                if is_first:
                    user = UserTool.update_user(client, status=Status.playing)
                else:
                    user = UserTool.current_user(client)
                if is_first and index == 0:
                    user = UserTool.update_user(client, status=Status.action)
                    current_user = user
                elif next_flag:
                    user = UserTool.update_user(client, status=Status.action)
                    current_user = user
                    flag = True
                    next_flag = False
                elif not is_first and not flag and user[
                        'status'] == Status.action:
                    user = UserTool.update_user(client, status=Status.playing)
                    next_flag = True
                    print index

                p_id = int(user['p_id'])
                users[p_id] = user

            index += 1

        # over = False
        if not is_first and not flag:  # game over
            # over = True

            for client in seq_clients:

                if client:
                    user = UserTool.update_user(client, status=Status.action)
                    current_user = user
                    break

            try:
                p_id = int(user.get('p_id', None))
                users[p_id] = user
            except TypeError as e:
                pass

        begin_client = None

        other_clients = []
        index = 0
        for client in clients:
            if client:
                user = UserTool.current_user(client)
                if user['status'] != Status.action:
                    other_clients.append(client)
                else:
                    begin_client = client
                    action_army = u'白色方' if index < mid else u'黑色方'
            index += 1
        return {
            'room': RoomTool.update_room(room_id, users=users),
            'user': current_user,
            'army': action_army,
            'begin_client': begin_client
        }
Пример #21
0
 def game_over():
     room = RoomTool.game_over(room_clients, room_id)
     time.sleep(5)
     Packet(header='rooms.info', data=room,
            msg=None).write_message_to(clients=clients)