377 lines
11 KiB
Lua
377 lines
11 KiB
Lua
|
||
------------------------------------------------
|
||
--作者: 王圣
|
||
--日期: 2019-08-01
|
||
--文件: WorldAnswerSystem.lua
|
||
--模块: WorldAnswerSystem
|
||
--描述: 世界答题
|
||
------------------------------------------------
|
||
--引用
|
||
local TimeUtils = CS.Thousandto.Core.Base.TimeUtils;
|
||
local WorldAnswerSystem = {
|
||
--当前状态默认准备状态
|
||
CurState = WorldAnswerState.ReadyState,
|
||
--获得积分
|
||
Score = 0,
|
||
--获得经验
|
||
Exp = 0,
|
||
--获得金钱
|
||
Money = 0,
|
||
--展示奖励
|
||
ShowItemList = List:New(),
|
||
--最终道具奖励
|
||
RewardItemList = List:New(),
|
||
--公告
|
||
ListDes = List:New(),
|
||
--剩余时间(准备时间, 答题时间,重选时间 ,等待时间)
|
||
LeftTime = 0,
|
||
SyncTime = 0,
|
||
--题目总数
|
||
TCount = 0,
|
||
--当前题目数
|
||
Count = 0,
|
||
--问题
|
||
Question = nil,
|
||
--答案
|
||
ListAnswer = List:New(),
|
||
--支持人数
|
||
ListSupport = List:New(),
|
||
--玩家公告文字模板List
|
||
ListGongGaoTemp = List:New(),
|
||
--玩家公告
|
||
ListGongGao = List:New(),
|
||
--客户端显示的公告条数
|
||
GongGaoCount = 4,
|
||
--是否选择了答案
|
||
IsChoose = false,
|
||
--当前配置表数据
|
||
Cfg = nil,
|
||
--设置结束百分比
|
||
Percent = 0,
|
||
--在daily配置表的活动id
|
||
DailyId = 104,
|
||
--玩家选择的答案Id
|
||
SelectId = -1,
|
||
}
|
||
|
||
function WorldAnswerSystem:Initialize()
|
||
self:InitGongGao()
|
||
self:InitQuestoinTCount()
|
||
self:InitShowItemData()
|
||
--初始化 支持率
|
||
self.ListSupport:Add(-1)
|
||
self.ListSupport:Add(-1)
|
||
self.ListSupport:Add(-1)
|
||
self.ListSupport:Add(-1)
|
||
end
|
||
|
||
function WorldAnswerSystem:UnInitialize()
|
||
end
|
||
|
||
--初始化玩家公告模板
|
||
function WorldAnswerSystem:InitGongGao()
|
||
self.ListGongGaoTemp:Add(DataConfig.DataMessageString.Get("WorldAnswerNotice1"))
|
||
self.ListGongGaoTemp:Add(DataConfig.DataMessageString.Get("WorldAnswerNotice2"))
|
||
self.ListGongGaoTemp:Add(DataConfig.DataMessageString.Get("WorldAnswerNotice3"))
|
||
self.ListGongGaoTemp:Add(DataConfig.DataMessageString.Get("WorldAnswerNotice4"))
|
||
end
|
||
|
||
--获取题目总数
|
||
function WorldAnswerSystem:InitQuestoinTCount()
|
||
self.TCount = 10
|
||
end
|
||
|
||
--设置展示奖励道具数据
|
||
function WorldAnswerSystem:InitShowItemData()
|
||
self.ShowItemList:Clear()
|
||
local cfg = DataConfig.DataDaily[self.DailyId]
|
||
if cfg ~= nil then
|
||
local list = Utils.SplitStr(cfg.Reward,'_')
|
||
for i = 1,list ~= nil and #list do
|
||
self.ShowItemList:Add(tonumber(list[i]))
|
||
end
|
||
end
|
||
end
|
||
|
||
--随机一条公告模板
|
||
function WorldAnswerSystem:RandGongGao()
|
||
math.randomseed(tostring(os.time()):reverse():sub(1, 7))
|
||
if #self.ListGongGaoTemp >0 then
|
||
local id = math.random(1, #self.ListGongGaoTemp)
|
||
if id<= #self.ListGongGaoTemp then
|
||
return self.ListGongGaoTemp[id]
|
||
end
|
||
end
|
||
end
|
||
|
||
--设置公告
|
||
function WorldAnswerSystem:SetGongGao(str)
|
||
if #self.ListGongGao <self.GongGaoCount then
|
||
--如果小于最大显示条目
|
||
self.ListGongGao:Insert(str,1)
|
||
else
|
||
--如果超过最大显示条目
|
||
self.ListGongGao:RemoveAt(#self.ListGongGao)
|
||
self:SetGongGao(str)
|
||
end
|
||
end
|
||
|
||
--清除所有公告
|
||
function WorldAnswerSystem:ClearGongGao()
|
||
self.ListGongGao:Clear()
|
||
end
|
||
|
||
--
|
||
function WorldAnswerSystem:FormatAnswer(id)
|
||
if id == 1 then
|
||
return "A"
|
||
elseif id == 2 then
|
||
return "B"
|
||
elseif id == 3 then
|
||
return "C"
|
||
elseif id == 4 then
|
||
return "D"
|
||
end
|
||
end
|
||
|
||
--获取配置表
|
||
function WorldAnswerSystem:SetCfg(cfgId)
|
||
if cfgId ~= 0 then
|
||
self.Cfg = DataConfig.DataWorldQuestion[cfgId]
|
||
end
|
||
end
|
||
|
||
--通过配置表获取题目
|
||
function WorldAnswerSystem:SetQuestion()
|
||
if self.Cfg ~= nil then
|
||
self.Question = self.Cfg.Describe
|
||
end
|
||
end
|
||
|
||
--通过配置表获取四个答案
|
||
function WorldAnswerSystem:SetAnswerList()
|
||
self.ListAnswer:Clear()
|
||
if self.Cfg ~= nil then
|
||
self.ListAnswer:Add(self.Cfg.Answer1)
|
||
self.ListAnswer:Add(self.Cfg.Answer2)
|
||
self.ListAnswer:Add(self.Cfg.Answer3)
|
||
self.ListAnswer:Add(self.Cfg.Answer4)
|
||
end
|
||
end
|
||
|
||
--获取剩余时间
|
||
function WorldAnswerSystem:GetleftTime()
|
||
local time = self.LeftTime - (Time.GetRealtimeSinceStartup()- self.SyncTime)
|
||
return time
|
||
end
|
||
|
||
--设置剩余时间
|
||
function WorldAnswerSystem:SetLeftTime(t)
|
||
self.LeftTime = t
|
||
self.SyncTime = Time.GetRealtimeSinceStartup()
|
||
end
|
||
|
||
--消息提示增加的积分,经验,金钱
|
||
function WorldAnswerSystem:ShowMsg(result)
|
||
if result.questionRound == 3 then
|
||
if result.integral ~= nil then
|
||
local score = result.integral.integral - self.Score
|
||
local exp = result.integral.exp - self.Exp
|
||
local money = result.integral.money - self.Money
|
||
if score>0 then
|
||
Utils.ShowPromptByEnum("GetIntegralByAnswer",score)
|
||
end
|
||
if exp>0 then
|
||
Utils.ShowPromptByEnum("GetExpByAnswer",exp)
|
||
end
|
||
end
|
||
end
|
||
end
|
||
|
||
--切换答题状态
|
||
function WorldAnswerSystem:ChangeState(round,time,choose)
|
||
--如果是准备答题阶段
|
||
if round == -1 then
|
||
self.CurState = WorldAnswerState.ReadyState
|
||
--获取活动准备倒计时
|
||
local readyTime = 0
|
||
local _cfg = DataConfig.DataDaily[104]
|
||
if _cfg ~= nil then
|
||
local _list = Utils.SplitNumber(_cfg.Time, '_')
|
||
local _openTime = _list[1] * 60
|
||
local hour, min, sec = TimeUtils.GetStampTimeHHMMSSNotZone(math.floor(GameCenter.HeartSystem.ServerZoneTime))
|
||
local _curSec = hour * 3600 + min * 60 + sec
|
||
readyTime = _openTime - _curSec
|
||
end
|
||
self:SetLeftTime(readyTime)
|
||
self.IsChoose = false
|
||
self.SelectId = -1
|
||
--第一轮选择题目阶段
|
||
elseif round == 1 then
|
||
self.CurState = WorldAnswerState.ChooseState
|
||
--设置题目
|
||
self:SetQuestion()
|
||
--设置选择答案
|
||
self:SetAnswerList()
|
||
self:SetLeftTime(time)
|
||
self:ClearGongGao()
|
||
self.SelectId = choose
|
||
self.IsChoose = choose ~= 0
|
||
--第二轮重选阶段
|
||
elseif round == 2 then
|
||
self.CurState = WorldAnswerState.ReChooseState
|
||
self:SetLeftTime(time)
|
||
if choose > 10 then
|
||
self.SelectId = choose % 10
|
||
self.IsChoose = false
|
||
else
|
||
self.IsChoose = choose ~= 0
|
||
end
|
||
--第三轮等待下一轮答题阶段
|
||
elseif round == 3 then
|
||
self.CurState = WorldAnswerState.WaitState
|
||
self:SetLeftTime(time)
|
||
self:ClearGongGao()
|
||
if self.Count ~= self.TCount then
|
||
self:SetGongGao(DataConfig.DataMessageString.Get("NextProblemBeComing"))
|
||
end
|
||
self.IsChoose = true
|
||
elseif round == 4 then
|
||
self.CurState = WorldAnswerState.FinishState
|
||
end
|
||
end
|
||
|
||
--通过配置表id获取
|
||
|
||
-------------------req消息(Msg)相关------------------
|
||
--请求报名答题
|
||
function WorldAnswerSystem:ReqApplyAnswer()
|
||
GameCenter.Network.Send("MSG_WorldAnswer.ReqApplyAnswer")
|
||
end
|
||
|
||
--向无服务提交选择结果
|
||
function WorldAnswerSystem:ReqAnswerResult(id)
|
||
GameCenter.Network.Send("MSG_WorldAnswer.ReqAnswerResult",{resultIndex = id})
|
||
self.IsChoose = true
|
||
end
|
||
|
||
--离开答题界面
|
||
function WorldAnswerSystem:ReqLeaveOutAnswer()
|
||
GameCenter.Network.Send("MSG_WorldAnswer.ReqLeaveOutAnswer")
|
||
end
|
||
-------------------res消息(Msg)相关------------------
|
||
|
||
--打开答题界面返回消息
|
||
function WorldAnswerSystem:ResApplyAnswerResult(result)
|
||
if result == nil then
|
||
return
|
||
end
|
||
--设置Cfg
|
||
self:SetCfg(result.questionID)
|
||
self:ShowMsg(result)
|
||
if result.integral ~= nil then
|
||
self.Score = result.integral.integral
|
||
self.Exp = result.integral.exp
|
||
self.Money = result.integral.money
|
||
end
|
||
--设置当前是第几题
|
||
self.Count = result.curQuestionNum
|
||
if self.Count == self.TCount and (result.questionRound == 3 or result.questionRound == 4) then
|
||
Utils.ShowPromptByEnum("AnswerEnd")
|
||
return
|
||
end
|
||
--设置支持率
|
||
self.ListSupport:Clear()
|
||
if result.chooseNum ~= nil then
|
||
local tCount = result.chooseNum.chooseACount + result.chooseNum.chooseBCount + result.chooseNum.chooseCCount + result.chooseNum.chooseDCount
|
||
self.ListSupport:Add(result.chooseNum.chooseACount)
|
||
self.ListSupport:Add(result.chooseNum.chooseBCount)
|
||
self.ListSupport:Add(result.chooseNum.chooseCCount)
|
||
self.ListSupport:Add(result.chooseNum.chooseDCount)
|
||
else
|
||
self.ListSupport:Add(-1)
|
||
self.ListSupport:Add(-1)
|
||
self.ListSupport:Add(-1)
|
||
self.ListSupport:Add(-1)
|
||
end
|
||
self.SelectId = result.curChoose
|
||
self:ChangeState(result.questionRound,result.lastTime,result.curChoose)
|
||
--打开世界答题界面
|
||
GameCenter.PushFixEvent(UIEventDefine.UIWorldAnswerForm_Open);
|
||
GameCenter.BISystem:ReqClickEvent(BiIdCode.SJDTEnter)
|
||
end
|
||
|
||
--中途更新题目
|
||
function WorldAnswerSystem:ResSendQuestion(result)
|
||
GameCenter.PushFixEvent(UIEventDefine.UI_WAITING_CLOSE);
|
||
if result == nil then
|
||
return
|
||
end
|
||
--设置Cfg
|
||
self:SetCfg(result.questionID)
|
||
--设置支持率
|
||
self.ListSupport:Clear()
|
||
if result.chooseNum ~= nil then
|
||
local tCount = result.chooseNum.chooseACount + result.chooseNum.chooseBCount + result.chooseNum.chooseCCount + result.chooseNum.chooseDCount
|
||
self.ListSupport:Add(result.chooseNum.chooseACount)
|
||
self.ListSupport:Add(result.chooseNum.chooseBCount)
|
||
self.ListSupport:Add(result.chooseNum.chooseCCount)
|
||
self.ListSupport:Add(result.chooseNum.chooseDCount)
|
||
else
|
||
self.ListSupport:Add(-1)
|
||
self.ListSupport:Add(-1)
|
||
self.ListSupport:Add(-1)
|
||
self.ListSupport:Add(-1)
|
||
end
|
||
self:ShowMsg(result)
|
||
if result.integral ~= nil then
|
||
self.Score = result.integral.integral
|
||
self.Exp = result.integral.exp
|
||
self.Money = result.integral.money
|
||
end
|
||
--设置当前是第几题
|
||
self.Count = result.curQuestionNum
|
||
--设置状态
|
||
self:ChangeState(result.questionRound,result.lastTime,0)
|
||
--更新UI
|
||
GameCenter.PushFixEvent(LogicLuaEventDefine.EID_EVENT_WORLDANSWER_CHANGESTATE)
|
||
end
|
||
|
||
--同步公告
|
||
function WorldAnswerSystem:ResSendOtherPlayerSelect(result)
|
||
if result == nil then
|
||
return
|
||
end
|
||
--获取一条随机模板
|
||
local gongGao = self:RandGongGao()
|
||
gongGao = UIUtils.CSFormat(gongGao, result.roleName,self:FormatAnswer(result.questionIndex))
|
||
self:SetGongGao(gongGao)
|
||
GameCenter.PushFixEvent(LogicLuaEventDefine.EID_EVENT_WORLDANSWER_GONGGAO)
|
||
end
|
||
|
||
--答题结束
|
||
function WorldAnswerSystem:ResWorldAnswerOver(result)
|
||
if result == nil then
|
||
return
|
||
end
|
||
--更新积分
|
||
if result.integral ~= nil then
|
||
self.Score = result.integral.integral
|
||
self.Exp = result.integral.exp
|
||
self.Money = result.integral.money
|
||
end
|
||
--设置奖励道具
|
||
if result.reward ~= nil then
|
||
self.RewardItemList:Clear()
|
||
for i = 1, #result.reward do
|
||
self.RewardItemList:Add(result.reward[i])
|
||
end
|
||
end
|
||
self.Percent = result.rankPer
|
||
self:ChangeState(4,0,0)
|
||
--更新UI
|
||
GameCenter.PushFixEvent(LogicLuaEventDefine.EID_EVENT_WORLDANSWER_CHANGESTATE)
|
||
end
|
||
|
||
return WorldAnswerSystem
|