%%%-------------------------------------------------------------------
%%% @doc    废土战场(饥饿游戏)玩法对外接口
%%% Module  : wasteland_war_api.erl
%%% Created : 2020-08-03
%%% @Author : tyl
%%%-------------------------------------------------------------------
-module(wasteland_war_api).

-include("common.hrl").
-include("server.hrl").
-include("figure.hrl").
-include("errcode.hrl").
-include("predefine.hrl").
-include("wasteland_war.hrl").
-include("def_event.hrl").
-include("rec_event.hrl").

%% API
-export([
	reconnect/1
	,handle_event/2
	%% 复活cd相关
	,check_cd_time/1
	,put_revive_cd/0
	%% gm接口
	,gm_start/0
	,gm_end/0
]).


%% reconnect断线重连
reconnect(Player) ->
	#player_status{id = RoleId, scene = Scene} = Player,
	ServerId = config:get_server_id(),
	IsWasteland = lib_wasteland_war:is_wasteland_scene(Scene),
	if
		IsWasteland ->
			Node = clusters_node:get_node(),
			mod_wasteland_war:reconnect(Node, ServerId, RoleId),
			{ok, Player};
		true ->
			{next, Player}
	end.

%% ------------------------------------------------------------------------
%% 战力变更
handle_event(Player, #event_callback{type_id = ?EVENT_COMBAT_POWER}) ->
	#player_status{server_id = ServerId, id = RoleId, combat_power = Power} = Player,
	Node = clusters_node:get_node(),
	mod_wasteland_war:power_change(Node, RoleId, ServerId, Power),
	{ok, Player};

%% vip等级变更
handle_event(Player, #event_callback{type_id = ?EVENT_VIP}) ->
	#player_status{server_id = ServerId, id = RoleId, figure = Figure} = Player,
	Node = clusters_node:get_node(),
	mod_wasteland_war:figure_change(Node, RoleId, ServerId, ?CHANGE_TYPE_VIP, Figure#figure.vip),
	{ok, Player};

%% 贵族变更
handle_event(Player, #event_callback{type_id = ?EVENT_SUP_VIP_CHANGE}) ->
	#player_status{server_id = ServerId, id = RoleId, figure = Figure} = Player,
	Node = clusters_node:get_node(),
	mod_wasteland_war:figure_change(Node, RoleId, ServerId, ?CHANGE_TYPE_SUP_VIP, Figure#figure.sup_vip),
	{ok, Player};

%% 名字变更
handle_event(Player, #event_callback{type_id = ?EVENT_RENAME}) ->
	#player_status{server_id = ServerId, id = RoleId, figure = Figure} = Player,
	Node = clusters_node:get_node(),
	mod_wasteland_war:figure_change(Node, RoleId, ServerId, ?CHANGE_TYPE_NAME, Figure#figure.name),
	{ok, Player};

%% 死亡复活cd设置,更新cd时间 & 记录死亡状态
handle_event(#player_status{scene = SceneId, id = RoleId} = Player, #event_callback{type_id = ?EVENT_PLAYER_DIE}) ->
	ServerId = config:get_server_id(),
	Node = clusters_node:get_node(),
	case lib_wasteland_war:is_wasteland_scene(SceneId) of
		true ->
			put_revive_cd(),
			mod_wasteland_war:record_die_state(Node, ServerId, RoleId);
		_ ->
			ignore
	end,
	{ok, Player};

%% 复活后清除死亡状态
handle_event(#player_status{scene = SceneId, id = RoleId} = Player, #event_callback{type_id = ?EVENT_REVIVE}) ->
	ServerId = config:get_server_id(),
	Node = clusters_node:get_node(),
	case lib_wasteland_war:is_wasteland_scene(SceneId) of
		true ->
			mod_wasteland_war:clean_die_state(Node, ServerId, RoleId);
		_ ->
			ignore
	end,
	{ok, Player};

%% 切换场景之后推送当前轮数
handle_event(#player_status{id = RoleId, scene = SceneId} = Player, #event_callback{type_id = ?EVENT_FIN_CHANGE_SCENE}) ->
	AllScenes = data_wasteland_war:get_all_scene(),
	AllClsScene = data_wasteland_war:get_cls_all_scene(),
	ServerId  = config:get_server_id(),
	case lists:member(SceneId, AllScenes ++ AllClsScene) of
		true ->
			Node = clusters_node:get_node(),
			mod_wasteland_war:notice_round(Node, ServerId, RoleId);
		_ ->
			ignore
	end,
	{ok, Player}.

%% 上线后检测玩家状态,如果是掉线则拉回
%%handle_event(#player_status{id = RoleId, scene = _SceneId} = Player, #event_callback{type_id = ?EVENT_FIN_CHANGE_SCENE}) ->
%%	ServerId  = config:get_server_id(),
%%	Node = clusters_node:get_node(),
%%	case lib_wasteland_war:get_custom_act() of
%%		false ->
%%			case lib_wasteland_war:get_act_cls_type() of
%%				1 -> clusters_node:apply_cast(mod_wasteland_war, relogin, [Node, ServerId, RoleId]);
%%				_ -> mod_wasteland_war:relogin(Node, ServerId, RoleId)
%%			end;
%%		_ -> mod_wasteland_war:relogin(Node, ServerId, RoleId)
%%	end,
%%	{ok, Player}.

%% 玩家进程
%% 10s免费复活CD
check_cd_time(Type) ->
	case Type of
		3 ->
			NowtTime = utime:unixtime(),
			OldTime  = get_revive_cd(),
			case NowtTime > (OldTime+?WASTELAND_REVIVE_CD) of
				true -> true;
				_    -> {false, ?ERRCODE(err602_cd_err)}
			end;
		_ ->
			true
	end.

%% 存储复活CD
put_revive_cd() ->
	CDKey = ?WASTELAND_REVIVE_CD_KEY,
	NowtTime = utime:unixtime(),
	put(CDKey, NowtTime).
%% 获取复活CD
get_revive_cd() ->
	CDKey = ?WASTELAND_REVIVE_CD_KEY,
	case get(CDKey) of
		Cd when is_integer(Cd) -> Cd;
		_ -> 0
	end.

%% -------------------------------------------------------------------
gm_start() ->
	case czone_api:zone_open_check() of
		{true, _ZoneId} ->
			clusters_node:apply_cast(mod_wasteland_war, gm_start, [999]);
		_ ->
			mod_wasteland_war:gm_start(999)
	end.

gm_end() ->
	case czone_api:zone_open_check() of
		{true, _ZoneId} ->
			clusters_node:apply_cast(mod_wasteland_war, gm_end, []);
		_ ->
			mod_wasteland_war:gm_end()
	end.