local Msg = require("core.Msg") local Broadcast = require("broadcast.Broadcast") local Lang = require("common.Lang") local ItemDefine = require("bag.ItemDefine") local Grid = require("bag.Grid") local DrawCardLogic = require("drawCard.DrawCardLogic") local MergeRule = require("excel.mergeConfig").rule local BagLogic = require("bag.BagLogic") local Log = require("common.Log") local Util = require("common.Util") local ItemLogic = require("bag.ItemLogic") local HeroLogic = require("hero.HeroLogic") local HeroExcel = require("excel.hero") local RoleDefine = require("role.RoleDefine") local ObjHuman = require("core.ObjHuman") local HeroGrid = require("hero.HeroGrid") AD_DRAW_REWARD_TYPE = 10 --观看广告领取召唤券 QUERY_MERGE_INFO_TYPE = 11 --获取融合信息 QUERY_MERGE_BEFORE_INFO_TYPE = 15 --获取融合前信息 HATCH_SCCUESS_TYPE = 12 -- 孵化成功 MERGE_HERO_TYPE = 13 --融合 QUICK_HATCH_TYPE = 14 --加速 function NewProto(human, type, param) -- print("newProto:",type,param) -- local msgRet = Msg.gc.GC_ROLE_CHANGE_BASEINFO -- msgRet.ret = type -- msgRet.tip = "test data" -- Msg.send(msgRet,human.fd) -- 观看广告获取召唤券 if type == AD_DRAW_REWARD_TYPE then human.db.adRewardCnt = human.db.adRewardCnt or 0 if human.db.adRewardCnt > 4 then Broadcast.sendErr(human, Lang.AD_DRAW_REWARD_LIMIT_ERROR) return end --增加今日观看次数 human.db.adRewardCnt = (human.db.adRewardCnt or 0) + 1 -- 添加高级召唤卷 BagLogic.addItem(human, 118, 1, "draw_ad_reward") local msgRet = Msg.gc.GC_ROLE_CHANGE_BASEINFO msgRet.ret = AD_DRAW_REWARD_TYPE msgRet.tip = Lang.AD_DRAW_REWARD_SUCCESS Msg.send(msgRet, human.fd) Broadcast.sendErr(human, Lang.AD_DRAW_REWARD_SUCCESS) end -- 开始融合 if type == MERGE_HERO_TYPE then local tb = Util.split(param, "|") if tb[1] and tb[2] then -- 初始化 initMergeInfo(human) human.db.heroBag = human.db.heroBag or {} local itemId = 178 --生命雨露 local fatherHeroBagIndex = tonumber(tb[1]) or 0 local motherHeroBagIndex = tonumber(tb[2]) or 0 Log.write(Log.LOGID_TEST, "开始融合 fatherHeroBagIndex: " .. fatherHeroBagIndex .. "motherHeroBagIndex: " .. motherHeroBagIndex) local fatherHeroGrid = human.db.heroBag[fatherHeroBagIndex] if not heroGrid then return end local motherHeroGrid = human.db.heroBag[motherHeroBagIndex] if not heroGrid then return end Log.write(Log.LOGID_TEST, "motherHeroGrid: " .. Json.Encode(motherHeroGrid)) Log.write(Log.LOGID_TEST, "fatherHeroGrid: " .. Json.Encode(fatherHeroGrid)) local fatherHeroAttr = ObjHuman.getHeroAttrs(human, fatherHeroBagIndex) local motherHeroAttr = ObjHuman.getHeroAttrs(human, motherHeroBagIndex) Log.write(Log.LOGID_TEST, "fatherHeroAttr: " .. Json.Encode(fatherHeroAttr)) Log.write(Log.LOGID_TEST, "motherHeroAttr: " .. Json.Encode(motherHeroAttr)) local mergeInfo = {} local attrs = RoleDefine.PANEL_ATTR_KEY local sonHeroID = mergeHero(fatherHeroGrid.id, motherHeroGrid.id) if sonHeroID <= 0 then return end Log.write(Log.LOGID_TEST, "sonHeroID: " .. sonHeroID) for key, value in pairs(attrs) do local param1 = fatherHeroAttr[key] or 0 local param2 = motherHeroAttr[key] or 0 attrs[key] = math.floor(((param1 + param2) / 4)) end Log.write(Log.LOGID_TEST, "融合后的属性: " .. Json.Encode(attrs)) local xLv = getxLv(fatherHeroGrid, motherHeroGrid) local cnt = math.floor(xLv / 2) + 1 local heroSimple = {} Log.write(Log.LOGID_TEST, "xLv: " .. xLv) Log.write(Log.LOGID_TEST, "cnt: " .. cnt) --使用道具 ItemLogic.use(human, itemId, cnt) Log.write(Log.LOGID_TEST, "删除开始") -- 删除英雄 HeroLogic.delHeroByIndex(human, fatherHeroBagIndex, "hero_merge") HeroLogic.delHeroByIndex(human, motherHeroBagIndex, "hero_merge") Log.write(Log.LOGID_TEST, "删除结束") -- 记录融合英雄 human.db.mergeInfo.heroInfo.heroID = sonHeroID human.db.mergeInfo.heroInfo.xLv = xLv human.db.mergeInfo.heroInfo.heroAttrs = attrs human.db.mergeInfo.startTime = os.time() human.db.mergeInfo.endTime = os.time() + getHatchTime(xLv) human.db.mergeInfo.heroInfo.fatherHeroBagIndex = fatherHeroBagIndex human.db.mergeInfo.heroInfo.motherHeroBagIndex = motherHeroBagIndex Log.write(Log.LOGID_TEST, "human.db.mergeInfo : " .. Json.Encode(human.db.mergeInfo)) local cf = sonHeroID and HeroExcel.hero[sonHeroID] heroSimple.name = cf and cf.name or "" mergeInfo.mergeTime = human.db.mergeInfo.hatchTime mergeInfo.xLv = xLv mergeInfo.heroData = heroSimple Log.write(Log.LOGID_TEST, "mergeInfo: " .. Json.Encode(mergeInfo)) Broadcast.sendErr(human, Lang.MERGE_SUCCESS) local msgRet = Msg.gc.GC_ROLE_CHANGE_BASEINFO msgRet.ret = MERGE_HERO_TYPE msgRet.tip = Json.Encode(mergeInfo) Msg.send(msgRet, human.fd) end return end -- 获取融合前的信息 if type == QUERY_MERGE_BEFORE_INFO_TYPE then local tb = Util.split(param, "|") if tb[1] and tb[2] then -- 初始化 initMergeInfo(human) human.db.heroBag = human.db.heroBag or {} local fatherHeroBagIndex = tonumber(tb[1]) or 0 local motherHeroBagIndex = tonumber(tb[2]) or 0 local fatherHeroGrid = human.db.heroBag[fatherHeroBagIndex] if not heroGrid then return end local motherHeroGrid = human.db.heroBag[motherHeroBagIndex] if not heroGrid then return end local mergeInfo = {} local heroSimple = {} local xLv = getxLv(fatherHeroGrid, motherHeroGrid) local cnt = math.floor(xLv / 2) + 1 local isHatch = 2 local mergeItem = {} local itemId = 178 for k = 1, 1 do mergeItem[k] = mergeItem[k] or {} mergeItem[k].getway = mergeItem[k].getway or {} mergeItem[k].suipian = mergeItem[k].suipian or {} mergeItem[k].equip = mergeItem[k].equip or {} mergeItem[k].fuwen = mergeItem[k].fuwen or {} Grid.makeItem(mergeItem[k], itemId, cnt) end local sonHeroID = mergeHero(fatherHeroGrid.id, motherHeroGrid.id) if sonHeroID <= 0 then return end local cf = sonHeroID and HeroExcel.hero[sonHeroID] heroSimple.name = cf and cf.name or "" mergeInfo.mergeTime = 0 mergeInfo.xLv = xLv mergeInfo.mergeItem = mergeItem mergeInfo.heroData = heroSimple mergeInfo.isHatch = isHatch Log.write(Log.LOGID_TEST, "mergeInfo: " .. Json.Encode(mergeInfo)) local msgRet = Msg.gc.QUERY_MERGE_BEFORE_INFO_TYPE msgRet.ret = QUERY_MERGE_INFO_TYPE msgRet.tip = Json.Encode(mergeInfo) Msg.send(msgRet, human.fd) end return end -- 获取融合中的信息 if type == QUERY_MERGE_INFO_TYPE then -- 初始化 initMergeInfo(human) human.db.heroBag = human.db.heroBag or {} local mergeInfo = {} local mergeTime = 0 local heroSimple = {} local xLv = human.db.mergeInfo.heroInfo.xLv or 0 local heroIndex = 0 local sonHeroID = human.db.mergeInfo.heroInfo.heroID or 0 local isHatch = 2 if sonHeroID == 0 then mergeInfo.mergeTime = mergeTime mergeInfo.xLv = xLv mergeInfo.heroData = heroSimple mergeInfo.isHatch = isHatch msgRet.ret = QUERY_MERGE_INFO_TYPE msgRet.tip = Json.Encode(mergeInfo) Msg.send(msgRet, human.fd) return end local mergeTime = human.db.mergeInfo.endTime - os.time() if mergeTime < 0 then -- 孵化成功 mergeTime = 0 isHatch = 3 heroIndex = hatchHero(human) end if heroIndex ~= 0 then local heroGrid = human.db.heroBag[heroIndex] if type(heroGrid) ~= "table" then return end heroSimple.general = heroSimple.general or {} HeroGrid.makeHeroSimple(heroSimple, heroGrid, nil, human) else local cf = sonHeroID and HeroExcel.hero[sonHeroID] heroSimple.name = cf and cf.name or "" isHatch = 1 end mergeInfo.mergeTime = mergeTime mergeInfo.xLv = xLv mergeInfo.heroData = heroSimple mergeInfo.isHatch = isHatch Log.write(Log.LOGID_TEST, "mergeInfo: " .. Json.Encode(mergeInfo)) local msgRet = Msg.gc.GC_ROLE_CHANGE_BASEINFO msgRet.ret = QUERY_MERGE_INFO_TYPE msgRet.tip = Json.Encode(mergeInfo) Msg.send(msgRet, human.fd) return end -- 加速孵化 if type == QUICK_HATCH_TYPE then local tb = Util.split(param, "|") local itemId = tonumber(tb[1]) or 0 local cnt = tonumber(tb[2]) or 0 local isHatch = 2 if tb[1] and tb[2] then -- 初始化 initMergeInfo(human) -- 剩余时间 local nowTime = os.time() local hatchTime = human.db.mergeInfo.endTime - nowTime if hatchTime <= 0 then return end local itemSpeedTime = 0 if itemId == 179 then local speedTime = (60 * 5 * cnt) -- 正常数量加速券的加速时间 local lastSpeedTime = (60 * 5 * (cnt - 1)) --少一张加速券的加速时间 local itemCnt = 0 --使用道具 if hatchTime > lastSpeedTime and hatchTime <= speedTime then itemCnt = cnt elseif hatchTime > 0 and hatchTime <= lastSpeedTime then itemCnt = cnt - 1 elseif hatchTime >= speedTime then itemCnt = cnt else itemCnt = 0 end itemSpeedTime = (60 * 5 * itemCnt) --使用道具 ItemLogic.use(human, itemId, itemCnt) else local zuanshiCnt = doCalcNeedZuanshi(hatchTime) -- 判断消耗 if not ObjHuman.checkRMB(human, zuanshiCnt) then return end itemSpeedTime = (30 * zuanshiCnt) -- 扣钻石 ObjHuman.decZuanshi(human, -zuanshiCnt, "hero_merge") end human.db.mergeInfo.endTime = human.db.mergeInfo.endTime - itemSpeedTime Log.write(Log.LOGID_TEST, "human.db.mergeInfo.endTime: " .. human.db.mergeInfo.endTime) local hatchTime = human.db.mergeInfo.endTime - nowTime -- 孵化成功 if hatchTime < 0 then hatchTime = 0 isHatch = 3 end Log.write(Log.LOGID_TEST, "加速后 human.db.mergeInfo : " .. Json.Encode(human.db.mergeInfo)) Broadcast.sendErr(human, Lang.QUICK_HATCH_SUCCESS) local mergeInfo = {} local heroSimple = {} local msgRet = Msg.gc.GC_ROLE_CHANGE_BASEINFO mergeInfo.mergeTime = hatchTime mergeInfo.heroData = heroSimple mergeInfo.xLv = human.db.mergeInfo.heroInfo.xLv mergeInfo.isHatch = isHatch msgRet.ret = QUICK_HATCH_TYPE msgRet.tip = Json.Encode(mergeInfo) Msg.send(msgRet, human.fd) return end return end end -- 孵化英雄 function hatchHero(human) --添加英雄 local heroIndex, fjlist = HeroLogic.addHero(human, human.db.mergeInfo.heroInfo.heroID, nil, 1, "hero_merge") human.db.mergeInfo.startTime = 0 human.db.mergeInfo.endTime = 0 human.db.mergeInfo.heroInfo = {} human.db.mergeInfo.heroInfo.heroID = 0 human.db.mergeInfo.heroInfo.xLv = 0 human.db.mergeInfo.heroInfo.fatherHeroBagIndex = 0 human.db.mergeInfo.heroInfo.motherHeroBagIndex = 0 human.db.mergeInfo.heroInfo.heroAttrs = RoleDefine.PANEL_ATTR_KEY human.db.heroBag = human.db.heroBag or {} return heroIndex -- local heroGrid = human.db.heroBag[heroIndex] -- if type(heroGrid) ~= "table" then return end -- heroSimple.general = heroSimple.general or {} -- HeroGrid.makeHeroSimple(heroSimple, heroGrid, nil, human) -- data.heroData = heroSimple -- data.mergeTime = human.db.mergeInfo.mergeTime -- msgRet.ret = HATCH_SCCUESS_TYPE -- msgRet.tip = Json.Encode(data) -- Msg.send(msgRet, human.fd) end -- 初始化融合信息 function initMergeInfo(human) human.db.mergeInfo = human.db.mergeInfo or {} human.db.mergeInfo.mergeTime = human.db.mergeInfo.mergeTime or 0 --融合时间 时间戳 human.db.mergeInfo.hatchTime = human.db.mergeInfo.hatchTime or 0 --孵化时间 单位s human.db.mergeInfo.heroInfo = human.db.mergeInfo.heroInfo or {} human.db.mergeInfo.heroInfo.heroID = human.db.mergeInfo.heroInfo.heroID or 0 human.db.mergeInfo.heroInfo.xLv = human.db.mergeInfo.heroInfo.xLv or 0 human.db.mergeInfo.heroInfo.fatherHeroBagIndex = human.db.mergeInfo.heroInfo.fatherHeroBagIndex or 0 human.db.mergeInfo.heroInfo.motherHeroBagIndex = human.db.mergeInfo.heroInfo.motherHeroBagIndex or 0 human.db.mergeInfo.heroInfo.heroAttrs = human.db.mergeInfo.heroInfo.heroAttrs or RoleDefine.PANEL_ATTR_KEY end -- 获取孵化时间 单位s function getHatchTime(xLv) local time = (xLv * 5) + (xLv - 1) * (xLv - 1) - 5 if time < 0 then return 0 end return time * 60 end -- 计算需要钻石数量 function doCalcNeedZuanshi(hatchTime) local cnt = 0 if hatchTime > 0 then cnt = math.ceil(hatchTime / 30) end return cnt end -- 获取xlv function getxLv(fatherHeroGrid, motherHeroGrid) local params1 = math.max(fatherHeroGrid.xLv, motherHeroGrid.xLv) local params2 = math.floor((fatherHeroGrid.lv + motherHeroGrid.lv) / 200) + 1 return params1 + params2 end -- 融合获取英雄ID function mergeHero(fatherHeroID, motherHeroID) local fatherHeroConfig = HeroExcel.hero[fatherHeroID] local motherHeroConfig = HeroExcel.hero[motherHeroID] if not fatherHeroConfig or not motherHeroConfig then return 0 end --规则3 heroID + heroID for _, config in pairs(MergeRule[3].items) do if fatherHeroID == config[1] and motherHeroID == config[2] then return config[3] end end --规则2 种族 + heroID for _, config in pairs(MergeRule[2].items) do if fatherHeroConfig.camp == config[1] and motherHeroID == config[2] then return config[3] end end --规则1 种族 + 种族 for _, config in pairs(MergeRule[1].items) do if fatherHeroConfig.camp == config[1] and motherHeroConfig.camp == config[2] then return config[3] end end return 0 end