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

read(60200, _) ->
    {ok, []};
read(60201, _) ->
    {ok, []};
read(60202, _) ->
    {ok, []};
read(60205, _) ->
    {ok, []};
read(_Cmd, _R) -> {error, no_match}.

write (60200,[
    State,
    StartTime,
    ActType
] = Src_Data) ->
    Data = <<
        State:8,
        StartTime:32,
        ActType:8
    >>,
    {ok, pt:encode(60200, Data, Src_Data)};

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

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

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

write (60204,[
    MyScore,
    MyRank
] = Src_Data) ->
    Data = <<
        MyScore:32,
        MyRank:16
    >>,
    {ok, pt:encode(60204, Data, Src_Data)};

write (60205,[
    RankList,
    MyRank,
    MyRound,
    MyKillNum,
    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,
        MyRound:8,
        MyKillNum:16,
        MyCombo:16,
        MyScore:32
    >>,
    {ok, pt:encode(60205, Data, Src_Data)};

write (60206,[
    Round,
    RoundEndTime
] = Src_Data) ->
    Data = <<
        Round:8,
        RoundEndTime:32
    >>,
    {ok, pt:encode(60206, Data, Src_Data)};

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

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

    Bin_Deader = pt:write_picture(Deader), 

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

write (60209,[
    Type,
    Rank,
    Score
] = Src_Data) ->
    Data = <<
        Type:8,
        Rank:16,
        Score:32
    >>,
    {ok, pt:encode(60209, Data, Src_Data)};

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

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

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

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


item_to_bin_0 ({
    Rank,
    RoleId,
    RoleName,
    RolePower,
    Sex,
    VipLv,
    SupVip,
    ServerId,
    ServerNum,
    Round,
    KillNum,
    MaxCombo,
    Score
}) ->
    Bin_RoleName = pt:write_string(RoleName), 

    Data = <<
        Rank:16,
        RoleId:64,
        Bin_RoleName/binary,
        RolePower:64,
        Sex:8,
        VipLv:8,
        SupVip:8,
        ServerId:16,
        ServerNum:16,
        Round:8,
        KillNum:16,
        MaxCombo:16,
        Score:32
    >>,
    Data.
