Store.lua 16.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
-- 商店数据

local Store = class("Store", require("shared.ModelBase"))

function Store:ctor(properties)
	Store.super.ctor(self, properties)
end

ActGoodsType = {
    paySignIn = 1,  -- 付费签到
}

Store.schema = {
	buyR		= {"table", {}},		-- 购买商品记录 {id=count}
	payR	= {"table", {}},		-- 充值记录 {id=count}
	growFund		= {"number", 0},		-- 成长基金
    growFundR  = {"string", ""},        -- 成长基金领取记录

	monthCardEx	= {"number", 0},		-- 月卡过期时间戳
    smonthCardEx	= {"number", 0},		-- 超级月卡过期时间戳

	battleCard	= {"number", 0},		-- 赛季卡
	battleFR = {"string", ""},		-- 免费赛季卡领取记录
    battleLR = {"string", ""},		-- 付费赛季卡领取记录

    limitTPack = {"table", {}},      -- 限时礼包 {id={expire_ts, trigger_type}}
    privCardEx = {"number", 0},          -- 特权卡过期时间戳
    getMailT1 = {"number",0},       -- 上次发送月卡福利邮件的时间
    getMailT2 = {"number",0},       -- 上次发送超级月卡福利邮件的时间
    --packTrigger = {"table", {}},     -- 礼包触发记录 {关卡难度1={id, 通关关卡数,升级数,爬塔层数}, ...}
    
    -- 活动商品购买记录
    actGoodsFlag = {"table", {}},       -- ActGoodsType  1购买,0未购买

    bpInfo = {"table", {}},         -- battle pass 探索指令  1={flag=0 为1表示买了,br=""付费领取记录, fr=""免费领取记录},2,3,4
    
    totalRR  = {"string", ""},        -- 累计充值奖励领取记录
}

function Store:updateProperty(params)
	params = params or {}
	if not self.schema[params.field] then
		return
	end
	local oldValue = self:getProperty(params.field)
	if params.value then
		self:setProperty(params.field, params.value)
	elseif params.delta then
		self:incrProperty(params.field, params.delta)
	else
		return
	end
	local newValue = self:getProperty(params.field)
	if not params.notNotify then
		self:notifyUpdateProperty(params.field, newValue, oldValue)
	end
end

function Store:onCrossDay()
    self:sendMonthCardEmail()
    self:deleteExpireLimitGoods()
    --self:checkPaySignReward()
end

-- 删除过期商品
function Store:deleteExpireLimitGoods()
    local timeNow = skynet.timex()
    local limitGoodsList = self:getProperty("limitTPack")
    for k, v in pairs(limitGoodsList) do
        if timeNow > v[1] then
            limitGoodsList[k] = nil
        end
    end
    self:updateProperty({field = "limitTPack", value = limitGoodsList, notNotify = true})
end

-- 发送月卡邮件
function Store:sendMonthCardEmail()
    local timeNow = skynet.timex()
    local tabs = {{ex="monthCardEx", t="getMailT1", mail=MailId.MonthCard, alert=MailId.MonthCardEx},
    {ex="smonthCardEx", t="getMailT2", mail=MailId.SuperMonthCard, alert=MailId.SuperMonthCardEx}}
    for _, v in ipairs(tabs) do
        local ex  = self:getProperty(v.ex)
        local ts  = self:getProperty(v.t) or 0
        local mailId = v.mail
        local alertId = v.alert
        local alertTs = dayLater(ex) - DAY_SEC
        if ex > timeNow then
            local cnt = 0
            if ts == 0 then
                cnt = 1
            else
                local diff = math.floor((dayLater(timeNow) - dayLater(ts))/DAY_SEC)
                diff = diff < 0 and 0 or diff
                diff = diff > 31 and 31 or diff
                cnt = diff
            end
            for i = cnt - 1, 0, -1  do
                local createTs = timeNow - i * DAY_SEC
                self.owner:sendMail(mailId, createTs)
                -- 过期头一天发提醒邮件
                if dayLater(createTs) == alertTs then
                    self.owner:sendMail(alertId, createTs)
                end
            end
            if cnt > 0 then
                self:updateProperty({field=v.t, value=timeNow})
            end
        end
    end
end

function Store:isMonthCardExpire()
    local timeNow = skynet.timex()
    local ts = self:getProperty("monthCardEx")
    return ts < timeNow
end

function Store:isSuperMonthCardExpire()
    local timeNow = skynet.timex()
    local ts = self:getProperty("smonthCardEx")
    return ts < timeNow
