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

Computronics : Tape Drive error

Started by Arektor, 15 July 2014 - 03:44 PM
Arektor #1
Posted 15 July 2014 - 05:44 PM
————————————————- !!! ———————————————————-
Go to the page 2 for the current question please
————————————————- !!! ———————————————————-


First excuse me If I do mistakes in the language, it's not my first one.

Well, I'm making a program who's talking with an other computer to work, and the requireds informations are stocked in a table.
When I try to display one of the informations in one of these tables, it return me the following error :
"Test:113: attempt to index ? (a nil value)"

Concretly I know it mean the value is null, but I don't understand why :s

Here is the code of the client computer:

---Ask the server---
rednet.open("top")
serverID = 14
rednet.send(serverID,"opcode1")
local event, senderID, message, distance = os.pullEvent()
if event == "rednet_message" then
users = textutils.unserialize(message)
end
sleep(0.1)
rednet.send(serverID,"opcode2")
local event, senderID, message, distance = os.pullEvent()
if event == "rednet_messag" then
moneys = textutils.unserialize(message)
end
sleep(0.1)
rednet.send(serverID,"opcode3")
local event, senderID, message, distance = os.pullEvent()
if event == "rednet_message" then
lockeds = textutils.unserialize(message)
end
sleep(0.1)
rednet.send(serverID,"opcode4")
local event, senderID, message, distance = os.pullEvent()
if event == "rednet_message" then
codes = textutils.unserialize(message)
end
sleep(0.1)
rednet.send(serverID,"opcode5")
local event, senderID, message, distance = os.pullEvent()
if event == "rednet_message" then
isInactive = textutils.unserialize(message)
end
--Define functions--
function getData()
if actualUser == "Arektor" then
  user = users[1]
  money = moneys[1]
  code = codes[1]
  isLocked = lockeds[1]
elseif actualUser == "Leo" then
  user = users[2]
  money = moneys[2]
  code = codes[2]
  isLocked = lockeds[2]
elseif actualUser == "Maxime" then
  user = users[3]
  money = moneys[3]
  code = codes[3]
  isLocked = lockeds[3]
elseif actualUser == "Crosf" then
  user = users[4]
  money = moneys[4]
  code = codes[4]
  isLocked = lockeds[4]
elseif actualUser == "Magni" then
  user = users[5]
  money = moneys[5]
  code = codes[5]
  isLocked = lockeds[5]
end
end
--------------------
function displayData()
term.setCursorPos(1,1)
term.setBackgroundColor(8)
write("User: "..user)
term.setCursorPos(30,1)
print("Money: "..money)
term.setCursorPos(1,2)
print("Code: "..code)
term.setCursorPos(30,2)
print("Locked: "..isLocked)
end
--------------------
function displayPanel()
loadImage("Menu")
getData()									-- getData = Obtain informations about the user's account
displayData()								-- displayData = Display these informations
term.setCursorPos(51,1)
term.setBackgroundColor(16384)
write("X")
term.setCursorPos(8,16)
term.setBackgroundColor(512)
write("Get")
term.setCursorPos(8,17)
write("Money")
term.setCursorPos(21,16)
write("Stock")
term.setCursorPos(21,17)
write("Money")
term.setCursorPos(40,18)
write("Lock Account")
term.setCursorPos(27,4)
write("Send Money")
term.setCursorPos(27,5)
write("to an other")
term.setCursorPos(27,6)
write("account.")
end
--------------------
function loadImage(name)
image = paintutils.loadImage("/Data/Images/"..name)
paintutils.drawImage(image,1,1)
end
--------------------

--Authentification--
loadImage("Intro")
term.setCursorPos(4,13)
term.setBackgroundColor(8)
write("Username")
write(users[1])
term.setCursorPos(2,16)
write("Password Code")
term.setCursorPos(19,11)
term.setTextColor(8192)
write("Remote Access Edition")
h = 0
while h == 0 do
term.setTextColor(1)
term.setCursorPos(3,14)
term.setBackgroundColor(256)
user = read()
term.setCursorPos(3,17)
code = read("*")
if user == users[1] and code == codes[1] then
  actualUser = "Arektor"
  h = 1
elseif user == users[2] and code == codes[2] then
  actualUser = "Leo"
  h = 1
elseif user == users[3] and code == codes[3] then
  actualUser = "Maxime"
  h = 1
elseif user == users[4] and code == codes[4] then
  actualUser = "Crosf"
  h = 1
elseif user == users[5] and code == codes[5] then
  actualUser = "Magni"
  h = 1
