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

read(14200, _) ->
    {ok, []};
read(14201, _) ->
    {ok, []};
read(14202, _) ->
    {ok, []};
read(14203, _) ->
    {ok, []};
read(14205, _) ->
    {ok, []};
read(14207, _) ->
    {ok, []};
read(14208, _) ->
    {ok, []};
read(14209, Bin0) ->
    <<Rid:64, Bin1/binary>> = Bin0, 
    <<Item:32, _Bin2/binary>> = Bin1, 
    {ok, [Rid, Item]};
read(14210, Bin0) ->
    <<Rid:64, Bin1/binary>> = Bin0, 
    <<Add:16, _Bin2/binary>> = Bin1, 
    {ok, [Rid, Add]};
read(14212, Bin0) ->
    <<Item:32, Bin1/binary>> = Bin0, 
    <<RoleId:64, _Bin2/binary>> = Bin1, 
    {ok, [Item, RoleId]};
read(14213, _) ->
    {ok, []};
read(14214, _) ->
    {ok, []};
read(14215, _) ->
    {ok, []};
read(_Cmd, _R) -> {error, no_match}.

write (14200,[
    State,
    Etime,
    Substate,
    Subetime,
    Round
] = Src_Data) ->
    Data = <<
        State:8,
        Etime:32,
        Substate:8,
        Subetime:32,
        Round:8
    >>,
    {ok, pt:encode(14200, Data, Src_Data)};

write (14201,[
    Round,
    Runners
] = Src_Data) ->
    BinList_Runners = [
        item_to_bin_0(Runners_Item) || Runners_Item <- Runners
    ], 

    Runners_Len = length(Runners), 
    Bin_Runners = list_to_binary(BinList_Runners),

    Data = <<
        Round:8,
        Runners_Len:16, Bin_Runners/binary
    >>,
    {ok, pt:encode(14201, Data, Src_Data)};

write (14202,[
    Trusts
] = Src_Data) ->
    BinList_Trusts = [
        item_to_bin_1(Trusts_Item) || Trusts_Item <- Trusts
    ], 

    Trusts_Len = length(Trusts), 
    Bin_Trusts = list_to_binary(BinList_Trusts),

    Data = <<
        Trusts_Len:16, Bin_Trusts/binary
    >>,
    {ok, pt:encode(14202, Data, Src_Data)};

write (14203,[
    Heads
] = Src_Data) ->
    BinList_Heads = [
        item_to_bin_2(Heads_Item) || Heads_Item <- Heads
    ], 

    Heads_Len = length(Heads), 
    Bin_Heads = list_to_binary(BinList_Heads),

    Data = <<
        Heads_Len:16, Bin_Heads/binary
    >>,
    {ok, pt:encode(14203, Data, Src_Data)};

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

write (14205,[
    Money,
    Items
] = Src_Data) ->
    BinList_Items = [
        item_to_bin_3(Items_Item) || Items_Item <- Items
    ], 

    Items_Len = length(Items), 
    Bin_Items = list_to_binary(BinList_Items),

    Data = <<
        Money:32,
        Items_Len:16, Bin_Items/binary
    >>,
    {ok, pt:encode(14205, Data, Src_Data)};

write (14206,[
    Opty,
    RoleId,
    Vip,
    SubVip,
    Name,
    Aim,
    Value,
    Time
] = Src_Data) ->
    Bin_Name = pt:write_string(Name), 

    Bin_Aim = pt:write_string(Aim), 

    Data = <<
        Opty:8,
        RoleId:64,
        Vip:8,
        SubVip:8,
        Bin_Name/binary,
        Bin_Aim/binary,
        Value:32,
        Time:32
    >>,
    {ok, pt:encode(14206, Data, Src_Data)};

write (14207,[
    Adds,
    Total,
    Results
] = Src_Data) ->
    BinList_Results = [
        item_to_bin_4(Results_Item) || Results_Item <- Results
    ], 

    Results_Len = length(Results), 
    Bin_Results = list_to_binary(BinList_Results),

    Data = <<
        Adds:32,
        Total:32,
        Results_Len:16, Bin_Results/binary
    >>,
    {ok, pt:encode(14207, Data, Src_Data)};

write (14208,[
    Total,
    Focusers
] = Src_Data) ->
    BinList_Focusers = [
        item_to_bin_5(Focusers_Item) || Focusers_Item <- Focusers
    ], 

    Focusers_Len = length(Focusers), 
    Bin_Focusers = list_to_binary(BinList_Focusers),

    Data = <<
        Total:16,
        Focusers_Len:16, Bin_Focusers/binary
    >>,
    {ok, pt:encode(14208, Data, Src_Data)};

