def analysis_message_login(sock, cmd):
    """
    Login Message
    @param sock:
    @param cmd:
    @return:
    """
    if db.check_user_login(cmd.get_string(Argument.ARG_PLAYER_USERNAME),
                           cmd.get_string(Argument.ARG_PLAYER_PASSWORD)):
        if cmd.get_string(Argument.ARG_PLAYER_USERNAME) in name_sock_map.keys():
            #Send message disconnect to old client
            old_sock = name_sock_map[cmd.get_string(Argument.ARG_PLAYER_USERNAME)]
            disconnect_cmd = Command(Command.CMD_DISCONNECT)
            disconnect_cmd.add_int(Argument.ARG_CODE, 1)
            disconnect_cmd.add_string(Argument.ARG_MESSAGE, Message.MSG_DISCONNECT)
            send(old_sock, disconnect_cmd)
            #Send message login success to new client
            """Add player to list"""
            name_sock_map[cmd.get_string(Argument.ARG_PLAYER_USERNAME)] = sock
            sock_name_map[sock] = cmd.get_string(Argument.ARG_PLAYER_USERNAME)
            sock_name_map.pop(old_sock)
            send_cmd = Command(Command.CMD_LOGIN)
            send_cmd.add_int(Argument.ARG_CODE, 1)
            send(sock, send_cmd)
            """Send player info"""
            info = db.get_user_info(cmd.get_string(Argument.ARG_PLAYER_USERNAME))
            send_cmd = Command(Command.CMD_PLAYER_INFO)
            send_cmd.add_int(Argument.ARG_PLAYER_LEVEL, int(info["u_level"]))
            send_cmd.add_int(Argument.ARG_PLAYER_LEVEL_UP_POINT, int(info["u_levelup_point"]))
            send_cmd.add_int(Argument.ARG_PLAYER_CUP, int(info["u_cup"]))
            #TODO need modify
            send_cmd.add_int(Argument.ARG_PLAYER_LEVEL_UP_REQUIRE, 1000)
            send(sock, send_cmd)
            pass
        else:
            """Add player to list"""
            name_sock_map[cmd.get_string(Argument.ARG_PLAYER_USERNAME)] = sock
            sock_name_map[sock] = cmd.get_string(Argument.ARG_PLAYER_USERNAME)
            send_cmd = Command(Command.CMD_LOGIN)
            send_cmd.add_int(Argument.ARG_CODE, 1)
            send(sock, send_cmd)
            """Send player info"""
            info = db.get_user_info(cmd.get_string(Argument.ARG_PLAYER_USERNAME))
            send_cmd = Command(Command.CMD_PLAYER_INFO)
            send_cmd.add_int(Argument.ARG_PLAYER_LEVEL, int(info["u_level"]))
            send_cmd.add_int(Argument.ARG_PLAYER_LEVEL_UP_POINT, int(info["u_levelup_point"]))
            send_cmd.add_int(Argument.ARG_PLAYER_CUP, int(info["u_cup"]))
            #TODO need modify
            send_cmd.add_int(Argument.ARG_PLAYER_LEVEL_UP_REQUIRE, 1000)
            send(sock, send_cmd)
    else:
        send_cmd = Command(Command.CMD_LOGIN)
        send_cmd.add_int(Argument.ARG_CODE, 0)
        send_cmd.add_string(Argument.ARG_MESSAGE, "Invalid login info, please check again or register first!")
        send(sock, send_cmd)
