local In_situ_login_on_off = "on" ; --Ƿ񿪷ԭص¼ on--  off--
local In_situ_login_Count = 20; --Ĭϵԭص¼npcȡ
local In_situ_login_TimeOut =604800; --ԭص¼ʱʱ䣬λΪ
local In_situ_login_Limit = false; --Ƿ񿪷޴ԭص¼

local NPCName = "ԭص¼ȡԱ"; --ȡԭص¼NPC
local NPCPoint = {106602,1000,247,91,6}; --ԭص¼ȡԱԭͣͼX꣬Y꣬
--  ***************************************************************************************************** --


--ע¼ί
Delegate.RegDelLoginEvent("MyLogin_LoginEvent");
Delegate.RegDelAllOutEvent("MyLogin_OutEvent");
Delegate.RegDelWarpEvent("MyLogin_WarpEvent");
Delegate.RegInit("MyLogin_Init");
-- Delegate.RegDelTalkEvent("MyLogin_TalkEvent");

--洢Ϣ
tbl_dropinfo = {};
tbl_In_situsetting =
{
    defcount = In_situ_login_Count;
    timeout = In_situ_login_TimeOut; 
    limit = In_situ_login_Limit; 
};

function new_tbl_dropinfoobject(player) 
	
	local lef = 0;
	if(tbl_dropinfo[Playerkey(player)] ~= nil)then
		lef = tbl_dropinfo[Playerkey(player)].leftCount;
	end
	
	local _tbl_dropinfo =
	{
		leftCount = lef;
		LoginCount = Char.GetData(player,%_½%);
		
		MapType = Char.GetData(player,%_MAP%);  
		MapId = Char.GetData(player,%_ͼ%);	
		X = Char.GetData(player,%_X%);
		Y = Char.GetData(player,%_Y%);
		
		DengonMapId = 0;
		DengonName = "";
		DengonX = 0;
		DengonY = 0;
		time = os.time();
		IsFeverTime = Char.IsFeverTime(player);
		
	};
	return _tbl_dropinfo;
end

function new_tbl_dropinfoobject_without_setting(player)
	
	local _tbl_dropinfo =
	{
		leftCount = 0;
		LoginCount = Char.GetData(player,%_½%);
		
		MapType = tbl_dropinfo[Playerkey(player)].MapType;        
		MapId = tbl_dropinfo[Playerkey(player)].MapId;	
		X = tbl_dropinfo[Playerkey(player)].X;
		Y = tbl_dropinfo[Playerkey(player)].Y;
		
		DengonMapId = 0;
		DengonName = "";
		DengonX = 0;
		DengonY = 0;
		time = os.time();
		IsFeverTime = Char.IsFeverTime(player);
	};
	return _tbl_dropinfo;
end

function getdropinfofromdb(player)

	local dropinfo = Field.Get(player,"dropinfo");

	if(dropinfo == "") then
		return;
	end

	local dropinfoArray = Split(dropinfo,"_");

	local _tbl_dropinfo =
	{
		leftCount = tonumber(dropinfoArray[1]);
		LoginCount = tonumber(dropinfoArray[2]);
		
		MapType = tonumber(dropinfoArray[3]);
		MapId = tonumber(dropinfoArray[4]);
		X = tonumber(dropinfoArray[5]);
		Y = tonumber(dropinfoArray[6]);
		
		DengonMapId = tonumber(dropinfoArray[7]);
		DengonName = tonumber(dropinfoArray[8]);
		DengonX = tonumber(dropinfoArray[9]);
		DengonY = tonumber(dropinfoArray[10]);
		time = tonumber(dropinfoArray[11]);
		IsFeverTime = tonumber(dropinfoArray[12]);
		
	};

	return _tbl_dropinfo;
end

function savedropinfotodb(player)

	local dropinfostr = tbl_dropinfo[Playerkey(player)].leftCount.."_"..tbl_dropinfo[Playerkey(player)].LoginCount.."_"..tbl_dropinfo[Playerkey(player)].MapType.."_"..tbl_dropinfo[Playerkey(player)].MapId.."_"..tbl_dropinfo[Playerkey(player)].X.."_"..tbl_dropinfo[Playerkey(player)].Y.."_"..tbl_dropinfo[Playerkey(player)].DengonMapId.."_"..tbl_dropinfo[Playerkey(player)].DengonName.."_"..tbl_dropinfo[Playerkey(player)].DengonX.."_"..tbl_dropinfo[Playerkey(player)].DengonY.."_"..tbl_dropinfo[Playerkey(player)].time.."_"..tbl_dropinfo[Playerkey(player)].IsFeverTime;
	Field.Set(player,"dropinfo",""..dropinfostr);
	return;

end

