-module(pt_650).
-include("common.hrl").
-export([
    read/2,
    write/2
]).

read(65000, _) ->
    {ok, []};
read(65001, _) ->
    {ok, []};
read(65002, _) ->
    {ok, []};
read(65003, _) ->
    {ok, []};
read(65004, Bin0) ->
    <<Index:8, _Bin1/binary>> = Bin0,
    {ok, [Index]};
read(65005, _) ->
    {ok, []};
read(65010, _) ->
    {ok, []};
read(65011, _) ->
    {ok, []};
read(65012, Bin0) ->
    <<TeamId:32, _Bin1/binary>> = Bin0,
    {ok, [TeamId]};
read(65013, Bin0) ->
    <<PowerLimit:32, Bin1/binary>> = Bin0,
    <<LvLimit:16, Bin2/binary>> = Bin1,
    <<Psw:32, Bin3/binary>> = Bin2,
    <<AutoStart:8, _Bin4/binary>> = Bin3,
    {ok, [PowerLimit, LvLimit, Psw, AutoStart]};
read(65014, Bin0) ->
    <<Op:8, _Bin1/binary>> = Bin0,
    {ok, [Op]};
read(65015, Bin0) ->
    <<TeamId:32, Bin1/binary>> = Bin0,
    <<Psw:32, _Bin2/binary>> = Bin1,
    {ok, [TeamId, Psw]};
read(65016, Bin0) ->
    <<TeamId:32, Bin1/binary>> = Bin0,
    <<Psw:32, _Bin2/binary>> = Bin1,
    {ok, [TeamId, Psw]};
read(65017, _) ->
    {ok, []};
read(65018, Bin0) ->
    {ServerName, Bin1} = pt:read_string(Bin0),
    <<ServerNum:16, Bin2/binary>> = Bin1,
    <<RoleId:64, _Bin3/binary>> = Bin2,
    {ok, [ServerName, ServerNum, RoleId]};
read(65019, Bin0) ->
    <<RoleId:64, _Bin1/binary>> = Bin0,
    {ok, [RoleId]};
read(65021, _) ->
    {ok, []};
read(65022, _) ->
    {ok, []};
read(65023, Bin0) ->
    <<Ready:8, _Bin1/binary>> = Bin0,
    {ok, [Ready]};
read(65024, Bin0) ->
    <<AutoStart:8, _Bin1/binary>> = Bin0,
    {ok, [AutoStart]};
read(65030, _) ->
    {ok, []};
read(65038, _) ->
    {ok, []};
read(65039, Bin0) ->
    <<Page:8, _Bin1/binary>> = Bin0,
    {ok, [Page]};
read(65040, _) ->
    {ok, []};
read(65041, Bin0) ->
    <<Range:8, _Bin1/binary>> = Bin0,
    {ok, [Range]};
read(65045, Bin0) ->
    <<PowerLimit:32, Bin1/binary>> = Bin0,
    <<LvLimit:16, Bin2/binary>> = Bin1,
    <<Psw:32, _Bin3/binary>> = Bin2,
    {ok, [PowerLimit, LvLimit, Psw]};
read(_Cmd, _R) -> {error, no_match}.

write (65000,[
    GradeNum,
    StartNum,
    SerialWin,
    DailyWin,
    DailyCount,
    SeasonCount,
    RewardInfos,
    HonorValue,
    HonorTime
]) ->
    BinList_RewardInfos = [
        item_to_bin_0(RewardInfos_Item) || RewardInfos_Item <- RewardInfos
    ],

    RewardInfos_Len = length(RewardInfos),
    Bin_RewardInfos = list_to_binary(BinList_RewardInfos),

    Data = <<
        GradeNum:8,
        StartNum:16,
        SerialWin:8,
        DailyWin:8,
        DailyCount:8,
        SeasonCount:32,
        RewardInfos_Len:16, Bin_RewardInfos/binary,
        HonorValue:32,
        HonorTime:32
    >>,
    {ok, pt:pack(65000, Data)};