Пример #2
0
def read(sock, data):
    try:
        """
        Analysis command
        @param sock the socket of client
        @param data the read data
        """
        read_count = 0
        code = int(unpack("<H", data[0:2])[0])
        read_count += 2
        cmd = Command(code)
        num_arg = int(unpack("<H", data[2:4])[0])
        read_count += 2
        for i in range(0, num_arg, 1):
            """Read all argument"""
            arg_code = int(unpack("<H", data[read_count:read_count+2])[0])
            read_count += 2
            #Argument type
            arg_type = int(unpack("B", data[read_count:read_count + 1])[0])
            read_count += 1
            if arg_type == Argument.STRING:
                #string len
                str_len = int(unpack("<I", data[read_count:read_count + 4])[0])
                read_count += 4
                str_val = str(unpack(str(str_len)+"s", data[read_count:read_count + str_len])[0])
                read_count += str_len
                cmd.add_string(arg_code, str_val)
            elif arg_type == Argument.RAW:
                raw_len = int(unpack("<I", data[read_count:read_count + 4])[0])
                read_count += 4
                raw_data = data[read_count, read_count + raw_len]
                read_count += raw_len
                cmd.add_raw(arg_code, raw_data)
            elif arg_type == Argument.BYTE:
                byte_val = int(unpack("B", data[read_count:read_count + 1])[0])
                read_count += 1
                cmd.add_byte(arg_code, byte_val)
            elif arg_type == Argument.SHORT:
                short_val = int(unpack("<H", data[read_count:read_count + 2])[0])
                read_count += 2
                cmd.add_short(arg_code, short_val)
            elif arg_type == Argument.INT:
                int_val = int(unpack("<i", data[read_count:read_count + 4])[0])
                read_count += 4
                cmd.add_int(arg_code, int_val)
            elif arg_type == Argument.LONG:
                long_val = long(unpack("<L", data[read_count:read_count + 8])[0])
                read_count += 8
                cmd.add_long(arg_code, long_val)
        analysis_message(sock, cmd)
    except Exception:
        pass
    pass
Пример #3
0
def analysis_message_player_move(sock,cmd):
    room_id = cmd.get_int(Argument.ARG_ROOM_ID, 0)
    room = room_list.get(room_id)
    if None != room:
        #move_from = cmd.get_int(Argument.ARG_MOVE_FROM)
        move_to = cmd.get_int(Argument.ARG_MOVE_TO)
        send_cmd = Command(Command.CMD_PLAYER_MOVE)
        send_cmd.add_string(Argument.ARG_PLAYER_USERNAME, sock_name_map.get(sock))
        #send_cmd.add_int(Argument.ARG_MOVE_FROM, move_from)
        send_cmd.add_int(Argument.ARG_MOVE_TO, move_to)
        send(room.sock1, send_cmd)
        send(room.sock2, send_cmd)
        pass
def analysis_message_chat(sock, cmd):
    """
    Chat message
    @param sock:
    @param cmd:
    @return:
    """
    from_user = sock_name_map[sock]
    to_user = cmd.get_string(Argument.ARG_PLAYER_USERNAME, "no name")
    message = cmd.get_string(Argument.ARG_MESSAGE, "no content")
    send_cmd = Command(Command.CMD_PLAYER_CHAT)
    send_cmd.add_string(Argument.ARG_PLAYER_USERNAME, from_user)
    send_cmd.add_string(Argument.ARG_MESSAGE, message)
    if check_player_online(to_user):
        name_sock_map[to_user].sendall(send_cmd.get_bytes())
    pass
def analysis_message_add_friend(sock, cmd):
    """
    Add friend message
    @param sock:
    @param cmd:
    @return:
    """
    send_cmd = Command(Command.CMD_ADD_FRIEND)
    if db.invite_friend(cmd.get_string(Argument.ARG_PLAYER_USERNAME), cmd.get_string(Argument.ARG_PLAYER_USERNAME)):
        send_cmd.add_int(Argument.ARG_CODE, 1)
        send_cmd.add_string(Argument.ARG_MESSAGE, "Send invite friend successful!")
        #TODO send to friend invite message
    else:
        send_cmd.add_int(Argument.ARG_CODE, 0)
        send_cmd.add_string(Argument.ARG_MESSAGE, "Send invite friend failure! Please try again!")
    sock.sendall(send_cmd.get_bytes())
Пример #6
0
def analysis_message_turn_timeout(sock, cmd):
    room_id = cmd.get_int(Argument.ARG_ROOM_ID, 0)
    room = room_list.get(room_id)
    if None != room:
        if sock == room.sock1:
            change_turn = Command(Command.CMD_PLAYER_TURN)
            change_turn.add_string(Argument.ARG_PLAYER_USERNAME, sock_name_map.get(room.sock2))
            send(room.sock1, change_turn)
            send(room.sock2, change_turn)
            pass
        else:
            change_turn = Command(Command.CMD_PLAYER_TURN)
            change_turn.add_string(Argument.ARG_PLAYER_USERNAME, sock_name_map.get(room.sock1))
            send(room.sock1, change_turn)
            send(room.sock2, change_turn)
            pass
        pass
