Delegate.RegInit("shituxitong_init");



local shuliang_stxt;
local Me_Account_stxt;
local tudicdkey_stxt;
local tudimingzi_stxt;
local dangetudimingzi_stxt;
local tudidangqiandengji;
local tudi80ji;
local tudi90ji;
local tudi100ji;
local tudi110ji;
local tudi120ji;
local tudi130ji;
local tudimingdan;

function stxt_create_npc()
	local PlaceNumNpc_stxt = NL.CreateNpc("lua/Module/shitu.lua", "stxt_create_npc_init");
	Char.SetData(PlaceNumNpc_stxt, 1, 100987);
	Char.SetData(PlaceNumNpc_stxt, 2, 100987);
	Char.SetData(PlaceNumNpc_stxt, 3, 0);
	Char.SetData(PlaceNumNpc_stxt, 4, 1000);
	Char.SetData(PlaceNumNpc_stxt, 5, 247);
	Char.SetData(PlaceNumNpc_stxt, 6, 84);
	Char.SetData(PlaceNumNpc_stxt, 7, 6);
	Char.SetData(PlaceNumNpc_stxt, 2000, "ʦͽϵͳ");
	NLG.UpChar(PlaceNumNpc_stxt)


	if (Char.SetTalkedEvent(nil, "stxt_Talked",PlaceNumNpc_stxt) < 0) then
		print("stxt_Talked ע¼ʧܡ");
		return false;
	end
	
	if (Char.SetWindowTalkedEvent(nil, "Shitu__Window_Talked", PlaceNumNpc_stxt) < 0) then
			print("Shitu__Window_Talked ע¼ʧܡ");
			return false;
	end
	
	
	return true;
end

function stxt_Talked( _MeIndex, _PlayerIndex)
	
	
	NLG.ShowWindowTalked(_PlayerIndex,_MeIndex, 2, 2, 0, 
			"1\n                    ѡ"..
			"\n\nʦͽܡ"..
			"\n\nȡ\n");
			
	return;
	
end