write (65001,[
    Code
]) ->
    Data = <<
        Code:32
    >>,
    {ok, pt:pack(65001, Data)};



write (65003,[
    Res
]) ->
    Data = <<
        Res:32
    >>,
    {ok, pt:pack(65003, Data)};

write (65004,[
    Res,
    Index
]) ->
    Data = <<
        Res:32,
        Index:8
    >>,
    {ok, pt:pack(65004, Data)};

write (65005,[
    Time
]) ->
    Data = <<
        Time:32
    >>,
    {ok, pt:pack(65005, Data)};

write (65010,[
    Res,
    TeamId,
    PowerLimit,
    LvLimit,
    TeamPsw,
    AutoStart,
    LeaderId,
    AveragePower,
    Members
]) ->
    BinList_Members = [
        item_to_bin_1(Members_Item) || Members_Item <- Members
    ],

    Members_Len = length(Members),
    Bin_Members = list_to_binary(BinList_Members),

    Data = <<
        Res:32,
        TeamId:32,
        PowerLimit:32,
        LvLimit:16,
        TeamPsw:32,
        AutoStart:8,
        LeaderId:64,
        AveragePower:32,
        Members_Len:16, Bin_Members/binary
    >>,
    {ok, pt:pack(65010, Data)};

write (65011,[
    Res,
    TeamList
]) ->
    BinList_TeamList = [
        item_to_bin_2(TeamList_Item) || TeamList_Item <- TeamList
    ],

    TeamList_Len = length(TeamList),
    Bin_TeamList = list_to_binary(BinList_TeamList),

    Data = <<
        Res:32,
        TeamList_Len:16, Bin_TeamList/binary
    >>,
    {ok, pt:pack(65011, Data)};

write (65012,[
    Res,
    TeamId,
    LeaderServName,
    LeaderServNum,
    LeaderId,
    LeaderName,
    LeaderLv,
    LeaderPower,
    TeamNum,
    PowerLimit,
    LvLimit,
    NeedPsw,
    AutoStart
]) ->
    Bin_LeaderServName = pt:write_string(LeaderServName),

    Bin_LeaderName = pt:write_string(LeaderName),

    Data = <<
        Res:32,
        TeamId:32,
        Bin_LeaderServName/binary,
        LeaderServNum:16,
        LeaderId:64,
        Bin_LeaderName/binary,
        LeaderLv:16,
        LeaderPower:32,
        TeamNum:8,
        PowerLimit:32,
        LvLimit:16,
        NeedPsw:8,
        AutoStart:8
    >>,
    {ok, pt:pack(65012, Data)};

write (65013,[
    Res
]) ->
    Data = <<
        Res:32
    >>,
    {ok, pt:pack(65013, Data)};

write (65014,[
    Res,
    Op
]) ->
    Data = <<
        Res:32,
        Op:8
    >>,
    {ok, pt:pack(65014, Data)};

write (65015,[
    Res
]) ->
    Data = <<
        Res:32
    >>,
    {ok, pt:pack(65015, Data)};

write (65016,[
    Res
]) ->
    Data = <<
        Res:32
    >>,
    {ok, pt:pack(65016, Data)};

write (65017,[
    Res
]) ->
    Data = <<
        Res:32
    >>,
    {ok, pt:pack(65017, Data)};

write (65018,[
    Res
]) ->
    Data = <<
        Res:32
    >>,
    {ok, pt:pack(65018, Data)};

write (65019,[
    Res,
    RoleId
]) ->
    Data = <<
        Res:32,
        RoleId:64
    >>,
    {ok, pt:pack(65019, Data)};

