%%%-----------------------------------
%%% @Module  : pt_200
%%% @Author  : xyao
%%% @Email   : jiexiaowen@gmail.com
%%% @Created : 2011.07.25
%%% @Description: 20战斗信息
%%%-----------------------------------
-module(pt_200).
-export([read/2, write/2]).
-include("common.hrl").

%%
%%客户端 -> 服务端 ----------------------------
%%


%% 玩家发起战斗
read(20001, Bin) ->
    FMonList = fun(<<Id:32, RestBin/binary>>) -> {Id, RestBin} end,
    {MonList, ResBin} = pt:read_array(FMonList, Bin),

    FPlayerList = fun(<<Id:64, RestBin/binary>>) -> {Id, RestBin} end,
    {PlayerIdList, ResBin2} = pt:read_array(FPlayerList, ResBin),

    <<SkillId:32, AttX:16, AttY:16, AttAngle:16, AttObjectId:32, SkillStage:8, MainType:8, _/binary>> = ResBin2,
    {ok, [MonList, PlayerIdList, SkillId, AttX, AttY, AttAngle, AttObjectId, SkillStage, MainType]};

%%加载玩家战斗buff列表
read(20002, _) ->
    {ok, []};

%%复活:1正常复活，2原地复活
read(20004, <<Type:8>>) ->
    {ok, Type};

%%使用辅助技能
read(20006, <<Id:64, AttObjectId:32, SkillId:32>>) ->
    {ok, [Id, AttObjectId, SkillId]};

%%采集怪物
read(20008, <<MonId:32, Mid:32, Type:8>>) ->
    {ok, [MonId, Mid, Type]};

%%复活时间戳
read(20009, _) ->
    {ok, []};

%%拾取怪物
read(20010, Data) ->
    {MonList, _Rest} = pt:read_array(fun(<<Id:32, Rest/binary>>) -> {Id, Rest} end, Data),
    {ok, MonList};

%% 与赏金型怪物对话
read(20011, <<MonId:32>>) ->
    {ok, MonId};

%% 客户端申请扣取血量
read(20012, <<HpDel:32>>) ->
    {ok, HpDel};

%% 客户端申请扣取血量
read(20013, _) ->
    {ok, []};

%% 玩家五分钟内回城复活的次数
read(20017, _) ->
    {ok, []};

read(_Cmd, _R) ->
    {error, no_match}.

%%
%%服务端 -> 客户端 ------------------------------------
%%

%%广播战斗结果
write(20001, [AtterType, Id, Hp, Anger, SkillId, SkillStage, SkillLv, AerX, AerY, AttX, AttY, AttAngle, AttObjectId, AttBuffList, AttGodHp, DefList]) ->
    Data1 = <<AtterType:8, Id:64, Hp:64, Anger:32, SkillId:32, SkillStage:8, SkillLv:16,
    AerX:16, AerY:16, AttX:16, AttY:16, AttAngle:16, AttObjectId:32>>,
    DefList_b     = def_list(DefList),
    Data          = <<Data1/binary, AttBuffList/binary, AttGodHp:64, DefList_b/binary>>,
    {ok, pt:pack(20001, Data)};

%%加载玩家战斗buff列表
write(20002, [AerBuffList]) ->
    {ok, pt:pack(20002, <<AerBuffList/binary>>)};

%% 攻击方触发的被动技能
write(20003, [ResList]) ->
    F = fun({SkillId, EndTime}) -> <<SkillId:32, EndTime:64>> end,
    Data = pt:write_array(F, ResList),
    {ok, pt:pack(20003, Data)};

%%复活结果
write(20004, [Type, Res]) ->
    Data = <<Type:8, Res:32 >>,
    {ok, pt:pack(20004, Data)};