function MyLogin_LoginEvent(player)
	

	if(Char.GetData(player,%_½%) == 999)then
		Char.SetData(player, %_½% , 1);
	end
	

	local suss_link = false; --Ƿɹԭص¼
	local suss_Message = "";
	local _playerkey = Playerkey(player);
	local Drop_list = getdropinfofromdb(player);

	if(Drop_list == nil) then
		tbl_dropinfo[_playerkey] = new_tbl_dropinfoobject(player);  --״ε¼ĿǰϢֱӷ
		return;
	else
		tbl_dropinfo[_playerkey] = Drop_list;
	end

	if(tbl_In_situsetting.limit == true) then --޴ԭص¼
		suss_Message = "Ŀǰ޴ԭص¼Ѿָ";
		suss_link = true;
	end
	
	
	if (tonumber(Char.GetData(player,%_½%)) - Drop_list.LoginCount <= 1 and Drop_list.leftCount > 0)then --ǷʱǷ񻻹ߣԭص¼Ƿ
	    tbl_dropinfo[_playerkey].leftCount = tbl_dropinfo[_playerkey].leftCount - 1; --һ
		--print("in is"..tbl_dropinfo[_playerkey].leftCount);
		suss_link = true;
	end
	
	--NLG.SystemMessage(player,(os.time() - Drop_list.time).." "..tbl_In_situsetting.timeout.." "..tonumber(Char.GetData(player,%_½%)) - Drop_list.LoginCount.." "..Drop_list.leftCount);
	if(Drop_list.MapType ~= 0) then --Թڵ
		local Walkable = NLG.Walkable(1,Drop_list.DengonMapId,Drop_list.DengonX,Drop_list.DengonY);
		if (NLG.GetMapName(1,Drop_list.DengonMapId) == Drop_list.DengonName and Walkable == 1) then --ԹδʧͻԹ
			Drop_list.MapType = 1;
			Drop_list.MapId = Drop_list.DengonMapId;
			Drop_list.X = Drop_list.DengonX;
			Drop_list.Y = Drop_list.DengonY;
			
			suss_Message = "ԹδʧѾָ";
		else --ͻԹ
			Drop_list.MapType = 0;
			suss_Message = "Թʧˣ͵Թڣ";
		end
	
	else --ڳͼڵ߻ߵǳ
			suss_Message = "Ѿָ";
	end
	suss_Message = suss_Message.."ԭص¼ʣ:"..tbl_dropinfo[_playerkey].leftCount.."Ρ";
	if(In_situ_login_on_off == "on" and suss_link == true ) then
		--NLG.SystemMessage(player,Drop_list.MapType.." "..Drop_list.MapId.." "..Drop_list.X.." "..Drop_list.Y.." "..Drop_list.DengonName);
		Char.Warp(player,Drop_list.MapType,Drop_list.MapId,Drop_list.X,Drop_list.Y);
		if(tbl_dropinfo[_playerkey].IsFeverTime == 1) then
			Char.FeverStart(player);
		end
		NLG.SystemMessage(player,suss_Message);
	end
	--NLG.SystemMessage(player,Drop_list.MapType.." "..Drop_list.MapId.." "..Drop_list.X.." "..Drop_list.Y.." "..Drop_list.DengonName);
end

function MyLogin_OutEvent(player)

	local Playerinfo = new_tbl_dropinfoobject(player);
	if(Playerinfo.MapType == 0) then  --Թڣ򱣴泣ͼϢ
		Playerinfo.leftCount = tbl_dropinfo[Playerkey(player)].leftCount; --浱ǰԭص¼
		--print("key is "..Playerkey(player));
		--print("value is "..tbl_dropinfo[Playerkey(player)].leftCount);		
		tbl_dropinfo[Playerkey(player)] = Playerinfo; 
		savedropinfotodb(player);
		tbl_dropinfo[Playerkey(player)] = nil;
		return;
	end
	
	if(Playerinfo.MapType == 1) then --Թڣ򱣴ԹϢ
		local dropinfo = new_tbl_dropinfoobject_without_setting(player); 
		local MapName = NLG.GetMapName(1,Playerinfo.MapId); --ȡԹ֣ԹʧصԹ
		dropinfo.MapType = 1;
		dropinfo.DengonMapId = Playerinfo.MapId;
		dropinfo.DengonName = MapName;
		dropinfo.DengonX = Playerinfo.X;
		dropinfo.DengonY = Playerinfo.Y;
		dropinfo.leftCount = tonumber(tbl_dropinfo[Playerkey(player)].leftCount); --浱ǰԭص¼
		tbl_dropinfo[Playerkey(player)] = dropinfo; 
		savedropinfotodb(player);
		tbl_dropinfo[Playerkey(player)] = nil;
	end
	
end


