Untitled

 avatar
unknown
lua
4 years ago
6.4 kB
6
Indexable
--[[ -- ADD em login.lua
    -- Vip Checker
    player:loadVipData()
    player:updateVipTime()
    if player:isVip() then
        player:sendVipEffect()
    end
    ]]

VIP_SYSTEM_CONFIG = {
    expBonus = 1.1, -- Exp Bonus para quem é Vip em %. (1.1 = 10%)
    skillsBonus = 1.1, -- Skill Bonus para quem é Vip em %. (1.1 = 10%, 1.5 = 50%, 2 = 100% double.)
    lootBonus = 1.2, -- Loot bonus para quem é Vip em %. (1.1 = 10%, 1.5 = 50%, 2 = 100% double.)
    trainDummyInFight = true, -- Treinar em dummy mesmo em fight.
    expirationTeleport = true, -- Ao terminar a Vip, o jogador deve ser teleportado?
    expirationPosition = Position(32369, 32234, 7), -- Posição para onde o jogador será teleportado ao finalizar a vip.
    expirationMessage = "Sua vip acabou.", -- Msg ao acabar sua vip.
    slotsAutoLoot = 25, -- Slots+ Autoloot.
    vipRegenStamina = 1.1, -- Porcentagem de stamina a mais que irá recuperar em comparação a quem é free. (1.1 = 10%, 1.5 = 50%, 2 = 100% double.)
    useEffect = true, -- Ativar ou desativar o uso de aura effect para vips.
    sendEffect = CONST_ME_MAGIC_POWDER, -- Efeito para quem é vip.
    addonsVip = {
        [1] = {male = 1371, female = 1372}, -- Rascoohan
        --[2] = {male = 128, female = 136}, -- Citizen
    },
    mountsVip = {
        [1] = {id = 173}, -- Mossmasher
        --[2] = {id = 2}, -- Racing bird
    },
    vipData ={ -- Cache, não mexa aqui.
        --VipData[self:getId()] = {days = 0, lastDay = 0}
    },  
}


if not VIP_SYSTEM_CONFIG.vipData then
    VIP_SYSTEM_CONFIG.vipData = {}
end

local function vipEffect(playerid)
    local player = Player(playerid)
    if not player then
        return
    end

    player:getPosition():sendMagicEffect(VIP_SYSTEM_CONFIG.sendEffect)
    addEvent(vipEffect, 2000, playerid)
end

function Player.onRemoveVip(self)
    if VIP_SYSTEM_CONFIG.expirationTeleport then
        self:teleportTo(VIP_SYSTEM_CONFIG.expirationPosition)
        self:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
    end

    self:sendTextMessage(MESSAGE_EVENT_ADVANCE, VIP_SYSTEM_CONFIG.expirationMessage)
end

function Player.getVipDays(self)
    return VIP_SYSTEM_CONFIG.vipData[self:getId()].days
end

function Player.getLastVipDay(self)
    return VIP_SYSTEM_CONFIG.vipData[self:getId()].lastDay
end

function Player.isVip(self)
    return self:getVipDays() > 0
end

function Player.sendVipEffect(self)
    return vipEffect(self:getId())
end

function Player.addVipAddons(self)
    for i = 1, #VIP_SYSTEM_CONFIG.addonsVip do
        self:addOutfitAddon(VIP_SYSTEM_CONFIG.addonsVip[i].male, 3)
        self:addOutfitAddon(VIP_SYSTEM_CONFIG.addonsVip[i].female, 3)
        self:addMount(VIP_SYSTEM_CONFIG.mountsVip[i].id)
    end
end

function Player.addInfiniteVip(self)
    local data = VIP_SYSTEM_CONFIG.vipData[self:getId()]
    data.days = 0xFFFF
    data.lastDay = 0

    db.query(string.format('UPDATE `accounts` SET `vipdays` = %i, `viplastday` = %i WHERE `id` = %i;', 0xFFFF, 0, self:getAccountId()))
end

function Player.addVipDays(self, amount)
    local data = VIP_SYSTEM_CONFIG.vipData[self:getId()]
    local amount = math.min(0xFFFE - data.days, amount)
    if amount > 0 then
        if data.days == 0 then
            local time = os.time()
            db.query(string.format('UPDATE `accounts` SET `vipdays` = `vipdays` + %i, `viplastday` = %i WHERE `id` = %i;', amount, time, self:getAccountId()))
            data.lastDay = time
        else
            db.query(string.format('UPDATE `accounts` SET `vipdays` = `vipdays` + %i WHERE `id` = %i;', amount, self:getAccountId()))
        end
        data.days = data.days + amount
    end

    return true
end

function Player.removeVipDays(self, amount)
    local data = VIP_SYSTEM_CONFIG.vipData[self:getId()]
    if data.days == 0xFFFF then
        return false
    end

    local amount = math.min(data.days, amount)
    if amount > 0 then
        db.query(string.format('UPDATE `accounts` SET `vipdays` = `vipdays` - %i WHERE `id` = %i;', amount, self:getAccountId()))
        data.days = data.days - amount

        if data.days == 0 then
            self:onRemoveVip()
        end
    end

    return true
end

function Player.removeVip(self)
    local data = VIP_SYSTEM_CONFIG.vipData[self:getId()]
    if data.days == 0 then
        return
    end

    data.days = 0
    data.lastDay = 0

    self:onRemoveVip()

    db.query(string.format('UPDATE `accounts` SET `vipdays` = 0, `viplastday` = 0 WHERE `id` = %i;', self:getAccountId()))
end

function Player.loadVipData(self)
    local resultId = db.storeQuery(string.format('SELECT `vipdays`, `viplastday` FROM `accounts` WHERE `id` = %i;', self:getAccountId()))
    if resultId then
        VIP_SYSTEM_CONFIG.vipData[self:getId()] = {
            days = result.getDataInt(resultId, 'vipdays'),
            lastDay = result.getDataInt(resultId, 'viplastday')
        }

        result.free(resultId)
        return true
    end

    VIP_SYSTEM_CONFIG.vipData[self:getId()] = {days = 0, lastDay = 0}
    return false
end

function Player.updateVipTime(self)
    local save = false

    local data = VIP_SYSTEM_CONFIG.vipData[self:getId()]
    local days, lastDay = data.days, data.lastDay
    local daysBefore = days
    if days == 0 or days == 0xFFFF then
        if lastDay ~= 0 then
            lastDay = 0
            save = true
        end
    elseif lastDay == 0 then
        lastDay = os.time()
        save = true
    else
        local time = os.time()
        local elapsedDays = math.floor((time - lastDay) / 86400)
        if elapsedDays > 0 then
            if elapsedDays >= days then
                days = 0
                lastDay = 0
            else
                days = days - elapsedDays
                lastDay = time - ((time - lastDay) % 86400)
            end
            save = true
        end
    end

    if save then
        if daysBefore > 0 and days == 0 then
            self:onRemoveVip()
        end

        db.query(string.format('UPDATE `accounts` SET `vipdays` = %i, `viplastday` = %i WHERE `id` = %i;', days, lastDay, self:getAccountId()))
        data.days = days
        data.lastDay = lastDay
    end
end
Editor is loading...