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

read(13900, _) ->
    {ok, []};
read(13901, _) ->
    {ok, []};
read(13902, _) ->
    {ok, []};
read(13903, Bin0) ->
    <<Uid:64, _Bin1/binary>> = Bin0, 
    {ok, [Uid]};
read(13904, Bin0) ->
    FunArray0 = fun(<<RestBin0/binary>>) -> 
        <<Uid:64, _Args1/binary>> = RestBin0, 
        {Uid,_Args1}
        end,
    {Uids, _Bin1} = pt:read_array(FunArray0, Bin0),

    {ok, [Uids]};
read(13905, Bin0) ->
    <<Pos:8, Bin1/binary>> = Bin0, 
    <<AutoBuy:8, Bin2/binary>> = Bin1, 
    <<AutoLv:8, _Bin3/binary>> = Bin2, 
    {ok, [Pos, AutoBuy, AutoLv]};
read(13906, Bin0) ->
    <<Pos:8, _Bin1/binary>> = Bin0, 
    {ok, [Pos]};
read(13907, Bin0) ->
    <<Uid:64, Bin1/binary>> = Bin0, 
    <<Loc:8, _Bin2/binary>> = Bin1, 
    {ok, [Uid, Loc]};
read(13908, Bin0) ->
    <<Pos:8, _Bin1/binary>> = Bin0, 
    {ok, [Pos]};
read(13909, Bin0) ->
    <<Pos:8, _Bin1/binary>> = Bin0, 
    {ok, [Pos]};
read(13910, _) ->
    {ok, []};
read(13911, Bin0) ->
    <<Pool:8, Bin1/binary>> = Bin0, 
    <<Opty:8, _Bin2/binary>> = Bin1, 
    {ok, [Pool, Opty]};
read(13912, Bin0) ->
    <<Sort:16, Bin1/binary>> = Bin0, 
    <<Num:16, _Bin2/binary>> = Bin1, 
    {ok, [Sort, Num]};
read(13914, _) ->
    {ok, []};
read(_Cmd, _R) -> {error, no_match}.

write (13900,[
    Lv,
    Exp,
    Holes
] = Src_Data) ->
    BinList_Holes = [
        item_to_bin_0(Holes_Item) || Holes_Item <- Holes
    ], 

    Holes_Len = length(Holes), 
    Bin_Holes = list_to_binary(BinList_Holes),

    Data = <<
        Lv:16,
        Exp:16,
        Holes_Len:16, Bin_Holes/binary
    >>,
    {ok, pt:encode(13900, Data, Src_Data)};

write (13901,[
    Result,
    Lv,
    Exp,
    Reports
] = Src_Data) ->
    BinList_Reports = [
        item_to_bin_2(Reports_Item) || Reports_Item <- Reports
    ], 

    Reports_Len = length(Reports), 
    Bin_Reports = list_to_binary(BinList_Reports),

    Data = <<
        Result:32,
        Lv:16,
        Exp:16,
        Reports_Len:16, Bin_Reports/binary
    >>,
    {ok, pt:encode(13901, Data, Src_Data)};

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

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

write (13904,[
    Result,
    Gains
] = Src_Data) ->
    BinList_Gains = [
        item_to_bin_3(Gains_Item) || Gains_Item <- Gains
    ], 

    Gains_Len = length(Gains), 
    Bin_Gains = list_to_binary(BinList_Gains),

    Data = <<
        Result:32,
        Gains_Len:16, Bin_Gains/binary
    >>,
    {ok, pt:encode(13904, Data, Src_Data)};

write (13905,[
    Result,
    Pos,
    Lv,
    Exp
] = Src_Data) ->
    Data = <<
        Result:32,
        Pos:8,
        Lv:16,
        Exp:16
    >>,
    {ok, pt:encode(13905, Data, Src_Data)};

write (13906,[
    Result,
    Pos,
    Grade,
    Lv,
    Exp
] = Src_Data) ->
    Data = <<
        Result:32,
        Pos:8,
        Grade:8,
        Lv:16,
        Exp:16
    >>,
    {ok, pt:encode(13906, Data, Src_Data)};

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

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

write (13909,[
    Result,
    Pos,
    Attrs,
    Reports
] = Src_Data) ->
    BinList_Attrs = [
        item_to_bin_4(Attrs_Item) || Attrs_Item <- Attrs
    ], 

    Attrs_Len = length(Attrs), 
    Bin_Attrs = list_to_binary(BinList_Attrs),

    BinList_Reports = [
        item_to_bin_5(Reports_Item) || Reports_Item <- Reports
    ], 

    Reports_Len = length(Reports), 
    Bin_Reports = list_to_binary(BinList_Reports),

    Data = <<
        Result:32,
        Pos:8,
        Attrs_Len:16, Bin_Attrs/binary,
        Reports_Len:16, Bin_Reports/binary
    >>,
    {ok, pt:encode(13909, Data, Src_Data)};

