diff --git a/src/models/Capsule.lua b/src/models/Capsule.lua index 9020d9d..e4a12b5 100644 --- a/src/models/Capsule.lua +++ b/src/models/Capsule.lua @@ -145,6 +145,56 @@ function Capsule:isShow() return true end +function Capsule:resetTimeOut(curTime, now) + now = now or skynet.timex() + local cur4Time = curTime or (specTime({hour = 4},now)) + + local interval + if cur4Time > now then + interval = cur4Time - now + elseif cur4Time < now then + local nextTime = dayLater(now) + interval = nextTime - now + elseif cur4Time == now then + self:init() + self:create() + + local nextTime = dayLater(now) + interval = nextTime - now + end + interval = (interval + 1)* 100 + skynet.timeout(interval, handler(self,self.resetTimeOut)) +end + +function Capsule:reset4TimeOut(curTime, now) + now = now or skynet.timex() + local interval + if curTime > now then + interval = curTime - now + elseif curTime < now then + local nextTime = dayLater(now) + interval = nextTime - now + elseif curTime == now then + self:init() + self:create() + + return false + end + interval = (interval + 1)* 100 + skynet.timeout(interval, handler(self,self.reset4TimeOut)) +end + +function Capsule:checkTime(resetArr, now) + if resetArr[1] == "3" then + local cur4Time = specTime({hour = 4},now) + if now == cur4Time then return true end + self:resetTimeOut(cur4Time, now) + elseif resetArr[1] == "4" then + if now == resetArr[2] then return true end + self:reset4TimeOut(resetArr[2], now) + end +end + function Capsule:refreshing(now) local id = self:getProperty("id") local room = self:getProperty("room") @@ -173,11 +223,10 @@ function Capsule:refreshing(now) end return false - elseif resetArr[1] == "3" then - - elseif resetArr[1] == "4" then - if now >= resetArr[2] then return true end + else + self:checkTime(resetArr, now) end + end return false @@ -490,17 +539,32 @@ function Capsule:getKing(record,now) local specials = self:getProperty("specials") or {} local specialsRecord = self:getProperty("specialsRecord") or {} + local rank = self:getProperty("rank") or {} + if not next(rank) then return nil end local roleRecord = {} - for _, v in ipairs(record) do - if not roleRecord[v.roleId]then - roleRecord[v.roleId] = {amount = v.amount } - else - roleRecord[v.roleId] = {amount = (roleRecord[v.roleId]["amount"] or 0) + v.amount} - end + + for roleId, count in pairs(rank) do + table.insert(roleRecord, {roleId = roleId, count = count}) + end + table.sort(roleRecord, function(a, b) return a.count > b.count end) + + --tmp[spKey] = {name = name, good_id = spKey, typ = RewardType.SPECIAL, award = award, amount = 1, quality = typ, create_time= now} + + local count = math.min(special["amount"], #roleRecord) + + local rewardByRole = {} + local index = 1 + while (count > 0 ) do + count = count - 1 + local tmp = roleRecord[index] + if not tmp then break end + index = index + 1 + + local name = getNameByRoleId(tmp.roleId) + rewardByRole[tmp.roleId][spKey] = {name = name, good_id = spKey, typ = RewardType.SPECIAL, award = special["award"], amount = 1, SpecialType.KING, create_time= now} end - local rewardByRole, count = getSpecialRoleNotify(roleRecord, special["amount"], special["award"], spKey, SpecialType.KING,now) special["amount"] = count specials[spKey] = special @@ -614,6 +678,8 @@ function Capsule:drawByCount(roleId, count) local goods = self:getProperty("goods") or {} local record = self:getProperty("record") or {} + local rank = self:getProperty("rank") or {} + local rankRole = rank[roleId] or 0 local recordByRole = self:getProperty("recordByRole") or {} local roleRecord = recordByRole[roleId] or {} @@ -636,6 +702,9 @@ function Capsule:drawByCount(roleId, count) if good and good.amount > 0 then good.amount = good.amount - 1 + --插入rank + rankRole = rankRole + 1 + --插入记录 local tmpNotify = {roleId = roleId, name= name, good_id = good_id, typ = RewardType.GOODS, award = good.award, amount = 1, quality = good.quality, create_time= now} table.insert(record, tmpNotify) @@ -659,8 +728,9 @@ function Capsule:drawByCount(roleId, count) end end + rank[roleId] = rankRole recordByRole[roleId] = roleRecord - self:setProperties({recordByRole = recordByRole, record = record, goods = goods}) + self:setProperties({recordByRole = recordByRole, record = record, goods = goods, rank = rank}) local tmpNotify = self:checkIncentive(roleId, name, now) for k, v in pairs(tmpNotify) do @@ -692,6 +762,8 @@ end function Capsule:drawAll(roleId) local goods = self:getProperty("goods") or {} local record = self:getProperty("record") or {} + local rank = self:getProperty("rank") or {} + local rankRole = rank[roleId] or 0 local recordByRole = self:getProperty("recordByRole") or {} local roleRecord = recordByRole[roleId] or {} local now = skynet.timex() @@ -701,6 +773,9 @@ function Capsule:drawAll(roleId) notify[roleId] = {} for good_id, good in pairs(goods) do if good.amount > 0 then + --插入rank + rankRole = rankRole + good.amount + --插入记录 local tmpNotify = {roleId = roleId, name= name, good_id = good_id, typ = RewardType.GOODS, award = good.award, amount = good.amount, quality = good.quality, create_time = now} for i = 1, good.amount do @@ -725,8 +800,9 @@ function Capsule:drawAll(roleId) end end + rank[roleId] = rankRole recordByRole[roleId] = roleRecord - self:setProperties({recordByRole = recordByRole, record = record, goods = goods}) + self:setProperties({recordByRole = recordByRole, record = record, goods = goods, rank = rank}) local tmpNotify = self:checkIncentive(roleId, name, now) for k, v in pairs(tmpNotify) do diff --git a/src/services/capsuled.lua b/src/services/capsuled.lua index eb4b248..7742fd3 100644 --- a/src/services/capsuled.lua +++ b/src/services/capsuled.lua @@ -51,7 +51,9 @@ function getNameByRoleId(roleId) end function broadCastCapsule(roleId, capsuleId, broadInfo) - local capsule = capsules[capsuleId] or {} + local capsule = capsules[capsuleId] + if not capsule then print("not capsule :" .. capsuleId) return end + local register = capsule:getProperty("register") or {} if next(capsule) then for id, _ in pairs(register) do @@ -63,21 +65,22 @@ function broadCastCapsule(roleId, capsuleId, broadInfo) end function broadCastSpecial(roleId, capsuleId, broadInfo) - local capsule = capsules[capsuleId] or {} + local capsule = capsules[capsuleId] + if not capsule then skynet.error("not capsule: " .. capsuleId) return end + local register = capsule:getProperty("register") or {} - if next(capsule) then - for id, _ in pairs(register) do - if id ~= roleId then - if broadInfo[id] then - rpcRole(id, "paySpecialReward", id, broadInfo[id]) - end + for id, _ in pairs(register) do + if id ~= roleId then + if broadInfo[id] then + rpcRole(id, "paySpecialReward", id, broadInfo[id]) end end end end local function add(roleId, capsuleId) - local capsule = capsules[capsuleId] or {} + local capsule = capsules[capsuleId] + if not capsule then skynet.error("not capsule: " .. capsuleId) return end if next(capsule) then capsule:join(roleId) broadCastCapsule(roleId, capsuleId, {notifyType= NotifyChangeType.JOIN, roleId = roleId}) @@ -183,11 +186,11 @@ end function CMD.exit(roleId, capsuleId) if capsuleId then - local capsule = capsules[capsuleId] or {} - if next(capsule) then - capsule:exit(roleId) - broadCastCapsule(roleId, capsuleId, {notifyType = NotifyChangeType.EXIT, roleId = roleId}) - end + local capsule = capsules[capsuleId] + if not capsule then skynet.error("not capsule: " .. capsuleId) return end + + capsule:exit(roleId) + broadCastCapsule(roleId, capsuleId, {notifyType = NotifyChangeType.EXIT, roleId = roleId}) else for _, capsule in pairs(capsules) do if next(capsule) then @@ -199,30 +202,35 @@ function CMD.exit(roleId, capsuleId) end function CMD.draw_capsule(roleId, capsuleId, full, drawsNum, cares) - local capsule = capsules[capsuleId] or {} - if next(capsule) then - local ret, reward, rewardByGoods, notify = capsule:draw(roleId, full, drawsNum, cares) - if ret > 5 then - --broadCastCapsule(roleId, capsuleId, {notifyType = NotifyChangeType.DRAW, notify = notify}) - broadCastSpecial(roleId, capsuleId, notify) - end - return ret, reward, rewardByGoods, capsule:data(roleId) + local capsule = capsules[capsuleId] + if not capsule then skynet.error("not capsule: " .. capsuleId) return 2 end + + local ret, reward, rewardByGoods, notify = capsule:draw(roleId, full, drawsNum, cares) + if ret > 5 then + --broadCastCapsule(roleId, capsuleId, {notifyType = NotifyChangeType.DRAW, notify = notify}) + broadCastSpecial(roleId, capsuleId, notify) end - return 2 + return ret, reward, rewardByGoods, capsule:data(roleId) end function CMD.register(roleId, capsuleId) - local capsule = capsules[capsuleId] or {} + local capsule = capsules[capsuleId] + if not capsule then skynet.error("not capsule: " .. capsuleId) return nil end + return capsule:register(roleId) end function CMD.goods_stock(capsuleId) - local capsule = capsules[capsuleId] or {} + local capsule = capsules[capsuleId] + if not capsule then skynet.error("not capsule: " .. capsuleId) return 0 end + return capsule:getGoodsAmount(), capsule:getProperty("token") end function CMD.capsule_data(roleId, capsuleId) - local capsule = capsules[capsuleId] or {} + local capsule = capsules[capsuleId] + if not capsule then skynet.error("not capsule: " .. capsuleId) return nil end + return capsule:data(roleId) end -- libgit2 0.21.2