读用本地数据,写用网络数据, 同时网络更新

时间:2022-01-09 22:15:56
function ZcBase(_CONST_PAR)
	local m_public = {}
	local m_protected = {}
	m_protected.data = {}
	m_protected.data.CONST_PAR = _CONST_PAR or
	{
		T_GROUP_PLAYER_MAX = 1,
		T_GROUP_MAX = 1,
		SU_S = {0, 0},
		SU_E = {0, 0},
		ET_S = {0, 0},
		ET_E = {0, 0},
		LM = 0,
	}
	m_protected.data.tGropu1 = {}
	m_protected.data.tGropu2 = {}
	m_protected.data.tGropu3 = {}
	m_protected.data.tHadPlayer = {}
	m_protected.data.lasterError = "UnKnow"

	--初始化
	function m_public.Init(_data)
		myPrint("初始化未实现",1)
	end
	--是否可报名时间段
	function m_public.i_CanSignUp()
		--时间检验
		local curTime = d_getCurrentDayTime()
		local st = T2I(m_protected.data.CONST_PAR.SU_S[1], m_protected.data.CONST_PAR.SU_S[2], 0)
		local et = T2I(m_protected.data.CONST_PAR.SU_E[1], m_protected.data.CONST_PAR.SU_E[2], 0)
		if curTime < st then
			return -1
		elseif curTime > et then
			return 1
		end
		return 0
	end
	--是否可进场时间段
	function m_public.i_CanEnter()
		--时间检验
		local curTime = d_getCurrentDayTime()
		local st = T2I(m_protected.data.CONST_PAR.ET_S[1], m_protected.data.CONST_PAR.ET_S[2], 0)
		local et = T2I(m_protected.data.CONST_PAR.ET_E[1], m_protected.data.CONST_PAR.ET_E[2], 0)
		myPrint(curTime .. ":" .. st .. ":" .. et, 1)
		if curTime < st then
			return -1
		elseif curTime > et then
			return 1
		end
		return 0
	end
	--报名
	function m_public.SetGroupID(_from, _playerID, _ChooseID)
		myPrint("报名未实现",1)
	end
	--进入
	function m_public.SetPlayer(_from, _playerID)
		myPrint("进场未实现",1)
	end
	--获得最新的错误
	function m_public.GetError(_playerID)
		return m_protected.data.lasterError
	end
	--获得数据
	function m_public.GetData()
		return m_protected.data
	end

	return m_public, m_protected
end

function API_MultiBroadcast(_Key, _Context, _CallBack)
	if nil == _Context then
		return _CallBack()
	end
	local dLen = table.getn(_Context)
	if 0 == dLen then
		return _CallBack()
	elseif 1 == dLen then
		return _CallBack(_Context[1])
	elseif 2 == dLen then
		return _CallBack(_Context[1], _Context[2])
	elseif 3 == dLen then
		return _CallBack(_Context[1], _Context[2], _Context[3])
	elseif 4 == dLen then
		return _CallBack(_Context[1], _Context[2], _Context[3], _Context[4])
	elseif 5 == dLen then
		return _CallBack(_Context[1], _Context[2], _Context[3], _Context[4], _Context[5])
	elseif 6 == dLen then
		return _CallBack(_Context[1], _Context[2], _Context[3], _Context[4], _Context[5], _Context[6])
	elseif 7 == dLen then
		return _CallBack(_Context[1], _Context[2], _Context[3], _Context[4], _Context[5], _Context[6], _Context[7])
	elseif 8 == dLen then
		return _CallBack(_Context[1], _Context[2], _Context[3], _Context[4], _Context[5], _Context[6], _Context[7], _Context[8])
	elseif 9 == dLen then
		return _CallBack(_Context[1], _Context[2], _Context[3], _Context[4], _Context[5], _Context[6], _Context[7], _Context[8], _Context[9])
	elseif 10 == dLen then
		return _CallBack(_Context[1], _Context[2], _Context[3], _Context[4], _Context[5], _Context[6], _Context[7], _Context[8], _Context[9], _Context[10])
	elseif 11 == dLen then
		return _CallBack(_Context[1], _Context[2], _Context[3], _Context[4], _Context[5], _Context[6], _Context[7], _Context[8], _Context[9], _Context[10], _Context[11])
	elseif 12 == dLen then
		return _CallBack(_Context[1], _Context[2], _Context[3], _Context[4], _Context[5], _Context[6], _Context[7], _Context[8], _Context[9], _Context[10], _Context[11], _Context[12])
	elseif 13 == dLen then
		return _CallBack(_Context[1], _Context[2], _Context[3], _Context[4], _Context[5], _Context[6], _Context[7], _Context[8], _Context[9], _Context[10], _Context[11], _Context[12], _Context[13])
	else
		myPrint("API_MultiBroadcast",1)	--------------------------------------------------------
		return true
	end