else
  loadImage("Intro")
  term.setCursorPos(4,13)
  term.setBackgroundColor(8)
  write("Username")
  term.setCursorPos(2,16)
  write("Password Code")
  term.setCursorPos(19,11)
  term.setTextColor(8192)
  write("Remote Access Edition")
  term.setCursorPos(38,4)
  term.setBackgroundColor(256)
  term.setTextColor(16384)
  print("Invalid")
  term.setCursorPos(38,5)
  print("combinaison of")
  term.setCursorPos(38,6)
  print("username and")
  term.setCursorPos(38,7)
  print("password code.")
end
end
--Bank Account Control--
displayPanel()
v = 0
while v == 0 do
local event, button, x, y = os.pullEvent("mouse_click")
  if x >= 4 and y >= 16 and x <= 6 and y <= 17 then
   getMoney()
   v = 1
  elseif x >= 17 and x <= 19 and y <= 17 and y >= 16 then
   stockMoney()
   v = 1
  elseif x >= 44 and y >= 15 and x <= 48 and y <= 17 then
   lockAcount()
   v = 1
  elseif x == 51 and y == 1 then
   os.shutdown()
  end
end

And here is the server computer code:

rednet.open("top")
term.clear()
term.setCursorPos(1,1)
term.setTextColor(32)
print("A'Bank Server Platform System")
print(" ")
term.setTextColor(1)
local users = {
"Arektor";
"Léo";
"Maxime";
"Crosf";
"Magni";
}
--------------------
local moneys = {
0;
0;
0;
0;
0;
}
--------------------
local lockeds = {
"false";
"false";
"false";
"false";
"false";
}
--------------------
local codes = {
"4921s-ender";
"7436p-zlqng";
"941rt-nrj95";
"41237-eaq7m";
"uig25-etn25";
}
--------------------
local isInactive = {
"false";
"false";
"false";
"false";
"false";
}
--------------------
local falseOpcode = {
"uiglenf571ev5";
"arzeioh546vx312";
"endptzearek2684";
"rezabjn7-4z2f";
"rzeijor-4245n-z41f";
}
--Prepare opcodes packets for transmission--
userss = textutils.serialize(users)
moneyss = textutils.serialize(moneys)
lockedss = textutils.serialize(lockeds)
codess = textutils.serialize(codes)
isInactives = textutils.serialize(isInactive)
falseOpcodes = textutils.serialize(falseOpcode)
--Start the platform system--
i = 50
while i == 50 do
local event, senderID, message, distance = os.pullEventRaw()
if event == "rednet_message" then
  if message == "opcode1" then
   sleep(0.1)
   rednet.send(senderID,userss)
   print("Opcode 1 (Users) was sent to the router "..senderID)
  elseif message == "opcode2" then
   sleep(0.1)
   rednet.send(senderID,moneyss)
   print("Opcode 2 (Moneys) was sent to the router "..senderID)
  elseif message == "opcode3" then
   sleep(0.1)
   rednet.send(senderID,lockedss)
   print("Opcode 3 (Lockeds) was sent to the router "..senderID)
  elseif message == "opcode4" then
   sleep(0.1)
   rednet.send(senderID,codess)
   print("Opcode 4 (Password Codes) was sent to the router "..senderID)
  elseif message == "opcode5" then
   sleep(0.1)
   rednet.send(senderID,isInactives)
   print("Opcode 5 (Is Inactive ?) was sent to the router "..senderID)
  else -- Detect if a false message is sended for trying to receive an opcode.
   print("Router "..senderID.." tried to send a wrong message!")
   rednet.send(senderID,falseOpcodes)
  end
elseif event == "terminate" then
  print("Bank Server Platform ended! A'Bank is now disabled!!")
  i = 51
end
end

Maybe the error is obvious… But I can't find it T_T
Edited on 18 July 2014 - 07:35 PM
hilburn #2
Posted 15 July 2014 - 05:53 PM
in your client code you need to use term.write(…) not write(…)
Arektor #3
Posted 15 July 2014 - 05:59 PM
Tried it, but it still gave me the same error…