Пример #7
0
 def read(self):
     read_count = 0
     code = int(unpack("<H", self.data[0:2])[0])
     read_count += 2
     cmd = Command(code)
     num_arg = int(unpack("<H", self.data[2:4])[0])
     read_count += 2
     for i in range(0, num_arg, 1):
         #Read all argument
         arg_code = int(unpack("<H", self.data[read_count:read_count+2])[0])
         read_count += 2
         #Argument type
         arg_type = int(unpack("B", self.data[read_count:read_count + 1])[0])
         read_count += 1
         if arg_type == Argument.STRING:
             #string len
             str_len = int(unpack("<I", self.data[read_count:read_count + 4])[0])
             read_count += 4
             str_val = str(unpack(str(str_len)+"s", self.data[read_count:read_count + str_len])[0])
             read_count += str_len
             cmd.add_string(arg_code, str_val)
         elif arg_type == Argument.RAW:
             raw_len = int(unpack("<I", self.data[read_count:read_count + 4])[0])
             read_count += 4
             raw_data = self.data[read_count, read_count + raw_len]
             read_count += raw_len
             cmd.add_raw(arg_code, raw_data)
         elif arg_type == Argument.BYTE:
             byte_val = int(unpack("B", self.data[read_count:read_count + 1])[0])
             read_count += 1
             self.add_byte(arg_code, byte_val)
         elif arg_type == Argument.SHORT:
             short_val = int(unpack("<H", self.data[read_count:read_count + 2])[0])
             read_count += 2
             cmd.add_short(arg_code, short_val)
         elif arg_type == Argument.INT:
             int_val = int(unpack("<I", self.data[read_count:read_count + 4])[0])
             read_count += 4
             cmd.add_int(arg_code, int_val)
         elif arg_type == Argument.LONG:
             long_val = long(unpack("<L", self.data[read_count:read_count + 8])[0])
             read_count += 8
             cmd.add_long(arg_code, long_val)
     print cmd.get_log()
def thread_game_matching(sleep_time=0):
    #TODO
    while reading:
        #In one time, send message matched game for 2 user.
        # After that pop that from waiting_list
        if len(waiting_list) >= 2:
            #Send to user1
            cmd1 = Command(Command.CMD_GAME_MATCHING)
            cmd1.add_int(Argument.ARG_CODE, 1)
            send(waiting_list[0], cmd1)
            "Send other player info"
            user2_name = sock_name_map.get(waiting_list[1])
            info = db.get_user_info(user2_name)
            user2_info = Command(Command.CMD_PLAYER_INFO)
            user2_info.add_string(Argument.ARG_PLAYER_USERNAME, str(info["u_username"]))
            user2_info.add_int(Argument.ARG_PLAYER_LEVEL, int(info["u_level"]))
            user2_info.add_int(Argument.ARG_PLAYER_LEVEL_UP_POINT, int(info["u_levelup_point"]))
            user2_info.add_int(Argument.ARG_PLAYER_CUP, int(info["u_cup"]))
            user2_info.add_int(Argument.ARG_PLAYER_LEVEL_UP_REQUIRE, 1000)
            send(waiting_list[0], user2_info)
            #Send to user2
            cmd2 = Command(Command.CMD_GAME_MATCHING)
            cmd2.add_int(Argument.ARG_CODE, 1)
            send(waiting_list[1], cmd2)
            "Send other player info"
            user1_name = sock_name_map.get(waiting_list[0])
            info = db.get_user_info(user1_name)
            user1_info = Command(Command.CMD_PLAYER_INFO)
            user1_info.add_string(Argument.ARG_PLAYER_USERNAME, str(info["u_username"]))
            user1_info.add_int(Argument.ARG_PLAYER_LEVEL, int(info["u_level"]))
            user1_info.add_int(Argument.ARG_PLAYER_LEVEL_UP_POINT, int(info["u_levelup_point"]))
            user1_info.add_int(Argument.ARG_PLAYER_CUP, int(info["u_cup"]))
            user1_info.add_int(Argument.ARG_PLAYER_LEVEL_UP_REQUIRE, 1000)
            send(waiting_list[0], user1_info)
            "Remove from Waiting_List"
            waiting_list.pop(0)
            waiting_list.pop(1)
            log.log("Remove from waiting_list")
            pass
    pass
