------------------------------------------------------------------------------
------------------------------------------------------------------------------
--                 ű˵
------------------------------------------------------------------------------
--ֵ߳ȡLuaű For GA
--
--ݿѯӦֶΣϷһΪֵ
--
--by DuckyΏͻ
--in 2013.8.9
------------------------------------------------------------------------------
------------------------------------------------------------------------------
--                 NPCá
------------------------------------------------------------------------------
local NpcName = "ȡ"			--NpcƣͬʱҲǽűƣ
local NpcMapPos = {0, 5, 26, 42}		--Npcͼ
local NpcImage = 14205					--Npcͼ104561
local NpcDir = 6							--Npc0Ϊϣ˳ʱת
------------------------------------------------------------------------------
------------------------------------------------------------------------------
--                 ȡá
------------------------------------------------------------------------------
local CreditsName = ""		--ƣֵ
local ItemName = ""			--Ʒƣֿ
local ItemID = 50006			--ƷIDֿID
local ItemStack = 10000			--Ʒѵֿѵڼȡ
local ExchangeNum = 1			--ÿֿȡĴ
local CheckCountDown = 30		--ÿβѯȴʱ䣨ʱ䣬λ룩
------------------------------------------------------------------------------
------------------------------------------------------------------------------
--                 ݱá
------------------------------------------------------------------------------
local SqlTable = "tbl_user"
local SqlField = "money"
local SqlCdKey = "cdkey"
------------------------------------------------------------------------------
------------------------------------------------------------------------------
--                 űϵͳáרҵ޸ģ
------------------------------------------------------------------------------
if type(PlayerCredits)~="table" then PlayerCredits={} end
if type(PlayerTotal)~="table" then PlayerTotal={} end
if type(tbl_LuaNpcIndex)~="table" then tbl_LuaNpcIndex={} end
local PlayerCheckCD = {}
------------------------------------------------------------------------------

--Ʒid
R_ItemID = 50006;
R_buro = 1;
-- ע⣬ֻ6ͱӻɾҪô࣬д9999999
-- Ҫıʿд0
-- Ǵﵽ
R_ZV =
{
	[1] = 100;
	[2] = 500;
	[3] = 1000;
	[4] = 2000;	
	[5] = 5000;	
	[6] = 99999;
	[7] = 999999
};

-- Ͱٷֱ,֧С
R_ZV_S =
{
	[1] = 5;
	[2] = 10;
	[3] = 15;
	[4] = 20;
	[5] = 25;	
	[6] = 30
};

Delegate.RegInit("ExtractCreditsNpc_Init");

function ExtractCreditsNpc_Init()
	local ExtractCreditsNpc = NL.CreateNpc(nil, "ExtractCreditsNpc_Func");
	Char.SetData( ExtractCreditsNpc, %_%, NpcImage);
	Char.SetData( ExtractCreditsNpc, %_ԭ%, NpcImage);
	Char.SetData( ExtractCreditsNpc, %_MAP%, NpcMapPos[1]);
	Char.SetData( ExtractCreditsNpc, %_ͼ%, NpcMapPos[2]);
	Char.SetData( ExtractCreditsNpc, %_X%, NpcMapPos[3]);
	Char.SetData( ExtractCreditsNpc, %_Y%, NpcMapPos[4]);
	Char.SetData( ExtractCreditsNpc, %_%, NpcDir);
	Char.SetData( ExtractCreditsNpc, %_ԭ%, NpcName);
	NLG.UpChar( ExtractCreditsNpc)

	tbl_LuaNpcIndex["ExtractCreditsNpc"] = ExtractCreditsNpc
	
	Char.SetTalkedEvent(nil, "ExtractCreditsNpc_Talked", ExtractCreditsNpc)
	Char.SetWindowTalkedEvent(nil, "ExtractCreditsNpc_WindowTalked", ExtractCreditsNpc)
	
	return true;
end

function ExtractCreditsNpc_Func(index)
	return true
end

function ExtractCreditsNpc_Talked( _MeIndex, _PlayerIndex, _Mode)

	if(NLG.CheckInFront(_PlayerIndex, _MeIndex, 2) == false and _Mode~=1) then
		return ;
	end 

	NLG.ShowWindowTalked(_PlayerIndex, _MeIndex, 2, 2, 1,
			"5|\nֵ֧ϵͳֵ֧ɹ" ..
			"\nɵ˴ȡֵ[" .. CreditsName .. "]" ..
			"\nȡӦvipΡ" ..
			"\nÿȡǰȽһβѯ" ..
			"\nȷ1Ը" ..
			"\nܵʣѯ" ..
			"\nȡ𿨡" ..
			"\nȡVIPͷΡ" 
			);

	return ;
