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

read(60001, _) ->
    {ok, []};
read(60002, _) ->
    {ok, []};
read(60003, _) ->
    {ok, []};
read(60004, _) ->
    {ok, []};
read(60008, Bin0) ->
    <<Opty:8, _Bin1/binary>> = Bin0, 
    {ok, [Opty]};
read(60010, _) ->
    {ok, []};
read(60011, _) ->
    {ok, []};
read(_Cmd, _R) -> {error, no_match}.

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

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

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

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

write (60004,[
    Etime,
    Floor,
    Mfloor,
    Score,
    Kills,
    Grade
] = Src_Data) ->
    Data = <<
        Etime:32,
        Floor:8,
        Mfloor:8,
        Score:32,
        Kills:32,
        Grade:8
    >>,
    {ok, pt:encode(60004, Data, Src_Data)};

write (60005,[
    Score,
    Kills,
    MultiKills,
    Grade
] = Src_Data) ->
    Data = <<
        Score:32,
        Kills:32,
        MultiKills:32,
        Grade:32
    >>,
    {ok, pt:encode(60005, Data, Src_Data)};

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

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



write (60009,[
    Rank,
    Floor,
    Score,
    Grade,
    Sort
] = Src_Data) ->
    Data = <<
        Rank:32,
        Floor:8,
        Score:32,
        Grade:32,
        Sort:32
    >>,
    {ok, pt:encode(60009, Data, Src_Data)};

write (60010,[
    RankList,
    MyRank,
    MyStage,
    MyTkill,
    MyCombo,
    MyScore
] = Src_Data) ->
    BinList_RankList = [
        item_to_bin_0(RankList_Item) || RankList_Item <- RankList
    ], 

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

    Data = <<
        RankList_Len:16, Bin_RankList/binary,
        MyRank:16,
        MyStage:8,
        MyTkill:16,
        MyCombo:16,
        MyScore:32
    >>,
    {ok, pt:encode(60010, Data, Src_Data)};

write (60011,[
    Click,
    Dtime
] = Src_Data) ->
    Data = <<
        Click:8,
        Dtime:32
    >>,
    {ok, pt:encode(60011, Data, Src_Data)};

write (60012,[
    Type,
    Killer,
    Deader
] = Src_Data) ->
    Bin_Killer = pt:write_picture(Killer), 

    Bin_Deader = pt:write_picture(Deader), 

    Data = <<
        Type:8,
        Bin_Killer/binary,
        Bin_Deader/binary
    >>,
    {ok, pt:encode(60012, Data, Src_Data)};

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


item_to_bin_0 ({
    Rank,
    Rid,
    Head,
    ServerNum,
    Stage,
    Tkill,
    Combo,
    Score,
    Vip,
    Sex
}) ->
    Bin_Head = pt:write_picture(Head), 

    Data = <<
        Rank:16,
        Rid:64,
        Bin_Head/binary,
        ServerNum:32,
        Stage:8,
        Tkill:16,
        Combo:16,
        Score:32,
        Vip:8,
        Sex:8
    >>,
    Data.