Пример #9
0
def analysis_message_player_drop(sock, cmd):
    room_id = cmd.get_int(Argument.ARG_ROOM_ID, 0)
    room = room_list.get(room_id)
    if None != room:
        rotation = cmd.get_string(Argument.ARG_DROP_ROTATION, "")
        vel_x = cmd.get_int(Argument.ARG_DROP_VEL_X, 0)
        vel_y = cmd.get_int(Argument.ARG_DROP_VEL_Y, 0)
        send_cmd = Command(Command.CMD_PLAYER_DROP)
        send_cmd.add_string(Argument.ARG_PLAYER_USERNAME, sock_name_map.get(sock))
        send_cmd.add_int(Argument.ARG_DROP_VEL_X, vel_x)
        send_cmd.add_int(Argument.ARG_DROP_VEL_Y, vel_y)
        send_cmd.add_string(Argument.ARG_DROP_ROTATION, rotation)
        if sock == room.sock1:
            send(room.sock1, send_cmd)
            send(room.sock2, send_cmd)
            pass
        else:
            send(room.sock2, send_cmd)
            send(room.sock1, send_cmd)
            pass
        pass
    pass
Пример #10
0
def analysis_message_add_friend(sock, cmd):
    """
    Add friend message
    @param sock:
    @param cmd:
    @return:
    """
    if db.invite_friend(cmd.get_string(Argument.ARG_PLAYER_USERNAME), cmd.get_string(Argument.ARG_PLAYER_USERNAME)):
        send_cmd = Command(Command.CMD_ADD_FRIEND)
        send_cmd.add_int(Argument.ARG_CODE, 1)
        send_cmd.add_string(Argument.ARG_MESSAGE, "Send invite friend successful!")
        send(sock, send_cmd)
        send_cmd = Command(Command.CMD_ADD_FRIEND)
        send_cmd.add_int(Argument.ARG_CODE, 2)
        send_cmd.add_string(Argument.ARG_MESSAGE, "You have a friend request from "+str(sock_name_map.get(sock)))
        send_cmd.add_string(Argument.ARG_PLAYER_USERNAME, str(sock_name_map.get(sock)))
        send(name_sock_map.get(cmd.get_string(Argument.ARG_PLAYER_USERNAME)), send_cmd)
        pass
    else:
        send_cmd = Command(Command.CMD_ADD_FRIEND)
        send_cmd.add_int(Argument.ARG_CODE, 0)
        send_cmd.add_string(Argument.ARG_MESSAGE, "Send invite friend failure! Please try again!")
        send(sock, send_cmd)
        pass
Пример #11
0
def analysis_message_game_ready(sock, cmd):
    is_ready = cmd.get_int(Argument.ARG_CODE, 0)
    room_id = cmd.get_int(Argument.ARG_ROOM_ID, 0)
    """Search sock in room_info list"""
    room = room_list.get(room_id)
    if None != room:
        if room.sock1 == sock:
            #Send to opponent sock
            ready_cmd = Command(Command.CMD_GAME_READY)
            ready_cmd.add_string(Argument.ARG_PLAYER_USERNAME, sock_name_map.get(sock, "no name"))
            ready_cmd.add_byte(Argument.ARG_CODE, is_ready)
            send(room.sock2, ready_cmd)
            if is_ready == 1:
                room.ready[0] = True
            else:
                room.ready[0] = False

        elif room.sock2 == sock:
            ready_cmd = Command(Command.CMD_GAME_READY)
            ready_cmd.add_string(Argument.ARG_PLAYER_USERNAME, sock_name_map.get(sock, "no name"))
            ready_cmd.add_byte(Argument.ARG_CODE, is_ready)
            send(room.sock1, ready_cmd)
            #Send to opponent sock
            if is_ready == 1:
                room.ready[1] = True
            else:
                room.ready[1] = False

        if room.is_all_ready():
            "Send start game message if 2 player ready"
            start_cmd = Command(Command.CMD_GAME_START)
            send(room.sock1, start_cmd)
            send(room.sock2, start_cmd)
            "Generate map"
            map_cmd = Command(Command.CMD_MAP_INFO)
            map_cmd.add_string(Argument.ARG_PLAYER_USERNAME, sock_name_map[room.sock1])
            # map_cmd.add_string(Argument.ARG_PLAYER_USERNAME, "linhnv")
            map_id = random.randint(1, 2)
            map_cmd.add_int(Argument.ARG_MAP_ID, map_id)
            send(room.sock1, map_cmd)
            send(room.sock2, map_cmd)
            pass
        pass
    pass
