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

read(61101, Bin0) ->
    <<DunId:32, Bin1/binary>> = Bin0, 
    FunArray0 = fun(<<RestBin0/binary>>) -> 
        <<PetId:32, _Args01/binary>> = RestBin0, 
        <<Pos:32, _Args02/binary>> = _Args01, 
        {{PetId, Pos},_Args02}
        end,
    {PetList, Bin2} = pt:read_array(FunArray0, Bin1),

    <<IsEnter:8, _Bin3/binary>> = Bin2, 
    {ok, [DunId, PetList, IsEnter]};
read(61102, _) ->
    {ok, []};
read(61103, _) ->
    {ok, []};
read(61104, _) ->
    {ok, []};
read(61105, Bin0) ->
    <<DunId:32, _Bin1/binary>> = Bin0, 
    {ok, [DunId]};
read(61106, Bin0) ->
    <<DunId:32, _Bin1/binary>> = Bin0, 
    {ok, [DunId]};
read(61107, _) ->
    {ok, []};
read(61108, Bin0) ->
    <<DunId:32, _Bin1/binary>> = Bin0, 
    {ok, [DunId]};
read(61109, Bin0) ->
    {Msg, _Bin1} = pt:read_string(Bin0), 
    {ok, [Msg]};
read(61112, Bin0) ->
    <<DunId:32, Bin1/binary>> = Bin0, 
    <<Index:8, _Bin2/binary>> = Bin1, 
    {ok, [DunId, Index]};
read(61113, _) ->
    {ok, []};
read(61114, _) ->
    {ok, []};
read(61115, _) ->
    {ok, []};
read(61116, Bin0) ->
    <<Start:32, Bin1/binary>> = Bin0, 
    <<Len:32, _Bin2/binary>> = Bin1, 
    {ok, [Start, Len]};
read(61117, Bin0) ->
    <<Start:32, Bin1/binary>> = Bin0, 
    <<Len:32, _Bin2/binary>> = Bin1, 
    {ok, [Start, Len]};
read(61118, _) ->
    {ok, []};
read(61119, Bin0) ->
    <<Index:16, _Bin1/binary>> = Bin0, 
    {ok, [Index]};
read(61120, _) ->
    {ok, []};
read(61121, _) ->
    {ok, []};
read(61122, Bin0) ->
    <<DunId:32, _Bin1/binary>> = Bin0, 
    {ok, [DunId]};
read(61123, Bin0) ->
    <<DunId:32, _Bin1/binary>> = Bin0, 
    {ok, [DunId]};
read(61124, _) ->
    {ok, []};
read(61125, Bin0) ->
    <<DunId:32, _Bin1/binary>> = Bin0, 
    {ok, [DunId]};
read(61126, _) ->
    {ok, []};
read(61127, Bin0) ->
    <<DunId:32, _Bin1/binary>> = Bin0, 
    {ok, [DunId]};
read(61128, _) ->
    {ok, []};
read(61129, _) ->
    {ok, []};
read(61130, _) ->
    {ok, []};
read(_Cmd, _R) -> {error, no_match}.

write (61101,[
    Errcode,
    DunId,
    PetList,
    IsEnter
] = Src_Data) ->
    BinList_PetList = [
        item_to_bin_0(PetList_Item) || PetList_Item <- PetList
    ], 

    PetList_Len = length(PetList), 
    Bin_PetList = list_to_binary(BinList_PetList),

    Data = <<
        Errcode:32,
        DunId:32,
        PetList_Len:16, Bin_PetList/binary,
        IsEnter:8
    >>,
    {ok, pt:encode(61101, Data, Src_Data)};

write (61102,[
    ChooseList
] = Src_Data) ->
    BinList_ChooseList = [
        item_to_bin_1(ChooseList_Item) || ChooseList_Item <- ChooseList
    ], 

    ChooseList_Len = length(ChooseList), 
    Bin_ChooseList = list_to_binary(BinList_ChooseList),

    Data = <<
        ChooseList_Len:16, Bin_ChooseList/binary
    >>,
    {ok, pt:encode(61102, Data, Src_Data)};