(write without term. works too, I've tried it on a test computer)
Lyqyd #4
Posted 15 July 2014 - 06:21 PM
Yeah, write is a perfectly legitimate function. I'm assuming the larger of the two scripts is called "Test"? Line 113 doesn't seem to point to anything useful, though line 112 is trying to print from the users table. Are you sure you're getting the events you expect from those os.pullEvent calls? You might want to get rid of the sleeps and change to use rednet.receive to pull in the packets.

Edit: Someone pointed out the semicolons in the tables as a problem. They're fine.

I wanted to clarify that you're most likely getting a modem_message event before the rednet_message event shows up, so your unserialization will not work. Changing to rednet.receive should alleviate the problem.
hilburn #5
Posted 15 July 2014 - 06:22 PM
Sorry, my bad, you have a typo in your server code

rednet.send(senderID,userss)

You might want to consider creating a user object along the lines of user={["name"]="Arektor", ["moneys"]=0…} then have a table of users
Edited on 15 July 2014 - 04:23 PM
Arektor #6
Posted 15 July 2014 - 06:30 PM
The sleep is necessary to receive all the informations (if I remove it, it will not send me the fourth packet)
The line 113 is my method to see if the table is correct (if there is still the error) quickly than typing each time my username and password to see the error. The line 113 is temporary so. The line 112 is just displaying "Username" on the top of the grey bar where the user type his username.

And the ; separation character is good for the table, but the , is good too.
Lyqyd #7
Posted 15 July 2014 - 07:39 PM
Semicolons are fine in place of commas in table declarations. There is no further need to "correct" it.
TheOddByte #8
Posted 15 July 2014 - 07:57 PM
As Lyqyd pointed out

local t = {
    foo = "bar";
}
print( foo )
And I would suggest using os.pullEvent for receiveing packets

while true do
    local e = { os.pullEvent() }
    if e[1] == "rednet_message" then
        -- Do something
    end
end
Arektor #9
Posted 15 July 2014 - 08:08 PM
I don't really understand what's the difference with your suggestion and what I have did… Isn't it the same ? It's an os.pullEvent()


@Lyqyd: I've changed my code for receiving the packets with rednet.receive… but it gave me always a nil message. Here is the part of the code :

rednet.open("top")
serverID = 14
rednet.send(serverID,"opcode1")
rednet.receive(3)
if not message == nil then
users = textutils.unserialize(message)
else
print("Could not connect to A'Bank Server Platform. Exiting...")
sleep(2)
shell.exit()
end
sleep(0.1)
rednet.send(serverID,"opcode2")
rednet.receive(3)
if not message == nil then
moneys = textutils.unserialize(message)
else
print("Could not connect to A'Bank Server Platform. Exiting...")
sleep(2)
shell.exit()
end
sleep(0.1)
rednet.send(serverID,"opcode3")
rednet.receive(3)
if not message == nil then
lockeds = textutils.unserialize(message)
else
print("Could not connect to A'Bank Server Platform. Exiting...")
sleep(2)
shell.exit()
end
sleep(0.1)
rednet.send(serverID,"opcode4")
rednet.receive(3)
if not message == nil then
codes = textutils.unserialize(message)
else
print("Could not connect to A'Bank Server Platform. Exiting...")
sleep(2)
shell.exit()
end
sleep(0.1)
rednet.send(serverID,"opcode5")
rednet.receive(3)
if not message == nil then
isInactive = textutils.unserialize(message)
else
print("Could not connect to A'Bank Server Platform. Exiting...")
sleep(2)
shell.exit()
end
Edited on 15 July 2014 - 06:14 PM
Lyqyd #10
Posted 15 July 2014 - 08:21 PM
You still need to catch the values the function returns:


local sender, message = rednet.receive()
Arektor #11
Posted 15 July 2014 - 08:35 PM
Still getting the same error (again T_T)
I've tried to restart my game, with no effects.
Lyqyd #12
Posted 15 July 2014 - 09:36 PM
Oh, your conditions are set up incorrectly. Dunno how I missed that. You'd want `if message ~= nil then` instead, since `not message == nil` can never evaluate true.
Arektor #13
Posted 16 July 2014 - 06:16 AM
Well, I've changed with if message ~= nil then and switched places the actions, but I've still the same error, so the received message is null, I think it came from the server but I really don't know where and why…

The changed code for the users table:

if message ~= nil then
print("Could not connect to A'Bank Server Platform. Exiting...")
sleep(2)
shell.exit()
else
users = textutils.unserialize(message)
end
Edited on 16 July 2014 - 04:16 AM
hilburn #14
Posted 16 July 2014 - 06:40 AM
Have you tried putting a "print(userss)" in the server code just before or after it is sent? just to check that it is sending it correctly, similarly a print of the received string would check that something hasn't gone squiffy
Arektor #15
Posted 16 July 2014 - 06:51 AM
The print of userss is the useless "write(users[1])" who's displaying the first user of the table.

When I did this on the server program, it's showing me that:

{
"Arektor",
"Léo",
"Maxime",
"Crosf",
"Magni",
}

I think the sended message is correct. So it is the reception ?
hilburn #16
Posted 16 July 2014 - 06:58 AM
I was hoping you could try printing it out before you unserialize it, just a print (message) which will allow you to see if the failure is in the unserialization or in the transmission
Arektor #17
Posted 16 July 2014 - 07:12 AM
I did it, and the received message is exactly the same:

{
"Arektor",
"Léo",
"Maxime",
"Crosf",
"Magni",
}
So the problem is from the unserialization but I don't know too why the message is null ? Cause for my if condition, it's returning me always the part where if the message is null…
hilburn #18
Posted 16 July 2014 - 07:18 AM
One thing I noticed:


if message ~= nil then --#this returns true if the message contains content, you want this to be if message==nil then
	print("Could not connect to A'Bank Server Platform. Exiting...")
	sleep(2)
	shell.exit()
else
	users = textutils.unserialize(message)
end
Arektor #19
Posted 16 July 2014 - 07:25 AM
Problem solved, thanks you guys :D/>
Arektor #20
Posted 16 July 2014 - 11:48 AM
I'm making a program and I want to when the user click on a button, it will send a message to the server (an other computer) and modify a specific line of the server's program.

I know how to make a button and how to send a message, but the thing I don't know is how to modify a specific line of a program with an other program ? (If possible with the same program)
I've searched a lot but I haven't find (maybe not enough or not with the right terms) so I'm asking her for helping.

Thanks in advance for your answers

(Edit: I've found the FS api but I can't modify anything with it)
Edited on 16 July 2014 - 09:49 AM
hilburn #21
Posted 16 July 2014 - 12:17 PM
It's.. doable, in theory at least. However I'm guessing this is for your banking code, changing the money?

A better method of doing it would be to store that sort of data in a file, load it up when the program starts, save to it whenever the amount of money changes.

The code for that kind of thing would look something like this:


function saveFile(data, filename)
      if fs.exists(filename) then
         fs.delete(filename)
      end
      file = fs.open(filename, "w")
      file.write(textutils.serialize(pages))
      file.close()
end

function loadFile(filename)
       if fs.exists(filename) then
          file = fs.open(filename, "r")
          data = textutils.unserialize(file.readAll())
          file.close()
		else
           --#if the file doesn't exist you can put some error handling routines in here
        end
		return data
end

So you'd copy that into the start of your program, and then rather than initialise a table in the way you did at the beginning of your server code you would use

users=loadFile("users.txt")
and to change it you would say

saveFile(newusers,"users.txt")
Sorry it's a bit rough, but that will work a lot better than trying to edit a program
Edited on 16 July 2014 - 10:18 AM
theoriginalbit #22
Posted 16 July 2014 - 12:26 PM
be aware that you cannot modify anything on the computer if it resides in the rom directory; since rom is read-only memory.
Arektor #23
Posted 16 July 2014 - 12:50 PM
Thanks for the code but I'm a bit confused, could you explain how can I use the saveFile function ?

I'm starting the program, I'll load the money file with loadFile("money"), I'll send it to any client who need it.
A client send me that he want to remove 50 money from his account, and he have 103.
And… Here I don't know what to do with the saveFile function.
hilburn #24
Posted 16 July 2014 - 01:01 PM
Ok, so presumably the server gets a message along the lines of (user, -50) and then you update the money table with money[user] = money[user] - 50. Something like that at least right?

All you then have to do is call saveFile(money,"money.txt") and it will save the update money table to the file, next time you open it, boom that user is still 50 credits poorer
Arektor #25
Posted 16 July 2014 - 01:08 PM
So, when I type saveFile(money,"money") it will replace all the content of the file money and replace it with the money table, right ?
hilburn #26
Posted 16 July 2014 - 08:17 PM
yes
Bomb Bloke #27
Posted 16 July 2014 - 08:22 PM
Well, if not for the bug where you try to serialise the content of "pages" instead of "data".

Some minor cleaning:

local function saveFile(data, filename)
	local file = fs.open(filename, "w")
	if file then
		file.write(textutils.serialize(data))
		file.close()
	else error("Failure writing to file \""..filename.."\".") end
end

local function loadFile(filename)
	if fs.exists(filename) then
		local file = fs.open(filename, "r")
		local data = textutils.unserialize(file.readAll())
		file.close()
		return data
	else error("Can't locate file \""..filename.."\" for reading.") end
end
Arektor #28
Posted 16 July 2014 - 08:27 PM
Thanks you for your answers, problem solved :)/>
Arektor #29
Posted 16 July 2014 - 08:43 PM
Hi again, I hope you will not take me for someone who can not find the solution itself…

