%%%===================================================================
%%% @author zmh
%%% @doc 征途活动
%%% @end
%%%===================================================================
-module(pp_journey).
-include("server.hrl").
-include("custom_journey.hrl").
-include("custom_act.hrl").
-include("predefine.hrl").
-include("errcode.hrl").
-include("figure.hrl").
-export([handle/3]).

%%任务数据
handle(13800,Status,[SubType,OpTy])->
	#player_status{id=RoleID,sid=Sid}=Status,
	Type = ?CUSTOM_ACT_TYPE_JOURNEY,
	MapTasks = case lib_custom_act_api:is_open_act(Type,SubType) of
		true->
			custom_act_journey:get_act_tasks(RoleID,SubType);
		_->
			[]
	end,
	Tasks = pack_13800(MapTasks,OpTy,[]),
	{ok, Bin} = pt_138:write(13800,[SubType,OpTy,Tasks]),
	lib_server_send:send_to_sid(Sid,Bin),
	{ok,Status};

%%领取
handle(13802,Status,[SubType,Map,Id])->
	#player_status{sid=Sid}=Status,
	case custom_act_journey:fetch_award(SubType,Map,Id,Status) of
		{ok,NStatus}->
			Res = ?SUCCESS;
		{false,Res}->
			NStatus = Status
	end,
	{ok, Bin} = pt_138:write(13802,[Res]),
	lib_server_send:send_to_sid(Sid,Bin),
	{ok,NStatus};

%%购买
handle(13803,Status,[SubType,Map,Id])->
	#player_status{sid=Sid}=Status,
	case custom_act_journey:custom_task(SubType,Map,Id,Status) of
		{ok,NStatus}->
			Res = ?SUCCESS;
		{false,Res}->
			NStatus = Status,
			skip
	end,
	{ok, Bin} = pt_138:write(13803,[Res]),
	lib_server_send:send_to_sid(Sid,Bin),
	{ok,NStatus};


handle(_Cmd, _Status, _Data) ->
	ok.

pack_13800([],_OpTy,Tasks)->
	Tasks;
pack_13800([H|T],OpTy,Tasks) when OpTy=<0->
	#map_journey{
		map = Map,
		tasks = MapTasks
	}=H,
	Task0 = [{Id,CurS,CurV,Grade}||#journey_task{id=Id,state=CurS,value=CurV,grade=Grade}<-MapTasks],
	pack_13800(T,OpTy,[{Map,Task0}|Tasks]);
pack_13800([H|T],OpTy,Tasks)->
	#map_journey{
		map = Map,
		tasks = MapTasks
	}=H,
	case OpTy =:= Map of
		true->
			Task0 = [{Id,CurS,CurV,Grade}||#journey_task{id=Id,state=CurS,value=CurV,grade=Grade}<-MapTasks],
			pack_13800(T,OpTy,[{Map,Task0}|Tasks]);
		_->
			pack_13800(T,OpTy,Tasks)
	end.