%%%-------------------------------------------------------------------
%%% @doc    废土战场(饥饿游戏) - 协议文件
%%% Module  : pp_wasteland_war.erl
%%% Created : 2020-08-03
%%% @Author : tyl
%%%-------------------------------------------------------------------
-module(pp_wasteland_war).


-include("server.hrl").
-include("wasteland_war.hrl").
-include("figure.hrl").
-include("errcode.hrl").
-include("common.hrl").
-include("def_module.hrl").
-include("offline_hosting.hrl").
-include("attr.hrl").

%% API
-export([handle/3]).

%% 活动状态
handle(60200, PS, []) ->
	#player_status{id = RoleId} = PS,
	Node = clusters_node:get_node(),
	mod_wasteland_war:act_status(Node, RoleId),
	ok;

%% 进入场景
handle(60201 = Cmd, PS, []) ->
	#player_status{id = RoleId, server_num = ServerNum, figure = Figure,
		offline_hosting = OffLineHosting, battle_attr = BattleAttr} = PS,
	#offline_hosting{handler = Handler} = OffLineHosting,
	ServerId = config:get_server_id(),
	ConfigLv = data_module:get_module_open_lv(?MOD_WASTELAND_WAR),
	if
		Figure#figure.lv < ConfigLv ->
			send_to_client(PS#player_status.sid, Cmd, ?ERRCODE(err602_lv_not_enough));
		true ->
			case lib_player_check:check_all(PS) of
				true ->
					Node = clusters_node:get_node(),
					IsHosting =
					case Handler == offline_hosting_wasteland_war of
						true -> ?IN_HOSTING;
						_    -> ?IN_HOSTING_NOT
					end,
					mod_wasteland_war:enter_scene(Node, RoleId, Figure, ServerId,
						ServerNum, IsHosting, BattleAttr#battle_attr.hp_lim
					);
				{false, ErrCode} ->
					send_to_client(PS#player_status.sid, Cmd, ErrCode)
			end
	end,
	ok;

%% 退出场景
handle(60202, PS, []) ->
	#player_status{id = RoleId} = PS,
	Node = clusters_node:get_node(),
	ServerId = config:get_server_id(),
	mod_wasteland_war:quit_scene(Node, ServerId, RoleId),
	ok;

%% 查询积分排行
handle(60205 = Cmd, PS, []) ->
	#player_status{id = RoleId, server_id = ServerId, figure = Figure} = PS,
	#figure{lv = RoleLv} = Figure,
	ConfigLv = data_module:get_module_open_lv(?MOD_WASTELAND_WAR),
	if
		RoleLv < ConfigLv ->
			send_to_client(PS#player_status.sid, Cmd, ?ERRCODE(err602_lv_not_enough));
		true ->
			Node = clusters_node:get_node(),
			mod_wasteland_war:fetch_score_rank(Node, RoleId, ServerId)
	end,
	ok;

handle(_Code, PS, _Args) ->
	?ERR("Nomatch Cmd:~p Args:~p", [_Code, _Args]),
	{ok, PS}.

send_to_client(Sid, Cmd, Msg) ->
	{ok, BinData} = pt_602:write(Cmd, [Msg]),
	lib_server_send:send_to_sid(Sid, BinData).