323 lines
13 KiB
Lua
323 lines
13 KiB
Lua
GemManager = {}
|
||
local this = GemManager
|
||
local gemConfig = ConfigManager.GetConfig(ConfigName.GemConfig)
|
||
local propertyConfig = ConfigManager.GetConfig(ConfigName.PropertyConfig)
|
||
local ItemConfig = ConfigManager.GetConfig(ConfigName.ItemConfig)
|
||
function this.Initialize()
|
||
this.myGemList = {}--我穿戴命石的id
|
||
this.typeGemList = {}
|
||
this.gemLottery = {} --抽卡次数记录
|
||
local value=ConfigManager.GetConfigData(ConfigName.SpecialConfig,169).Value
|
||
this.drawLimit = string.split(value,"#")
|
||
end
|
||
|
||
|
||
function this.GetLimitNumByType(_type)
|
||
return tonumber(this.drawLimit[_type])
|
||
end
|
||
--获取服务器发来的背包中的命石数据信息
|
||
function this.GetGemsFromBagByType(_type)
|
||
local data = {}
|
||
local bagdata = BagManager.GetBagAllDataByItemType(ItemType.Gem)
|
||
for i = 1, #bagdata do
|
||
local tempdata = {}
|
||
tempdata.id = bagdata[i].id
|
||
tempdata.num = bagdata[i].num
|
||
tempdata.type = gemConfig[bagdata[i].id].Type
|
||
tempdata.name = gemConfig[bagdata[i].id].Name
|
||
if (tempdata.type == _type or _type == 0) and bagdata[i].num > 0 then
|
||
-- LogGreen("Id:"..tostring(bagdata[i].id).." num:"..tostring(bagdata[i].num))
|
||
table.insert(data,tempdata)
|
||
end
|
||
end
|
||
table.sort(data,function (a,b)
|
||
return a.id > b.id
|
||
end)
|
||
return data
|
||
end
|
||
|
||
function this.UpdateSingleGem(msg)
|
||
if msg and msg.lifeGridInfo and msg.lifeGridInfo.gridId and msg.lifeGridInfo.gridId > 0 then
|
||
if not this.myGemList[msg.lifeGridInfo.gridId] then
|
||
this.myGemList[msg.lifeGridInfo.gridId] = {}
|
||
end
|
||
if not this.myGemList[msg.lifeGridInfo.gridId][msg.lifeGridInfo.gridIndex] then
|
||
this.myGemList[msg.lifeGridInfo.gridId][msg.lifeGridInfo.gridIndex] = nil
|
||
end
|
||
-- LogPink(string.format( "修改的位置[%s][%s] ID:%s",msg.lifeGridInfo.gridId,msg.lifeGridInfo.gridIndex,tostring(msg.lifeGridInfo.itemId)))
|
||
this.myGemList[msg.lifeGridInfo.gridId][msg.lifeGridInfo.gridIndex] = msg.lifeGridInfo.itemId
|
||
end
|
||
end
|
||
|
||
--获取穿戴着的命石
|
||
function this.GetWearedGem(_GemList)
|
||
for i,v in ipairs(_GemList) do
|
||
-- LogPink(string.format("命格:%s index:%s ID:%s",v.gridId,v.gridIndex,v.itemId))
|
||
if v and v.gridId then
|
||
if not this.myGemList[v.gridId] then
|
||
this.myGemList[v.gridId] = {}
|
||
end
|
||
if not this.myGemList[v.gridId][v.gridIndex] then
|
||
this.myGemList[v.gridId][v.gridIndex] = nil
|
||
end
|
||
if v.itemId and v.itemId > 0 then
|
||
this.myGemList[v.gridId][v.gridIndex] = v.itemId
|
||
end
|
||
end
|
||
end
|
||
end
|
||
|
||
function this.GetAllAttri()
|
||
local data = {}
|
||
for i = 1, 8 do
|
||
for j = 1, 3 do
|
||
if this.myGemList[i] and this.myGemList[i][j] and this.myGemList[i][j] > 0 then
|
||
local pro = gemConfig[this.myGemList[i][j]].Property
|
||
for k = 1, #pro do
|
||
if not data[pro[k][1]] then
|
||
data[pro[k][1]] = 0
|
||
end
|
||
data[pro[k][1]] = data[pro[k][1]] + pro[k][2]
|
||
end
|
||
end
|
||
end
|
||
end
|
||
return data
|
||
end
|
||
|
||
function this.GetSortAttri()
|
||
local tempData = {}
|
||
local data = this.GetAllAttri()
|
||
for key, value in pairs(data) do
|
||
local t = {}
|
||
t.id = key
|
||
t.num = value
|
||
t.sort = propertyConfig[key].SortId
|
||
table.insert( tempData,t )
|
||
end
|
||
table.sort(tempData,function (a,b)
|
||
return a.sort < b.sort
|
||
end )
|
||
return tempData
|
||
end
|
||
|
||
--========================一键合成============================
|
||
--获取表中所有命石(data[类型1-8][等级1-10]=表数据),组成map
|
||
function this.GetMapOfBagData()
|
||
local data = {}
|
||
local bagdata = BagManager.GetBagAllDataByItemType(ItemType.Gem)
|
||
for _, configInfo in ConfigPairs(gemConfig) do
|
||
if not data[configInfo.Type] then
|
||
data[configInfo.Type] = {}
|
||
end
|
||
if not data[configInfo.Type][configInfo.Level] then
|
||
data[configInfo.Type][configInfo.Level] = {}
|
||
end
|
||
-- LogPink("type:"..tostring(configInfo.Type).." Level:"..tostring(configInfo.Level))
|
||
data[configInfo.Type][configInfo.Level] = configInfo
|
||
end
|
||
return data
|
||
end
|
||
|
||
function this.OneKeyConpound()
|
||
--获取表中所有命石,组成map
|
||
local totalMoney = 0
|
||
local finalList = {}
|
||
local configMap = this.GetMapOfBagData()
|
||
local curMoney = BagManager.GetTotalItemNum(14)--当前金币数量
|
||
--1-8的外部循环
|
||
for i = 1, #configMap do
|
||
-- LogError("循环"..i.."================================================================")
|
||
local money = this.DoCircle1(finalList,configMap[i],curMoney)
|
||
curMoney = curMoney - money
|
||
totalMoney = totalMoney + money
|
||
-- LogWarn("totalMoney:"..tostring(totalMoney).." leftMongey:"..tostring(curMoney))
|
||
end
|
||
local tempList = {}
|
||
for key, value in pairs(finalList) do--存在数据为0的情况,剔除
|
||
if value > 0 then
|
||
tempList[key] = value
|
||
end
|
||
end
|
||
return tempList,totalMoney
|
||
end
|
||
|
||
--一键合成循环
|
||
function this.DoCircle1(finalList,data,money)
|
||
local totalMoney = 0
|
||
local lastGNum = 0--上一次合成出来的数量
|
||
for i = 1, #data do
|
||
local tempData = data[i]
|
||
-- LogPink("Id:"..tostring(tempData.Id).." 名字:"..tostring(ItemConfig[tempData.Id].Name).." lastGNum:"..tostring(lastGNum))
|
||
--判断是否满级
|
||
if not tempData.NextGem or tempData.NextGem == 0 then
|
||
if lastGNum > 0 then
|
||
finalList[tempData.Id] = lastGNum
|
||
-- LogPurple("0:ID:"..tostring(tempData.Id).." num:"..tostring(finalList[tempData.Id]))
|
||
end
|
||
else
|
||
local bagGemNum = BagManager.GetTotalItemNum(tempData.Id)--当前背包中数量
|
||
local curGemNum = lastGNum--当前可使用合成的该等级的Gem数量
|
||
if bagGemNum > 0 then
|
||
curGemNum = bagGemNum + curGemNum--当前一共拥有命石数量(包括已经合成的数量)
|
||
end
|
||
--命石不足
|
||
-- LogGreen("curGemNum:"..tostring(curGemNum))
|
||
if curGemNum < tempData.UpgradeNum then
|
||
if lastGNum > 0 then
|
||
finalList[tempData.Id] = lastGNum
|
||
lastGNum = 0
|
||
-- LogPurple("1:ID:"..tostring(tempData.Id).." num:"..tostring(finalList[tempData.Id]))
|
||
end
|
||
else
|
||
local remainNum = curGemNum%tempData.UpgradeNum--如果合成到下一级,本级所剩余的数量
|
||
if remainNum ~= 0 then
|
||
curGemNum = curGemNum - remainNum--此处curGemNum变为实际花费的命石数量
|
||
end
|
||
if curGemNum > 0 then
|
||
local needMoneyNum = curGemNum/tempData.UpgradeNum * tempData.UpgradeCost[2]--按照命石可合成的数量,要花费的金币
|
||
local curMoneyNum = money--BagManager.GetTotalItemNum(tempData.UpgradeCost[1])--当前金币数量
|
||
-- LogYellow("needMoneyNum:"..tostring(needMoneyNum).." totalMoney:"..tostring(totalMoney))
|
||
if needMoneyNum + totalMoney > curMoneyNum then--如果当前金币不足要花费的数量,计算可以合成几个
|
||
local upNum = math.floor((curMoneyNum - totalMoney)/tempData.UpgradeCost[2])--实际真的合成了几个
|
||
totalMoney = totalMoney + upNum*tempData.UpgradeCost[2]--金币计入总数
|
||
local endCostNum = upNum*tempData.UpgradeNum--例如命石数量可消耗6,实际合成2,要把未使用的4加入最终list
|
||
if lastGNum > endCostNum then
|
||
finalList[tempData.Id] = lastGNum - endCostNum--要把未使用的加入最终list
|
||
-- LogPurple("2:ID:"..tostring(tempData.Id).." num:"..tostring(finalList[tempData.Id]))
|
||
end
|
||
finalList[tempData.NextGem] = upNum
|
||
-- LogPurple("3:ID:"..tostring(tempData.NextGem).." num:"..tostring(upNum))
|
||
-- LogRed("Bool:"..tostring(false).." totalMoney:"..tostring(totalMoney))
|
||
return totalMoney
|
||
else--如果金币足额
|
||
totalMoney = totalMoney + needMoneyNum--金币计入总数
|
||
if remainNum > 0 then
|
||
if remainNum - bagGemNum > 0 then--如果扣除给下一次循环所需要的,本次剩余如果大于0,则全部加入最终list
|
||
finalList[tempData.Id] = remainNum - bagGemNum
|
||
-- LogPurple("4:ID:"..tostring(tempData.Id).." num:"..tostring(finalList[tempData.Id]))
|
||
end
|
||
end
|
||
end
|
||
lastGNum = curGemNum/tempData.UpgradeNum
|
||
-- Log("lastGNum:"..tostring(lastGNum))
|
||
end
|
||
end
|
||
end
|
||
end
|
||
-- LogRed("Bool:"..tostring(true).." totalMoney:"..tostring(totalMoney))
|
||
return totalMoney
|
||
end
|
||
|
||
--=========================快速合成================================
|
||
function this.FastConpound(id)
|
||
local totalMoney = 0
|
||
local finalList = {}
|
||
local configMap = this.GetMapOfBagData()[gemConfig[id].Type]
|
||
local selectLv = gemConfig[id].Level
|
||
local lastGNum = 0--上一次合成出来的数量
|
||
|
||
for i = 1, #configMap do
|
||
local tempData = configMap[i]
|
||
local bagGemNum = BagManager.GetTotalItemNum(tempData.Id)--当前背包中数量
|
||
if tempData.Level == selectLv then
|
||
if lastGNum > 0 then
|
||
break
|
||
end
|
||
local num = BagManager.isBagPanel and 0 or 1
|
||
if bagGemNum < tempData.UpgradeNum - num then
|
||
break
|
||
else
|
||
lastGNum = 1
|
||
finalList[tempData.Id] = 1
|
||
break
|
||
end
|
||
end
|
||
if tempData.Level > selectLv then
|
||
break
|
||
end
|
||
local curGemNum = 0--当前可使用合成的该等级的Gem数量
|
||
if bagGemNum > 0 then
|
||
curGemNum = bagGemNum + lastGNum--当前一共拥有命石数量(包括已经合成的数量)
|
||
end
|
||
if curGemNum < tempData.UpgradeNum then--如果出现断层,需要清空数据继续遍历,
|
||
finalList = {}
|
||
totalMoney = 0
|
||
lastGNum = 0
|
||
else
|
||
local needNum = this.DoCircle2(tempData.Type,tempData.Level,selectLv)
|
||
local remainNum = curGemNum%tempData.UpgradeNum
|
||
if curGemNum > needNum then
|
||
remainNum = curGemNum - needNum
|
||
curGemNum = needNum
|
||
end
|
||
lastGNum = math.floor(curGemNum/tempData.UpgradeNum)
|
||
totalMoney = totalMoney + lastGNum*tempData.UpgradeCost[2]
|
||
if bagGemNum > 0 then
|
||
finalList[tempData.Id] = bagGemNum - remainNum
|
||
end
|
||
end
|
||
end
|
||
totalMoney = totalMoney + gemConfig[id].UpgradeCost[2]
|
||
local tempList = {}
|
||
for key, value in pairs(finalList) do--存在数据为0的情况,剔除
|
||
if value > 0 then
|
||
tempList[key] = value
|
||
end
|
||
end
|
||
return lastGNum==1,tempList,totalMoney,BagManager.GetTotalItemNum(14) >= totalMoney
|
||
end
|
||
|
||
function this.DoCircle2(type,curLevel,targetLevel)
|
||
local needNum = 1
|
||
local configMap = this.GetMapOfBagData()[type]
|
||
for i = 1, #configMap do
|
||
if configMap[i].Level >= curLevel then
|
||
if configMap[i].Level == targetLevel then
|
||
break
|
||
end
|
||
needNum = needNum*configMap[i].UpgradeNum
|
||
end
|
||
end
|
||
return needNum
|
||
end
|
||
|
||
--红点检测
|
||
function this.RedPointCheck()
|
||
for i = 1, 8 do
|
||
local data = this.GetGemsFromBagByType(i)
|
||
if this.myGemList[i] then--如果有数据
|
||
-- LogYellow("i:"..tostring(i))
|
||
for j = 1, #this.myGemList[i] do
|
||
-- LogYellow("j:"..tostring(j))
|
||
if this.myGemList[i][j] and this.myGemList[i][j] > 0 then--如果有数据切大于零
|
||
-- LogYellow("myGemList[i][j]:"..tostring(this.myGemList[i][j]))
|
||
for key, value in pairs(data) do
|
||
local numInBag = BagManager.GetTotalItemNum(this.myGemList[i][j])
|
||
-- LogYellow(value.id.." "..this.myGemList[i][j].." "..(numInBag + 1).." "..gemConfig[this.myGemList[i][j]].UpgradeNum)
|
||
if value.id > this.myGemList[i][j] then
|
||
return true
|
||
elseif numInBag + 1 >= gemConfig[this.myGemList[i][j]].UpgradeNum and gemConfig[this.myGemList[i][j]].NextGem > 0 then
|
||
return true
|
||
end
|
||
end
|
||
else--如果没有数据,如果背包里有命石就显示红点
|
||
for key, value in pairs(data) do
|
||
if value.num > 0 then
|
||
return true
|
||
end
|
||
end
|
||
end
|
||
end
|
||
else--如果没有数据,如果背包里有命石就显示红点
|
||
for key, value in pairs(data) do
|
||
if value.num > 0 then
|
||
return true
|
||
end
|
||
end
|
||
end
|
||
end
|
||
return false
|
||
end
|
||
|
||
return this |