write (61103,[
    Errcode,
    NextTime
] = Src_Data) ->
    Data = <<
        Errcode:32,
        NextTime:8
    >>,
    {ok, pt:encode(61103, Data, Src_Data)};

write (61104,[
    RoleList
] = Src_Data) ->
    BinList_RoleList = [
        item_to_bin_3(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:encode(61104, Data, Src_Data)};

write (61105,[
    DunList
] = Src_Data) ->
    BinList_DunList = [
        item_to_bin_4(DunList_Item) || DunList_Item <- DunList
    ], 

    DunList_Len = length(DunList), 
    Bin_DunList = list_to_binary(BinList_DunList),

    Data = <<
        DunList_Len:16, Bin_DunList/binary
    >>,
    {ok, pt:encode(61105, Data, Src_Data)};

write (61106,[
    Errcode,
    DunId
] = Src_Data) ->
    Data = <<
        Errcode:32,
        DunId:32
    >>,
    {ok, pt:encode(61106, Data, Src_Data)};

write (61107,[
    WaveList,
    MaxWaveId
] = Src_Data) ->
    BinList_WaveList = [
        item_to_bin_6(WaveList_Item) || WaveList_Item <- WaveList
    ], 

    WaveList_Len = length(WaveList), 
    Bin_WaveList = list_to_binary(BinList_WaveList),

    Data = <<
        WaveList_Len:16, Bin_WaveList/binary,
        MaxWaveId:16
    >>,
    {ok, pt:encode(61107, Data, Src_Data)};

write (61108,[
    DunList
] = Src_Data) ->
    BinList_DunList = [
        item_to_bin_7(DunList_Item) || DunList_Item <- DunList
    ], 

    DunList_Len = length(DunList), 
    Bin_DunList = list_to_binary(BinList_DunList),

    Data = <<
        DunList_Len:16, Bin_DunList/binary
    >>,
    {ok, pt:encode(61108, Data, Src_Data)};

write (61109,[
    Errcode,
    Figure,
    Msg
] = Src_Data) ->
    Bin_Figure = pt:write_figure(Figure), 

    Bin_Msg = pt:write_string(Msg), 

    Data = <<
        Errcode:32,
        Bin_Figure/binary,
        Bin_Msg/binary
    >>,
    {ok, pt:encode(61109, Data, Src_Data)};

write (61110,[
    DieNum
] = Src_Data) ->
    Data = <<
        DieNum:16
    >>,
    {ok, pt:encode(61110, Data, Src_Data)};

write (61111,[
    MonList
] = Src_Data) ->
    BinList_MonList = [
        item_to_bin_8(MonList_Item) || MonList_Item <- MonList
    ], 

    MonList_Len = length(MonList), 
    Bin_MonList = list_to_binary(BinList_MonList),

    Data = <<
        MonList_Len:16, Bin_MonList/binary
    >>,
    {ok, pt:encode(61111, Data, Src_Data)};

write (61112,[
    Errcode,
    DunId,
    Index
] = Src_Data) ->
    Data = <<
        Errcode:32,
        DunId:32,
        Index:8
    >>,
    {ok, pt:encode(61112, Data, Src_Data)};

write (61113,[
    RewardList
] = Src_Data) ->
    BinList_RewardList = [
        item_to_bin_9(RewardList_Item) || RewardList_Item <- RewardList
    ], 

    RewardList_Len = length(RewardList), 
    Bin_RewardList = list_to_binary(BinList_RewardList),

    Data = <<
        RewardList_Len:16, Bin_RewardList/binary
    >>,
    {ok, pt:encode(61113, Data, Src_Data)};

write (61114,[
    Res
] = Src_Data) ->
    Data = <<
        Res:32
    >>,
    {ok, pt:encode(61114, Data, Src_Data)};

write (61115,[
    Res,
    GoodsList
] = Src_Data) ->
    BinList_GoodsList = [
        item_to_bin_10(GoodsList_Item) || GoodsList_Item <- GoodsList
    ], 

    GoodsList_Len = length(GoodsList), 
    Bin_GoodsList = list_to_binary(BinList_GoodsList),

    Data = <<
        Res:32,
        GoodsList_Len:16, Bin_GoodsList/binary
    >>,
    {ok, pt:encode(61115, Data, Src_Data)};

write (61116,[
    RankList,
    MyStar,
    MyRank,
    Sum
] = Src_Data) ->
    BinList_RankList = [
        item_to_bin_11(RankList_Item) || RankList_Item <- RankList
    ], 

    RankList_Len = length(RankList), 
    Bin_RankList = list_to_binary(BinList_RankList),

    Data = <<
        RankList_Len:16, Bin_RankList/binary,
        MyStar:16,
        MyRank:8,
        Sum:8
    >>,
    {ok, pt:encode(61116, Data, Src_Data)};

write (61117,[
    MyRank,
    MyDunId,
    MySweepNum,
    Sum,
    RankList
] = Src_Data) ->
    BinList_RankList = [
        item_to_bin_12(RankList_Item) || RankList_Item <- RankList
    ], 

    RankList_Len = length(RankList), 
    Bin_RankList = list_to_binary(BinList_RankList),

    Data = <<
        MyRank:32,
        MyDunId:32,
        MySweepNum:16,
        Sum:32,
        RankList_Len:16, Bin_RankList/binary
    >>,
    {ok, pt:encode(61117, Data, Src_Data)};

write (61118,[
    StarList
] = Src_Data) ->
    BinList_StarList = [
        item_to_bin_13(StarList_Item) || StarList_Item <- StarList
    ], 

    StarList_Len = length(StarList), 
    Bin_StarList = list_to_binary(BinList_StarList),

    Data = <<
        StarList_Len:16, Bin_StarList/binary
    >>,
    {ok, pt:encode(61118, Data, Src_Data)};

write (61119,[
    Errcode,
    Index
] = Src_Data) ->
    Data = <<
        Errcode:32,
        Index:16
    >>,
    {ok, pt:encode(61119, Data, Src_Data)};

write (61120,[
    EnergyNum
] = Src_Data) ->
    Data = <<
        EnergyNum:16
    >>,
    {ok, pt:encode(61120, Data, Src_Data)};

write (61121,[
    HaveTimes,
    ResetTime
] = Src_Data) ->
    Data = <<
        HaveTimes:8,
        ResetTime:32
    >>,
    {ok, pt:encode(61121, Data, Src_Data)};

write (61122,[
    Result,
    DunId,
    RewardList
] = Src_Data) ->
    Bin_RewardList = pt:write_string(RewardList), 

    Data = <<
        Result:32,
        DunId:32,
        Bin_RewardList/binary
    >>,
    {ok, pt:encode(61122, Data, Src_Data)};

write (61123,[
    DunId,
    RewardList,
    CanSweep
] = Src_Data) ->
    Bin_RewardList = pt:write_string(RewardList), 

    Data = <<
        DunId:32,
        Bin_RewardList/binary,
        CanSweep:8
    >>,
    {ok, pt:encode(61123, Data, Src_Data)};

write (61124,[
    SceneId,
    DunId,
    StartTime,
    EndTime,
    WaveNum,
    WaveStartTime,
    LevelEndTime,
    Errcode
] = Src_Data) ->
    Data = <<
        SceneId:32,
        DunId:32,
        StartTime:32,
        EndTime:32,
        WaveNum:32,
        WaveStartTime:32,
        LevelEndTime:32,
        Errcode:32
    >>,
    {ok, pt:encode(61124, Data, Src_Data)};

write (61125,[
    Result,
    DunId,
    RewardList
] = Src_Data) ->
    Bin_RewardList = pt:write_object_list(RewardList), 

    Data = <<
        Result:32,
        DunId:32,
        Bin_RewardList/binary
    >>,
    {ok, pt:encode(61125, Data, Src_Data)};

write (61126,[
    Result,
    DunList
] = Src_Data) ->
    BinList_DunList = [
        item_to_bin_14(DunList_Item) || DunList_Item <- DunList
    ], 

    DunList_Len = length(DunList), 
    Bin_DunList = list_to_binary(BinList_DunList),

    Data = <<
        Result:32,
        DunList_Len:16, Bin_DunList/binary
    >>,
    {ok, pt:encode(61126, Data, Src_Data)};

write (61127,[
    Errcode,
    DunId
] = Src_Data) ->
    Data = <<
        Errcode:32,
        DunId:32
    >>,
    {ok, pt:encode(61127, Data, Src_Data)};

write (61128,[
    IsSweep
] = Src_Data) ->
    Data = <<
        IsSweep:8
    >>,
    {ok, pt:encode(61128, Data, Src_Data)};

write (61129,[
    Result
] = Src_Data) ->
    Data = <<
        Result:32
    >>,
    {ok, pt:encode(61129, Data, Src_Data)};

write (61130,[
    Errcode
] = Src_Data) ->
    Data = <<
        Errcode:32
    >>,
    {ok, pt:encode(61130, Data, Src_Data)};

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


item_to_bin_0 ({
    PetId,
    Pos
}) ->
    Data = <<
        PetId:32,
        Pos:32
    >>,
    Data.
item_to_bin_1 ({
    DunId,
    PetList
}) ->
    BinList_PetList = [
        item_to_bin_2(PetList_Item) || PetList_Item <- PetList
    ], 

    PetList_Len = length(PetList), 
    Bin_PetList = list_to_binary(BinList_PetList),

    Data = <<
        DunId:32,
        PetList_Len:16, Bin_PetList/binary
    >>,
    Data.
item_to_bin_2 ({
    PetId,
    Pos
}) ->
    Data = <<
        PetId:32,
        Pos:32
    >>,
    Data.
item_to_bin_3 ({
    Rank,
    RoleId,
    Figure,
    ServerNum,
    Val
}) ->
    Bin_Figure = pt:write_figure(Figure), 

    Data = <<
        Rank:16,
        RoleId:64,
        Bin_Figure/binary,
        ServerNum:32,
        Val:32
    >>,
    Data.
item_to_bin_4 ({
    DunId,
    RoleList,
    Time
}) ->
    BinList_RoleList = [
        item_to_bin_5(RoleList_Item) || RoleList_Item <- RoleList
    ], 

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

    Data = <<
        DunId:32,
        RoleList_Len:16, Bin_RoleList/binary,
        Time:32
    >>,
    Data.
item_to_bin_5 ({
    RoleId,
    Figure,
    ServerNum
}) ->
    Bin_Figure = pt:write_figure(Figure), 

    Data = <<
        RoleId:64,
        Bin_Figure/binary,
        ServerNum:32
    >>,
    Data.
item_to_bin_6 (
    WaveId
) ->
    Data = <<
        WaveId:16
    >>,
    Data.
item_to_bin_7 ({
    DunId,
    Status
}) ->
    Data = <<
        DunId:32,
        Status:8
    >>,
    Data.
item_to_bin_8 ({
    Wave,
    Sequence,
    MonId,
    CreateTime
}) ->
    Data = <<
        Wave:16,
        Sequence:8,
        MonId:32,
        CreateTime:32
    >>,
    Data.
item_to_bin_9 ({
    DunId,
    Index
}) ->
    Data = <<
        DunId:32,
        Index:8
    >>,
    Data.
item_to_bin_10 ({
    GoodsType,
    GoodsId,
    Num
}) ->
    Data = <<
        GoodsType:16,
        GoodsId:32,
        Num:32
    >>,
    Data.
item_to_bin_11 ({
    Rank,
    Name,
    RoleId,
    Star,
    Time
}) ->
    Bin_Name = pt:write_string(Name), 

    Data = <<
        Rank:8,
        Bin_Name/binary,
        RoleId:64,
        Star:16,
        Time:64
    >>,
    Data.
item_to_bin_12 ({
    PlayerId,
    Figure,
    DunId,
    SweepNum,
    Rank
}) ->
    Bin_Figure = pt:write_picture(Figure), 

    Data = <<
        PlayerId:64,
        Bin_Figure/binary,
        DunId:32,
        SweepNum:16,
        Rank:32
    >>,
    Data.
item_to_bin_13 ({
    Index,
    State
}) ->
    Data = <<
        Index:16,
        State:16
    >>,
    Data.
item_to_bin_14 ({
    DunId,
    RewardList
}) ->
    Bin_RewardList = pt:write_object_list(RewardList), 

    Data = <<
        DunId:32,
        Bin_RewardList/binary
    >>,
    Data.
