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

AppliedEnergetics and CC Project

Started by want_some, 12 August 2014 - 12:48 PM
want_some #1
Posted 12 August 2014 - 02:48 PM
First off, i am kinda new to lua. but i been doing some programming and learning lua for a while now. but just the basics of lua tho. Now i want to learn more and get into more advanced stuff like username/password programming to allow users to access different things.

Second, I been working on a little project on my own server. This project was going to use CC and AE ME system. Basically, program will ask the user for their username/password to gain access to their ME Drive (it will have their own ME storage disks that they will need to make themselves). The login computer will then send a message to the computer that will then run the program to send a redstone signal to the dark ME cable to allow that ME Drive to be apart of the ME system.

Here is the picture of a test setup:
Spoiler

How this (should) work:

The player will need to login (of course!) and the program on the Login Computer will then check the table for the verification. If true, it will then send a message to the computer with the ID (username) the player logged in as. First, the message will send a command to run the "On" program to turn on the redstone output to turn the dark cable on (duh!) The program will then wait for a set time (default of 60 seconds) then will automatically send the command to run the "Off" program to the same computer to turn off the dark cable.

What is going wrong:
The login displays perfectly. Asks for the username (aka ID) and password. After I type in the username 393 then the password player1, the program says the welcome message and sleeps for 2 secs like i coded, but then displays the "error" message and quits the program. I'm not sure if the problem is from the table (i did a quick google on the setup) or the "if" statements (i wrote them myself, still learning about the more complex setups of theses like this one)

I thought about not doing a table setup to store the username and password so later i can add a "Change Password" option to the program in case a player gave the password out to others.

This is my code as of right now.
Spoiler

local uTable = {
393 = "player1",
394 = "player2",
395 = "player3",
396 = "player4",
397 = "player5",
398 = "player6"
}
local i = 60

write("ID: ")
ID = read()
write("Password: ")
pass = read('*')
if uTable[ID] == pass then
  print("Welcome! Here is your items, "..ID.."!")
  sleep(2)
else --uTable[ID] ~= pass then
  print"ID/Password Combination is wrong!"
  sleep(5)
  --os.shutdown()
end
if ID == 393 then
  shell.run"clear"
  rednet.open("back")
  rednet.send(393, On)
  print"Connection Open."
  sleep(1)
  repeat
  shell.run("clear")
  print("Conection Will close in "..i.." secs.")
  sleep(1)
  i = i - 1
  until i == 0
  shell.run("clear")
  print"Connection Closed!"
  rednet.send(393, Off)
  os.shutdown()
elseif ID == 394 then
  shell.run"clear"
  rednet.open("back")
  rednet.send(394, On)
  print"Connection Open."
  sleep(1)
  repeat
  shell.run("clear")
  print("Conection Will close in "..i.." secs.")
  sleep(1)
  i = i - 1
  until i == 0
  shell.run("clear")
  print"Connection Closed!"
  rednet.send(394, Off)
  os.shutdown()
elseif ID == 395 then
  shell.run"clear"
  rednet.open("back")
  rednet.send(395, On)
  print"Connection Open."
  sleep(1)
  repeat
  shell.run("clear")
  print("Conection Will close in "..i.." secs.")
  sleep(1)
  i = i - 1
  until i == 0
  shell.run("clear")
  print"Connection Closed!"
  rednet.send(395, Off)
  os.shutdown()
elseif ID == 396 then
  shell.run"clear"
  rednet.open("back")
  rednet.send(396, On)
  print"Connection Open."
  sleep(1)
  repeat
  shell.run("clear")
  print("Conection Will close in "..i.." secs.")
  sleep(1)
  i = i - 1
  until i == 0
  shell.run("clear")
  print"Connection Closed!"
  rednet.send(396, Off)
  os.shutdown()
elseif ID == 397 then
  shell.run"clear"
  rednet.open("back")
  rednet.send(397, On)
  print"Connection Open."
  sleep(1)
  repeat
  shell.run("clear")
  print("Conection Will close in "..i.." secs.")
  sleep(1)
  i = i - 1
  until i == 0
  shell.run("clear")
  print"Connection Closed!"
  rednet.send(397, Off)
  os.shutdown()
elseif ID == 398 then
  shell.run"clear"
  rednet.open("back")
  rednet.send(398, On)
  print"Connection Open."
  sleep(1)
  repeat
  shell.run("clear")
  print("Connection Will close in "..i.." secs.")
  sleep(1)
  i = i - 1
  until i == 0
  shell.run("clear")
  print"Connection Closed!"
  rednet.send(398, Off)
  os.shutdown()
else
  print"Error!"
end