%% 攻击失败返回
write(20005, [ErrCode, Sign1, User1, Hp1, X1, Y1, Sign2, User2, Hp2, X2, Y2, SkillId, InexistenceList]) ->
    Len = length(InexistenceList),
    InexistenceList_b = list_to_binary([<<E:32>>||E<-InexistenceList]),
    {ok, pt:pack(20005, <<ErrCode:8, Sign1:8, User1:64, Hp1:64, X1:16, Y1:16,
            Sign2:8, User2:64, Hp2:64, X2:16, Y2:16, Len:16,
            InexistenceList_b/binary, SkillId:32>>)
    };

%% 广播战斗结果 - 辅助技能
write(20006, [Sign, Id, SkillId, SkillLv, AttObjectId, AssList]) ->
    Data1 = <<Id:64, Sign:8, SkillId:32, SkillLv:8, AttObjectId:32>>,
    Data2 = assist_list(AssList),
    Data = << Data1/binary, Data2/binary>>,
    {ok, pt:pack(20006, Data)};

%% 攻击方触发的被动技能
write(20007, [SkillList, RoleId]) ->
    F = fun(SkillId) -> <<SkillId:32>> end,
    Data = pt:write_array(F, SkillList),
    {ok, pt:pack(20007, <<Data/binary, RoleId:64>>)};

%%采集怪物
write(20008, [Res, MonId, ObjectId]) ->
    {ok, pt:pack(20008, <<Res:32, MonId:32, ObjectId:32>>)};

%%复活时间戳
write(20009, [IsRevive, ReviveTime]) ->
    {ok, pt:pack(20009, <<IsRevive:8, ReviveTime:32>>)};

%%拾取怪物
write(20010, ResList) ->
    F = fun({Res, SrcId}) ->
            <<Res:8, SrcId:32>>
    end,
    Data = pt:write_array(F, ResList),
    {ok, pt:pack(20010, Data)};

%%与赏金怪物对话
write(20011, [Res]) ->
    {ok, pt:pack(20011, <<Res:8>>)};

%% 被杀信息
write(20013, [AttSign, Name, PkValue, BGold, Lv, Turn, AttId]) ->
    NameB = pt:write_string(Name),
    {ok, pt:pack(20013, <<AttSign:8, NameB/binary, PkValue:16, BGold:8, Lv:16, Turn:8, AttId:64>>)};

%% 击杀信息
write(20014, [Name, IsShowPkV, PkValue]) ->
    NameB = pt:write_string(Name),
    {ok, pt:pack(20014, <<NameB/binary, IsShowPkV:8, PkValue:16>>)};

%% 广播pk值
write(20015, [RoleId, PkValue]) ->
    {ok, pt:pack(20015, <<RoleId:64, PkValue:16>>)};

%% 通知客户端最近五分钟内的回城复活次数
write(20017, [ReviveNum, EndTime]) ->
    {ok, pt:pack(20017, <<ReviveNum:16, EndTime:32>>)};

%% 通知玩家技能减少cd
write(20018, [ResList]) ->
    F = fun({SkillId, EndTime}) -> <<SkillId:32, EndTime:64>> end,
    Data = pt:write_array(F, ResList),
    {ok, pt:pack(20018, Data)};

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

def_list([]) -> <<0:16, <<>>/binary>>;
def_list(DefList) ->
    F = fun([Sign, Id, Hp, _Mp, Anger, Hurt, HurtType, X, Y, Move, InteruptSkillId, DBuffList, GodHp]) ->
        <<Sign:8, Id:64, Hp:64, Anger:32, Hurt:32, HurtType:8, X:16, Y:16, Move:8, InteruptSkillId:32, DBuffList/binary, GodHp:64>>
    end,
    L  = [F(D) || D <- DefList, D =/= []],
    RB = list_to_binary(L),
    RLen = length(L),
    <<RLen:16, RB/binary>>.

assist_list([]) -> <<0:16, <<>>/binary>>;
assist_list(List) ->
    Rlen = length(List),
    F = fun([Sign, Id, Hp, DBuffList]) ->
            <<Sign:8, Id:64, Hp:64, DBuffList/binary>>
    end,
    RB = list_to_binary([F(D) || D <- List]),
    <<Rlen:16, RB/binary>>.