I want to reload my table "moneys" without restarting my server program, but I don't know how…

Here is the part of the code where I'm reloading the table:

elseif message == "addmoney" then
   print("Router "..senderID.." has sent a request to adding money to his account.")
   local sender, message = rednet.receive(2)
   if sender == senderID and message == users[1] or message == users[2] or message == users[3] or message == users[4] or message == users[5] then
	print("Router "..senderID.." is logged as the user "..message)
	local sender, count = rednet.receive(2)
	if sender == senderID then
	 if message == users[1] then
	  print(count.." added to "..message.."'s account.")
	  moneys[1] = moneys[1] + count
	  saveFile(moneys, "money")
	  local moneys = loadFile("money")
(Thanks to hilburn for the loadFile and saveFile functions)

At the start of my program, I'm loading my table with this :
local moneys = loadFile("money")

The file where is stocked the table is called money.

Here are the two functions:

function saveFile(data, filename)
	  if fs.exists(filename) then
		 fs.delete(filename)
	  end
	  file = fs.open(filename, "w")
	  file.write(textutils.serialize(data))
	  file.close()
end
---------------------
function loadFile(filename)
	   if fs.exists(filename) then
		  file = fs.open(filename, "r")
		  data = textutils.unserialize(file.readAll())
		  file.close()
	   else
		   print("Could not load money table. Exiting...")
		   sleep(1.3)
		   i = 51
		   shell.exit()
	   end
				   return data