FYI: i checked the message sending code with a program that will send the message with a "computerID" var equal to the network and it went to the right computer and was able to turn on the dark cabel and then 60 seconds later, turn it off. So i know that portion is good.
Edited on 12 August 2014 - 03:12 PM
want_some #2
Posted 12 August 2014 - 08:27 PM
I been searching how to fix this but not getting anything. I'm really starting to think the problem is with the "if" statements. Can anyone give me an example of the correct way the if statements should be for this? I been reading the wikis and other pages on the web but not getting anywhere..any help will be great!
hilburn #3
Posted 12 August 2014 - 08:57 PM
At the beginning of each of your if statements you have made a typo


shell.run("clear") not shell.run"clear"

even better is
term.clear()
Edited on 12 August 2014 - 06:58 PM
0099 #4
Posted 12 August 2014 - 09:41 PM
1. The mistake is that read() always returns a string, even if it is a string of digits, so convert it to a number with tonumer(). Also (what a coincidence!) { 393 = "player1" } makes a field with a string key, to make the key a number, you need { [393] = "player1" }.

2. Keep in mind, that when the key is a string, its concatenation with message "Welcome! Here is your items, " is wrong, so you will need a reverse conversion with tostring().

3. Instead of using so many ifs and identical chunks of code, when your ID is a number, you can just write rednet.send(ID, message).

4. Speaking of messages, you will need to use "On" and "Off" words in quotes, because they are strings.

5. hilburn, it isn't a typo. If a function requires only 1 argument and this argument is a string or a table, you can write it without parentheses.
want_some #5
Posted 12 August 2014 - 10:20 PM
ok, i updated the code.

1. the table was updated to {[393] = "player1"} for all "users"

2. the welcome message works fine, why would i change it? the number is the ID, and the "player1" is the password. Later on, i will change this to be a set user name in later updates.

3. i know there is repeated code, this is just a rough draft of the program and i will go back later to shorten it up. Right now i just want to get things working first so i can learn from this.

4. they worked fine when i used it to send the message to each computer manually. the client computer just sends the message basically like a command to run the On and Oft program that i have writen on the other computers.

So far with the updates, nothing worked. I still get the display asking for ID and Password. i enter in the password and ID and i get the password/ID wrong.
0099 #6
Posted 13 August 2014 - 12:26 AM
4. they worked fine when i used it to send the message to each computer manually. the client computer just sends the message basically like a command to run the On and Oft program that i have writen on the other computers.

Let me guess, you then checked them on another computer like if message == On? If so, you just sent non-existing variable (nil) to the other computer and compared it to another non-existing variable. And of course nil == nil returns true, and On == On. But it won't work if you send Off, because Off == nil == nil == On.

Anyway, you forgot to add tonumber(), that's why the welcome message is still working. And, when you do this, please post your updated code.
Edited on 12 August 2014 - 11:10 PM
natedogith1 #7
Posted 13 August 2014 - 02:19 AM
Anyway, you forgot to add tonumber(), that's why the welcome message is still working. And, when you do this, please post your updated code.
Actually, since lua does automatic conversions this shouldn't be an issue
Bomb Bloke #8
Posted 13 August 2014 - 04:10 AM
Sometimes. It depends on the situation. It's generally easier from a bug-tracking perspective not to rely on that behaviour, and instead be specific about the types you want.
Edited on 13 August 2014 - 02:14 AM
Dragon53535 #9
Posted 13 August 2014 - 04:10 AM
I decided to look through your code and fix anything that either bugs me, or would cause it not to work correctly. If i get it wrong, well then you should be able to fix the rest of it. Now I will be commenting it as i go through to help you.
Spoiler

local uTable = {
["393"] = "player1",
["394"] = "player2",
["395"] = "player3",
["396"] = "player4",
["397"] = "player5",
["398"] = "player6"
} --[[ each key is a string you're looking for as that's the username you're typing, so you need to put square brackets around it and quotes inside of that as you are looking for it.']]
function main() -- a function called main, which is a block of code that contains the other bits of code
local i = 60
term.clear() -- clears the screen
term.setCursorPos(1,1) -- moves the cursor to the top left as it normally would if you were to run the program clear
print("ID: ")
write("Password: ")
term.setCursorPos(5,1) -- sets the spot where you input your username to be after ID:
local ID = read() -- you should really define these as local variables.
term.setCursorPos(11,2) -- sets the spot where you input the password after Password:
local pass = read('*')
if uTable[ID] === pass then -- you're looking in your table for what you inputed as the ID so if i put in 393 it would look for uTable["393"] which is defined in the table above as the pass being "player1"'
  print("Welcome! Here are your items, "..ID.."!") -- i'm a grammar nazi, it should be here ARE not here IS
  sleep(2)
elseif uTable[ID] ~= pass then -- this should be an elseif because you're looking for a condition to match rather than if all else fails.
  print("ID/Password Combination is wrong!") -- print is a function and as so you need ()
  sleep(5)
  main() -- restarts the function so that it can keep going.
