% %% ----------------------------------------------------
% %% @FileName : pt_137.erl
% %% @Author : zmh
% %% @Description : 擂台赛
% %% @Created : 2017-06-14
% %% ----------------------------------------------------

-module(pt_137).
-include("drumwar.hrl").
-include("figure.hrl").
-export([read/2,write/2]).

read(13700, _) ->
    {ok, []};

read(13701, _) ->
    {ok, []};

read(13702, _) ->
    {ok, []};

read(13704, _) ->
    {ok, []};

read(13705, _) ->
    {ok, []};

read(13707, <<Type:8>>) ->
    {ok, [Type]};

read(13706,<<Num:8>>)->
    {ok, [Num]};

read(13709, _) ->
    {ok, []};

read(13710, _) ->
    {ok, []};

read(13711, <<DrumID:32>>) ->
    {ok, [DrumID]};

read(13713, _) ->
    {ok, []};

read(13714, Bin0) ->
    <<Type:8, Bin1/binary>> = Bin0,
    <<Uid:64, _Bin2/binary>> = Bin1,
    {ok, [Type,Uid]};

read(13715,<<SKillID:32>>)->
    {ok,[SKillID]};

read(13718, Bin0) ->
    {Msg, Bin1} = pt:read_string(Bin0),
    <<TkTime:32, _Bin2/binary>> = Bin1,
    {ok, [Msg, TkTime]};

read(_Cmd, _Recv) ->
    {error, nomatch}.

write(13700, [State,ReTime])->
    {ok, pt:pack(13700, <<State:8, ReTime:32>>)};

write(13701,[IsSign,Drums,One,Zid,Act,Choice])->
    Len = length(Drums),
    Bin = list_to_binary([<<D:32>>||D<-Drums]),
    CLen = length(Choice),
    BinChoose = list_to_binary(make_choose_bin(Choice,[])),
    % LenC = length(Choice),
    % ChoiceBin = list_to_binary([<<C:64>>||C<-Choice]),
    {ok, pt:pack(13701, <<IsSign:8,Len:16,Bin/binary,One:8,Zid:8,Act:8,CLen:16,BinChoose/binary>>)};

write(13702,[Res])->
    {ok, pt:pack(13702, <<Res:32>>)};

write(13703, [Type,ReTime])->
    {ok, pt:pack(13703, <<Type:8,ReTime:32>>)};

write(13704,[Res])->
    {ok, pt:pack(13704, <<Res:32>>)};

write(13705,[Act, DrumRole])->
    #drum_role{
        win = W,
        zid = Zid,
        lose = L,
        live = Live
    } = DrumRole,
    IsUp = case Live>0 of
        true->
            1;
        _->
            0
    end,
    RAct = case W>0 orelse L>0 of
        true->
            Act;
        _->
            0
    end,
    % Zid = mod_drumwar_mgr:trans_zone(Figure),
    {ok, pt:pack(13705, <<IsUp:8,Zid:8,RAct:8,W:8,L:8,Live:8>>)};

write(13706,[Res,Live])->
    {ok, pt:pack(13706, <<Res:32,Live:8>>)};

write(13707,[Res])->
    {ok, pt:pack(13707, <<Res:32>>)};

write(13708,[IsOver,Res,Act])->
    {ok, pt:pack(13708, <<IsOver:8,Res:8,Act:8>>)};

write(13710,[A, Bid, B])->
    {ok, pt:pack(13710, <<A:8, Bid:64, B:8>>)};

write(13711,[DrumID,Ranks])->
    Len = length(Ranks),
    BinList = [begin
        {Zid,Rank,Rid,SId,Form,Snum,RName,RGName,Vip,Pow,Career} = RankRole,
        PForm = pt:write_string(Form),
        Name = pt:write_string(RName),
        GName = pt:write_string(RGName),
        <<Zid:8,Rank:8,Rid:64,SId:32,PForm/binary,Snum:32,Name/binary,GName/binary,Vip:8,Pow:32,Career:8>>
    end  || RankRole<-Ranks],
    Bin = list_to_binary(BinList),
    {ok, pt:pack(13711,<<DrumID:32,Len:16,Bin/binary>>)};

write(13712,[RoleId, Zone,Name])->
    NameB = pt:write_string(Name),
    {ok,pt:pack(13712,<<RoleId:64, Zone:8, NameB/binary>>)};