end
Edited on 16 July 2014 - 07:01 PM
Lyqyd #30
Posted 16 July 2014 - 08:52 PM
Threads merged. Please stick to one topic for all questions about a given piece of code. It is much easier to follow the progress of the program and ensures that we do not duplicate efforts in trying to help. Feel free to edit the topic title to reflect the current question.
Arektor #31
Posted 16 July 2014 - 08:59 PM
Ok sorry.
hilburn #32
Posted 16 July 2014 - 09:45 PM
Well there is not really any need to reload the file, the data is already stored in your "money" table

But if you do want to reload it from the file you need to do it without the local, as the variable already has been declared, so just

moneys = loadFile("money")
will suffice
Arektor #33
Posted 17 July 2014 - 11:50 AM
I want to reload the table because even with rewriting the amount of money after receiving the new packet, it's not working.

I've removed the local, but It doesn't did anything. I must restart the program.
hilburn #34
Posted 17 July 2014 - 02:16 PM
I'm slightly confused, as far as I can tell the following is happening:

1. "moneys" has some values
2. "moneys" gets updated
3. save the new values of "moneys" to the file for long term storage

are you saying that after step 3 the values of "moneys" aren't different to they were in step 1?
Arektor #35
Posted 17 July 2014 - 03:27 PM
Yep. But in the file "money", this is the new value.
hilburn #36
Posted 17 July 2014 - 03:34 PM
I have a hard time understanding how that's happened, maybe clearing the money's table (moneys={}) followed by moneys=loadFile…

The only other possibility I can think of is, where are you querying the updated moneys table? If it is on the client it won't have changed because you haven't sent the updated moneys data back from the server.
Arektor #37
Posted 17 July 2014 - 06:07 PM
Still not working…

Here is the code of the server:

rednet.open("top")
term.clear()
i = 50
term.setCursorPos(1,1)
term.setTextColor(32)
print("A'Bank Server Platform System")
print(" ")
term.setTextColor(1)
--Declare functions--
function saveFile(data, filename)
	  if fs.exists(filename) then
		 fs.delete(filename)
	  end
	  file = fs.open(filename, "w")
	  file.write(textutils.serialize(data))
	  file.close()
end
---------------------
function loadFile(filename)
	   if fs.exists(filename) then
		  file = fs.open(filename, "r")
		  data = textutils.unserialize(file.readAll())
		  file.close()
			    else
		   print("Could not load money table. Exiting...")
	 sleep(1.3)
	 i = 51
	 shell.exit()
	    end
			    return data
