• 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!

TFS 1.X+ What`s the best way to reorgazine a table?

roriscrave

Advanced OT User
Joined
Dec 7, 2011
Messages
1,188
Solutions
34
Reaction score
200
My table can have a maximum of 4 numbers.
Lua:
local table = {1000, 880, 720, 600}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if player:getStorageValue(1200) > Some Value in table then
        table[X] = player:getStorageValue(1200)
    end
    return true
end

for example if the player's storage is 900 (it is greater than 880 and less than 1000), then it must enter the table between these values, like it:

newTable ={1000, 900, 880, 720}

and the value 600 leaves the table, since it can only have 4 values, the smallest always leaves, when some other value enters
 
Solution
Lua:
local function customInsert(t, val)
    t[#t+1] = val
    table.sort(t, function(a, b) return a > b end)
    t[5] = nil
end

Example:
Lua:
local t = {65, 2, 568, 20}

customInsert(t, 70)

for k, v in ipairs(t) do
    print(k, v)
end

Result:
Code:
1    568
2    70
3    65
4    20
The value 2 leaves once 70 is inserted.
Lua:
local function customInsert(t, val)
    t[#t+1] = val
    table.sort(t, function(a, b) return a > b end)
    t[5] = nil
end

Example:
Lua:
local t = {65, 2, 568, 20}

customInsert(t, 70)

for k, v in ipairs(t) do
    print(k, v)
end

Result:
Code:
1    568
2    70
3    65
4    20
The value 2 leaves once 70 is inserted.
 
Solution
and if i have a string, is possible to make it?
exemple my table are:
Lua:
local table = {1000, 880, 720, 600}
local pNames = {Smavel, Quartz, Newbie, Louk}

if a player "Noyek", have storage 900, he will be insert in the table and the new tables will be like this:
Lua:
local table = {1000, 900, 880, 720}
local pNames = {Smavel, Noyek, Quartz, Newbie}

Noyek will be inserted in certain position, and the last player (Louk) will leave, because have the slower storage (600)
table is player storage value
pNames is the player who have this storage
 
Lua:
local sortTable = {}

local function customInsert(t, val)
    t[#t+1] = val
    table.sort(t, function(a, b) return a.stg > b.stg end)
    t[5] = nil
end

local p1 = {name = "lowest", stg = 200}
local p2 = {name = "highest", stg = 800}
local p3 = {name = "secondLowest", stg = 500}
local p4 = {name = "secondHigest", stg = 700}
local p5 = {name = "replacesLowest", stg = 300}

customInsert(sortTable, p1)
customInsert(sortTable, p2)
customInsert(sortTable, p3)
customInsert(sortTable, p4)
print("Before replacing lowest")
for k, v in ipairs(sortTable) do
    print(k, v.name, v.stg)
end
--Output
--Before replacing lowest
--1 highest          800
--2 secondHigest     700
--3 secondLowest    500
--4 lowest                 200
customInsert(sortTable, p5)
print("After replacing lowest")
for k, v in ipairs(sortTable) do
    print(k, v.name, v.stg)
end
--Output
--After replacing lowest
--1 highest          800
--2 secondHigest     700
--3 secondLowest    500
--4 replacesLowest  300

what about something like this, instead of having two tables
 
Back
Top