Пример #12
0
def analysis_message_player_drop_result(sock, cmd):
    room_id = cmd.get_int(Argument.ARG_ROOM_ID, 0)
    room = room_list.get(room_id)
    if None == room:
        return
    code = cmd.get_int(Argument.ARG_CODE, 0)

    if code == 1:
        obj_type = cmd.get_int(Argument.ARG_MAP_OBJ_TYPE, 0)
        add_score = Command(Command.CMD_ADD_SCORE)
        add_score.add_string(Argument.ARG_PLAYER_USERNAME, sock_name_map.get(sock))
        if obj_type == -1:
            if room.sock1 == sock:
                if room.score[0] > 100:
                    room.score[0] += -100
                else:
                    room.score[0] = 0
                pass
            else:
                if room.score[1] > 100:
                    room.score[1] += 100
                else:
                    room.score[1] = 0
                pass
            add_score.add_int(Argument.ARG_SCORE, -100)
            pass
        else:
            if room.sock1 == sock:
                add = 0
                if obj_type == 1:
                    add = 10
                    pass
                elif obj_type == 2:
                    add = 50
                    pass
                elif obj_type == 3:
                    add = 250
                    pass
                elif obj_type == 4:
                    add = 500
                    pass
                elif obj_type == 5:
                    add = 660
                    pass
                room.score[0] += add
                add_score.add_int(Argument.ARG_SCORE, add)
                pass
            else:
                add = 0
                if obj_type == 1:
                    add = 10
                    pass
                elif obj_type == 2:
                    add = 50
                    pass
                elif obj_type == 3:
                    add = 250
                    pass
                elif obj_type == 4:
                    add = 500
                    pass
                elif obj_type == 5:
                    add = 660
                    pass
                room.score[1] += add
                add_score.add_int(Argument.ARG_SCORE, add)
                pass
        send(room.sock1, add_score)
        send(room.sock2, add_score)
        pass
    "Change player turn"
    if room.sock1 == sock:
        change_turn = Command(Command.CMD_PLAYER_TURN)
        change_turn.add_string(Argument.ARG_PLAYER_USERNAME, sock_name_map.get(room.sock2))
        send(room.sock1, change_turn)
        send(room.sock2, change_turn)
        pass
    elif room.sock2 == sock:
        change_turn = Command(Command.CMD_PLAYER_TURN)
        change_turn.add_string(Argument.ARG_PLAYER_USERNAME, sock_name_map.get(room.sock1))
        send(room.sock1, change_turn)
        send(room.sock2, change_turn)
        pass
    pass
Пример #13
0
def gen_msg_join(username=""):
    msg = Command(Command.CMD_GAME_MATCHING)
    msg.add_string(Argument.ARG_PLAYER_USERNAME, username)
    return msg
Пример #14
0
def gen_msg_register(username="", password=""):
    msg = Command(Command.CMD_REGISTER)
    msg.add_string(Argument.ARG_PLAYER_USERNAME, username)
    msg.add_string(Argument.ARG_PLAYER_PASSWORD, password)
    return msg
Пример #15
0
    log.log("Send: "+cmd.get_log())
    sock.sendall(cmd.get_bytes())
    pass

HOST, PORT = "182.48.50.239", 5555
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((HOST, PORT))
# cmd = Command(Command.CMD_ADD_FRIEND)
# cmd.add_string(Argument.ARG_PLAYER_USERNAME, "giapnh")