--ѡѡ
function Shitu__Window_Talked(_MeIndex, _TalkIndex, _Seqno, _Select, _Data)
	--NLG.SystemMessage(_TalkIndex, "ʾ2");
	--NLG.SystemMessage(_TalkIndex, "ѯԼĴڲʾ _Seqno=".._Seqno.."  _Select=".._Select.."  _Data=".._Data);
	
	
	
	
	
	if (_Seqno==0 and _Data=='2') then
		--ʦͽܡ
		
		NLG.ShowWindowTalked(_TalkIndex, _MeIndex, 0, 1, 999,
			"ʦ:"..
			"\n    ɫȼСڵ20԰ʦ,ɫȼڵ120ͽ"..
			"\nʦ:"..
			"\n    ʦ,Ҽʦ,Ȼѡ'Ϊʦ',ʦԺͰʦɹ"..
			"\n\nʦͽ:"..
			"\n    ͽָȼ,ʦͽܶн,影鿴̳"..
			"\n    ͽֵܳʦܻ÷"
			);
		
		return;
	end
	
	if (_Seqno==0 and _Data=='4') then
		--ȡ
		
		NLG.ShowWindowTalked(_TalkIndex,_MeIndex, 2, 2, 1, 
			"1\n               ѡ"..
			"\n\n        ʦ,ȡͽ"..
			"\n\n        ʦ,ȡͽֵܳ"..
			"\n\n        ͽ,ȡ");
		
		return;
	
	end
	
	
	
	if (_Seqno==1 and _Data=='2') then
		--ʦ,ȡͽ
		if Char.ItemSlot(_TalkIndex) > 18 then
			NLG.ShowWindowTalked(_TalkIndex,_MeIndex, 0, 1, 999,"\n\n    Ѱճ2λ");
			
			return ;
		end
		
		
		Me_Account_stxt = Char.GetData(_TalkIndex,%_˺%);
		
		
		--ݿȡδʦͽܵ
		
		shuliang_stxt = SQL.Run("select count(*) from shituxitong where shifucdkey = '"..Me_Account_stxt.."' and zhuangtai = 2 and 130ji < 10;");

		tudimingzi_stxt = SQL.Run("select tudimingzi from shituxitong where shifucdkey = '"..Me_Account_stxt.."' and zhuangtai = 2 and 130ji < 10;");
		if tonumber(shuliang_stxt["0_0"]) == 0 then
			NLG.ShowWindowTalked(_TalkIndex,_MeIndex, 0, 1, 999,"\n\n    ûͽ,ȡ");
		
			return;
		
		end
		
		tudimingdan_stxt = "";
		for i=0,tonumber(shuliang_stxt["0_0"])-1 do
			
			tudimingdan_stxt = tudimingdan_stxt .. i .. "." .. tudimingzi_stxt[i.."_0"] .." ";
		end
		
		if tonumber(shuliang_stxt["0_0"]) > 15 then
			
			NLG.SystemMessage(_TalkIndex, "ܹ"..tonumber(shuliang_stxt["0_0"]).."λͽ:\n    "..tudimingdan_stxt);
			
			tudimingdan_stxt = "ͽ̫,ʾ,鿴ĻĻʾ,һֶûʾȫ,밤";
		
		end
		
		NLG.ShowWindowTalked(_TalkIndex,_MeIndex, 1, 1, 11, "ܹ"..tonumber(shuliang_stxt["0_0"]).."λͽ,:\n    "..tudimingdan_stxt.."\n\n    ͽܵıţ");
		
		
		return;
	end
	
	--ʦȡͽܵȼ ˱
	if(_Seqno==11 and _Select==1) then
		--NLG.SystemMessage(_TalkIndex, "select sum(`count`) from tbl_alipay where cdkey in ("..tudicdkey_stxtmingdan..") and shifulingqu = 0 ;");
		_Data = tonumber(_Data);
		if tonumber(_Data) == nil or tonumber(_Data) < 0 then
			NLG.ShowWindowTalked(_TalkIndex,_MeIndex, 0, 1, 999,"\n\n"..String.Centering("Чͽܱ"));
			return;
		end
		
		--Me_Account_stxt = Char.GetData(_TalkIndex,%_˺%);
		--ݿȡδʦͽܵ
		
		if tonumber(shuliang_stxt["0_0"]) <= tonumber(_Data) then
			NLG.ShowWindowTalked(_TalkIndex,_MeIndex, 0, 1, 999,"\n\n    ֻ"..shuliang_stxt["0_0"].."λͽ,Чͽܱ");
		
			return ;
		end
		
		local sqltemp_stxt = SQL.Run("select * from shituxitong where tudimingzi = '"..tudimingzi_stxt[_Data.."_0"].."' and zhuangtai = 2;");
		
		tudicdkey_stxt = sqltemp_stxt["0_0"];
		dangetudimingzi_stxt = sqltemp_stxt["0_1"];
		tudidangqiandengji = tonumber(SQL.Run("select Lv from tbl_character where CdKey = '"..tudicdkey_stxt.."' and Name = '"..dangetudimingzi_stxt.."';")["0_0"]);
		tudi80ji = tonumber(sqltemp_stxt["0_5"]);
		tudi90ji = tonumber(sqltemp_stxt["0_6"]);
		tudi100ji = tonumber(sqltemp_stxt["0_7"]);
		tudi110ji = tonumber(sqltemp_stxt["0_8"]);
		tudi120ji = tonumber(sqltemp_stxt["0_9"]);
		tudi130ji = tonumber(sqltemp_stxt["0_10"]);
		
		if tudidangqiandengji >= 80 then
			if tudi80ji < 10 then
				--NLG.SystemMessage(_TalkIndex, "update shituxitong set 80ji = "..(tudi80ji+10).." where tudimingzi = '"..dangetudimingzi_stxt.."' and zhuangtai = 2;");
				SQL.Run("update shituxitong set 80ji = "..(tudi80ji+10).." where tudimingzi = '"..dangetudimingzi_stxt.."' and zhuangtai = 2;");
				Char.GiveItem(_TalkIndex,1,1);
				NLG.UpChar(_TalkIndex);
				NLG.ShowWindowTalked(_TalkIndex,_MeIndex, 0, 1, 999,"\n\n    Ѿŵ,");
				
				return ;
			end
		else
			NLG.ShowWindowTalked(_TalkIndex,_MeIndex, 0, 1, 999,"\n\n    "..dangetudimingzi_stxt.."ûдﵽ80,޷ȡ");
			return;
		end
		
		if tudidangqiandengji >= 90 then
			if tudi90ji < 10 then
				SQL.Run("update shituxitong set 90ji = "..(tudi90ji+10).." where tudimingzi = '"..dangetudimingzi_stxt.."' and zhuangtai = 2;");

				Char.GiveItem(_TalkIndex,1,1);
				NLG.UpChar(_TalkIndex);
				NLG.ShowWindowTalked(_TalkIndex,_MeIndex, 0, 1, 999,"\n\n    Ѿŵ,");
				
				return ;
			end
		end
		
		if tudidangqiandengji >= 100 then
			if tudi100ji < 10 then
				SQL.Run("update shituxitong set 100ji = "..(tudi100ji+10).." where tudimingzi = '"..dangetudimingzi_stxt.."' and zhuangtai = 2;");

				Char.GiveItem(_TalkIndex,1,1);
				NLG.UpChar(_TalkIndex);
				NLG.ShowWindowTalked(_TalkIndex,_MeIndex, 0, 1, 999,"\n\n    Ѿŵ,");
				
				return ;
			end
		end
		
		if tudidangqiandengji >= 110 then
			if tudi110ji < 10 then
				SQL.Run("update shituxitong set 110ji = "..(tudi110ji+10).." where tudimingzi = '"..dangetudimingzi_stxt.."' and zhuangtai = 2;");

				Char.GiveItem(_TalkIndex,1,1);
				NLG.UpChar(_TalkIndex);
				NLG.ShowWindowTalked(_TalkIndex,_MeIndex, 0, 1, 999,"\n\n    Ѿŵ,");
				
				return ;
			end
		end
		
		if tudidangqiandengji >= 120 then
			if tudi120ji < 10 then
				SQL.Run("update shituxitong set 120ji = "..(tudi120ji+10).." where tudimingzi = '"..dangetudimingzi_stxt.."' and zhuangtai = 2;");

				Char.GiveItem(_TalkIndex,1,1);
				NLG.UpChar(_TalkIndex);
				NLG.ShowWindowTalked(_TalkIndex,_MeIndex, 0, 1, 999,"\n\n    Ѿŵ,");
				
				return ;
			end
		end
		if tudidangqiandengji >= 130 then
			if tudi130ji < 10 then
				SQL.Run("update shituxitong set 130ji = "..(tudi130ji+10).." where tudimingzi = '"..dangetudimingzi_stxt.."' and zhuangtai = 2;");

				Char.GiveItem(_TalkIndex,1,1);
				NLG.UpChar(_TalkIndex);
				NLG.ShowWindowTalked(_TalkIndex,_MeIndex, 0, 1, 999,"\n\n    Ѿŵ,");
				
				return ;
			end
		end
		
		
		NLG.ShowWindowTalked(_TalkIndex,_MeIndex, 0, 1, 999,"\n\n    ͽܻûдﵽһ콱ĵȼ(ͽ߲Żˢµȼ)");
		
		return;
		
		
	end

	

	if (_Seqno==1 and _Data=='4') then
		--ʦ,ȡͽֵܳ
		if Char.ItemSlot(_TalkIndex) > 18 then
			NLG.ShowWindowTalked(_TalkIndex,_MeIndex, 0, 1, 999,"\n\n    Ѱճ2λ");
			
			return ;
		end
		
		Me_Account_stxt = Char.GetData(_TalkIndex,%_˺%);
		--ݿȡͽܵ(ֵ¼Ч,ͽܳʦҲЧ)
		--sqltemp_stxt = sql.run("select count(*),tudicdkey from shituxitong where shifucdkey = '"..Me_Account_stxt.."' and zhuangtai = 2 ;");
		
		local shuliang_stxt_chongzhijiangli = SQL.Run("select count(*) from shituxitong where shifucdkey = '"..Me_Account_stxt.."' and zhuangtai = 2 ;");
		
		local tudimingzi_stxt_chongzhijiangli = SQL.Run("select tudicdkey from shituxitong where shifucdkey = '"..Me_Account_stxt.."' and zhuangtai = 2 ;");
		
		--NLG.SystemMessage(_TalkIndex, "shuliang_stxt_chongzhijiangli[0_0]:"..shuliang_stxt_chongzhijiangli["0_0"]);
		
		if shuliang_stxt_chongzhijiangli["0_0"] == nil then
			NLG.ShowWindowTalked(_TalkIndex,_MeIndex, 0, 1, 999,"\n\n    ûͽ,ȡ1");
			NLG.SystemMessage(_TalkIndex, "jinru");
		
			return;
		
		end
		if tonumber(shuliang_stxt_chongzhijiangli["0_0"]) == 0 then
			NLG.ShowWindowTalked(_TalkIndex,_MeIndex, 0, 1, 999,"\n\n    ûͽ,ȡ2");
			--NLG.SystemMessage(_TalkIndex, "jinru2");
		
			return;
		
		end
		
		
		
		
		local tudicdkey_stxtmingdan = "'"..tudimingzi_stxt_chongzhijiangli["0_0"].."'";
		
		
		for i=1,shuliang_stxt_chongzhijiangli["0_0"]-1 do
			tudicdkey_stxtmingdan = tudicdkey_stxtmingdan .. "," .. "'"..tudimingzi_stxt_chongzhijiangli[i.."_0"].."'";
		end
		--ݿȡͽܵĳֵܶ,ûбʦȡ
		local sqltemp_stxt2 = SQL.Run("select sum(`count`) from tbl_alipay where cdkey in ("..tudicdkey_stxtmingdan..") and shifulingqu = 0 ;");
		--NLG.SystemMessage(_TalkIndex, "select sum(`count`) from tbl_alipay where cdkey in ("..tudicdkey_stxtmingdan..") and shifulingqu = 0 ;");
		
		--NLG.SystemMessage(_TalkIndex, "sqltemp_stxt2:"..sqltemp_stxt2);
		
		--NLG.SystemMessage(_TalkIndex, "sqltemp_stxt2[0_0]:"..sqltemp_stxt2["0_0"]);  nil
		
		
		if sqltemp_stxt2["0_0"] == nil then
			NLG.ShowWindowTalked(_TalkIndex,_MeIndex, 0, 1, 999,"\n\n    Ŀǰûͽֵܳ");
		
			return;
		elseif tonumber(sqltemp_stxt2["0_0"]) < 10  then
			NLG.ShowWindowTalked(_TalkIndex,_MeIndex, 0, 1, 999,"\n\n    ͽֵܳܺͲ10Ԫ");
		
			return;
		elseif tonumber(sqltemp_stxt2["0_0"]) >= 10 then
			SQL.Run("update tbl_alipay set shifulingqu = 1 where cdkey in ("..tudicdkey_stxtmingdan..");");
			
			Char.GiveItem(_TalkIndex,666000,(tonumber(sqltemp_stxt2["0_0"])*0.3));
				NLG.UpChar(_TalkIndex);
			NLG.ShowWindowTalked(_TalkIndex,_MeIndex, 0, 1, 999,"\n\n    ȡͽֵܳɹ");
			
			return;
		end
		
		
	end
	
	
	

	if (_Seqno==1 and _Data=='6') then
		--NLG.SystemMessage(_TalkIndex, "tudi1");
		
		--ͽ,ȡ
		if Char.ItemSlot(_TalkIndex) > 18 then
			NLG.ShowWindowTalked(_TalkIndex,_MeIndex, 0, 1, 999,"\n\n    Ѱճ2λ");
			
			return ;
		end
		
		
		Me_Account_stxt = Char.GetData(_TalkIndex,2002);
		dangetudimingzi_stxt = Char.GetData(_TalkIndex,%_ԭ%);
		
		--ݿȡͽcdkey
		local tudilingjiang_sqljieguo = SQL.Run("Select count(*) from shituxitong Where tudimingzi = '"..dangetudimingzi_stxt.."' and zhuangtai = 2 ;");
		local tudilingjiang_sqljieguo1 = SQL.Run("Select * from shituxitong Where tudimingzi = '"..dangetudimingzi_stxt.."' and zhuangtai = 2 ;");

