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

File save for shop

Started by micmou, 13 May 2014 - 01:03 AM
micmou #1
Posted 13 May 2014 - 03:03 AM
Okay so I have this program I wrote to save a persistent file so It survives restart ther is a password to change the shop name it sends the data to the server the server dispalys the shop number and name the numbers are hardcodded
When I start the program in the shop it displays the text for both of the challenges and then quits.


function check()
  if not fs.exists("Name") then
    local file = io.open("Name", "w")
    file:write(PName)
    file:close()
  end
end

function save()
  local file = io.open("Name", "w")
  file:write("PName")
  file:close()
end

function read()
  local file = io.open("Name", "r")
  PName = file:read()
  file:close()
end



function send()
  rednet.open("bottom")
  rednet.send(73,PName)
  rednet.close("bottom")
end

  
local PName = ""
local Pass = "da5655"
local Input = ""

check()
read()
send()
while true do
  term.clear()
  term.setCursorPos(1,1)
  term.write("Enter the Password to change the name.")
  Input = read("*")
  if Input == Pass then
    term.clear()
    term.setCursorPos(1,1)
    term.write("Enter the name of your store.")
    PName = read()
    save()
  else
    term.write("Wrong Password")
    sleep(3)
    os.reboot()
  end
end
Agent Silence #2
Posted 13 May 2014 - 04:27 AM
try saving the file in a directory that starts with a period, as this hides it from 'ls'
also save the file in that directory with a period as well.
CometWolf #3
Posted 13 May 2014 - 07:46 AM
move your local definitions to the top, and remove the qoutes around pname in the save function.
micmou #4
Posted 14 May 2014 - 11:22 PM
I did that except it still displays Enter the Password to change the name.Enter the name of your store. on the same line and then quits.


local PName = ""
local Pass = "da5655"
local Input = ""
function check()
  if not fs.exists("Name") then
    local file = io.open("Name", "w")
    file:write(PName)
    file:close()
  end
end
function save()
  local file = io.open("Name", "w")
  file:write(PName)
  file:close()
end
function read()
  local file = io.open("Name", "r")
  PName = file:read()
  file:close()
end
function send()
  rednet.open("bottom")
  rednet.send(73,PName)
  rednet.close("bottom")
end

check()
read()
send()
while true do
  term.clear()
  term.setCursorPos(1,1)
  term.write("Enter the Password to change the name.")
  Input = read("*")
  if Input == Pass then
    term.clear()
    term.setCursorPos(1,1)
    term.write("Enter the name of your store.")
    PName = read()
    save()
  else
    term.write("Wrong Password")
    sleep(3)
    os.reboot()
  end
end
CometWolf #5
Posted 14 May 2014 - 11:52 PM
You overwrote the global read function with your own.

function read()
just change the name
micmou #6
Posted 15 May 2014 - 12:44 AM
Some other guy wrote the receiving code here it is I am not quite sure how to get it to auto refresh.


monitor= peripheral.wrap("top")
monitor.clear()
monitor.setCursorPos(2, 2)
monitor.setTextScale(2)
rednet.open ("left")
while true do
  event, id, text = os.pullEvent()
  shopID = id
  if shopID == 56 then
    id = 101
  elseif shopID == 57 then
    id = 102
  elseif shopID == 58 then
    id = 103
  elseif shopID == 59 then
    id = 104
  elseif shopID == 60 then
    id = 105
  elseif shopID == 61 then
    id = 106 
  elseif shopID == 19 then
    id = 107   
  elseif shopID == 18 then
    id = 108
  elseif shopID == 17 then
    id = 109
  elseif shopID == 16 then
    id = 110
  elseif shopID == 15 then
    id = 111
  elseif shopID == 14 then
    id = 112
  end
  if event == "rednet_message" then
    if id == 101 then
	  monitor.setCursorPos(2, 2)
	  monitor.clearLine()
	  monitor.write(id .. "- " .. text)
    elseif id == 102 then
	  monitor.setCursorPos(2, 3)
	  monitor.clearLine()
	  monitor.write(id .. "- " .. text)
    elseif id == 103 then
	  monitor.setCursorPos(2, 4)
	  monitor.clearLine()
	  monitor.write(id .. "- " .. text)
    elseif id == 104 then
	  monitor.setCursorPos(2, 5)
	  monitor.clearLine()
	  monitor.write(id .. "- " .. text)
    elseif id == 105 then
	  monitor.setCursorPos(2, 6)
	  monitor.clearLine()
	  monitor.write(id .. "- " .. text)
    elseif id == 106 then
	  monitor.setCursorPos(2, 7)
	  monitor.clearLine()
	  monitor.write(id .. "- " .. text)
    elseif id == 107 then
	  monitor.setCursorPos(2, 8)
	  monitor.clearLine()
	  monitor.write(id .. "- " .. text)
    elseif id == 108 then
	  monitor.setCursorPos(2, 9)
	  monitor.clearLine()
	  monitor.write(id .. "- " .. text)
    elseif id == 109 then
	  monitor.setCursorPos(2, 10)
	  monitor.clearLine()
	  monitor.write(id .. "- " .. text)
    elseif id == 110 then
	  monitor.setCursorPos(2, 11)
	  monitor.clearLine()
	  monitor.write(id .. "- " .. text)
    elseif id == 111 then
	  monitor.setCursorPos(2, 12)
	  monitor.clearLine()
	  monitor.write(id .. "- " .. text)
    elseif id == 112 then
	  monitor.setCursorPos(2, 13)
	  monitor.clearLine()
	  monitor.write(id .. "- " .. text)
    end
  end
end