cmd = Command(Command.CMD_LOGIN)
# username = raw_input("Username:"******"Password:"******"giapnh"
password = "******"
cmd.add_string(Argument.ARG_PLAYER_USERNAME, username)
cmd.add_string(Argument.ARG_PLAYER_PASSWORD, password)
sock.sendall(cmd.get_bytes())
data = sock.recv(1024)
read(data)
data = sock.recv(1024)
read(data)
send(sock, message_helper.gen_msg_join(username))
data = sock.recv(1024)
read(data)
data = sock.recv(1024)
read(data)
data = sock.recv(1024)
read(data)
cmd = Command(Command.CMD_GAME_READY)
cmd.add_int(Argument.ARG_CODE, 1)
Пример #16
0
def thread_game_matching(sleep_time=0):
    room_id = 0
    while reading:
        time.sleep(sleep_time)
        #In one time, send message matched game for 2 user.
        # After that pop that from waiting_list
        if len(waiting_list) >= 2:
            "Increment room_id 1 unit"
            room_id += 1
            "Create RoomInfo object and add to RoomList"
            room_info = RoomInfo(room_id, waiting_list[len(waiting_list) - 1], waiting_list[len(waiting_list)-2])
            room_list[room_id] = room_info
            "Send message join room to user 1"
            matching_cmd = Command(Command.CMD_GAME_MATCHING)
            matching_cmd.add_int(Argument.ARG_CODE, 1)
            send(waiting_list[len(waiting_list) - 1], matching_cmd)

            cmd1 = Command(Command.CMD_ROOM_INFO)
            cmd1.add_string(Argument.ARG_PLAYER_USERNAME, str(sock_name_map.get(room_info.sock1)))
            cmd1.add_int(Argument.ARG_ROOM_ID, room_id)
            cmd1.add_int(Argument.ARG_CUP_WIN, 5)
            cmd1.add_int(Argument.ARG_CUP_LOST, -5)

            send(waiting_list[len(waiting_list) - 1], cmd1)
            "Send other player info"
            user2_name = sock_name_map.get(waiting_list[len(waiting_list) - 2])
            info = db.get_user_info(user2_name)
            user2_info = Command(Command.CMD_FRIEND_INFO)
            user2_info.add_string(Argument.ARG_PLAYER_USERNAME, str(info["username"]))
            user2_info.add_int(Argument.ARG_PLAYER_LEVEL, int(info["level"]))
            user2_info.add_int(Argument.ARG_PLAYER_LEVEL_UP_POINT, int(info["levelup_point"]))
            user2_info.add_int(Argument.ARG_PLAYER_CUP, int(info["cup"]))
            user2_info.add_int(Argument.ARG_PLAYER_LEVEL_UP_REQUIRE, int(info["require_point"]))
            user2_info.add_int(Argument.ARG_PLAYER_SPEED_MOVE, int(info["speed_move"]))
            user2_info.add_int(Argument.ARG_PLAYER_SPEED_DRAG, int(info["speed_drag"]))
            user2_info.add_int(Argument.ARG_PLAYER_SPEED_DROP, int(info["speed_drop"]))
            send(waiting_list[len(waiting_list) - 1], user2_info)

            "Send message join room to user2"
            cmd2 = Command(Command.CMD_GAME_MATCHING)
            cmd2.add_int(Argument.ARG_CODE, 1)
            send(waiting_list[len(waiting_list) - 2], cmd2)
            cmd2 = Command(Command.CMD_ROOM_INFO)
            cmd2.add_string(Argument.ARG_PLAYER_USERNAME, str(sock_name_map.get(room_info.sock1)))
            cmd2.add_int(Argument.ARG_ROOM_ID, room_id)
            cmd2.add_int(Argument.ARG_CUP_WIN, 5)
            cmd2.add_int(Argument.ARG_CUP_LOST, -5)
            send(waiting_list[len(waiting_list) - 2], cmd2)
            "Send other player info"
            user1_name = sock_name_map.get(waiting_list[len(waiting_list) - 1])
            info = db.get_user_info(user1_name)
            user1_info = Command(Command.CMD_FRIEND_INFO)
            user1_info.add_string(Argument.ARG_PLAYER_USERNAME, str(info["username"]))
            user1_info.add_int(Argument.ARG_PLAYER_LEVEL, int(info["level"]))
            user1_info.add_int(Argument.ARG_PLAYER_LEVEL_UP_POINT, int(info["levelup_point"]))
            user1_info.add_int(Argument.ARG_PLAYER_CUP, int(info["cup"]))
            user1_info.add_int(Argument.ARG_PLAYER_LEVEL_UP_REQUIRE, int(info["require_point"]))
            user1_info.add_int(Argument.ARG_PLAYER_SPEED_MOVE, int(info["speed_move"]))
            user1_info.add_int(Argument.ARG_PLAYER_SPEED_DRAG, int(info["speed_drag"]))
            user1_info.add_int(Argument.ARG_PLAYER_SPEED_DROP, int(info["speed_drop"]))
            send(waiting_list[len(waiting_list) - 2], user1_info)
            "Add to room_list"
            room_list[room_id] = RoomInfo(room_id, waiting_list[len(waiting_list) - 1], waiting_list[len(waiting_list)-2])
            log.log("Apend new room")
            "Remove from Waiting_List"
            waiting_list.pop()
            waiting_list.pop()
            log.log("Remove from waiting_list; Now waiting list size = " + str(len(waiting_list)))
            pass
    pass
