Store.lua 9.31 KB
-- 商店数据

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

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

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}
    privCardEx = {"number", 0},          -- 特权卡过期时间戳
    getMailT1 = {"number",0},       -- 上次发送月卡福利邮件的时间
    getMailT2 = {"number",0},       -- 上次发送超级月卡福利邮件的时间
}

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()
end

-- 删除过期商品
function Store:deleteExpireLimitGoods()
    local timeNow = skynet.timex()
    local limitGoodsList = self:getProperty("limitTPack")
    for k, v in pairs(limitGoodsList) do
        if timeNow > v then
            limitGoodsList[k] = nil
        end
    end
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:onBuyCard(type, duration)
    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})
    end
end

--检测购买是否超过限制数量
function Store:checkRechargeRecord(limit, id)
    local rechargeRecord = self:getProperty("payR")
    if limit ~= 0 and limit <= (rechargeRecord[id] or 0) then
        skynet.error(string.format("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
        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
        end
    end
    self:updateProperty({field = "buyR", value = buyRecord})
end

function Store:data()
	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"),
	}
end

return Store