local skynet = require "skynet" local oo = require "Class" local log = require "Log" local pb = require "pb" local dataType = require "DataType" local errorInfo = require "ErrorInfo" local shop = require "Shop" local CoffeeShop = oo.class(shop) CoffeeShop.shopType = dataType.ShopType_Coffee CoffeeShop.maxCDTime = 300 --60秒 CoffeeShop.MethodType_AD = 1 --广告 CoffeeShop.MethodType_Volute = 2 --蜗壳 --花店展示 function CoffeeShop:Show( player , c2sData , s2cData ) c2sData.data = assert(pb.decode("C2SCoffeeMergeShow", c2sData.data )) --player.gameData.shop[ self.shopType ].currency[ 1 ] = 99 --测试要删 --player.gameData.shop[ self.shopType ].currency[ 2 ] = 99 --player.gameData.shop[ self.shopType ].currency[ 3 ] = 99 local curShop = player.gameData.shop[ self.shopType ] local data = {} data.supplierBoxes = curShop.supplierBoxes data.grids = curShop.grids data.currency = self:GetCurrency( player ) s2cData.cmd = pb.enum("MsgType","CMD_S2C_CoffeeMergeShow") s2cData.data = assert(pb.encode("S2CCoffeeMergeShow", data)) end --供应信息点击 function CoffeeShop:SupplyClick( player , c2sData , s2cData ) c2sData.data = assert(pb.decode("C2SCoffeeMergeSupplyClick", c2sData.data )) local shopType = dataType.ShopType_Coffee local data = {} local type = c2sData.data.type local isVaild = self:IsVaildSupplierBoxes( type ) if not type or not isVaild then s2cData.code = errorInfo.ErrorCode.ErrRequestParam else local gridIndex = self:GetFreeGrid( player ) if not gridIndex then s2cData.code = errorInfo.ErrorCode.CoffeeShopGridFull else local curSupplier = self:GetSupplierBoxes( player , type ) if curSupplier and curSupplier.count >= 1 then --数量减1,重新CD时间 curSupplier.count = curSupplier.count - 1 curSupplier.nextRefreshTime = skynet.GetTime() + self.maxCDTime --放到格子里去 self:AddToFreeGrids( player , gridIndex , type , 1 ) skynet.server.passCheck:Modify( player , 13 , 1 ) data.supplierBox = curSupplier data.gridTo = self:GetGrid( player , gridIndex ) --player.gameData.shop[ self.shopType ].grids[ gridIndex ] else s2cData.code = errorInfo.ErrorCode.NoEnoughGoods end end end s2cData.cmd = pb.enum("MsgType","CMD_S2C_CoffeeMergeSupplyClick") s2cData.data = assert(pb.encode("S2CCoffeeMergeSupplyClick", data)) end --获取最新供货信息 function CoffeeShop:SupplyShow( player , c2sData , s2cData ) c2sData.data = assert(pb.decode("C2SCoffeeMergeSupplyShow", c2sData.data )) local data = {} local type = c2sData.data.type local isVaild = self:IsVaildSupplierBoxes( type ) if not type or not isVaild then s2cData.code = errorInfo.ErrorCode.ErrRequestParam else local curSupplier = self:GetSupplierBoxes( player , type ) if curSupplier then data.supplierBox = curSupplier else s2cData.code = errorInfo.ErrorCode.ErrRequestParam end end s2cData.cmd = pb.enum("MsgType","CMD_S2C_CoffeeMergeSupplyShow") s2cData.data = assert(pb.encode("S2CCoffeeMergeSupplyShow", data)) end --获取最新供货信息 function CoffeeShop:Sort( player , c2sData , s2cData ) c2sData.data = assert(pb.decode("C2SCoffeeMergeSort", c2sData.data )) local data = {} --排序得第一优先级为type得升序(1-3,0), 第二优先级为level的降序(5-1) local grids = player.gameData.shop[ self.shopType ].grids table.sort( grids , function(a,b) if a.type == b.type then return a.level > b.level else return a.type < b.type end end) --将有数据的格子先整理出来 local tmpGrids = {} local count = 1 for k, v in pairs( grids ) do if 0 ~= v.type then table.insert( tmpGrids , { type = v.type , index = count , level = v.level }) count = count + 1 end end --填充剩余格子 for i = count, 30, 1 do table.insert( tmpGrids , { type = 0 , index = i, level = 0 }) end player.gameData.shop[ self.shopType ].grids = tmpGrids data.grids = tmpGrids s2cData.cmd = pb.enum("MsgType","CMD_S2C_CoffeeMergeSort") s2cData.data = assert(pb.encode("S2CCoffeeMergeSort", data)) end --合并 function CoffeeShop:Merge( player , c2sData , s2cData ) c2sData.data = assert(pb.decode("C2SCoffeeMergeAttempt", c2sData.data )) local data = {} data.addCurrencyType = dataType.CoffeeShopGoodsType_Empty local gridFrom = c2sData.data.gridFrom local gridTo = c2sData.data.gridTo if not gridFrom or not gridTo or not self:IsVaildGridID( gridFrom ) or not self:IsVaildGridID( gridTo ) then s2cData.code = errorInfo.ErrorCode.ErrRequestParam else gridFrom = gridFrom gridTo = gridTo local grid1 = self:GetGrid( player , gridFrom ) --player.gameData.shop[ self.shopType ].grids[ gridFrom ] local grid2 = self:GetGrid( player , gridTo ) --player.gameData.shop[ self.shopType ].grids[ gridTo ] if not grid1 or not grid2 then s2cData.code = errorInfo.ErrorCode.ErrRequestParam else if not self:IsFreeGrid( player , gridFrom ) and self:IsFreeGrid( player , gridTo ) then --始非空格 终为空格 就移动过去 self:MoveGrid( player , gridFrom , gridTo ) else ---始非空格 终非空格 就合成过去 if self:IsSame( player , gridFrom , gridTo ) then --开始合成 data.addCurrencyType = self:StartMerge( player ,gridFrom , gridTo ) if dataType.CoffeeShopGoodsType_Empty ~= data.addCurrencyType then --有货币变化,向客户端同步下最新的货币数据 local currencyData = {} currencyData.currency = {} currencyData.currency.coffeeBean = player.gameData.shop[ self.shopType ].currency[1] currencyData.currency.sugar = player.gameData.shop[ self.shopType ].currency[2] currencyData.currency.milk = player.gameData.shop[ self.shopType ].currency[3] skynet.server.gameServer:SendMsgToUser( player.userId , "CMD_S2C_CoffeeMergeCurrency" , currencyData ) end skynet.server.levelTask:Modify( player , 49 , 1 ) else --交换格子 self:ExchangeGrid( player , gridFrom , gridTo ) end end data.gridFrom = self:GetGrid( player , gridFrom ) --player.gameData.shop[ self.shopType ].grids[ gridFrom ] data.gridTo = self:GetGrid( player , gridTo ) --player.gameData.shop[ self.shopType ].grids[ gridTo ] end end s2cData.cmd = pb.enum("MsgType","CMD_S2C_CoffeeMergeAttempt") s2cData.data = assert(pb.encode("S2CCoffeeMergeAttempt", data)) end --补充 function CoffeeShop:Replenish( player , c2sData , s2cData ) c2sData.data = assert(pb.decode("C2SCoffeeSupplierReplenish", c2sData.data )) local data = {} data.addCurrencyType = dataType.CoffeeShopGoodsType_Empty local supplierType = c2sData.data.supplierType local methodType = c2sData.data.methodType local coffeeAdCount = player.gameData.todayGain.coffeeReplenishAdCount local isVaild = self:IsVaildSupplierBoxes( supplierType ) if not supplierType or not methodType or not isVaild then s2cData.code = errorInfo.ErrorCode.ErrRequestParam else local curSupplier = self:GetSupplierBoxes( player , supplierType ) local isSuc = false if self.MethodType_AD == methodType then if not coffeeAdCount[ supplierType ] then coffeeAdCount[ supplierType ] = 0 end coffeeAdCount[ supplierType ] = coffeeAdCount[ supplierType ] + 1 if coffeeAdCount[ supplierType ] <= 1 then --每天广告数量定额 --需要检测下该广告领取没有 skynet.server.ad:AddCount(player , 1) isSuc = true end elseif self.MethodType_Volute == methodType then local cfgCoffeeMakingPara = skynet.server.gameConfig.SValue.coffeeMakingPara if player:MoneyChange( dataType.MoneyType_Volute , - cfgCoffeeMakingPara[4] ) then isSuc = true end end if isSuc then if not coffeeAdCount[ supplierType ] then curSupplier.adTimes = 0 else curSupplier.adTimes = coffeeAdCount[ supplierType ] end local cfgCoffeeMakingPara = skynet.server.gameConfig.SValue.coffeeMakingPara curSupplier.count = cfgCoffeeMakingPara[1] data.supplierBox = curSupplier log.info(string.format("玩家 %d 咖啡店 补充商品 补充类型 %d 补充商品类型 %d ", player.userId , methodType , supplierType)) else s2cData.code = errorInfo.ErrorCode.TodayMaxLimit end end s2cData.cmd = pb.enum("MsgType","CMD_S2C_CoffeeSupplierReplenish") s2cData.data = assert(pb.encode("S2CCoffeeSupplierReplenish", data)) end --研发 function CoffeeShop:Research( player , c2sData , s2cData ) c2sData.data = assert(pb.decode("C2SCoffeeResearch", c2sData.data )) local data = {} data.addCurrencyType = dataType.CoffeeShopGoodsType_Empty local isNew = false --是否研发出新产品 --[[ local supplyIndex = { 0 , 0 ,0 } --保存3种必要供应品的索引 local curGrid = nil for i = dataType.CoffeeShopGoodsType_CoffeeBean, dataType.CoffeeShopGoodsType_Milk , 1 do for j = 1, 30, 1 do curGrid = self:GetGrid( player , j ) --player.gameData.shop[ self.shopType ].grids[ j ] if curGrid.type == i then --找到该供应品的索引 supplyIndex[ i ] = j log.info("1111111a") break end end end]] local curShop = player.gameData.shop[ self.shopType ] --[[ local currency = {} currency.coffeeBean = curShop.currency[1] currency.sugar = curShop.currency[2] currency.milk = curShop.currency[3] ]] --检查是否有效 local isVaild = true for i = dataType.CoffeeShopGoodsType_CoffeeBean, dataType.CoffeeShopGoodsType_Milk , 1 do if 0 == curShop.currency[i] then isVaild = false break end end if not isVaild then s2cData.code = errorInfo.ErrorCode.NoEnoughGoods else --供应品足够,可以研发 --开始研发 --扣除货币 for i = dataType.CoffeeShopGoodsType_CoffeeBean, dataType.CoffeeShopGoodsType_Milk , 1 do curShop.currency[i] = curShop.currency[i] - 1 end local coffeeId = 1 --研发出来的咖啡ID local randCoffee = self:GetRandCoffee( player , self.CoffeeType_Coffee , 1) coffeeId = randCoffee[1] --研发成功 skynet.server.levelTask:Modify( player , 29 , 1 ) skynet.server.achieveTask:Modify( player , 29 , 1 ) skynet.server.passCheck:Modify( player , 11 , 1 ) local curCoffees = player.gameData.shop[ self.shopType ].coffees local isExist = false for k, v in pairs( curCoffees ) do if v == coffeeId then isExist = true end end if not isExist then --研发出来了新的咖啡 isNew = true table.insert( curCoffees , coffeeId ) --skynet.server.achieveTask:Modify( player , 8 , 1) end --发送奖励 local curCfgCoffeeType = skynet.server.gameConfig:GetCurCfg( "CoffeeType" , coffeeId ) player:GiveRewardNpc( curCfgCoffeeType.inventReward , 4 ) data.currency = self:GetCurrency( player ) data.inventedCoffee = coffeeId data.isNew = isNew end s2cData.cmd = pb.enum("MsgType","CMD_S2C_CoffeeResearch") s2cData.data = assert(pb.encode("S2CCoffeeResearch", data)) end --图鉴显示 function CoffeeShop:IllustrationShow( player , c2sData , s2cData ) c2sData.data = assert(pb.decode("C2SCoffeeIllustrationShow", c2sData.data )) local curShop = player.gameData.shop[ self.shopType ] local data = {} local cfg = {} local cfgSValue = skynet.server.gameConfig.SValue local cfgIllustationsReward = cfgSValue.coffeeIllustationReward local cfgReward = skynet.server.gameConfig.Reward data.rewards = {} data.currentCoffees = curShop.coffees local coffeeCount = #curShop.coffees for k1, v1 in pairs( curShop.rewards ) do for k2, v2 in pairs( cfgIllustationsReward ) do local reward = skynet.server.common:Split( v2 ,"_" ) --满足条件就设置可领状态 if k2 == v1.id and dataType.Status_NoGain == v1.status and coffeeCount >= tonumber(reward[ 1 ]) then v1.status = dataType.Status_CanGain end end --能领和领取的发给客户端 if dataType.Status_CanGain == v1.status or dataType.Status_AlreadyGain == v1.status then table.insert( data.rewards , v1 ) end end s2cData.cmd = pb.enum("MsgType","CMD_S2C_CoffeeIllustrationShow") s2cData.data = assert(pb.encode("S2CCoffeeIllustrationShow", data)) end --图鉴奖励 function CoffeeShop:IllustrationPrize( player , c2sData , s2cData ) c2sData.data = assert(pb.decode("C2SCoffeeIllustrationPrize", c2sData.data )) local data = {} local id = c2sData.data.id if not id then s2cData.code = errorInfo.ErrorCode.ErrRequestParam else local curShop = player.gameData.shop[ self.shopType ] data.id = id data.awardMoney = {} --奖励的货币 data.awardGoods = {} --奖励的物品 data.rewards = {} local rewardId = 0 local cfgSValue = skynet.server.gameConfig.SValue for k, v in pairs( cfgSValue.coffeeIllustationReward ) do local reward = skynet.server.common:Split( v ,"_" ) if id == k then rewardId = tonumber(reward[2]) break end end for k, v in pairs( curShop.rewards ) do if id == v.id and dataType.Status_CanGain == v.status then v.status = dataType.Status_AlreadyGain --发奖 local cfgReward = skynet.server.gameConfig:GetCurCfg("Reward" , rewardId ) player:GiveReward( rewardId ) table.insert( data.awardMoney , { type = dataType.GoodsType_Clovers , count = cfgReward.mapCoin }) local cfgSceneObject = cfgReward.sceneObject for k, v in pairs(cfgSceneObject) do local tmp = skynet.server.common:Split( v ,"_" ) local goodsType = 0 if 1 == tonumber(tmp[ 1 ]) then goodsType = dataType.GoodsType_Furniture elseif 2 == tonumber(tmp[ 1 ]) then goodsType = dataType.GoodsType_Decorate end table.insert( data.awardGoods , { goodsType = goodsType , goodsId = tonumber(tmp[ 2 ]) , goodsCount = 1}) end end --能领和领取的发给客户端 if dataType.Status_CanGain == v.status or dataType.Status_AlreadyGain == v.status then table.insert( data.rewards , v ) end end end s2cData.cmd = pb.enum("MsgType","CMD_S2C_CoffeeIllustrationPrize") s2cData.data = assert(pb.encode("S2CCoffeeIllustrationPrize", data)) end --添加格子商品 function CoffeeShop:AddToFreeGrids( player , index , type , level ) local curGrid = self:GetGrid( player , index ) --player.gameData.shop[ self.shopType ].grids[ index ] curGrid.type = type curGrid.level = level log.info(string.format("玩家 %d 咖啡店 添加格子商品 格子索引 %d 商品类型 %d 等级 %d", player.userId , index , type , level )) end --获取空闲格子 function CoffeeShop:GetFreeGrid( player ) local grids = player.gameData.shop[ self.shopType ].grids for i = 1, 30, 1 do local curGrid = self:GetGrid( player , i ) if dataType.CoffeeShopGoodsType_Empty == curGrid.type then return i end end return nil end --设置为空闲格子 function CoffeeShop:SetFreeGrid( player , index ) local grid = self:GetGrid( player , index ) --local grid = player.gameData.shop[ self.shopType ].grids[ index ] if grid then grid.type = dataType.CoffeeShopGoodsType_Empty grid.level = 0 end end --是否为空闲格子 function CoffeeShop:IsFreeGrid( player , index ) --local grid = player.gameData.shop[ self.shopType ].grids[ index ] local grid = self:GetGrid( player , index ) if grid and dataType.CoffeeShopGoodsType_Empty == grid.type then return true else return false end end --是否相同 function CoffeeShop:IsSame( player , gridFrom , gridTo ) --local grid = self:GetGrid( player , index ) local curGridFrom = self:GetGrid( player , gridFrom ) local curGridTo = self:GetGrid( player , gridTo ) if curGridFrom.type == curGridTo.type and curGridFrom.level == curGridTo.level then return true else return false end end --开始合成 function CoffeeShop:StartMerge( player , gridFrom , gridTo ) local curGridFrom = self:GetGrid( player , gridFrom ) local curGridTo = self:GetGrid( player , gridTo ) local currencyType = dataType.CoffeeShopGoodsType_Empty curGridTo.level = curGridTo.level + 1 if 6 == curGridTo.level then --合成了最高等级 for k, v in pairs( player.gameData.shop[ self.shopType ].currency ) do if curGridTo.type == k then player.gameData.shop[ self.shopType ].currency[ k ] = player.gameData.shop[ self.shopType ].currency[ k ] + 1 player.gameData.shop[ self.shopType ].mergeCount = player.gameData.shop[ self.shopType ].mergeCount + 1 currencyType = k break end end self:SetFreeGrid( player , gridFrom ) self:SetFreeGrid( player , gridTo ) else self:SetFreeGrid( player , gridFrom ) end return currencyType end --移动格子 function CoffeeShop:MoveGrid( player , gridFrom , gridTo ) local curGridFrom = self:GetGrid( player , gridFrom ) local curGridTo = self:GetGrid( player , gridTo ) curGridTo.type = curGridFrom.type curGridTo.level = curGridFrom.level --清空原格子 self:SetFreeGrid( player , gridFrom ) end --交换格子 function CoffeeShop:ExchangeGrid( player , gridFrom , gridTo ) local curGridFrom = self:GetGrid( player , gridFrom ) local curGridTo = self:GetGrid( player , gridTo ) curGridTo.type , curGridFrom.type = curGridFrom.type , curGridTo.type curGridTo.level , curGridFrom.level = curGridFrom.level , curGridTo.level end --获取供应端信息 function CoffeeShop:GetSupplierBoxes( player , type ) for k, v in pairs( player.gameData.shop[ self.shopType ].supplierBoxes ) do if type == v.type then --如果CD时间到了,就给他充满 if skynet.GetTime() > v.nextRefreshTime then local cfgCoffeeMakingPara = skynet.server.gameConfig.SValue.coffeeMakingPara v.count = cfgCoffeeMakingPara[1] end return v end end return nil end --获取当前货币 function CoffeeShop:GetCurrency( player ) local curShop = player.gameData.shop[ self.shopType ] local currency = {} currency.coffeeBean = curShop.currency[1] currency.sugar = curShop.currency[2] currency.milk = curShop.currency[3] return currency end function CoffeeShop:GetGrid( player , gridIndex ) for k, v in pairs( player.gameData.shop[ self.shopType ].grids ) do if gridIndex == v.index then return v end end end --是否有效的补充盒 function CoffeeShop:IsVaildSupplierBoxes( type ) if nil == type or type < dataType.CoffeeShopGoodsType_CoffeeBean or type > dataType.CoffeeShopGoodsType_Milk then return false end return true end --是否有效的格子ID function CoffeeShop:IsVaildGridID( id ) if nil == id or id < 1 and id >= 30 then return false end return true end skynet.server.coffeeShop = CoffeeShop return CoffeeShop