end

function Store:isPrivCardExpire()
    local timeNow = skynet.timex()
    local ts = self:getProperty("privCardEx")
    return ts < timeNow
end

-- 挂机栏位 特权卡额外个数
function Store:getHangSlotExtraCount()
    if self:isPrivCardExpire() then
        return 0
    end

    return globalCsv.shop_priv_card_hang_slot_cnt or 4
end

-- 探索加速/餐厅加速 特权卡系数
function Store:getProduceItemSpeedCoef()
    if self:isPrivCardExpire() then
        return 1
    end
    local coef = (globalCsv.shop_priv_card_produce_coef or 25)/ 100
    return 1 + coef
end

-- 拆解室栏位 特权卡额外个数
function Store:getTimeBoxSlotExtraCount()
    if self:isPrivCardExpire() then
        return 0
    end

    return globalCsv.shop_priv_time_box_slot_cnt or 3
end

-- 齿轮兑换 特权卡系数
function Store:getGearExchangeCoef()
    if self:isPrivCardExpire() then
        return 1
    end

    local coef = (globalCsv.shop_priv_exchange_gear_coef or 50)/ 100
    return 1 + coef
end

-- 奖励关卡 特权卡额外数量
function Store:getBonusExtraFightCount()
    if self:isPrivCardExpire() then
        return 0
    end

    local cnt = globalCsv.bonus_extra_fight_count or 1
    return cnt
end

-- 挂机道具掉落系数 特权卡挂机掉落系数
function Store:getHangDropCoef()
    if self:isPrivCardExpire() then
        return 1, 1
    end

    return (1 + globalCsv.hang_drop_exp_coef) or 1, (1 + globalCsv.hang_drop_item_coef) or 1
end


-- 购买通行证
function Store:onBuyCard(type, duration, id, actid)
    local timeNow = skynet.timex()
    if type == CardType.NormalMonthCard then
        if self:isMonthCardExpire() then
            self:updateProperty({field = "monthCardEx", value = timeNow + duration})
        else
            self:updateProperty({field = "monthCardEx", value = self:getProperty("monthCardEx") + duration})
        end
        self:sendMonthCardEmail()
    elseif type == CardType.SuperMonthCard then
        if self:isSuperMonthCardExpire() then
            self:updateProperty({field = "smonthCardEx", value = timeNow + duration})
        else
            self:updateProperty({field = "smonthCardEx", value = self:getProperty("smonthCardEx") + duration})
        end
        self:sendMonthCardEmail()
    elseif type == CardType.PrivilegeCard then
        if self:isPrivCardExpire() then
            self:updateProperty({field = "privCardEx", value = timeNow + duration})
        else
            self:updateProperty({field = "privCardEx", value = self:getProperty("privCardEx") + duration})
        end
    elseif type == CardType.GrowFund then
        self:updateProperty({field = "growFund", value = 1})
    elseif type == CardType.BattleCard then
        self:updateProperty({field = "battleCard", value = 1})
    elseif type == CardType.PaySignCard then
        self:onBuyPaySignCard(duration)
    elseif type == CardType.BattlePassCard then
        local index = id - 400          -- 401:初级 402:中级 403:高级 404:终极
        local bpInfo = self:getProperty("bpInfo") or {}
        local info = bpInfo[index] or {}
        info["flag"] = 1
        bpInfo[index] = info
        self:updateProperty({field = "bpInfo", value = bpInfo})
    elseif type == CardType.ActBattleCommandCard then
        if not self.owner.activity:isOpenById(actid, "BattleCommand") then
            return
        end
        local actCfg = csvdb["activity_ctrlCsv"][actid]
        if not actCfg then return end
        local actData = self.owner.activity:getActData("BattleCommand") or {}
        if actData["unlock"] == 1 then return end
        actData["unlock"] = 1
        if actCfg.condition ~= 0 then
            actData["lvl"] = (actData["lvl"] or 0) + actCfg.condition
        end
    end
end

--检测购买是否超过限制数量
function Store:checkRechargeRecord(limit, id)
    local rechargeRecord = self:getProperty("payR") or {}
    if limit ~= 0 and limit <= (rechargeRecord[id] or 0) then
        skynet.error(string.format("[recharge] recharge id:%d count over limit, user id:%d", id, self.owner:getProperty("id")))
        return false
    end
    rechargeRecord[id] = (rechargeRecord[id] or 0) + 1
    self:updateProperty({field = "payR", value = rechargeRecord})
    return true
