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

Need help with my rednet & file-writing program

Started by austinv11, 30 January 2014 - 09:33 AM
austinv11 #1
Posted 30 January 2014 - 10:33 AM
Hi, i was coding a "message-board" for my server, and it doesn't seem to work.
Here's the message submitter code:

--Setting up the screen
local ver = "1.0.1"
rednet.open("back")
function start()
  term.clear()
  term.setCursorPos(1,1)
  term.setTextColor(colors.yellow)
  print("Message Board V."..ver)
  term.setTextColor(colors.white)
  end
start()

--Actual code starts...
--now:
function getInfo()
  term.write("Please enter your username: ")
  usr = read()
  print("Enter your message (use only one line)(write 'delete' to delete the message you posted):")
  message = read()
  end

function fill()
  msg = nil
  local id, msg, dist = rednet.receive(15)
  if msg == nil then
	term.setTextColor(colors.red)
	print("Error! Please tell Austin or Panda!")
	sleep(10)
	os.reboot()
	end
  end

function msgSend()
  rednet.broadcast("start")
  local id, msg, dist = rednet.receive(15)
  if msg == "go on" then
	rednet.broadcast("user")
	fill()
	rednet.broadcast(usr)
	fill()
	rednet.broadcast("message")
	fill()
	rednet.broadcast(message)
	fill()
	rednet.broadcast("end")
  else
	term.setTextColor(colors.red)
	print("Error! Please tell Austin or Panda!")
	sleep(10)
	os.reboot()
	end
  os.reboot()
  end

getInfo()
msgSend()
Here's the Message board code:

rednet.open("bottom")
--ease of use function ftw!
function reply(text)
  rednet.broadcast(text)
  end

function rec(timeout)
  if timeout == nil then
	id, msg, d = rednet.receive()
  else
	id, msg, d = rednet.receive(timeout)
	end
  print("Message received!")
  end

--Real code now;

function loadFile()
  local check = fs.exists("messages")
  if check == true then
	shell.run("messages")
  else
	saveFile(true)
	end
  end

function saveFile(gen)
  if gen == nil then
	w = fs.open("messages", "a")
	w.writeLine("center("..[["]]..MESSAGE..[["]]..")")
	w.writeLine("p.setTextColor(colors.yellow)")
	w.writeLine("center("..[["]].."-"..USER..[["]]..")")
	w.writeLine("p.setTextColor(colors.white)")
	w.close()
	os.reboot()
  else
	w = fs.open("messages", "w")
	w.writeLine("p = peripheral.wrap("..[["]].."top"..[["]]..")")
	w.writeLine("p.setTextScale(1)")
	w.writeLine("p.clear()")
	w.writeLine("p.setCursorPos(1,1)")
	w.writeLine("function center(text)")
	w.writeLine("local x, y = p.getSize()")
	w.writeLine("local x2, y2 = p.getCursorPos()")
	w.writeLine("p.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2)")
	w.writeLine("p.write(text)")
	w.writeLine("y2 = y2 + 1")
	w.writeLine("p.setCursorPos(1, y2)")
	w.writeLine("end")
	w.writeLine("p.setTextColor(colors.orange)")
	w.writeLine("center("..[["]].."Server Message Board"..[["]]..")")
	w.writeLine("local x, y = p.getSize()")
	w.writeLine("for i = 1, x do")
	w.writeLine("p.write("..[["]].."~"..[["]]..")")
	w.writeLine("end")
	w.writeLine("p.setCursorPos(1, 3)")
	w.writeLine("p.setTextColor(colors.white)")
	w.close()
	os.reboot()
	end
  end

function re()
  rec()
  if msg == "start" then
	reply("go on")
	id, mssg, d = rednet.receive(15)
	if mssg == "user" then
	  reply(".")
	  id, USER, d = rednet.receive(15)
	  reply(".")
	elseif mssg == "message" then
	  reply(".")
	  id, MESSAGE, d = rednet.receive(15)
	  reply(".")
	elseif mssg == "end" then
	  saveFile()
	  end
  else
	os.reboot()
	end
  end

loadFile()
re()

When i submit a message, it is received by the board, but it doesn't actually write it on the save file, and therefore it doesn't display the messages. And it just quits.
Edited on 30 January 2014 - 09:40 AM
CometWolf #2
Posted 30 January 2014 - 12:18 PM
How do you know the correct messages are received? Did you make the computer actually print the received messages, so you could check?
austinv11 #3
Posted 30 January 2014 - 12:26 PM
How do you know the correct messages are received? Did you make the computer actually print the received messages, so you could check?
No the computer restarts and displays the message on the monitor
CometWolf #4
Posted 30 January 2014 - 12:44 PM
From what i can tell, it wouldn't be able to write the message at all, if it didn't actually save it to the file.
austinv11 #5
Posted 30 January 2014 - 12:46 PM
From what i can tell, it wouldn't be able to write the message at all, if it didn't actually save it to the file.
that's my issue, it doesnt actually save to the file
CometWolf #6
Posted 30 January 2014 - 12:51 PM
What is this… am i talking to a wall?
How do you know the correct messages are received? Did you make the computer actually print the received messages, so you could check?
austinv11 #7
Posted 30 January 2014 - 01:00 PM
What is this… am i talking to a wall?
How do you know the correct messages are received? Did you make the computer actually print the received messages, so you could check?
Oh, sorry lol, it prints a debug 'I received a message'-although not all the time since i was tinkering with the code trying to get it to work
CometWolf #8
Posted 30 January 2014 - 01:07 PM
Well yes i can see that, but you won't know if they are correct, and thus won't know if your whole "authentication" is really working either.
Give this a try.

  if msg == "start" then
	    print"message incoming"
	    reply("go on")
	    id, mssg, d = rednet.receive(15)
	    if mssg == "user" then
		  reply(".")
		  id, USER, d = rednet.receive(15)
		  print("received user: "..USER)
		  reply(".")
	    elseif mssg == "message" then
		  reply(".")
		  id, MESSAGE, d = rednet.receive(15)
		  print("received message: "..MESSAGE)
		  reply(".")
	    elseif mssg == "end" then
		  print("Transmission over, saving...")
		  saveFile()
		  end
  else
austinv11 #9
Posted 30 January 2014 - 01:29 PM
sorry to bother you guys, i know what the problem was, i need a loop! Thanks CometWolf!