write (14209,[
    Rid,
    Item,
    Result,
    Effect
] = Src_Data) ->
    Data = <<
        Rid:64,
        Item:32,
        Result:32,
        Effect:8
    >>,
    {ok, pt:encode(14209, Data, Src_Data)};

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

write (14211,[
    Round,
    Runners
] = Src_Data) ->
    BinList_Runners = [
        item_to_bin_6(Runners_Item) || Runners_Item <- Runners
    ], 

    Runners_Len = length(Runners), 
    Bin_Runners = list_to_binary(BinList_Runners),

    Data = <<
        Round:8,
        Runners_Len:16, Bin_Runners/binary
    >>,
    {ok, pt:encode(14211, Data, Src_Data)};

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

write (14213,[
    Logs
] = Src_Data) ->
    BinList_Logs = [
        item_to_bin_8(Logs_Item) || Logs_Item <- Logs
    ], 

    Logs_Len = length(Logs), 
    Bin_Logs = list_to_binary(BinList_Logs),

    Data = <<
        Logs_Len:16, Bin_Logs/binary
    >>,
    {ok, pt:encode(14213, Data, Src_Data)};

write (14214,[
    Runners
] = Src_Data) ->
    BinList_Runners = [
        item_to_bin_9(Runners_Item) || Runners_Item <- Runners
    ], 

    Runners_Len = length(Runners), 
    Bin_Runners = list_to_binary(BinList_Runners),

    Data = <<
        Runners_Len:16, Bin_Runners/binary
    >>,
    {ok, pt:encode(14214, Data, Src_Data)};

write (14215,[
    Heads
] = Src_Data) ->
    BinList_Heads = [
        item_to_bin_10(Heads_Item) || Heads_Item <- Heads
    ], 

    Heads_Len = length(Heads), 
    Bin_Heads = list_to_binary(BinList_Heads),

    Data = <<
        Heads_Len:16, Bin_Heads/binary
    >>,
    {ok, pt:encode(14215, Data, Src_Data)};

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


item_to_bin_0 ({
    Rid,
    Figure,
    Speed,
    Trip
}) ->
    Bin_Figure = pt:write_figure(Figure), 

    Data = <<
        Rid:64,
        Bin_Figure/binary,
        Speed:16,
        Trip:16
    >>,
    Data.
item_to_bin_1 ({
    Rid,
    Trust
}) ->
    Data = <<
        Rid:64,
        Trust:32
    >>,
    Data.
item_to_bin_2 ({
    Picture,
    Money
}) ->
    Bin_Picture = pt:write_picture(Picture), 

    Data = <<
        Bin_Picture/binary,
        Money:32
    >>,
    Data.
item_to_bin_3 ({
    Id,
    Num
}) ->
    Data = <<
        Id:32,
        Num:8
    >>,
    Data.
item_to_bin_4 ({
    Rank,
    Picture,
    Bet
}) ->
    Bin_Picture = pt:write_picture(Picture), 

    Data = <<
        Rank:16,
        Bin_Picture/binary,
        Bet:32
    >>,
    Data.
item_to_bin_5 ({
    Rank,
    Picture,
    Money
}) ->
    Bin_Picture = pt:write_picture(Picture), 

    Data = <<
        Rank:32,
        Bin_Picture/binary,
        Money:32
    >>,
    Data.
item_to_bin_6 ({
    Rid,
    Speed,
    Trip,
    Buffs,
    Btime
}) ->
    BinList_Buffs = [
        item_to_bin_7(Buffs_Item) || Buffs_Item <- Buffs
    ], 

    Buffs_Len = length(Buffs), 
    Bin_Buffs = list_to_binary(BinList_Buffs),

    Data = <<
        Rid:64,
        Speed:16,
        Trip:16,
        Buffs_Len:16, Bin_Buffs/binary,
        Btime:32
    >>,
    Data.
item_to_bin_7 ({
    Bufid,
    Num
}) ->
    Data = <<
        Bufid:32,
        Num:16
    >>,
    Data.
item_to_bin_8 ({
    Opty,
    RoleId,
    Vip,
    SubVip,
    Name,
    Aim,
    Value,
    Time
}) ->
    Bin_Name = pt:write_string(Name), 

    Bin_Aim = pt:write_string(Aim), 

    Data = <<
        Opty:8,
        RoleId:64,
        Vip:8,
        SubVip:8,
        Bin_Name/binary,
        Bin_Aim/binary,
        Value:32,
        Time:32
    >>,
    Data.
item_to_bin_9 ({
    Picture,
    MyBet
}) ->
    Bin_Picture = pt:write_picture(Picture), 

    Data = <<
        Bin_Picture/binary,
        MyBet:32
    >>,
    Data.
item_to_bin_10 (
    Picture
) ->
    Bin_Picture = pt:write_picture(Picture), 

    Data = <<
        Bin_Picture/binary
    >>,
    Data.