write (65020,[
    TeamId,
    LeaderId,
    LeaderName,
    Pic,
    PicVer,
    Title,
    GuildName,
    Career,
    Sex,
    Lv,
    Turn,
    VipLv,
    Psw
]) ->
    Bin_LeaderName = pt:write_string(LeaderName),

    Bin_Pic = pt:write_string(Pic),

    Bin_GuildName = pt:write_string(GuildName),

    Data = <<
        TeamId:32,
        LeaderId:64,
        Bin_LeaderName/binary,
        Bin_Pic/binary,
        PicVer:32,
        Title:8,
        Bin_GuildName/binary,
        Career:8,
        Sex:8,
        Lv:16,
        Turn:8,
        VipLv:8,
        Psw:32
    >>,
    {ok, pt:pack(65020, Data)};

write (65021,[
    Res
]) ->
    Data = <<
        Res:32
    >>,
    {ok, pt:pack(65021, Data)};

write (65022,[
    Res,
    ServerName,
    ServerNum,
    RoleId
]) ->
    Bin_ServerName = pt:write_string(ServerName),

    Data = <<
        Res:32,
        Bin_ServerName/binary,
        ServerNum:16,
        RoleId:64
    >>,
    {ok, pt:pack(65022, Data)};

write (65023,[
    Res,
    Ready
]) ->
    Data = <<
        Res:32,
        Ready:8
    >>,
    {ok, pt:pack(65023, Data)};

write (65024,[
    Res,
    AutoStart
]) ->
    Data = <<
        Res:32,
        AutoStart:8
    >>,
    {ok, pt:pack(65024, Data)};

write (65025,[
    ServerName,
    ServerNum,
    RoleId,
    RoleName
]) ->
    Bin_ServerName = pt:write_string(ServerName),

    Bin_RoleName = pt:write_string(RoleName),

    Data = <<
        Bin_ServerName/binary,
        ServerNum:16,
        RoleId:64,
        Bin_RoleName/binary
    >>,
    {ok, pt:pack(65025, Data)};

write (65026,[
]) ->
    Data = <<
    >>,
    {ok, pt:pack(65026, Data)};

write (65027,[
]) ->
    Data = <<
    >>,
    {ok, pt:pack(65027, Data)};

write (65029,[
    TeamList
]) ->
    BinList_TeamList = [
        item_to_bin_4(TeamList_Item) || TeamList_Item <- TeamList
    ],

    TeamList_Len = length(TeamList),
    Bin_TeamList = list_to_binary(BinList_TeamList),

    Data = <<
        TeamList_Len:16, Bin_TeamList/binary
    >>,
    {ok, pt:pack(65029, Data)};

write (65030,[
    Res,
    EndTime,
    Score,
    TeamId,
    RoleList,
    EnemyScore,
    EnemyTeamId
]) ->
    BinList_RoleList = [
        item_to_bin_5(RoleList_Item) || RoleList_Item <- RoleList
    ],

    RoleList_Len = length(RoleList),
    Bin_RoleList = list_to_binary(BinList_RoleList),

    Data = <<
        Res:32,
        EndTime:16,
        Score:16,
        TeamId:32,
        RoleList_Len:16, Bin_RoleList/binary,
        EnemyScore:16,
        EnemyTeamId:32
    >>,
    {ok, pt:pack(65030, Data)};

write (65031,[
]) ->
    Data = <<
    >>,
    {ok, pt:pack(65031, Data)};

write (65032,[
    TeamId,
    ServerName,
    ServerNum,
    RoleId
]) ->
    Bin_ServerName = pt:write_string(ServerName),

    Data = <<
        TeamId:32,
        Bin_ServerName/binary,
        ServerNum:16,
        RoleId:64
    >>,
    {ok, pt:pack(65032, Data)};

write (65033,[
    TeamId,
    ServerName,
    ServerNum,
    RoleId
]) ->
    Bin_ServerName = pt:write_string(ServerName),

    Data = <<
        TeamId:32,
        Bin_ServerName/binary,
        ServerNum:16,
        RoleId:64
    >>,
    {ok, pt:pack(65033, Data)};