--		NLG.SystemMessage(_TalkIndex, "tudilingjiang_sqljieguo0_0:"..tudilingjiang_sqljieguo);
		
		if tonumber(tudilingjiang_sqljieguo["0_0"])== 0 then
			NLG.ShowWindowTalked(_TalkIndex,_MeIndex, 0, 1, 999,"\n\n    ûаʦ,ȡ");
		
			return ;
		
		else
				
			if tonumber(tudilingjiang_sqljieguo1["0_10"]) == 1 or tonumber(tudilingjiang_sqljieguo1["0_10"]) == 11 then
				NLG.ShowWindowTalked(_TalkIndex,_MeIndex, 0, 1, 999,"\n\n    Ѿʦ");
			
				return ;
				
			end
			
			if tonumber(tudilingjiang_sqljieguo1["0_10"]) == 0 or tonumber(tudilingjiang_sqljieguo1["0_10"]) == 10 then
				--NLG.SystemMessage(_TalkIndex, "tudi2");
			
				--鿴ǰȼ
				local tudidangqiandengji = Char.GetData(_TalkIndex,%_ȼ%);
				
				if tudidangqiandengji < 80 then
				
					NLG.ShowWindowTalked(_TalkIndex,_MeIndex, 0, 1, 999,"\n\n    80ܿʼȡ");
					return ;
				
				end
				
				--ͽܵĵȼڵ80
				tudi80ji = tonumber(tudilingjiang_sqljieguo1["0_5"]);
				tudi90ji = tonumber(tudilingjiang_sqljieguo1["0_6"]);
				tudi100ji = tonumber(tudilingjiang_sqljieguo1["0_7"]);
				tudi110ji = tonumber(tudilingjiang_sqljieguo1["0_8"]);
				tudi120ji = tonumber(tudilingjiang_sqljieguo1["0_9"]);
				tudi130ji = tonumber(tudilingjiang_sqljieguo1["0_10"]);
				if tudidangqiandengji >= 80 and (tudi80ji == 0 or tudi80ji == 10) then
					SQL.Run("update shituxitong set 80ji = "..(tudi80ji+1).." where tudimingzi = '"..dangetudimingzi_stxt.."' and zhuangtai = 2;");

					Char.GiveItem(_TalkIndex,1,1);
   				NLG.UpChar(_TalkIndex);
					NLG.ShowWindowTalked(_TalkIndex,_MeIndex, 0, 1, 999,"\n\n    Ѿŵ,");	
					return ;
				
				
				end
				
				
				if tudidangqiandengji >= 90 and (tudi90ji == 0 or tudi90ji == 10) then
					SQL.Run("update shituxitong set 90ji = "..(tudi90ji+1).." where tudimingzi = '"..dangetudimingzi_stxt.."' and zhuangtai = 2;");

					Char.GiveItem(_TalkIndex,1,1);
   				NLG.UpChar(_TalkIndex);
 					NLG.ShowWindowTalked(_TalkIndex,_MeIndex, 0, 1, 999,"\n\n    Ѿŵ,");
					
					return ;
				
				
				end
				
				if tudidangqiandengji >= 100 and (tudi100ji == 0 or tudi100ji == 10) then
					SQL.Run("update shituxitong set 100ji = "..(tudi100ji+1).." where tudimingzi = '"..dangetudimingzi_stxt.."' and zhuangtai = 2;");

					Char.GiveItem(_TalkIndex,1,1);
   				NLG.UpChar(_TalkIndex);
					NLG.ShowWindowTalked(_TalkIndex,_MeIndex, 0, 1, 999,"\n\n    Ѿŵ,");
					
					return ;
				
				
				end
				
				if tudidangqiandengji >= 110 and (tudi110ji == 0 or tudi110ji == 10) then
					SQL.Run("update shituxitong set 110ji = "..(tudi110ji+1).." where tudimingzi = '"..dangetudimingzi_stxt.."' and zhuangtai = 2;");

					Char.GiveItem(_TalkIndex,1,1);
   				NLG.UpChar(_TalkIndex);
					NLG.ShowWindowTalked(_TalkIndex,_MeIndex, 0, 1, 999,"\n\n    Ѿŵ,");
					
					return ;
				
				
				end
				
				if tudidangqiandengji >= 120 and (tudi120ji == 0 or tudi120ji == 10) then
					SQL.Run("update shituxitong set 120ji = "..(tudi120ji+1).." where tudimingzi = '"..dangetudimingzi_stxt.."' and zhuangtai = 2;");

					Char.GiveItem(_TalkIndex,1,1);
   				NLG.UpChar(_TalkIndex);
					NLG.ShowWindowTalked(_TalkIndex,_MeIndex, 0, 1, 999,"\n\n    Ѿŵ,");
					
					return ;
				
				
				end
				
				if tudidangqiandengji >= 130 and (tudi130ji == 0 or tudi130ji == 10) then
					SQL.Run("update shituxitong set 130ji = "..(tudi130ji+1).." where tudimingzi = '"..dangetudimingzi_stxt.."' and zhuangtai = 2;");

					Char.GiveItem(_TalkIndex,1,1);
   				NLG.UpChar(_TalkIndex);
					NLG.ShowWindowTalked(_TalkIndex,_MeIndex, 0, 1, 999,"\n\n    Ѿŵ,");
					
					return ;
				
				
				end
				
				
				
				NLG.ShowWindowTalked(_TalkIndex, _MeIndex, 0, 1, 999,"\n\n    㻹ûдﵽһ콱");
				
				return ;
			end
		
		
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
		end
		
		
		
		
		
		
	end
	
	
	
	
	
end









function stxt_create_npc_init(index)
	return 1;
end
function shituxitong_init()
	stxt_create_npc();
end