end
---------------------
local users = {
"Arektor",
"Léo",
"Maxime",
"Crosf",
"Magni",
}
--------------------
local moneys = loadFile("money")
--------------------
local lockeds = {
"false";
"false";
"false";
"false";
"false";
}
--------------------
local codes = {
"4921s-ender";
"7436p-zlqng";
"941rt-nrj95";
"41237-eaq7m";
"uig25-etn25";
}
--------------------
local isInactive = {
"false";
"false";
"false";
"false";
"false";
}
--------------------
local falseOpcode = {
"uiglenf571ev5";
"arzeioh546vx312";
"endptzearek2684";
"rezabjn7-4z2f";
"rzeijor-4245n-z41f";
}
--Prepare opcodes packets for transmission--
userss = textutils.serialize(users)
moneyss = textutils.serialize(moneys)
lockedss = textutils.serialize(lockeds)
codess = textutils.serialize(codes)
isInactives = textutils.serialize(isInactive)
falseOpcodes = textutils.serialize(falseOpcode)
--Start the platform system--
while i == 50 do
local event, senderID, message, distance = os.pullEventRaw()
if event == "rednet_message" then
  if message == "opcode1" then
   sleep(0.1)
   rednet.send(senderID,userss)
   print("Opcode 1 (Users) was sent to the router "..senderID)
  elseif message == "opcode2" then
   sleep(0.1)
   rednet.send(senderID,moneyss)
   print("Opcode 2 (Moneys) was sent to the router "..senderID)
  elseif message == "opcode3" then
   sleep(0.1)
   rednet.send(senderID,lockedss)
   print("Opcode 3 (Lockeds) was sent to the router "..senderID)
  elseif message == "opcode4" then
   sleep(0.1)
   rednet.send(senderID,codess)
   print("Opcode 4 (Password Codes) was sent to the router "..senderID)
  elseif message == "opcode5" then
   sleep(0.1)
   rednet.send(senderID,isInactives)
   print("Opcode 5 (Is Inactive ?) was sent to the router "..senderID)
  elseif message == "removemoney" then
   local sender, message = rednet.receive(2)
   if sender == senderID and message == users[1] or message == users[2] or message == users[3] or message == users[4] or message == users[5] then
    local sender, count = rednet.receive(2)
if sender == senderID then
  if message == users[1] then
   moneys[1] = moneys[1] - count
   if moneys[1] < 0 then
    moneys[1] = moneys[1] + count
   else
    saveFile(moneys, "money")
    moneys = {}
	   moneys = loadFile("money")
   end
  elseif message == users[2] then
   moneys[2] = moneys[2] - count
   if moneys[2] < 0 then
    moneys[2] = moneys[2] + count
   else
    saveFile(moneys, "money")
    moneys = {}
	   moneys = loadFile("money")
   end
  elseif message == users[3] then
   moneys[3] = moneys[3] - count
   if moneys[3] < 0 then
    moneys[3] = moneys[3] + count
   else
    saveFile(moneys, "money")
    moneys = {}
	   moneys = loadFile("money")
   end
  elseif message == users[4] then
   moneys[4] = moneys[4] - count
   if moneys[4] < 0 then
    moneys[4] = moneys[4] + count
   else
    saveFile(moneys, "money")
    moneys = {}
	   moneys = loadFile("money")
   end
  elseif message == users[5] then
   moneys[5] = moneys[5] - count
   if moneys[5] < 0 then
    moneys[5] = moneys[5] + count
   else
    saveFile(moneys, "money")
    moneys = {}
	   moneys = loadFile("money")
   end
  end
end
   end
  elseif message == "addmoney" then
   print("Router "..senderID.." has sent a request to adding money to his account.")
   local sender, message = rednet.receive(2)
   if sender == senderID and message == users[1] or message == users[2] or message == users[3] or message == users[4] or message == users[5] then
    print("Router "..senderID.." is logged as the user "..message)
local sender, count = rednet.receive(2)
if sender == senderID then
  if message == users[1] then
   print(count.." added to "..message.."'s account.")
   moneys[1] = moneys[1] + count
   saveFile(moneys, "money")
   moneys = {}
	  moneys = loadFile("money")
  elseif message == users[2] then
   print(count.." added to "..message.."'s account.")
   moneys[2] = moneys[2] + count
   saveFile(moneys, "money")
   moneys = {}
	  moneys = loadFile("money")
  elseif message == users[3] then
   print(count.." added to "..message.."'s account.")
   moneys[3] = moneys[3] + count
   saveFile(moneys, "money")
   moneys = {}
	  moneys = loadFile("money")
  elseif message == users[4] then
   print(count.." added to "..message.."'s account.")
   moneys[4] = moneys[4] + count
   saveFile(moneys, "money")
   moneys = {}
	  moneys = loadFile("money")
  elseif message == users[5] then
   print(count.." added to "..message.."'s account.")
   moneys[5] = moneys[5] + count
   saveFile(moneys, "money")
   moneys = {}
	  moneys = loadFile("money")
  end
