• 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 highscore not repeat

darkmu

Well-Known Member
Joined
Aug 26, 2007
Messages
274
Solutions
1
Reaction score
50
Location
Paraná,Brazil
I am developing a highscore but I would like to know how I make it so that the players return without repeating the same thing.

And I would also like to put the names of the players, you think that making an inner join in a query is very bad, what would be the best way?

TFS 1.3

Lua:
function onSay(cid, words, param)
    local player = Player(cid)
  
    local players = 10
    
    local str = ""
    local player_id = 0
          
    str = "#                        [Respect Points]                       #"   
    local resultId = db.storeQuery("SELECT `recruiter` FROM `accounts`  WHERE `recruiter` > 0 ORDER BY `recruiter` DESC;")
        
    if resultId then
        local i = 1
        while TRUE do
            if i > players then
                break
            end
            local points = result.getDataInt(resultId, 'recruiter')
          
            str = str .. "\n " .. i .. ". [" .. points .. "] - "
            
            i = i+1
        end
        result:free()
    end
    
    if str ~= "" then
        doPlayerPopupFYI(player, str)
    end
    
    return TRUE
end


In this case, he was supposed to introduce only one person, because there is only one person on the server.
1607231739339.png
 
Solution
You want to display 10 rows
local players = 10

You start at row 1
local i = 1

You have a forever loop (while true) that breaks after 10 rows.
Lua:
while TRUE do
    if i > players then
        break
    end
    local points = result.getDataInt(resultId, 'recruiter')
 
    str = str .. "\n " .. i .. ". [" .. points .. "] - "
   
    i = i+1
end

I think this is a pretty bad way to approach looping through SQL results. The method I use for shop system on Znote AAC is this:

Lua:
local orderQuery = db.storeQuery([[
    SELECT
        MIN(`po`.`player_id`) AS `player_id`,
        `shop`.`id`,
        `shop`.`type`,
        `shop`.`itemid`,
        `shop`.`count`
    FROM `players_online` AS `po`
    INNER JOIN `players` AS `p`...
You want to display 10 rows
local players = 10

You start at row 1
local i = 1

You have a forever loop (while true) that breaks after 10 rows.
Lua:
while TRUE do
    if i > players then
        break
    end
    local points = result.getDataInt(resultId, 'recruiter')
 
    str = str .. "\n " .. i .. ". [" .. points .. "] - "
   
    i = i+1
end

I think this is a pretty bad way to approach looping through SQL results. The method I use for shop system on Znote AAC is this:

Lua:
local orderQuery = db.storeQuery([[
    SELECT
        MIN(`po`.`player_id`) AS `player_id`,
        `shop`.`id`,
        `shop`.`type`,
        `shop`.`itemid`,
        `shop`.`count`
    FROM `players_online` AS `po`
    INNER JOIN `players` AS `p`
        ON `po`.`player_id` = `p`.`id`
    INNER JOIN `znote_shop_orders` AS `shop`
        ON `p`.`account_id` = `shop`.`account_id`
    WHERE `shop`.`type` IN(]] .. table.concat({1,5,7}, ",") .. [[)
    GROUP BY `shop`.`id`
]])

-- Detect if we got any results
if orderQuery ~= false then
    repeat
        local player_id = result.getNumber(orderQuery, 'player_id')
        local orderId = result.getNumber(orderQuery, 'id')
        local orderType = result.getNumber(orderQuery, 'type')
        local orderItemId = result.getNumber(orderQuery, 'itemid')
        local orderCount = result.getNumber(orderQuery, 'count')
        local player = Player(player_id)

        -- handle row logic
       
    until not result.next(orderQuery)
    result.free(orderQuery)
end

If you want top 10 rows, just add LIMIT 10 at the end of the SQL query.
And yeah, using joins is usually fine as long as it links through PK/FK relations or a custom index.

You want to display player information, but you are grabbing info from accounts table. If you want to join in players, you will likely get the entire character list of the account that has most "respect points" at the top. (since its stored in accounts).
Perhaps you should determine the "main character" and only join in the player from the account with the highest experience.
While this can be done with a mysql windowed function such as row_number, it might be a bit costly. I think I would add an extra column in accounts table that reference the player_id of the main character, and use that in the join to quickly grab character information, without grabbing the entire character list.
 
Last edited:
Solution
Back
Top