ShowMessage("领取每日五行法帖 $Rev: 214 $")

--[[
[author]
descript="每天可领取1次"
cmd0str=领取免费法帖
cmd0=1
cmd1str=领取2次法帖
cmd1=2
cmd2str=领取3次法帖
cmd2=3
cmd3str=领取4次法帖
cmd3=4
cmd4str=领取5次法帖
cmd4=5
cmd_default=0
]]--

perday_max=1
script_name="领取每日五行法帖"


local nMaxClick = 1
function OnMission()

	local nRunTick = 0 
	while true do

		if nRunTick == 0 or UI:IsWindowShow("DanrenFB_Get") == false then

			local nowScene = GetActiveDataSceneID()

			if nowScene == 1 then
	  	 		MoveToNPC(200, 334, 1, "独孤悔")
			elseif nowScene == 2 then
	   			MoveToNPC(206, 56, 2, "金忆风")
			else
	    			MoveToNPC(218, 243, 0, "黄云禅")
			end
	
			QuestFrameOptionClicked(script_name , CMP_PART_MATCH)
		end

		nRunTick = nRunTick + 1

		System:Sleep(1000)
		local uClick = 6
		if UI:IsWindowShow("DanrenFB_Get") then
			
			uClick = GetConfimDataBySid( 20151207 , 0 , "data" )
			DbgPrintf("五行法帖当前步数:%d,%d" , uClick , GetConfimDataBySid( 20151207 , 1 , "data" ) )
			uClick = uClick + 1
			if uClick > 10 then
				uClick = 0 
			end

			if uClick >= nMaxClick then
				ShowMessage("领取每日五行法帖：已完成,共领取 %d 次！" , uClick )
				System:UpdateRecord( script_name )
				return
			end

			DoLuaString("DanrenFB_Get_Env" , "DanrenFB_Get_Click()")
		end
		
		if uClick < 5 then

			System:Sleep(500)
			if ( UI:IsWindowShow("LoginSelectServerQuest") ) then
				local jz = Player:GetData("MONEY_JZ")
				DbgPrintf("当前交子:%d" , jz ) 

				local isHadMoney = true 
				if uClick < 3 then
					if jz < 100000 then
						isHadMoney = false
					end
				elseif uClick < 4 then
					if jz < 150000 then
						isHadMoney = false
					end
				else
					if jz < 200000 then
						isHadMoney = false
					end
				end

				if isHadMoney == false then
					ShowMessage("领取每日五行法帖：交子不足！")
					return
				end

				DoLuaString("LoginSelectServerQuest_Env" , "SelectServerQuest_Bn1Click()")
			end

			local isNext,szMsg = GetDebugMessage()
			while isNext == 1 do

				if string.find(szMsg, "已经领取过了") then
					ShowMessage("领取每日五行法帖：已经领取过！")
					System:UpdateRecord( script_name )
					return
				elseif string.find(szMsg, "已经领取完") then
					ShowMessage("领取每日五行法帖：已经全部领取完毕！")
					System:UpdateRecord( script_name )
					return
				elseif string.find(szMsg,"背包空间不足") then
					return
				end
				isNext,szMsg = GetDebugMessage()

			end
		elseif uClick == 5 then
			ShowMessage("领取每日五行法帖：已经全部领取完毕！")
			System:UpdateRecord( script_name )
			return
		end
	end

end

function _SMain(...)

	if _CommandLine ~= nil then 
		nMaxClick =tonumber(_CommandLine)
	end

	OnMission()

	UI:WindowHide()
end

_SMain()