write (65035,[
    Score1,
    Score2
]) ->
    Data = <<
        Score1:16,
        Score2:16
    >>,
    {ok, pt:pack(65035, Data)};

write (65036,[
    ServerName,
    ServerNum,
    RoleId,
    KillCount,
    DieCount,
    HoldCount
]) ->
    Bin_ServerName = pt:write_string(ServerName),

    Data = <<
        Bin_ServerName/binary,
        ServerNum:16,
        RoleId:64,
        KillCount:16,
        DieCount:16,
        HoldCount:16
    >>,
    {ok, pt:pack(65036, Data)};

write (65037,[
    WinSide,
    HoldCount1,
    RoleList1,
    HoldCount2,
    RoleList2,
    StartNum,
    SerialWin,
    HonorValue
]) ->
    BinList_RoleList1 = [
        item_to_bin_6(RoleList1_Item) || RoleList1_Item <- RoleList1
    ],

    RoleList1_Len = length(RoleList1),
    Bin_RoleList1 = list_to_binary(BinList_RoleList1),

    BinList_RoleList2 = [
        item_to_bin_7(RoleList2_Item) || RoleList2_Item <- RoleList2
    ],

    RoleList2_Len = length(RoleList2),
    Bin_RoleList2 = list_to_binary(BinList_RoleList2),

    Data = <<
        WinSide:8,
        HoldCount1:8,
        RoleList1_Len:16, Bin_RoleList1/binary,
        HoldCount2:8,
        RoleList2_Len:16, Bin_RoleList2/binary,
        StartNum:16,
        SerialWin:8,
        HonorValue:16
    >>,
    {ok, pt:pack(65037, Data)};

write (65038,[
    RoleList
]) ->
    BinList_RoleList = [
        item_to_bin_8(RoleList_Item) || RoleList_Item <- RoleList
    ],

    RoleList_Len = length(RoleList),
    Bin_RoleList = list_to_binary(BinList_RoleList),

    Data = <<
        RoleList_Len:16, Bin_RoleList/binary
    >>,
    {ok, pt:pack(65038, Data)};

write (65039,[
    Page,
    RoleList,
    YesterdayGrade,
    YesterdayStart,
    Rank,
    GradeReward
]) ->
    BinList_RoleList = [
        item_to_bin_9(RoleList_Item) || RoleList_Item <- RoleList
    ],

    RoleList_Len = length(RoleList),
    Bin_RoleList = list_to_binary(BinList_RoleList),

    Data = <<
        Page:8,
        RoleList_Len:16, Bin_RoleList/binary,
        YesterdayGrade:8,
        YesterdayStart:16,
        Rank:16,
        GradeReward:32
    >>,
    {ok, pt:pack(65039, Data)};





write (65042,[
    KillerId,
    KillerName,
    KillerCareer,
    KillerTurn,
    KillerPicture,
    KillerPictureVer,
    BeKillerId,
    BeKillerName,
    BeKillerCareer,
    BeKillerTurn,
    BeKillerPicture,
    BeKillerPictureVer
]) ->
    Bin_KillerName = pt:write_string(KillerName),

    Bin_KillerPicture = pt:write_string(KillerPicture),

    Bin_BeKillerName = pt:write_string(BeKillerName),

    Bin_BeKillerPicture = pt:write_string(BeKillerPicture),

    Data = <<
        KillerId:64,
        Bin_KillerName/binary,
        KillerCareer:8,
        KillerTurn:8,
        Bin_KillerPicture/binary,
        KillerPictureVer:32,
        BeKillerId:64,
        Bin_BeKillerName/binary,
        BeKillerCareer:8,
        BeKillerTurn:8,
        Bin_BeKillerPicture/binary,
        BeKillerPictureVer:32
    >>,
    {ok, pt:pack(65042, Data)};

