miduo_client/Assets/ManagedResources/~Lua/Modules/Gem/GemManager.lua

304 lines
12 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

GemManager = {}
local this = GemManager
local gemConfig = ConfigManager.GetConfig(ConfigName.GemConfig)
local propertyConfig = ConfigManager.GetConfig(ConfigName.PropertyConfig)
function this.Initialize()
this.myGemList = {}--我穿戴命石的id
this.typeGemList = {}
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()
--1-8的外部循环
for i = 1, #configMap do
local bool,money = this.DoCircle1(finalList,configMap[i])
totalMoney = totalMoney + money
-- LogGreen("money:"..tostring(totalMoney))
if not bool then
-- LogError("合成结束")
break
end
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)
local totalMoney = 0
local lastGNum = 0--上一次合成出来的数量
for i = 1, #data do
local tempData = data[i]
-- LogPink("Id:"..tostring(tempData.Id).." lastGNum:"..tostring(lastGNum))
--判断是否满级
if not tempData.NextGem or tempData.NextGem == 0 then
if lastGNum > 0 then
finalList[tempData.Id] = lastGNum
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
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 = BagManager.GetTotalItemNum(tempData.UpgradeCost[1])--当前金币数量
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
end
finalList[tempData.NextGem] = upNum
return false,totalMoney
else--如果金币足额
totalMoney = totalMoney + needMoneyNum--金币计入总数
if bagGemNum > 0 then
if remainNum - bagGemNum > 0 then--如果扣除给下一次循环所需要的本次剩余如果大于0则全部加入最终list
finalList[tempData.NextGem] = remainNum - bagGemNum
end
end
end
lastGNum = curGemNum/tempData.UpgradeNum
-- LogGreen("lastGNum:"..tostring(lastGNum))
end
end
end
end
return true,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--如果有数据
for j = 1, #this.myGemList[i] do
if this.myGemList[i][j] and this.myGemList[i][j] > 0 then--如果有数据切大于零
for key, value in pairs(data) do
local numInBag = BagManager.GetTotalItemNum(this.myGemList[i][j])
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