Пример #17
0
def analysis_message_game_finish(sock, cmd):
    room_id = cmd.get_int(Argument.ARG_ROOM_ID, 0)
    room = room_list[room_id]
    if None != room:
        send_cmd = Command(Command.CMD_GAME_FINISH)
        if room.score[0] > room.score[1]:
            send_cmd.add_int(Argument.ARG_CODE, 1)
            send_cmd.add_string(Argument.ARG_PLAYER_USERNAME, sock_name_map.get(room.sock1))
            pass
        elif room.score[0] < room.score[1]:
            send_cmd.add_int(Argument.ARG_CODE, 1)
            send_cmd.add_int(Argument.ARG_PLAYER_USERNAME, sock_name_map.get(room.sock2))
            pass
        else:
            send_cmd.add_int(Argument.ARG_CODE, 0)
            pass
        send(room.sock1, send_cmd)
        send(room.sock2, send_cmd)
        "Send match result"
        "Player 1"
        player1_result = Command(Command.CMD_PLAYER_GAME_RESULT)
        player1_result.add_string(Argument.ARG_PLAYER_USERNAME, sock_name_map.get(room.sock1))
        player1_result.add_int(Argument.ARG_SCORE, room.score[0])
        if room.score[0] > room.score[1]:
            player1_result.add_int(Argument.ARG_PLAYER_BONUS_CUP, 5)
            player1_result.add_int(Argument.ARG_PLAYER_BONUS_LEVELUP_POINT, room.score[0]/10)
            # db.update_player_cup(sock_name_map.get(room.sock1), 5)
            # db.update_player_level_up_point(sock_name_map.get(room.sock1), room.score[0]/10)
            pass
        elif room.score[0] < room.score[1]:
            player1_result.add_int(Argument.ARG_PLAYER_BONUS_CUP, -5)
            player1_result.add_int(Argument.ARG_PLAYER_BONUS_LEVELUP_POINT, room.score[0]/10)
            # db.update_player_cup(sock_name_map.get(room.sock1), -5)
            # db.update_player_level_up_point(sock_name_map.get(room.sock1), room.score[0]/10)
            pass
        else:
            player1_result.add_int(Argument.ARG_PLAYER_BONUS_CUP, 2)
            player1_result.add_int(Argument.ARG_PLAYER_BONUS_LEVELUP_POINT, room.score[0]/10)
            # db.update_player_cup(sock_name_map.get(room.sock1), 2)
            # db.update_player_level_up_point(sock_name_map.get(room.sock1), room.score[0]/10)
        send(room.sock1, player1_result)
        send(room.sock2, player1_result)

        "Player 2"
        player2_result = Command(Command.CMD_PLAYER_GAME_RESULT)
        player2_result.add_string(Argument.ARG_PLAYER_USERNAME, sock_name_map.get(room.sock2))
        player2_result.add_int(Argument.ARG_SCORE, room.score[1])
        if room.score[1] > room.score[0]:
            player2_result.add_int(Argument.ARG_PLAYER_BONUS_CUP, 5)
            player2_result.add_int(Argument.ARG_PLAYER_BONUS_LEVELUP_POINT, room.score[1]/10)
            # db.update_player_cup(sock_name_map.get(room.sock2), 5)
            # db.update_player_level_up_point(sock_name_map.get(room.sock2), room.score[1]/10)
            pass
        elif room.score[1] < room.score[0]:
            player2_result.add_int(Argument.ARG_PLAYER_BONUS_CUP, -5)
            player2_result.add_int(Argument.ARG_PLAYER_BONUS_LEVELUP_POINT, room.score[1]/10)
            # db.update_player_cup(sock_name_map.get(room.sock2), -5)
            # db.update_player_level_up_point(sock_name_map.get(room.sock2), room.score[1]/10)
            pass
        else:
            player2_result.add_int(Argument.ARG_PLAYER_BONUS_CUP, 2)
            player2_result.add_int(Argument.ARG_PLAYER_BONUS_LEVELUP_POINT, room.score[1]/10)
            # db.update_player_cup(sock_name_map.get(room.sock2), 2)
            # db.update_player_level_up_point(sock_name_map.get(room.sock2), room.score[1]/10)
        send(room.sock1, player2_result)
        send(room.sock2, player2_result)

        "Send updated player info to player 1"
        info = db.get_user_info(str(sock_name_map.get(room.sock1)))
        send_cmd2 = Command(Command.CMD_PLAYER_INFO)
        send_cmd2.add_int(Argument.ARG_PLAYER_LEVEL, int(info["level"]))
        send_cmd2.add_int(Argument.ARG_PLAYER_LEVEL_UP_POINT, int(info["levelup_point"]))
        send_cmd2.add_int(Argument.ARG_PLAYER_CUP, int(info["cup"]))
        send_cmd2.add_int(Argument.ARG_PLAYER_LEVEL_UP_REQUIRE, int(info["require_point"]))
        send_cmd2.add_int(Argument.ARG_PLAYER_SPEED_MOVE, 10)
        send_cmd2.add_int(Argument.ARG_PLAYER_SPEED_DROP, 10)
        send_cmd2.add_int(Argument.ARG_PLAYER_SPEED_DRAG, 10)
        send(room.sock1, send_cmd2)
        "Send updated player info to player 2"
        info2 = db.get_user_info(str(sock_name_map.get(room.sock2)))
        send_cmd3 = Command(Command.CMD_PLAYER_INFO)
        send_cmd3.add_int(Argument.ARG_PLAYER_LEVEL, int(info2["level"]))
        send_cmd3.add_int(Argument.ARG_PLAYER_LEVEL_UP_POINT, int(info2["levelup_point"]))
        send_cmd3.add_int(Argument.ARG_PLAYER_CUP, int(info2["cup"]))
        send_cmd3.add_int(Argument.ARG_PLAYER_LEVEL_UP_REQUIRE, int(info2["require_point"]))
        send_cmd3.add_int(Argument.ARG_PLAYER_SPEED_MOVE, 10)
        send_cmd3.add_int(Argument.ARG_PLAYER_SPEED_DROP, 10)
        send_cmd3.add_int(Argument.ARG_PLAYER_SPEED_DRAG, 10)
        send(room.sock2, send_cmd3)
        del room_list[room_id]
        log.log("Removed room. Current number of room is "+len(room_list))
    pass
