Esempio n. 1
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()
Esempio n. 2
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)
Esempio n. 3
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)
Esempio n. 4
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
        }
Esempio n. 5
0
    def register(self, data):

        username, password, role_name, icon = data.get('username', None), data.get('password', None), data.get(
            'rolename', None), data.get('icon', None).encode("ascii")
        log(data)
        reason = UserTool.register(username, password, role_name, icon)
        result, msg = reason['result'], reason['msg']
        Packet(header='register.success', data=None, msg=msg).write_message_to(self.client) if result else Packet(
            header='register.error', data=None, msg=msg).write_message_to(self.client)
Esempio n. 6
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)
Esempio n. 7
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')
Esempio n. 8
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)
Esempio n. 9
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
Esempio n. 10
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)
Esempio n. 11
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)
Esempio n. 12
0
 def quit(self, client):
     if UserTool.is_login(client):
         UserTool.quit(client, self.clients)
Esempio n. 13
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
        }