end

G_ZcMediumData = nil
t_G_ZcClientData_S = {}


function ZcMediumData(_CONST_PAR)
	local m_public, m_protected = ZcBase(_CONST_PAR)
	--刷新
	function m_public.UpdataDate()
		local dLen = table.getn(t_G_ZcClientData_S)
		for i=1, dLen do
			API_MultiBroadcast(0, {m_protected.data}, t_G_ZcClientData_S[i].Init)
		end
	end
	--报名
	function m_public.SetGroupID(_pClient, _playerID, _ChooseID)
		--myPrint("服务器报名" .. _playerID .."->" .. _ChooseID,1)
		--帮会归属检验
		local GroupID = d_getPlayerFaction(_playerID)
		if GroupID <= 0 then
			return false
		end

		--时间检验
		local iRet = m_public.i_CanSignUp()
		if 0 ~= iRet then
			if iRet < 0 then
				m_protected.data.lasterError = "未到报名时间"
			else
				m_protected.data.lasterError = "已超过报名时间"
			end
			API_MultiBroadcast(0, {m_protected.data}, _pClient.Ret_CallBack)
			return
		end

		--报名检验
		local tG = {m_protected.data.tGropu1, m_protected.data.tGropu2, m_protected.data.tGropu3}
		for j=1, 3 do
			local dLen = table.getn(tG[j])
			for i=1, dLen do
				if tG[j][i] == GroupID then
					m_protected.data.lasterError = "玩家所处帮会已报过名"
					API_MultiBroadcast(0, {m_protected.data}, _pClient.Ret_CallBack)
					return
				end
			end
		end

		--选择检验
		if _ChooseID >= 1 and _ChooseID <= 3 then
			tG[_ChooseID][table.getn(tG[_ChooseID]) + 1] = GroupID
			m_protected.data.lasterError = "OK"
			API_MultiBroadcast(0, {m_protected.data}, _pClient.Ret_CallBack)
			m_public.UpdataDate()
		else
			m_protected.data.lasterError = "不存在玩家要报名的副本"
			API_MultiBroadcast(0, {m_protected.data}, _pClient.Ret_CallBack)
		end

	end
	--进入
	function m_public.SetPlayer(_pClient, _playerID)
		--myPrint("服务器进场" .. _playerID,1)
		--帮会归属检验
		local GroupID = d_getPlayerFaction(_playerID)
		if GroupID <= 0 then
			return false
		end

		--报名检验
		local choose = nil
		local tG = {m_protected.data.tGropu1, m_protected.data.tGropu2, m_protected.data.tGropu3}
		for j=1, 3 do
			local dLen = table.getn(tG[j])
			for i=1, dLen do
				if tG[j][i] == GroupID then
					choose = j
					break
				end
			end
			if nil ~= choose then
				break
			end
		end
		if nil == choose then
			m_protected.data.lasterError = "玩家所属帮会未报过名"
			API_MultiBroadcast(0, {m_protected.data}, _pClient.Ret_CallBack)
			return
		end

		--时间检验
		local iRet = m_public.i_CanEnter()
		if 0 ~= iRet then
			if iRet < 0 then
				m_protected.data.lasterError = "未到入场时间"
			else
				m_protected.data.lasterError = "已超过入场时间"
			end
			API_MultiBroadcast(0, {m_protected.data}, _pClient.Ret_CallBack)
			return
		end

		--入场检验
		local dLen = table.getn(m_protected.data.tHadPlayer)
		for i=1, dLen do
			if _playerID == m_protected.data.tHadPlayer[i] then
				m_protected.data.lasterError = "玩家已进入过本次副本"
				API_MultiBroadcast(0, {m_protected.data}, _pClient.Ret_CallBack)
				return
			end
		end

		m_protected.data.tHadPlayer[table.getn(m_protected.data.tHadPlayer) + 1] = _playerID
		m_protected.data.lasterError = "OK"
		_pClient.Ret_CallBack(m_protected.data)
		m_public.UpdataDate()
	end
	return m_public
