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

read(45901, Bin0) ->
    <<SubType:16, _Bin1/binary>> = Bin0, 
    {ok, [SubType]};
read(45902, Bin0) ->
    <<SubType:16, Bin1/binary>> = Bin0, 
    <<Status:8, _Bin2/binary>> = Bin1, 
    {ok, [SubType, Status]};
read(45909, Bin0) ->
    <<RoomId:32, Bin1/binary>> = Bin0, 
    <<Status:8, Bin2/binary>> = Bin1, 
    <<SubType:16, _Bin3/binary>> = Bin2, 
    {ok, [RoomId, Status, SubType]};
read(45910, Bin0) ->
    <<RoomId:32, Bin1/binary>> = Bin0, 
    <<X:32, Bin2/binary>> = Bin1, 
    <<Y:32, Bin3/binary>> = Bin2, 
    <<Status:8, Bin4/binary>> = Bin3, 
    <<Time:16, Bin5/binary>> = Bin4, 
    {Ticket, Bin6} = pt:read_string(Bin5), 
    <<SubType:16, _Bin7/binary>> = Bin6, 
    {ok, [RoomId, X, Y, Status, Time, Ticket, SubType]};
read(45913, _) ->
    {ok, []};
read(45919, Bin0) ->
    <<SubType:16, _Bin1/binary>> = Bin0, 
    {ok, [SubType]};
read(_Cmd, _R) -> {error, no_match}.

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

write (45901,[
    LeftCount,
    EndTime,
    SingleLifeCount
] = Src_Data) ->
    Data = <<
        LeftCount:8,
        EndTime:32,
        SingleLifeCount:8
    >>,
    {ok, pt:encode(45901, Data, Src_Data)};

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

write (45903,[
    RoleList,
    BeginTime
] = Src_Data) ->
    BinList_RoleList = [
        item_to_bin_0(RoleList_Item) || RoleList_Item <- RoleList
    ], 

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

    Data = <<
        RoleList_Len:16, Bin_RoleList/binary,
        BeginTime:32
    >>,
    {ok, pt:encode(45903, Data, Src_Data)};

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

write (45905,[
    RoomId,
    RoleList,
    Round,
    X,
    Y,
    Radius,
    Picture
] = Src_Data) ->
    BinList_RoleList = [
        item_to_bin_1(RoleList_Item) || RoleList_Item <- RoleList
    ], 

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

    Data = <<
        RoomId:32,
        RoleList_Len:16, Bin_RoleList/binary,
        Round:16,
        X:32,
        Y:32,
        Radius:16,
        Picture:32
    >>,
    {ok, pt:encode(45905, Data, Src_Data)};

write (45906,[
    X,
    Y,
    Radius,
    Picture,
    IsEnd
] = Src_Data) ->
    Data = <<
        X:32,
        Y:32,
        Radius:16,
        Picture:32,
        IsEnd:8
    >>,
    {ok, pt:encode(45906, Data, Src_Data)};

write (45907,[
    Round,
    RoleId,
    JumpTime
] = Src_Data) ->
    Data = <<
        Round:16,
        RoleId:64,
        JumpTime:32
    >>,
    {ok, pt:encode(45907, Data, Src_Data)};

write (45909,[
    RoleId,
    Status
] = Src_Data) ->
    Data = <<
        RoleId:64,
        Status:8
    >>,
    {ok, pt:encode(45909, Data, Src_Data)};

write (45910,[
    RoleId,
    X,
    Y,
    Status,
    Time
] = Src_Data) ->
    Data = <<
        RoleId:64,
        X:32,
        Y:32,
        Status:8,
        Time:16
    >>,
    {ok, pt:encode(45910, Data, Src_Data)};

write (45911,[
    RoleId,
    Life
] = Src_Data) ->
    Data = <<
        RoleId:64,
        Life:8
    >>,
    {ok, pt:encode(45911, Data, Src_Data)};

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



write (45914,[
    RoleList,
    Pattern
] = Src_Data) ->
    BinList_RoleList = [
        item_to_bin_2(RoleList_Item) || RoleList_Item <- RoleList
    ], 

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

    Data = <<
        RoleList_Len:16, Bin_RoleList/binary,
        Pattern:8
    >>,
    {ok, pt:encode(45914, Data, Src_Data)};

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

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

write (45917,[
    Name
] = Src_Data) ->
    Bin_Name = pt:write_string(Name), 

    Data = <<
        Bin_Name/binary
    >>,
    {ok, pt:encode(45917, Data, Src_Data)};

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

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

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


item_to_bin_0 ({
    Figure,
    ServerNum
}) ->
    Bin_Figure = pt:write_figure(Figure), 

    Data = <<
        Bin_Figure/binary,
        ServerNum:32
    >>,
    Data.
item_to_bin_1 ({
    Figure,
    Life,
    Online
}) ->
    Bin_Figure = pt:write_figure(Figure), 

    Data = <<
        Bin_Figure/binary,
        Life:8,
        Online:8
    >>,
    Data.
item_to_bin_2 ({
    Picture,
    Round,
    Rank,
    Reward,
    Server
}) ->
    Bin_Picture = pt:write_picture(Picture), 

    Bin_Reward = pt:write_object_list(Reward), 

    Data = <<
        Bin_Picture/binary,
        Round:16,
        Rank:8,
        Bin_Reward/binary,
        Server:32
    >>,
    Data.