end
   end
  else -- Detect if a false message is sended for trying to receive an opcode.
   term.setTextColor(16384)
   print("Router "..senderID.." tried to send a wrong message!")
   rednet.send(senderID,falseOpcodes)
  end
elseif event == "terminate" then
  term.setTextColor(16384)
  print(" ")
  print("Bank Server Platform ended! A'Bank is now disabled!!")
  i = 51
end
end

And the code of the client:

---Ask the server---
rednet.open("top")
serverID = 14
rednet.send(serverID,"opcode1")
local sender, message = rednet.receive(3)
if message ~= nil then
hash = sha256(message)
users = textutils.unserialize(message)
print("Receiving packet GUID 1")
print("Packet content: "..hash)
else
print("Could not connect to A'Bank Server Platform. Exiting...")
sleep(2)
shell.exit()
end
sleep(0.1)
rednet.send(serverID,"opcode2")
local sender, message = rednet.receive(3)
if message ~= nil then
hash = sha256(message)
moneys = textutils.unserialize(message)
print("Receiving packet GUID 2")
print("Packet content: "..hash)
else
print("Could not connect to A'Bank Server Platform. Exiting...")
sleep(2)
shell.exit()
end
sleep(0.1)
rednet.send(serverID,"opcode3")
local sender, message = rednet.receive(3)
if message ~= nil then
hash = sha256(message)
lockeds = textutils.unserialize(message)
print("Receiving packet GUID 3")
print("Packet content: "..hash)
else
print("Could not connect to A'Bank Server Platform. Exiting...")
sleep(2)
shell.exit()
end
sleep(0.1)
rednet.send(serverID,"opcode4")
local sender, message = rednet.receive(3)
if message ~= nil then
hash = sha256(message)
codes = textutils.unserialize(message)
print("Receiving packet GUID 4")
print("Packet content: "..hash)
else
print("Could not connect to A'Bank Server Platform. Exiting...")
sleep(2)
shell.exit()
end
sleep(0.1)
rednet.send(serverID,"opcode5")
local sender, message = rednet.receive(3)
if message ~= nil then
hash = sha256(message)
isInactive = textutils.unserialize(message)
print("Receiving packet GUID 5")
print("Packet content: "..hash)
else
print("Could not connect to A'Bank Server Platform. Exiting...")
sleep(2)
shell.exit()
end
--Define functions--
function getData()
if actualUser == "Arektor" then
  user = users[1]
  money = moneys[1]
  code = codes[1]
  isLocked = lockeds[1]
elseif actualUser == "Leo" then
  user = users[2]
  money = moneys[2]
  code = codes[2]
  isLocked = lockeds[2]
elseif actualUser == "Maxime" then
  user = users[3]
  money = moneys[3]
  code = codes[3]
  isLocked = lockeds[3]
elseif actualUser == "Crosf" then
  user = users[4]
  money = moneys[4]
  code = codes[4]
  isLocked = lockeds[4]
elseif actualUser == "Magni" then
  user = users[5]
  money = moneys[5]
  code = codes[5]
  isLocked = lockeds[5]
end
end
--------------------
function displayData()
term.setCursorPos(1,1)
term.setBackgroundColor(8)
write("User: "..user)
term.setCursorPos(30,1)
print("Money: "..money)
term.setCursorPos(1,2)
print("Code: "..code)
term.setCursorPos(30,2)
print("Locked: "..isLocked)
end
--------------------
function displayPanel()
loadImage("Menu")
getData()								    -- getData = Obtain informations about the user's account
displayData()							    -- displayData = Display these informations
term.setCursorPos(51,1)
term.setBackgroundColor(16384)
write("X")
term.setCursorPos(8,16)
term.setBackgroundColor(512)
write("Get")
term.setCursorPos(8,17)
write("Money")
term.setCursorPos(21,16)
write("Stock")
term.setCursorPos(21,17)
write("Money")
term.setCursorPos(40,18)
write("Lock Account")
term.setCursorPos(27,4)
write("Send Money")
term.setCursorPos(27,5)
write("to an other")
term.setCursorPos(27,6)
write("account")
end
--------------------
function loadImage(name)
image = paintutils.loadImage("/Data/Images/"..name)
paintutils.drawImage(image,1,1)
end
--------------------
function lockAccount()
image = paintutils.loadImage("/Data/Images/lockaccount")
paintutils.drawImage(image,13,2)
term.setCursorPos(15,2)
term.setTextColor(32768)
term.setBackgroundColor(1)
print("Locking the account")
term.setTextColor(1)
term.setBackgroundColor(16384)
term.setCursorPos(36,2)
write("X")
term.setBackgroundColor(2)
term.setCursorPos(18,4)
print("Etes-vous sûr")
term.setCursorPos(15,5)
print("de vouloir bloquer ce")
term.setCursorPos(16,6)
print("compte banquaire ?")
term.setCursorPos(16,17)
term.setBackgroundColor(2048)
write("Oui")
term.setCursorPos(31,17)
write("Non")
end
--------------------
function stockMoney()
rednet.send(serverID,"addmoney")
sleep(0.1)
rednet.send(serverID,actualUser)
sleep(0.1)
rednet.send(serverID,"50")
sleep(0.2)
rednet.send(serverID,"opcode2")
local sender, message = rednet.receive(3)
if message ~= nil then
  moneys = textutils.unserialize(message)