end

function ZcClientData()
	local m_public, m_protected = ZcBase()
	--结果回调函数
	function m_public.Ret_CallBack(_data)
		m_protected.data = _data
		myPrint(m_protected.data.lasterError, 1)
	end
	--初始数据
	function m_public.Init(_data)
		m_protected.data = _data
		myPrint("客户端初始化数据",1)
	end
	--读本地数据
	function m_public.Read()
		myPrint("客户端读数据",1)
	end
	--报名
	function m_public.SetGroupID(_from, _playerID, _ChooseID)
		--服务器报名
		API_MultiBroadcast(0, {m_public, _playerID, _ChooseID}, G_ZcMediumData.SetGroupID)
	end
	--进入
	function m_public.SetPlayer(_from, _playerID)
		--服务器进场
		API_MultiBroadcast(0, {m_public, _playerID}, G_ZcMediumData.SetPlayer)
	end
	return m_public
end


SERVER_CONST_PAR =
{
	T_GROUP_PLAYER_MAX = 1,
	T_GROUP_MAX = 1,
	SU_S = {15, 0},
	SU_E = {16, 0},
	ET_S = {16, 0},
	ET_E = {17, 0},
	LM = 0,
}
SERVER_CONST_PAR1 =
{
	T_GROUP_PLAYER_MAX = 1,
	T_GROUP_MAX = 1,
	SU_S = {18, 0},
	SU_E = {20, 0},
	ET_S = {20, 0},
	ET_E = {21, 0},
	LM = 0,
}


function UpdataDate_ZcMediumData()
	G_ZcMediumData = ZcMediumData(SERVER_CONST_PAR)
	t_G_ZcClientData_S[1] = ZcClientData()
	G_ZcMediumData.UpdataDate()
end
UpdataDate_ZcMediumData()

function UpdataDate_ZcClientData(_pClient)
	if 0 ~= _pClient.i_CanSignUp() then
		myPrint(" false == _pClient.CanSignUp() ", 1)
		G_ZcMediumData = ZcMediumData(SERVER_CONST_PAR1)
		G_ZcMediumData.UpdataDate()
	end
end


function GetCurrentClientData()
	return t_G_ZcClientData_S[1]
end