end

function Store:notifyUpdateProperty(field, newValue, oldValue)
	local datas = {
		key = field,
		newValue = newValue,
		oldValue = oldValue,
	}
	SendPacket(actionCodes.Store_updateproperty, MsgPack.pack(datas))
end

-- 赛季卡重置 需要把未能领取的奖励通过邮件发送
function Store:onBattleCardReset()
    local gift = {}
    local function concatGift(data)
        for key, v in pairs(data:toNumMap()) do
            gift[key] = (gift[key] or 0) + v
        end
    end
    local battleCardFlag = self:getProperty("battleCard")
    local battleCardFreeRecord = self:getProperty("battleFR")
    local battleCardLimitRecord = self:getProperty("battleLR")
    local battlePoint = self.owner:getProperty("battlePoint")

    for id, config in pairs(csvdb["reward_battlepassCsv"]) do
        if config then
            local freeFlag = string.char(string.getbit(battleCardFreeRecord, id))
            local limitFlag = string.char(string.getbit(battleCardLimitRecord, id))

            if battlePoint < config.point then
                break
            end
            if freeFlag == "0" then
                concatGift(config.giftFree)
            end
            if limitFlag == "0" and battleCardFlag == 1 then
                concatGift(config.giftLimit)
            end
        end
    end
    self.owner:sendMail(MailId.BattleCardAward, nil, gift)
    -- 计算剩余奖励
    self:updateProperty({field = "battleCard", value=0})
    self:updateProperty({field = "battleFR", value=""})
    self:updateProperty({field = "battleLR", value=""})
    self.owner:updateProperty({field = "battlePoint", value=0})
end

-- 重置购买记录
function Store:resetStoreReored(resetId)
    local payRecord = self:getProperty("payR") or {}
    local buyRecord = self:getProperty("buyR") or {}
    for k, v in pairs(payRecord) do
        local config = csvdb["shop_rechargeCsv"][k]
        if config then
            if config.resetTime == resetId then
                payRecord[k] = nil
                --通行证商店
                if config.shop == 2 then
                    if config.type == CardType.BattleCard then
                        self:onBattleCardReset()
                    end
                end
            end
        else
            payRecord[k] = nil
        end
    end
    self:updateProperty({field = "payR", value = payRecord})
    for k, v in pairs(buyRecord) do
        local config = csvdb["shop_normalCsv"][k]
        if config then
            if config.resetTime == resetId then
                buyRecord[k] = nil
            end
        else
            buyRecord[k] = nil
        end
    end
    self:updateProperty({field = "buyR", value = buyRecord})
end

--触发限时礼包
function Store:OnTriggerLimitTimePack(eventType, param)
    local limitPack = self:getProperty("limitTPack")
    --local payRecord = self:getProperty("payR")
    local timeNow = skynet.timex()
    --local find = false
    -- 有未过期的限时礼包不再推送
    --for k, v in pairs(limitPack) do
    --    if v > timeNow and not payRecord[k] then
    --        find = true
    --        break
    --    end
    --end
    --if find == true then
    --    return
    --end
    --local hangPass = self.owner:getProperty("hangPass")
    --local triggerRecord = self:getProperty("packTrigger")
    --local result = {}
    --local maxDiff = 0
    -- 取满足限时礼包关卡要求的对应数据
    --for diff, maxCarbonId in pairs(hangPass) do
    --    for id, cfg in pairs(csvdb["shop_packCsv"]) do
    --        local range = cfg.showRange:toArray(true, "=")
    --        local beginRange = range[1] or 0
    --        local endRange = range[2] or 0
    --        if maxCarbonId > beginRange and maxCarbonId <= endRange and cfg.type == eventType then
    --            result[diff] = cfg
    --            maxDiff = math.max(maxDiff, diff)
    --            break
    --        end
    --    end
    --end
    local config = nil
    for id, cfg in pairs(csvdb["shop_packCsv"]) do
        if cfg.type == eventType and cfg.condition == param then
            config = cfg
        end
    end
    if config ~= nil then
        local rechargeCfg = csvdb["shop_rechargeCsv"][config.packId]
        if rechargeCfg then
            limitPack[rechargeCfg.id] = {timeNow + rechargeCfg.time, config.id}
            self.owner:log("push_gift", {
                gift_id = rechargeCfg.id, --礼包ID
                gift_name = rechargeCfg.title,   --礼包名称
            })
            self:updateProperty({field = "limitTPack", value = limitPack})
        end
    end
    --if next(result) then
    --    self:updateProperty({field = "packTrigger", value = triggerRecord})
    --end
