Posted 26 January 2018 - 01:56 AM
Hey, so I'm super new to computercraft and coding as a general rule, so understand that this code will definitely have some issues. I know it can be done more efficiently, and that's part of why I'm here! So - I'm implementing a currency on my direwolf20 1.7.10 server, and I'm writing a code that will keep track of the value of player's "banks". So I finally got it working, the only problem is that if two players have the same amount of currency in their banks (counted by a different program), it will display one of the two players names twice. Any help and advice would be very much appreciated. Thank you!
Everything works fine except for the stated issue. I know I have some unnecessary tables.. the issue would be a problem with the "dispBal()" function.
x = (0)
mon = peripheral.wrap("left")
mod = peripheral.wrap("back")
win = window.create(mon, 1, 1, 20, 1)
smi = {"SMI", x}
mit = {"Mitch", x}
mac = {"Mac", x}
jos = {"Josh", x}
mad = {"Madi", x}
--Functions
local function sortInfo()
table.sort(balRank)
table.insert(balInfo, 1, balRank[5])
table.insert(balInfo, 2, balRank[4])
table.insert(balInfo, 3, balRank[3])
table.insert(balInfo, 4, balRank[2])
table.insert(balInfo, 5, balRank[1])
end
local function dispBal()
balRank = {smi[2], mit[2], mac[2], jos[2], mad[2]} -- values
balNames = {"SMI", "Mitchell", "Mac", "Josh", "Madi"}
balInfo = {} -- sorted
term.redirect(mon)
mon.setTextScale(2)
mon.setBackgroundColor(colors.lightBlue)
mon.setTextColor(colors.white)
mon.clear()
win.redraw()
mon.setCursorPos(3, 1)
mon.setTextColor(colors.blue)
mon.setCursorPos(1, 4)
sortInfo()
for k, v in pairs(balInfo) do
for i = 1, 5 do
if balInfo[i] == smi[2] then
term.clearLine()
term.setCursorPos(1, (i + 2))
print(i..":"..balNames[1].."> "..balInfo[i])
elseif balInfo[i] == mit[2] then
term.clearLine()
term.setCursorPos(1, (i + 2))
print(i..":"..balNames[2].."> "..balInfo[i])
elseif balInfo[i] == mac[2] then
term.clearLine()
term.setCursorPos(1, (i + 2))
print(i..":"..balNames[3].."> "..balInfo[i])
elseif balInfo[i] == jos[2] then
term.clearLine()
term.setCursorPos(1, (i + 2))
print(i..":"..balNames[4].."> "..balInfo[i])
elseif balInfo[i] == mad[2] then
term.clearLine()
term.setCursorPos(1, (i + 2))
print(i..":"..balNames[5].."> "..balInfo[i])
else return
end
end
end
end
-- "Mason eat'a the poo-poo"
local function getBal()
while true do
local balRec eV, sR, sC, rC, msg
= os.pullEvent("modem_message")
if sC == 1 then
smi[2] = msg
elseif sC == 2 then
mit[2] = msg
elseif sC == 3 then
mac[2] = msg
elseif sC == 4 then
jos[2] = msg
elseif sC == 5 then
mad[2] = msg
else
print("Error, unknown channel")
end
dispBal()
end
end
-- Program
for i = 1, 5 do
mod.open(i)
end
win.setBackgroundColor(colors.green)
win.clear()
win.setTextColor(colors.yellow)
win.write("Leaderboard")
while true do
getBal()
end
Everything works fine except for the stated issue. I know I have some unnecessary tables.. the issue would be a problem with the "dispBal()" function.