API_AddLUAReqFunc("SsYgZc_Choose")
function SsYgZc_Choose(_dRole, _NPCID, _TaskID)
	local lNpcFastID = API_VarDataGetNumber(_dRole, 0, 32712)
	LuaPack_API_GetNpcAllTask(lNpcFastID, _dRole)


	local curClient = GetCurrentClientData()
	UpdataDate_ZcClientData(curClient)
	if 0 ~= curClient.i_CanSignUp() then
		API_ResponseWrite('<a size="13" color="175,216,242" >' .. "目前时间段无副本开放" ..'</a><br>')
	else
		local data = curClient.GetData()
		API_ResponseWrite('<a size="13" color="175,216,242" >' .. "报名时间["..data.CONST_PAR.SU_S[1]  .. ":" .. data.CONST_PAR.SU_S[2]   .. "->" .. data.CONST_PAR.SU_E[1]  ..  ":" .. data.CONST_PAR.SU_E[2] .."]" ..'</a><br>')
		API_ResponseWrite('<a size="13" color="175,216,242" >' .. "入场时间["..data.CONST_PAR.ET_S[1]  .. ":" .. data.CONST_PAR.ET_S[2]   .. "->" .. data.CONST_PAR.ET_E[1]  ..  ":" .. data.CONST_PAR.ET_E[2] .."]" ..'</a><br>')
		local tG = {data.tGropu1, data.tGropu2, data.tGropu3}
		for i=1, 3 do
			local szGroupID = "{"
			local dLen = table.getn(tG[i])
			for j=1, dLen do
				szGroupID = szGroupID  .. tG[i][j]
				if j ~= dLen then
					szGroupID = szGroupID .. "|"
				end
			end
			szGroupID = szGroupID .. "}"
			API_ResponseWrite(ChuanSongStr..'<a size="13" color="175,216,242" href="SsYgZc_SignUp?1='.._dRole..'&2='.._NPCID..'&3='.. i ..'">' .. '击杀Boss' .. i ..
				szGroupID .. '</a><br>')
		end
	end
end


API_AddLUAReqFunc("SsYgZc_SignUp")
function SsYgZc_SignUp()
	local dRoleID	= API_RequestGetNumber(1)
	local dNPCID	= API_RequestGetNumber(2)
	local dTaskID	= API_RequestGetNumber(3)

	local curClient = GetCurrentClientData()
	curClient.SetGroupID(nil, dRoleID, dTaskID)
end


API_AddLUAReqFunc("SsYgZc_Enter")
function SsYgZc_Enter(_dRole, _NPCID, _TaskID)
	local curClient = GetCurrentClientData()
	UpdataDate_ZcClientData(curClient)

	local data = curClient.GetData()
	API_ResponseWrite('<a size="13" color="175,216,242" >' .. "报名时间["..data.CONST_PAR.SU_S[1]  .. ":" .. data.CONST_PAR.SU_S[2]   .. "->" .. data.CONST_PAR.SU_E[1]  ..  ":" .. data.CONST_PAR.SU_E[2] .."]" ..'</a><br>')
	API_ResponseWrite('<a size="13" color="175,216,242" >' .. "入场时间["..data.CONST_PAR.ET_S[1]  .. ":" .. data.CONST_PAR.ET_S[2]   .. "->" .. data.CONST_PAR.ET_E[1]  ..  ":" .. data.CONST_PAR.ET_E[2] .."]" ..'</a><br>')

	local lNpcFastID = API_VarDataGetNumber(_dRole, 0, 32712)
	LuaPack_API_GetNpcAllTask(lNpcFastID, _dRole)
	if 0 ~= curClient.i_CanEnter() then
		API_ResponseWrite('<a size="13" color="175,216,242" >' .. "目前时间段无副本可进场" ..'</a><br>')
	else
		API_ResponseWrite(ChuanSongStr..'<a size="13" color="175,216,242" href="SsYgZc_Enter_Choose?1='.._dRole..'&2='.._NPCID..'&3='.. 0 ..'">进场</a><br>')
	end
end


API_AddLUAReqFunc("SsYgZc_Enter_Choose")
function SsYgZc_Enter_Choose()
	local dRoleID	= API_RequestGetNumber(1)
	local dNPCID	= API_RequestGetNumber(2)
	local dTaskID	= API_RequestGetNumber(3)

	local curClient = GetCurrentClientData()
	curClient.SetPlayer(nil, dRoleID)
end