write (65043,[
    Type,
    MonId,
    CollectId,
    CollectName,
    CollectCareer,
    CollectTurn,
    CollectPicture,
    CollectPictureVer
]) ->
    Bin_CollectName = pt:write_string(CollectName),

    Bin_CollectPicture = pt:write_string(CollectPicture),

    Data = <<
        Type:8,
        MonId:32,
        CollectId:64,
        Bin_CollectName/binary,
        CollectCareer:8,
        CollectTurn:8,
        Bin_CollectPicture/binary,
        CollectPictureVer:32
    >>,
    {ok, pt:pack(65043, Data)};

write (65044,[
    KillerId,
    KillerName,
    KillerCareer,
    KillerTurn,
    KillerPicture,
    KillerPictureVer,
    KillNum
]) ->
    Bin_KillerName = pt:write_string(KillerName),

    Bin_KillerPicture = pt:write_string(KillerPicture),

    Data = <<
        KillerId:64,
        Bin_KillerName/binary,
        KillerCareer:8,
        KillerTurn:8,
        Bin_KillerPicture/binary,
        KillerPictureVer:32,
        KillNum:32
    >>,
    {ok, pt:pack(65044, Data)};

write (65045,[
    Res
]) ->
    Data = <<
        Res:32
    >>,
    {ok, pt:pack(65045, Data)};

write(_Cmd, _R) -> ?DEBUG("no_match:~p,~p~n", [_Cmd, _R]), {ok, pt:pack(0, <<>>)}.


item_to_bin_0 ({
    Index,
    Time
}) ->
    Data = <<
        Index:8,
        Time:32
    >>,
    Data.
item_to_bin_1 ({
    ServerName,
    ServerNum,
    ServerId,
    RoleId,
    RoleName,
    Career,
    Sex,
    Turn,
    Power,
    Fashion,
    Weapon,
    Wing,
    Leader,
    Ready
}) ->
    Bin_ServerName = pt:write_string(ServerName),

    Bin_RoleName = pt:write_string(RoleName),

    Data = <<
        Bin_ServerName/binary,
        ServerNum:16,
        ServerId:16,
        RoleId:64,
        Bin_RoleName/binary,
        Career:8,
        Sex:8,
        Turn:8,
        Power:32,
        Fashion:32,
        Weapon:16,
        Wing:16,
        Leader:8,
        Ready:8
    >>,
    Data.
item_to_bin_2 ({
    TeamId,
    LeaderServName,
    LeaderServNum,
    LeaderId,
    LeaderName,
    LeaderLv,
    LeaderPower,
    AveragePower,
    TeamNum,
    PowerLimit,
    LvLimit,
    NeedPsw,
    AutoStart,
    CareerList
}) ->
    Bin_LeaderServName = pt:write_string(LeaderServName),

    Bin_LeaderName = pt:write_string(LeaderName),

    BinList_CareerList = [
        item_to_bin_3(CareerList_Item) || CareerList_Item <- CareerList
    ],

    CareerList_Len = length(CareerList),
    Bin_CareerList = list_to_binary(BinList_CareerList),

    Data = <<
        TeamId:32,
        Bin_LeaderServName/binary,
        LeaderServNum:16,
        LeaderId:64,
        Bin_LeaderName/binary,
        LeaderLv:16,
        LeaderPower:32,
        AveragePower:32,
        TeamNum:8,
        PowerLimit:32,
        LvLimit:16,
        NeedPsw:8,
        AutoStart:8,
        CareerList_Len:16, Bin_CareerList/binary
    >>,
    Data.
item_to_bin_3 (
    Career
) ->
    Data = <<
        Career:8
    >>,
    Data.
item_to_bin_4 ({
    TeamId,
    ServerName,
    ServerNum,
    RoleId,
    RoleName,
    Power,
    VipLv,
    Career,
    Sex,
    Lv,
    Turn,
    Picture,
    PictureVer
}) ->
    Bin_ServerName = pt:write_string(ServerName),

    Bin_RoleName = pt:write_string(RoleName),

    Bin_Picture = pt:write_string(Picture),

    Data = <<
        TeamId:32,
        Bin_ServerName/binary,
        ServerNum:16,
        RoleId:64,
        Bin_RoleName/binary,
        Power:32,
        VipLv:8,
        Career:8,
        Sex:8,
        Lv:16,
        Turn:8,
        Bin_Picture/binary,
        PictureVer:32
    >>,
    Data.