end

function ExtractCreditsNpc_WindowTalked( _MeIndex, _PlayerIndex, _Seqno, _Select, _Data)
	
	--ȡť
	if _Select==2 then
		return
	end

	if _Seqno==1 then
		local PlayerSelect = tonumber(_Data)
		--ѯ
		if PlayerSelect==1 then
			local tCdKey = Char.GetData(_PlayerIndex, %_˺%)
			local tName = Char.GetData(_PlayerIndex, %_%)
			PlayerCheckCD[tCdKey] = PlayerCheckCD[tCdKey] or 0
			if PlayerCheckCD[tCdKey]+CheckCountDown>os.time() then
				local TalkBuf =	"\n" .. NpcName .. "\n\n\n" ..
								"\n" ..
								"\nѯȴУ" .. PlayerCheckCD[tCdKey]+CheckCountDown-os.time() .. "ԡ"
				NLG.ShowWindowTalked(_PlayerIndex, _MeIndex, 0, 2, 99999, TalkBuf)
				return
			end
			local tSQL = "select " .. SqlCdKey .. "," .. SqlField .. ",TotalMoney from " .. SqlTable .. " where " .. SqlCdKey .. "='" .. tCdKey .. "';"
			local RS = SQL.Run(tSQL)
			PlayerCredits[tCdKey] = tonumber(RS["0_1"] or 0)
			PlayerTotal[tCdKey] = tonumber(RS["0_2"] or 0)
			local tSQL = "select CDK,TotalMoney,XiaoFei from tbl_char where CDK='" .. tCdKey .. "' and Name='" .. tName .. "'"
			local RS = SQL.Run(tSQL)
			local TotalMoney = tonumber(RS["0_1"] or 0)
			local XiaoFei = tonumber(RS["0_2"] or 0)
			local TalkBuf =	"\n" .. NpcName .. "\n\n\n\n" ..
								"㹲[" .. PlayerCredits[tCdKey] .. "]" .. ItemName .. "δȡ\\nýɫۻȡֵ[" .. TotalMoney .. "]Ԫ\\nýɫۻһ[" .. XiaoFei .. "]ţ"
			NLG.ShowWindowTalked(_PlayerIndex, _MeIndex, 0, 2, 99999, TalkBuf)
			PlayerCheckCD[tCdKey] = os.time()
			return
		end

		--
		if PlayerSelect==2 then
			if(NLG.CanTalk(_MeIndex, _PlayerIndex)==false)then
				return;
			end
	
			NLG.ShowWindowTalked(_PlayerIndex, _MeIndex,%_%,%ť_ȷ%,2,"\\n\\n,ӭʹȡȯϵͳ֧ˮŵĺ6λ֣");
			return;
		end

		if PlayerSelect==3 then
			local tCdKey = Char.GetData(_PlayerIndex, %_˺%)
			local tName = Char.GetData(_PlayerIndex, %_%)
			local tSQL = "select CDK,TotalMoney,VipLevel,XiaoFei from tbl_char where CDK='" .. tCdKey .. "' and Name='" .. tName .. "'"
			local RS = SQL.Run(tSQL)
			local TotalMoney = tonumber(RS["0_1"] or 0)
			local VipLevel = tonumber(RS["0_2"] or 0)
			local TotalXiaoFei = tonumber(RS["0_3"] or 0)
			local VipMoneyArr = {100,300,500,1000,2000,5000}
			local VipNameArr = {"ͭ","","ƽ","׽","ʯ","ǿ"}
			local VipGiftArr = {51125,51126,51127,51128,51129,51131}
			if (VipLevel<10) then
				if (TotalMoney+math.floor(TotalXiaoFei/10)>=VipMoneyArr[VipLevel+1]) then
				Char.GiveItem(_PlayerIndex, VipGiftArr[VipLevel+1], 1)
				TalkBuf =	"\nȡͷγɹϲΪ"..VipNameArr[VipLevel+1].."";
				local tSQL = "update tbl_char set VipLevel=VipLevel+1 where CDK='" .. tCdKey .. "' and Name='" .. tName .. "'"
				local RS = SQL.Run(tSQL)
				else
					local TouShiIndex=Char.HaveItem(_PlayerIndex,VipGiftArr[VipLevel]);
					if(TouShiIndex == -1 and VipLevel>0)then
						Char.GiveItem(_PlayerIndex, VipGiftArr[VipLevel], 1);
					end
				TalkBuf = "\nٳֵ"..tostring(VipMoneyArr[VipLevel+1]-TotalMoney).."Ԫɵ"..VipNameArr[VipLevel+1]..""
				end
				NLG.ShowWindowTalked(_PlayerIndex, _MeIndex, 0, 2, 99999, TalkBuf)
			end
			return
		end
	end

	if _Seqno==2 then
		if(Char.ItemSlot(_PlayerIndex) >= 18)then
			NLG.SystemMessage(_PlayerIndex,"[ʧ]Ϊ˱ȡʧܣȿճλϵĿո");
			return;			
		end
		if(string.len(_Data) ~= 6 or tonumber(_Data) == nil or tonumber(_Data) == 0)then
			NLG.SystemMessage(_PlayerIndex,"[ʧ]ˮź6λ");
			return;
		end
		local query = "SELECT * from `tbl_alipay` WHERE `autoid2`='".._Data.."'";
		local mysql = SQL.Run(query);
		if(type(mysql)~="table")then
			NLG.SystemMessage(_PlayerIndex,"[ʧ]ˮŲڣѾֵ");	
			return;
		end
		local autoid1 = mysql["0_0"];
		--NLG.SystemMessage(_PlayerIndex,autoid1);
		local jinbi = tonumber(mysql["0_1"]);
		local ischange = nil;
		ischange = tonumber(mysql["0_2"]);
		if(ischange == 1 or ischange == nil)then
			NLG.SystemMessage(_PlayerIndex,"[ʧ]ˮŲڣѾֵ");	
			return;
		end
		--local query1 = "UPDATE `tbl_user` SET cash= cash + "..jinbi.." WHERE `CdKey`='"..Char.GetData(_PlayerIndex,%_˺%).."'";	
		--SQL.Run(query1);
		local query2 = "UPDATE `tbl_alipay` SET ischange= '1',cdkey = '"..Char.GetData(_PlayerIndex,%_˺%).."' WHERE `autoid`='"..autoid1.."'";	
		SQL.Run(query2);
		local tSQL = "update tbl_char set TotalMoney=TotalMoney+" .. jinbi .. " where CDK='" .. Char.GetData(_PlayerIndex,%_˺%) .. "'"
		local RS = SQL.Run(tSQL)
		if(jinbi >= tonumber(R_ZV[1]) and jinbi < tonumber(R_ZV[2]))then
			jinbi = jinbi + math.floor(jinbi * (tonumber(R_ZV_S[1]) / 100));
		elseif(jinbi >= tonumber(R_ZV[2]) and jinbi < tonumber(R_ZV[3]))then
			jinbi = jinbi + math.floor(jinbi * (tonumber(R_ZV_S[2]) / 100));
		elseif(jinbi >= tonumber(R_ZV[3]) and jinbi < tonumber(R_ZV[4]))then
			jinbi = jinbi + math.floor(jinbi * (tonumber(R_ZV_S[3]) / 100));
		elseif(jinbi >= tonumber(R_ZV[4]) and jinbi < tonumber(R_ZV[5]))then
			jinbi = jinbi + math.floor(jinbi * (tonumber(R_ZV_S[4]) / 100));
		elseif(jinbi >= tonumber(R_ZV[5]) and jinbi < tonumber(R_ZV[6]))then
			jinbi = jinbi + math.floor(jinbi * (tonumber(R_ZV_S[5]) / 100));
		elseif(jinbi >= tonumber(R_ZV[6]) and jinbi < tonumber(R_ZV[7]))then
			jinbi = jinbi + math.floor(jinbi * (tonumber(R_ZV_S[6]) / 100));
		end
		jinbi = jinbi * R_buro;
		Char.GiveItem(_PlayerIndex,R_ItemID,jinbi);

		NLG.SystemMessage(_PlayerIndex,"[ɹ]ֵΪ["..jinbi.."]Ѿˣ");	
	end

	return
end