write(13713,[Retime,Guess])->
    ZLen = length(Guess),
    BinZone = list_to_binary(make_zone_bin(Guess,[])),
    {ok,pt:pack(13713,<<Retime:32,ZLen:16,BinZone/binary>>)};

write(13714,[Res,Uid,Act])->
    {ok,pt:pack(13714,<<Res:32,Uid:64,Act:8>>)};

write(13715,[Res,SKillID,Time])->
    {ok,pt:pack(13715,<<Res:32,SKillID:32,Time:32>>)};

write(13716,[Pos, RoleId])->
    {ok,pt:pack(13716,<<Pos:8, RoleId:64>>)};

write(13717,[Act, Type, EndTime])->
    {ok,pt:pack(13717,<<Act:8, Type:8, EndTime:32>>)};

write(13718,[Code]) ->
    Data = <<Code:32>>,
    {ok, pt:pack(13718, Data)};

write(_Cmd, _Send) ->
    % util:errlog("~p, ~p, Cmd:~p, Send:~p~n", [?MODULE, ?LINE, _Cmd, _Send]),
    {error, nomatch}.

make_choose_bin([],ChooseBin) ->
    ChooseBin;
make_choose_bin([{Act,Uids}|Choose],ChooseBin) ->
    Len = length(Uids),
    BinUid = list_to_binary(make_choose_id_bin(Uids,[])),
    make_choose_bin(Choose,[<<Act:8,Len:16,BinUid/binary>>|ChooseBin]).

make_choose_id_bin([],ChooseId) -> ChooseId;
make_choose_id_bin([Id|T],ChooseId) ->
    make_choose_id_bin(T,[<<Id:64>>|ChooseId]).

make_zone_bin([],ZoneBin)->
    ZoneBin;
make_zone_bin([{Zone,Acts}|Guess],ZoneBin)->
    LenA = length(Acts),
    ActBin = list_to_binary(make_act_bin(Acts,[])),
    make_zone_bin(Guess,[<<Zone:8,LenA:16,ActBin/binary>>|ZoneBin]).

make_act_bin([],ActBin)->
    ActBin;
make_act_bin([{Act,Groups}|Acts],ActBin)->
    GroupLen = length(Groups),
    GroupBin = list_to_binary(make_group_bin(Groups,[])),
    make_act_bin(Acts,[<<Act:8,GroupLen:16,GroupBin/binary>>|ActBin]).

make_group_bin([],GroupBin)->
    GroupBin;
make_group_bin([Value|Groups],GroupBin)->
    case Value of
        {DRole}->
            #drum_role{
                rid = Uid0,
                figure = #figure{name=Name0,lv=Lv,picture=Pic0,picture_ver=PVer0,career=Career, turn = Turn, sex = Sex},
                power = Pow0
            }=DRole,
            NameA = pt:write_string(Name0),
            PicA = pt:write_string(Pic0),
            UidB = pt:write_string(""),
            Bin = <<Uid0:64,NameA/binary,PicA/binary,PVer0:32,Lv:16,Career:8,Turn:8,Sex:8,Pow0:32,0:64,UidB/binary,UidB/binary,0:32,0:16,0:8,0:8,0:8,0:32>>,
            make_group_bin(Groups,[Bin|GroupBin]);
        {DRoleA,DRoleB}->
            #drum_role{
                rid = Uid0,
                figure = #figure{name=Name0,lv=Lv,picture=Pic0,picture_ver=PVer0,career=Career,turn = Turn, sex = Sex},
                power = Pow0
            }=DRoleA,
            #drum_role{
                rid = Uid1,
                figure = #figure{name=Name1,lv=Lv1,picture=Pic1,picture_ver=PVer1,career=Career1,turn = Turn1, sex = Sex1},
                power = Pow1
            }=DRoleB,
            NameA = pt:write_string(Name0),
            PicA = pt:write_string(Pic0),
            NameB = pt:write_string(Name1),
            PicB = pt:write_string(Pic1),
            Bin = <<Uid0:64,NameA/binary,PicA/binary,PVer0:32,Lv:16,Career:8,Turn:8,Sex:8,Pow0:32,Uid1:64,NameB/binary,PicB/binary,PVer1:32,Lv1:16,Career1:8,Turn1:8,Sex1:8,Pow1:32>>,
            make_group_bin(Groups,[Bin|GroupBin]);
        _->
            make_group_bin(Groups,GroupBin)
    end.
