• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua Chest to receive season coin from season_ranking

ralke

(҂ ͠❛ ෴ ͡❛)ᕤ
Joined
Dec 17, 2011
Messages
1,470
Solutions
27
Reaction score
844
Location
Santiago - Chile
GitHub
ralke23
Twitch
ralke23
Hi there! I'm using this system for season rankings AAC - Create season rank based on highscores (https://otland.net/threads/create-season-rank-based-on-highscores.282083/#post-2703996) and I would like to know if someone is up to make a script for it :).

I need to make a chest, that if is opened by a player that has season_rank, it will receive an ammount of "season coin" (with itemid) based on his ranking.
This way, it should calculate with a formula:

  • if the season ranking is 1, give 200 season coin
  • if season ranking is 2, receive 199 season coin
  • if season ranking is 3, receive 198 season coin
  • if season ranking is 200, receive 1 season coin

and so on...

If the season ranking is greater than 200, receive a POFF effect and a message that says you don't have enough ranking or something.
If there also could be a chance to quickly modify the raking/coin ratio inside the script would be great.

Thanks in advance!
Regards :)
 
Solution
this should get you started.

Lua:
local rankRewards = Action()

function rankRewards.onUse(player, item, fromPosition, target, toPosition, isHotkey)

    local rank = getRankValue() -- however you want to get the rank from database..

    if rank > 200 then
        -- not enough rank
        return true
    end
    
    local rewardAmount = 201 - rank
    local rewardItemId = 111111
    player:addItem(rewardItemId, rewardAmount)
    
    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You've received " .. rewardAmount .. " reward_item_name for being rank " .. rank .. ".")
    return true
end

rankRewards:aid(12345)
rankRewards:register()
this should get you started.

Lua:
local rankRewards = Action()

function rankRewards.onUse(player, item, fromPosition, target, toPosition, isHotkey)

    local rank = getRankValue() -- however you want to get the rank from database..

    if rank > 200 then
        -- not enough rank
        return true
    end
    
    local rewardAmount = 201 - rank
    local rewardItemId = 111111
    player:addItem(rewardItemId, rewardAmount)
    
    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You've received " .. rewardAmount .. " reward_item_name for being rank " .. rank .. ".")
    return true
end

rankRewards:aid(12345)
rankRewards:register()
 
Solution
@Xikini thanks bro! I was thinking on adding something like this to player libs
Lua:
function Player.getRankValue(self)
    local query = db.storeQuery("SELECT 'rank_level FROM 'season_archive' WHERE `season_id` = 1" .. self:getGuid())
    local rank= result.getDataInt(query, 'rank_level ') or 0
    result.free(query)
    return rank
end

But I don't know a few things:
  • Is player lib where this should be added?
  • Is the lua syntax correct?
  • I have to mark WHERE season_id = 1 to only let players from season 1 use the chest?
  • Is SELECT 'rank_level FROM 'season_archive' the correct pathfinding?
Any feedback or correction to this would be appreaciated a lot!
Thanks in advance :)
 
Solved, thanks @Alpha
Lua:
local storageKey = 123123
local rewardId = 13186
local maximumRank = 200

local rankRewards = Action()
function rankRewards.onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if player:getStorageValue(storageKey) ~= -1 then
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You have already claimed your season reward.")
        return true
    end

    local rank = player:getRankValue()
    if not rank or rank > maximumRank then
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You did not qualify for a season reward.")
        return true
    end
    
    local rewards = {}
    local amount = 201 - rank
    local remainder = amount
    repeat
        local stackSize = math.min(remainder, 100)
        local reward =  Game.createItem(rewardId, stackSize)
        if not reward then
            return true
        end

        table.insert(rewards, reward)
        remainder = remainder - stackSize
    until remainder == 0

    for _, reward in pairs(rewards) do
        local returnValue = player:addItemEx(reward, false)
        if returnValue ~= RETURNVALUE_NOERROR then
            player:sendTextMessage(MESSAGE_INFO_DESCR, Game.getReturnMessage(returnValue))

            -- if adding to player fails, remove all previously added rewards
            for _, staleReward in pairs(rewards) do
                staleReward:remove()
            end
            return true
        end
    end
    
    local itemType = ItemType(rewardId)
    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, ("You have received %d %s for being rank %d."):format(amount, amount == 1 and itemType:getName() or itemType:getPluralName(), rank))
    player:setStorageValue(storageKey, 1)
    return true
end

rankRewards:aid(19038)
rankRewards:register()

On player lib
Lua:
function Player.getRankValue(self)
    local query = db.storeQuery(("SELECT `rank_level` FROM `season_archive` WHERE `season_id` = 1 AND `player_id` = %d LIMIT 1"):format(self:getGuid()))
    if not query then
        return
    end

    local rank = result.getNumber(query, "rank_level")
    result.free(query)
    return rank
end
 
Last edited:
Solved, thanks @Alpha
Lua:
local rewardId = 13186
local maximumRank = 200

local rankRewards = Action()
function rankRewards.onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local rank = player:getRankValue()
    if not rank or rank > maximumRank then
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You did not qualify for a season reward.")
        return true
    end
   
    local rewards = {}
    local amount = 201 - rank
    local remainder = amount
    repeat
        local stackSize = math.min(remainder, 100)
        local reward =  Game.createItem(rewardId, stackSize)
        if not reward then
            return true
        end

        table.insert(rewards, reward)
        remainder = remainder - stackSize
    until remainder == 0

    for _, reward in pairs(rewards) do
        local returnValue = player:addItemEx(reward, false)
        if returnValue ~= RETURNVALUE_NOERROR then
            player:sendTextMessage(MESSAGE_INFO_DESCR, Game.getReturnMessage(returnValue))

            -- if adding to player fails, remove all previously added rewards
            for _, staleReward in pairs(rewards) do
                staleReward:remove()
            end
            return true
        end
    end
   
    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, ("You have received %d %s for being rank %d."):format(amount, amount == 1 and item:getName() or item:getPluralName(), rank))
    return true
end

rankRewards:aid(19038)
rankRewards:register()

On player lib
Lua:
function Player.getRankValue(self)
    local query = db.storeQuery(("SELECT `rank_level` FROM `season_archive` WHERE `season_id` = 1 AND `player_id` = %d LIMIT 1"):format(self:getGuid()))
    if not query then
        return
    end

    local rank = result.getNumber(query, "rank_level")
    result.free(query)
    return rank
end

Thanks for sharing; will give this a try!
 
Back
Top