end
term.clear()
term.setCursorPos(1,1) -- i prefer doing this rather than shell.run("clear") just because i am more used to it.
  rednet.open("super")
  local compID = tonumber(ID) -- Making a variable called CompID to be the ID they entered.
  rednet.send(compID, "On") -- You need quotes for strings
  print("Connection Open.") -- i am not going to stress this enough, for print and many many many many MANY other things you need the parenthesis () or it WILL NOT work.
  sleep(1)
  repeat
  term.clear()
  term.setCursorPos(1,1)
  print("Conection Will close in "..i.." secs.")
  sleep(1)
  i = i - 1
  until i == 0
  term.clear()
  term.setCursorPos(1,1)
  print("Connection Closed!) -- () needed
  rednet.send(compID, "Off") -- the second part is a string, so it needs ""
  main()
end -- ends the function main() you need this after every function you create
main() -- after having the program load the function main() and know what it is, you start it up by typing main()
-- There your entire code is cleaned up and running.

I broke your code a little bit so that you can figure the little bits i messed up that would make the computer work correctly just because i'm slightly evil. Look through the code, correct the errors i made, and you should be fine. There are a total of 3 errors, and they should be relatively easy to find if you read it thoroughly
Edited on 13 August 2014 - 06:52 AM
want_some #10
Posted 13 August 2014 - 12:22 PM
I decided to look through your code and fix anything that either bugs me, or would cause it not to work correctly. If i get it wrong, well then you should be able to fix the rest of it. Now I will be commenting it as i go through to help you.
Spoiler

local uTable = {
["393"] = "player1",
["394"] = "player2",
["395"] = "player3",
["396"] = "player4",
["397"] = "player5",
["398"] = "player6"
} --[[ each key is a string you're looking for as that's the username you're typing, so you need to put square brackets around it and quotes inside of that as you are looking for it.']]
function main() -- a function called main, which is a block of code that contains the other bits of code
local i = 60
term.clear() -- clears the screen
term.setCursorPos(1,1) -- moves the cursor to the top left as it normally would if you were to run the program clear
print("ID: ")
write("Password: ")
term.setCursorPos(5,1) -- sets the spot where you input your username to be after ID:
local ID = read() -- you should really define these as local variables.
term.setCursorPos(11,2) -- sets the spot where you input the password after Password:
local pass = read('*')
if uTable[ID] === pass then -- you're looking in your table for what you inputed as the ID so if i put in 393 it would look for uTable["393"] which is defined in the table above as the pass being "player1"'
  print("Welcome! Here are your items, "..ID.."!") -- i'm a grammar nazi, it should be here ARE not here IS
  sleep(2)
elseif uTable[ID] ~= pass then -- this should be an elseif because you're looking for a condition to match rather than if all else fails.
  print("ID/Password Combination is wrong!") -- print is a function and as so you need ()
  sleep(5)
  main() -- restarts the function so that it can keep going.
end
term.clear()
term.setCursorPos(1,1) -- i prefer doing this rather than shell.run("clear") just because i am more used to it.
  rednet.open("super")
  local compID = tonumber(ID) -- Making a variable called CompID to be the ID they entered.
  rednet.send(compID, "On") -- You need quotes for strings
  print("Connection Open.") -- i am not going to stress this enough, for print and many many many many MANY other things you need the parenthesis () or it WILL NOT work.
  sleep(1)
  repeat
  term.clear()
  term.setCursorPos(1,1)
  print("Conection Will close in "..i.." secs.")
  sleep(1)
  i = i - 1
  until i == 0
  term.clear()
  term.setCursorPos(1,1)
  print("Connection Closed!) -- () needed
  rednet.send(compID, "Off") -- the second part is a string, so it needs ""
  main()
end -- ends the function main() you need this after every function you create
main() -- after having the program load the function main() and know what it is, you start it up by typing main()
-- There your entire code is cleaned up and running.

I broke your code a little bit so that you can figure the little bits i messed up that would make the computer work correctly just because i'm slightly evil. Look through the code, correct the errors i made, and you should be fine. There are a total of 3 errors, and they should be relatively easy to find if you read it thoroughly

Thank you for your help! and i found the errors, two was easy and one was kinda hard.

Errors i found and fixed.


if uTable[ID] === pass then -- you're looking in your table for what you inputed as the ID so if i put in 393 it would look for uTable["393"] which is defined in the table above as the pass being "player1"'
had an extra "=" , removed that.


print("Connection Closed!) -- () needed

missing the end " to finish the string. (was hard to catch before running the code the first time)


  rednet.open("super")

changed "super" to "back". kinda easy to spot after reading the code the second time.


This code worked perfectly! i will save this code and try to change it a bit to add options to let players change the id name and passwords after logging in for the first time. thank you for your help!
Dragon53535 #11
Posted 14 August 2014 - 05:54 PM
Well then there you go, now if you want to attempt to have username's and passwords i would look into the fs API, an easy tutorial for it is Here. Eventually if you're wanting to make the passwords more secure i would use GravityScore's SHA-256 CC lua adaptation Here.