end

function GetActGoodsIndex(goodsType)
    return ActGoodsType[goodsType] or 0
end

function Store:SetActGoodsFlag(goodsType, flag)
    local actGoodsFlag = self:getProperty("actGoodsFlag") or {}
    local goodsIndex = GetActGoodsIndex(goodsType)
    if goodsIndex == 0 then
        print("get act goods index fail :paySignIn")
        return
    end
    actGoodsFlag[goodsIndex] = flag
    self:updateProperty({field = "actGoodsFlag", value = actGoodsFlag})
end

-- 购买付费签到 按开服时间算奖励
function Store:onBuyPaySignCard(dur)
    local curTs = skynet.timex()
	curTs = getServerOpenTs()

    self:SetActGoodsFlag("paySignIn", curTs)
    local actData = self.owner.activity:getActData("PaySignIn")
    actData[0] = 1
    self.owner.activity:updateActData("PaySignIn", actData)

    --local actGoodsFlag = self:getProperty("actGoodsFlag") or {}
    --local goodsIndex = GetActGoodsIndex("paySignIn")
    --if goodsIndex == 0 then
    --    print("get act goods index fail :paySignIn")
    --    return
    --end
    --actGoodsFlag[goodsIndex] = 1
    --self:updateProperty({field = "actGoodsFlag", value = actGoodsFlag})
    -- 发钱
    --local change
    --local reward, curData = self.owner.activity:getPaySignReward()
	--if next(reward) then
    --    self.owner.activity:updateActData("PaySignIn", curData)
    --    reward, change = self.owner:award(reward, {log = {desc = "actPaySign"}})
	--end

	--self.owner:log("activity", {
	--	activity_id = 0, -- 活动ID(或活动指定任务的ID)
	--	activity_type = self.owner.activity.ActivityType.PaySignIn, -- 活动类型,见活动类型枚举表
	--	activity_reward = reward, -- 活动奖励,json格式记录,{'itemid1':123,'itemid2':456,………...}
	--})

	--SendPacket(actionCodes.Activity_actPaySignRewardNtf, MsgPack.pack(self.owner:packReward(reward, change)))
end

function Store:checkPaySignReward()
    local reward, curData  = self.owner.activity:getPaySignReward()
    if next(reward) then
        self.owner.activity:updateActData("PaySignIn", curData)
        self.owner:sendMail(MailId.PaySignAward, nil, reward)
	end
end

-- 临时用 登录触发限时礼包 时间点以后
function Store:OnTriggerLimitTimePackAfterTs(eventType, param)
    local limitPack = self:getProperty("limitTPack")
    local timeNow = skynet.timex()
    local config = nil
    for id, cfg in pairs(csvdb["shop_packCsv"]) do
        local ts = toUnixtime(""..cfg.condition)
        if cfg.type == eventType and ts < param then
            config = cfg

            if config ~= nil then
                local rechargeCfg = csvdb["shop_rechargeCsv"][config.packId]
                if rechargeCfg then
                    if not limitPack[rechargeCfg.id] then
                        limitPack[rechargeCfg.id] = {timeNow + rechargeCfg.time, config.id}
                        self.owner:log("push_gift", {
                            gift_id = rechargeCfg.id, --礼包ID
                            gift_name = rechargeCfg.title,   --礼包名称
                        })
                        self:updateProperty({field = "limitTPack", value = limitPack, notNotify = false})
                    end
                end
            end
        end
    end
end

function Store:data()
    self:OnTriggerLimitTimePackAfterTs(TriggerEventType.AfterTs, skynet.timex())
	return {
        buyR		= self:getProperty("buyR"),
        payR	= self:getProperty("payR"),
        growFund		= self:getProperty("growFund"),
        growFundR  = self:getProperty("growFundR"),
        monthCardEx  = self:getProperty("monthCardEx"),
        smonthCardEx  = self:getProperty("smonthCardEx"),
        battleCard  = self:getProperty("battleCard"),
        battleFR  = self:getProperty("battleFR"),
        battleLR  = self:getProperty("battleLR"),
        limitTPack = self:getProperty("limitTPack"),
        privCardEx = self:getProperty("privCardEx"),
        --packTrigger = self:getProperty("packTrigger"),
        actGoodsFlag = self:getProperty("actGoodsFlag"),
        bpInfo = self:getProperty("bpInfo"),
        totalRR = self:getProperty("totalRR"),
	}
end

return Store