%% --------------------------------------
%% desc     :社团跑环协助协议映射
%% module   :pp_guild_run_assist.erl
%% author   :hh
%% time     :21/04/17
%% --------------------------------------

-module (pp_guild_run_assist).

-export ([handle/3]).

-include ("server.hrl").
-include ("guild_run_assist.hrl").
-include ("def_module.hrl").
-include ("errcode.hrl").
-include ("common.hrl").
-include ("figure.hrl").

handle(Cmd, Player, Data) ->
    LvRet = lib_module:is_open(Player, ?MOD_GUILD_RUN_ASSIST),
    if
        LvRet ->
            do_handle(Cmd, Player, Data);
        true ->
            skip 
    end.

do_handle(41701, Player, _) ->
    #player_status{sid = Sid, role_guild_run_assist = RoleGuildRunAssist, figure = #figure{lv = RoleLv}} = Player,
    #role_guild_run_assist{res_sid = ResSid, extra_data = ExtraData} = RoleGuildRunAssist,
    QuizResult = maps:get(?GUILD_RUN_ASSIST_QUIZ, ExtraData, false),
    if
        ResSid == 0 ->
            lib_server_send:send_to_sid(Sid, pt_417, 41701, [?ERRCODE(err417_not_in_guild_run_assist), 0, 0]);
        not QuizResult ->
            lib_server_send:send_to_sid(Sid, pt_417, 41701, [?ERRCODE(err417_not_in_assist_quiz), 0, 0]);
        true ->
            case guild_run_assist_quiz:get_quiz(RoleLv, QuizResult) of
                {ok, Index, NowQuizId, NewQuizResult} ->
                    NewExtraData = maps:put(?GUILD_RUN_ASSIST_QUIZ, NewQuizResult, ExtraData),
                    NewRoleGuildRunAssist = RoleGuildRunAssist#role_guild_run_assist{extra_data = NewExtraData},
                    NewPlayer = Player#player_status{role_guild_run_assist = NewRoleGuildRunAssist},
                    lib_server_send:send_to_sid(Sid, pt_417, 41701, [?SUCCESS, NowQuizId, Index]),
                    {ok, NewPlayer};
                {false, Errcode} ->
                    lib_server_send:send_to_sid(Sid, pt_417, 41701, [Errcode, 0, 0])
            end
    end;

do_handle(41702, Player, [QuizId, Answer]) ->
    #player_status{sid = Sid, role_guild_run_assist = RoleGuildRunAssist} = Player,
    #role_guild_run_assist{res_sid = ResSid, extra_data = ExtraData} = RoleGuildRunAssist,
    QuizResult = maps:get(?GUILD_RUN_ASSIST_QUIZ, ExtraData, false),
    if
        ResSid == 0 ->
            lib_server_send:send_to_sid(Sid, pt_417, 41702, [?ERRCODE(err417_not_in_guild_run_assist), QuizId, Answer, 0]);
        not QuizResult ->
            lib_server_send:send_to_sid(Sid, pt_417, 41702, [?ERRCODE(err417_not_in_assist_quiz), QuizId, Answer, 0]);
        true ->
            guild_run_assist_quiz:answer(Player, QuizId, Answer)
    end;

do_handle(41704, Player, _) ->
    #player_status{sid = Sid, role_guild_run_assist = RoleGuildRunAssist} = Player,
    #role_guild_run_assist{res_sid = ResSid, extra_data = ExtraData} = RoleGuildRunAssist,
    QuizResult = maps:get(?GUILD_RUN_ASSIST_QUIZ, ExtraData, false),
    if
        ResSid == 0 ->
            lib_server_send:send_to_sid(Sid, pt_417, 41704, [?ERRCODE(err417_not_in_guild_run_assist)]);
        not QuizResult ->
            lib_server_send:send_to_sid(Sid, pt_417, 41704, [?ERRCODE(err417_not_in_assist_quiz)]);
        true ->
            guild_run_assist_quiz:active_quit_quiz(Player)
    end;

do_handle(41706, Player, _) ->
    #player_status{sid = Sid, role_guild_run_assist = RoleGuildRunAssist} = Player,
    #role_guild_run_assist{res_sid = ResSid, extra_data = ExtraData} = RoleGuildRunAssist,
    case maps:get(?GUILD_RUN_ASSIST_BOSS, ExtraData, []) of
        [BossId, BossPower, _] ->
            lib_server_send:send_to_sid(Sid, pt_417, 41706, [ResSid, BossId, BossPower]);
        _ -> skip
    end.