end
sleep(0.1)
getData()
term.setCursorPos(30,1)
print("Money: "..money)
end
--------------------

--Authentification--
loadImage("Intro")
term.setCursorPos(4,13)
term.setBackgroundColor(8)
write("Username")
term.setCursorPos(2,16)
write("Password Code")
term.setCursorPos(19,11)
term.setTextColor(8192)
write("Remote Access Edition")
h = 0
while h == 0 do
term.setTextColor(1)
term.setCursorPos(3,14)
term.setBackgroundColor(256)
user = read()
term.setCursorPos(3,17)
code = read("*")
if user == users[1] and code == codes[1] then
  actualUser = "Arektor"
  h = 1
elseif user == users[2] and code == codes[2] then
  actualUser = "Leo"
  h = 1
elseif user == users[3] and code == codes[3] then
  actualUser = "Maxime"
  h = 1
elseif user == users[4] and code == codes[4] then
  actualUser = "Crosf"
  h = 1
elseif user == users[5] and code == codes[5] then
  actualUser = "Magni"
  h = 1
else
  loadImage("Intro")
  term.setCursorPos(4,13)
  term.setBackgroundColor(8)
  write("Username")
  term.setCursorPos(2,16)
  write("Password Code")
  term.setCursorPos(19,11)
  term.setTextColor(8192)
  write("Remote Access Edition")
  term.setCursorPos(38,4)
  term.setBackgroundColor(256)
  term.setTextColor(16384)
  print("Invalid")
  term.setCursorPos(38,5)
  print("combinaison of")
  term.setCursorPos(38,6)
  print("username and")
  term.setCursorPos(38,7)
  print("password code.")
end
end
--Bank Account Control--
displayPanel()
v = 0
while v == 0 do
local event, button, x, y = os.pullEvent("mouse_click")
  if x >= 4 and y >= 16 and x <= 6 and y <= 17 then
   getMoney()
   v = 1
  elseif x >= 17 and x <= 19 and y <= 17 and y >= 16 then
   stockMoney()
   v = 1
  elseif x >= 44 and y >= 15 and x <= 48 and y <= 17 then
   lockAccount()
   v = 1
  elseif x == 51 and y == 1 then
   term.setBackgroundColor(32768)
   term.clear()
   term.setCursorPos(1,1)
   v = 1
   break
  end
end

(I haven't put the sha256 code here, if you want it you can find it here: http://www.computercraft.info/forums2/index.php?/topic/8169-sha-256-in-pure-lua/ )
Arektor #38
Posted 17 July 2014 - 06:24 PM
Oh my god… It was soooo obviously…

I was reloading the table, but not the variable who is sended to the client…
Problem solved again. But thanks you to have tried to help me :)/>
Arektor #39
Posted 18 July 2014 - 09:42 PM
Again me, now I have a little error with the mod computronics (Should I really ask it here ?), I've the Tape Drive at the left of my computer, and I'm calling it "p" (p = peripheral.wrap("left"))
But when I try anything with it (like p.write("something :o/>"), p.play(), p.setLabel("A label ! \o/") etc.. it gave me the following error :
test:4: attempt to index ? (a nil value)

In other words, he doesn't see the Tape Drive…

Here is the code:

local input = assert(io.open("/music/Button.DFPWM")
local data = input:read("*a")
local tapeDrive = peripheral.wrap("bottom") -- The tape drive is under the computer
tapeDrive.write(data)
tapeDrive.seek(-tapeDrive.getSize())
tapeDrive.play()

Anyone know ?
asie #40
Posted 29 July 2014 - 06:59 PM
No, you should ask me directly next time if you have issues with one of my mods.

Okay. Are you using Minecraft 1.6.4? If yes, do you have OpenPeripheral installed? If yes, that's a bug.