item_to_bin_5 ({
    ServerName,
    ServerNum,
    RoleId,
    RoleName,
    Career,
    Sex,
    Lv,
    Turn,
    KillCount,
    DieCount,
    HoldCount,
    Picture,
    PictureVer
}) ->
    Bin_ServerName = pt:write_string(ServerName),

    Bin_RoleName = pt:write_string(RoleName),

    Bin_Picture = pt:write_string(Picture),

    Data = <<
        Bin_ServerName/binary,
        ServerNum:16,
        RoleId:64,
        Bin_RoleName/binary,
        Career:8,
        Sex:8,
        Lv:16,
        Turn:8,
        KillCount:16,
        DieCount:16,
        HoldCount:16,
        Bin_Picture/binary,
        PictureVer:32
    >>,
    Data.
item_to_bin_6 ({
    ServerName,
    ServerNum,
    RoleId,
    RoleName,
    Career,
    Sex,
    Lv,
    Turn,
    Power,
    KillCount,
    DieCount,
    HoldCount,
    Mvp,
    Picture,
    PictureVer
}) ->
    Bin_ServerName = pt:write_string(ServerName),

    Bin_RoleName = pt:write_string(RoleName),

    Bin_Picture = pt:write_string(Picture),

    Data = <<
        Bin_ServerName/binary,
        ServerNum:16,
        RoleId:64,
        Bin_RoleName/binary,
        Career:8,
        Sex:8,
        Lv:16,
        Turn:8,
        Power:32,
        KillCount:16,
        DieCount:16,
        HoldCount:16,
        Mvp:8,
        Bin_Picture/binary,
        PictureVer:32
    >>,
    Data.
item_to_bin_7 ({
    ServerName,
    ServerNum,
    RoleId,
    RoleName,
    Career,
    Sex,
    Lv,
    Turn,
    Power,
    KillCount,
    DieCount,
    HoldCount,
    Mvp,
    Picture,
    PictureVer
}) ->
    Bin_ServerName = pt:write_string(ServerName),

    Bin_RoleName = pt:write_string(RoleName),

    Bin_Picture = pt:write_string(Picture),

    Data = <<
        Bin_ServerName/binary,
        ServerNum:16,
        RoleId:64,
        Bin_RoleName/binary,
        Career:8,
        Sex:8,
        Lv:16,
        Turn:8,
        Power:32,
        KillCount:16,
        DieCount:16,
        HoldCount:16,
        Mvp:8,
        Bin_Picture/binary,
        PictureVer:32
    >>,
    Data.
item_to_bin_8 ({
    ServerName,
    ServerNum,
    RoleName,
    StartNum
}) ->
    Bin_ServerName = pt:write_string(ServerName),

    Bin_RoleName = pt:write_string(RoleName),

    Data = <<
        Bin_ServerName/binary,
        ServerNum:16,
        Bin_RoleName/binary,
        StartNum:16
    >>,
    Data.
item_to_bin_9 ({
    ServerName,
    ServerNum,
    ServerId,
    RoleId,
    RoleName,
    Career,
    Sex,
    GradeNum,
    StartNum,
    Power
}) ->
    Bin_ServerName = pt:write_string(ServerName),

    Bin_RoleName = pt:write_string(RoleName),

    Data = <<
        Bin_ServerName/binary,
        ServerNum:16,
        ServerId:16,
        RoleId:64,
        Bin_RoleName/binary,
        Career:8,
        Sex:8,
        GradeNum:8,
        StartNum:16,
        Power:32
    >>,
    Data.
