This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
littlechompz's profile picture

Town Online Player list Via monitor Help

Started by littlechompz, 05 May 2013 - 11:54 PM
littlechompz #1
Posted 06 May 2013 - 01:54 AM
Title: Town Online Player list Via monitor Help

I need help making this code more efficient and able to allow multiple computers to send data via rednet to a main computer that decodes the id and set's the player as online or offline using an advanced computer and changing their name colour as Red or Lime to indicate Offline or Online.

——————————————————-

function cls()
term.clear()
term.setCursorPos(1,1)
end

function startdisplay()
display = peripheral.wrap("top")
display.setTextScale(2)
display.clear()
display.setCursorPos(1,1)
end

function testbundleback()
cwhite = rs.testBundledInput("back", 1) -- white true = on False = off
corange = rs.testBundledInput("back", 2) -- orange
cmagenta = rs.testBundledInput("back", 4) -- magenta
clightBlue = rs.testBundledInput("back", 8) -- lightBlue
cyellow = rs.testBundledInput("back", 16) -- yellow
clime = rs.testBundledInput("back", 32) -- lime
cpink = rs.testBundledInput("back", 64) -- pink
cgrey = rs.testBundledInput("back", 128) -- grey
clightgrey = rs.testBundledInput("back", 256) -- light-grey
ccyan = rs.testBundledInput("back", 512) -- cyan
cpurple = rs.testBundledInput("back", 1024) -- purple
cblue = rs.testBundledInput("back", 2048) -- blue
cbrown = rs.testBundledInput("back", 4096) -- brown
cgreen = rs.testBundledInput("back", 8192) -- green
cred = rs.testBundledInput("back", 16384) -- red
cblack = rs.testBundledInput("back", 32768) -- black
end

for i=1,10 do
cls()
startdisplay()
testbundleback()
display.setCursorPos(1,1)
display.write("Player List")
display.write("---------------")

if cwhite == true then



display.setCursorPos(1,2)
display.setTextColour(colours.lime) -- turns color of monitor text lime to indicate online
display.write("<PlayerName>")

else


display.setCursorPos(1,2)
display.setTextColour(colours.red) -- turns colour of monitor text Red to indicate offline
display.write("<PlayerName>")


end
if corange == true then


display.setCursorPos(1,3)
display.setTextColour(colours.lime)
display.write("<PlayerName>")


else


display.setCursorPos(1,3)
display.setTextColour(colours.red)
display.write("<PlayerName>")

end
if cmagenta == true then			 -- Code from here to*

display.setCursorPos(1,4)


display.setTextColour(colours.lime)
display.write("<playername>")


else


display.setCursorPos(1,4)
display.setTextColour(colours.red)
display.write("<playername>")
end		-- *Here represent's the section i want to make smaller.


-- This part is how i think it should be set to send a rednet signal to another computer to read the value

--rednet,open("side")

--if cmagenta == true then		  

--rednet.send("ID, value(a1,b1,c1 etc)")

--else
--rednet.send("ID, value(a0,b0,c0 etc.)")	  --The value will send, and the main computer wo
--end	  

--The value will send, and the main computer would take the code like

--rednet.receive("Id, value")
--if Id = ("ID") then
--end
--if ("value")== ("a1") then


--display.setCursorPos(1,4)


--display.setTextColour(colours.lime)
--display.write("<playername>")


--else
--if ("value") == ("a0") then


--display.setCursorPos(1,4)
--display.setTextColour(colours.red)
--display.write("<playername>")
--end
– I know most of the code in the – doesn't work but i think it makes a point of what im trying to accomplish, so if anyone can help me then that would be awesome!

–I found this code here: http://www.computerc...le-script-help/

– It's not mine but i wanted to add this to Ask a Pro because from the comment's i read, nothing seemed to help me.
Cranium #2
Posted 06 May 2013 - 11:37 AM
Wow, looks like the forum derped on the syntax highlighting. Fixing now.

Edit: Fixed for you. Added
 tags.
NanoBob #3
Posted 06 May 2013 - 11:41 AM
at some point you have
rednet,open
instead of
rednet.open
littlechompz #4
Posted 06 May 2013 - 01:02 PM
Again, anything after This has error's and doesn't work but explains what i want to do in a simple fashion.
– This part is how i think it should be set to send a rednet signal to another computer to read the value
Bubba #5
Posted 06 May 2013 - 10:25 PM
Here's how I would go about this. It should be fully functioning, although I can't test right now. You will need to open the modem at the top of both programs though.

Server code:

local players = {} --This will store a list of all the players. You can populate it manually if you wish to

local termX, termY = term.getSize() --Get the size of the terminal for drawing later on

local function printPlayers() --Print all of the players in the player table

  term.setCursorPos(1,1) write("Player List\n")
  write("---------------------")
  local x,y = 1,3 --Keep track of what x/y position we are drawing to
  for name, online in pairs(players) do --For every player in our players list do
    if online then --If online==true then set the text color to green
      term.setTextColor(colors.green)
    else
      term.setTextColor(colors.red) --Otherwise set online to false
    end
    term.setCursorPos(x,y) --Set our cursor position to the x/y and write the player's name
    term.write(name)
    x = y==termY and x = x+15 or x --If y==size of term then set x to x+15, otherwise keep it the same
    y = y==termY and 3 or y+1 --If y==size of term then set y to 3 otherwise add 1 to it
  end
end

while true do
  printPlayers()
  local id, message = rednet.receive() --Receive a rednet message
  local player, status = message:match("(.-):(/>/>.*)") --Essentially just checks that the message sent matches the format "player:status"
  if player and status then
    players[player] = status:match("^on") and true or false --If the status contains "on" at the beginning, they are online and we will set their status to true. Otherwise it will be false.
  end
end

And then the client code:

local server_ID = 5 --Change to your server id
local username, online_status = ... --Get the arguments provided by player input
if not username or not online_status then
  print("Usage:")
  print("program_name [username] [online or offline]")
  return
end
if online_status:match("^on") then --Check to see if the online_status matches "on" at the beginning of the string
  rednet.send(server_ID, username..":".."online") --If so, send that to the server
else
  rednet.send(server_ID, username..":".."offline")
end

I honestly don't understand what the purpose of testbundledback() is in your code, but I just ignored that.