Пример #18
0
def gen_msg_login(username="", password=""):
    msg = Command(Command.CMD_LOGIN)
    msg.add_string(Argument.ARG_PLAYER_USERNAME, username)
    msg.add_string(Argument.ARG_PLAYER_PASSWORD, password)
    return msg
Пример #19
0
        elif arg_type == Argument.LONG:
            long_val = long(unpack("<L", data[read_count:read_count + 8])[0])
            read_count += 8
            cmd.add_long(arg_code, long_val)
    print cmd.get_log()
    pass

HOST, PORT = "localhost", 9090
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((HOST, PORT))
# cmd = Command(Command.CMD_ADD_FRIEND)
# cmd.add_string(Argument.ARG_PLAYER_USERNAME, "giapnh")

cmd = Command(Command.CMD_LOGIN)
username = raw_input("Username:"******"Password:"******"You want chat with?")
    message = raw_input("Message:")
    cmd = Command(Command.CMD_PLAYER_CHAT)
    cmd.add_string(Argument.ARG_FRIEND_USERNAME, chat_with)
    cmd.add_string(Argument.ARG_MESSAGE, message)
    sock.sendall(cmd.get_bytes())
    print cmd.get_log()
    data = sock.recv(1024)
    read(data)

Пример #20
0
# -*- coding: utf-8 -*-
__author__ = 'Nguyen Huu Giap'
import socket
from command import Command
from argument import Argument

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(('localhost', 9999))
client.settimeout(10)
while True:
    msg = raw_input("Enter chat text: ")
    cmd = Command(Command.CMD_PLAYER_CHAT)
    cmd.add_string(Argument.ARG_MESSAGE, msg)
    client.send(cmd.get_bytes())
    data = client.recv(1000)
    print len(bytearray(data))