write (13910,[
    Pools
] = Src_Data) ->
    BinList_Pools = [
        item_to_bin_6(Pools_Item) || Pools_Item <- Pools
    ], 

    Pools_Len = length(Pools), 
    Bin_Pools = list_to_binary(BinList_Pools),

    Data = <<
        Pools_Len:16, Bin_Pools/binary
    >>,
    {ok, pt:encode(13910, Data, Src_Data)};

write (13911,[
    Result,
    Pool,
    Opty,
    Awards,
    Isfirst,
    Remain,
    Rtime,
    ShowAwards
] = Src_Data) ->
    BinList_Awards = [
        item_to_bin_8(Awards_Item) || Awards_Item <- Awards
    ], 

    Awards_Len = length(Awards), 
    Bin_Awards = list_to_binary(BinList_Awards),

    BinList_ShowAwards = [
        item_to_bin_9(ShowAwards_Item) || ShowAwards_Item <- ShowAwards
    ], 

    ShowAwards_Len = length(ShowAwards), 
    Bin_ShowAwards = list_to_binary(BinList_ShowAwards),

    Data = <<
        Result:32,
        Pool:8,
        Opty:8,
        Awards_Len:16, Bin_Awards/binary,
        Isfirst:8,
        Remain:8,
        Rtime:32,
        ShowAwards_Len:16, Bin_ShowAwards/binary
    >>,
    {ok, pt:encode(13911, Data, Src_Data)};

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

write (13913,[
    Pos,
    SkillId,
    SkillLv
] = Src_Data) ->
    Data = <<
        Pos:8,
        SkillId:32,
        SkillLv:16
    >>,
    {ok, pt:encode(13913, Data, Src_Data)};

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

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


item_to_bin_0 ({
    Pos,
    Grade,
    Lv,
    Exp,
    SkillId,
    SkillLv,
    SecLv,
    Attrs
}) ->
    BinList_Attrs = [
        item_to_bin_1(Attrs_Item) || Attrs_Item <- Attrs
    ], 

    Attrs_Len = length(Attrs), 
    Bin_Attrs = list_to_binary(BinList_Attrs),

    Data = <<
        Pos:8,
        Grade:8,
        Lv:16,
        Exp:16,
        SkillId:32,
        SkillLv:16,
        SecLv:8,
        Attrs_Len:16, Bin_Attrs/binary
    >>,
    Data.
item_to_bin_1 ({
    AttrId,
    AttrValue
}) ->
    Data = <<
        AttrId:16,
        AttrValue:32
    >>,
    Data.
item_to_bin_2 ({
    Double,
    Add
}) ->
    Data = <<
        Double:8,
        Add:16
    >>,
    Data.
item_to_bin_3 ({
    Gid,
    Num
}) ->
    Data = <<
        Gid:32,
        Num:16
    >>,
    Data.
item_to_bin_4 ({
    AttrId,
    AttrValue
}) ->
    Data = <<
        AttrId:16,
        AttrValue:32
    >>,
    Data.
item_to_bin_5 ({
    AttrId,
    AttrAdd
}) ->
    Data = <<
        AttrId:16,
        AttrAdd:32
    >>,
    Data.
item_to_bin_6 ({
    Pool,
    Isfirst,
    Remain,
    Rtime,
    ShowAwards
}) ->
    BinList_ShowAwards = [
        item_to_bin_7(ShowAwards_Item) || ShowAwards_Item <- ShowAwards
    ], 

    ShowAwards_Len = length(ShowAwards), 
    Bin_ShowAwards = list_to_binary(BinList_ShowAwards),

    Data = <<
        Pool:8,
        Isfirst:8,
        Remain:8,
        Rtime:32,
        ShowAwards_Len:16, Bin_ShowAwards/binary
    >>,
    Data.
item_to_bin_7 ({
    Color,
    Times
}) ->
    Data = <<
        Color:8,
        Times:8
    >>,
    Data.
item_to_bin_8 ({
    GoodsId,
    Uid,
    Num
}) ->
    Data = <<
        GoodsId:32,
        Uid:64,
        Num:16
    >>,
    Data.
item_to_bin_9 ({
    Color,
    Times
}) ->
    Data = <<
        Color:8,
        Times:8
    >>,
    Data.