function MyLogin_WarpEvent(player,x,y)
	--[[
	local dropinfo = new_tbl_dropinfoobject(player);  
	if(dropinfo.MapType == 0) then 
		dropinfo.leftCount = tonumber(tbl_dropinfo[Playerkey(player)].leftCount); 
		tbl_dropinfo[Playerkey(player)] = dropinfo; 
		
	end--]]
	local partyNum = Char.PartyNum(player); --жԱԹǰλϢ
		for i=0,partyNum do
			local this_player = Char.GetPartyMember(player,i);
			if(VaildChar(this_player))then
			   local dropinfo = new_tbl_dropinfoobject(this_player);  
				if(dropinfo.MapType == 0) then 
					tbl_dropinfo[Playerkey(this_player)] = dropinfo; 
					--NLG.SystemMessage(this_player,dropinfo.X);
				end
			end
		end
	
end


function inityddlNpc_Init(index)
	print("ԭص¼npc_index = " .. index);
	return 1;
end

function inityddlNpc()
	if (yddlnpc == nil) then
		yddlnpc = NL.CreateNpc("lua/Module/myLogin.lua", "inityddlNpc_Init");
		Char.SetData(yddlnpc,%_%,NPCPoint[1]);
		Char.SetData(yddlnpc,%_ԭ%,NPCPoint[1]);
		Char.SetData(yddlnpc,%_X%,NPCPoint[3]);
		Char.SetData(yddlnpc,%_Y%,NPCPoint[4]);
		Char.SetData(yddlnpc,%_ͼ%,NPCPoint[2]);
		Char.SetData(yddlnpc,%_%,NPCPoint[5]);
		Char.SetData(yddlnpc,%_ԭ%,NPCName);
		NLG.UpChar(yddlnpc);
		
		--Npc˵ʱ,ChangePassMsg
		Char.SetTalkedEvent("lua/Module/myLogin.lua","yddlnpcMsg", yddlnpc);
	end
	if (yddlnpc_1 == nil) then
		yddlnpc_1 = NL.CreateNpc("lua/Module/myLogin.lua", "inityddlNpc_Init");
		Char.SetData(yddlnpc_1,%_%,NPCPoint[1]);
		Char.SetData(yddlnpc_1,%_ԭ%,NPCPoint[1]);
		Char.SetData(yddlnpc_1,%_X%,34);
		Char.SetData(yddlnpc_1,%_Y%,35);
		Char.SetData(yddlnpc_1,%_ͼ%,777);
		Char.SetData(yddlnpc_1,%_%,4);
		Char.SetData(yddlnpc_1,%_ԭ%,NPCName);
		NLG.UpChar(yddlnpc_1);
		
		--Npc˵ʱ,ChangePassMsg
		Char.SetTalkedEvent("lua/Module/myLogin.lua","yddlnpcMsg", yddlnpc_1);
	end
end

function yddlnpcMsg(_me,_tome)
	if (NLG.CanTalk(_me,_tome) == true) then
		str_ChangeWindow = "\\n\\n ߣԭصǳΪ"..tbl_In_situsetting.defcount.."Σ\\n\\n ǳ3콫ؼ¼㡣";
	    tbl_dropinfo[Playerkey(_tome)].leftCount = tbl_In_situsetting.defcount; 
		NLG.ShowWindowTalked(_tome,_me,%_Ϣ%,%ť_ر%,1,str_ChangeWindow);
		return;
	end
end

function inityddltable_cleanNpc_Init(index)
	print("ԭص¼ڴԱnpc_index = " .. index);
	return 1;
end

function inittable_cleanNpc()
	if (table_cleanNpc == nil) then
		table_cleanNpc = NL.CreateNpc("lua/Module/myLogin.lua", "inityddltable_cleanNpc_Init");
		Char.SetData(table_cleanNpc,%_%,231088);
		Char.SetData(table_cleanNpc,%_ԭ%,231088);
		Char.SetData(table_cleanNpc,%_X%,47);
		Char.SetData(table_cleanNpc,%_Y%,47);
		Char.SetData(table_cleanNpc,%_ͼ%,777);
		Char.SetData(table_cleanNpc,%_%,4);
		Char.SetData(table_cleanNpc,%_ԭ%,"ڴԱ");
		NLG.UpChar(table_cleanNpc);
		
		--Npc˵ʱ,ChangePassMsg
		Char.SetLoopEvent("lua/Module/myLogin.lua","table_cleanNpcLoopEvent", table_cleanNpc,1800); --30ִһtable
	end
end

function table_cleanNpcLoopEvent(index)
   
   --ԭص¼table
   --ͣʱôֱشdropϢ
   
   for i,v in pairs(tbl_dropinfo) do
      if(os.time() - v.time > tbl_In_situsetting.timeout )then
         tbl_dropinfo[i] = nil;
      
      end
   
   end
   
   --
  
  
end



function MyLogin_Init()
	
	inityddlNpc();
	--inittable_cleanNpc();

end
