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

Away from home; Would this code work?

Started by Turb0s, 17 January 2017 - 04:05 AM
Turb0s #1
Posted 17 January 2017 - 05:05 AM
Hey guys, I am away from home right now and cannot run this code through CC but I have been able to use Notepad++. I would like to know if this code will run through Computercraft. It is a simple start to an OS but I would like to make sure that I am on the right track. Thank you very much. If you have any critiques I am more than happy to hear them.

-- ColtOS
os.pullEvent = os.pullEventRaw
term.clear()
term.setCursorPos(1,1)
print("ColtOS 0.1")
print("1 - Create Account")
print("2 - Login")
print("Any other key to exit to lua prompt")
input = read()
if input == "1" then {
term.clear()
term.setCursorPos(1,1)
-- Username Creation
write("Please Enter A Username:")
local username = ( read() )
term.clear()
term.setCursorPos(1,1)
-- Pass Creation
write("Thank you! Now, a Password:")
local password = ( read() )
term.clear()
term.setCursorPos(1,1)
}
-- Login
if input == "2"
write("Login")
write("Username:")
  if input = local username then {
  write("Password:")
  else
  -- Incorrect User
  write("User not found.")
  if input = local password then {
   term.clear()
   term.setCursorPos(1,1)
   print("ColtOS")
   -- Incorrect Pass
   else
   term.clear()
   term.setCursorPos(1,1)
   print("Incorrect!")
   os.reboot()
  }
}
else
term.clear()
term.setCursorPos(1,1)
end
																			 
Bomb Bloke #2
Posted 17 January 2017 - 05:39 AM
1) Open Mimic.
2) Edit and save a file.
3) Paste your code into that file via the sidebar.
4) ???
5) Profit.

You won't get full functionality, but it's handy for testing simpler bits of code like this.
Turb0s #3
Posted 18 January 2017 - 11:44 PM
Oh thanks :)/>
houseofkraft #4
Posted 23 January 2017 - 11:49 AM
I say this a lot, but please indent your code! It's very messy without indenting and you can spot issues a lot faster if you do. So please, indent your code.
zephar26 #5
Posted 24 January 2017 - 10:45 PM
Well, the user creation part is fine, but you're missing a few things for the login part. It might have just been a brain fart or something, but you're forgetting to ask the user for input when asking for both the username and password.

As for the initial input [check 1 for create user, and 2 for login], it's usually considered cleaner to make your second condition "if input == 1" an elseif statement instead, because it's checking the same condition from the same variable anyways.

There's more I could go on about, but maybe another day.

Edit:

On top of that, it might be better to set up a key event handler instead of relying on the user to input a letter and wait for them to hit enter to confirm.

Not to mention, it's usually most effective to write the username and passwords to an external file, that way you don't have to deal with them in your script and accidentally reset them or anything.
Edited on 25 January 2017 - 01:27 AM
KidBrine #6
Posted 26 January 2017 - 08:19 PM
If you haven't yet i fixed this program and made it export user info to a file named account. :)/>

the code is below.

-- ColtOS
--os.pullEvent = os.pullEventRaw
if fs.exists("account") then
  shell.run("account")
end
term.clear()
term.setCursorPos(1,1)
print("ColtOS 0.1")
print("1 - Create Account")
print("2 - Login")
print("Any other key to exit to lua prompt")
input = read()
if input == "1" then
  term.clear()
  term.setCursorPos(1,1)
  -- Username Creation
  write("Please Enter A Username:")
  local username = ( read() )
  term.clear()
  term.setCursorPos(1,1)
  -- Pass Creation
  write("Thank you! Now, a Password:")
  local password = ( read() )
  term.clear()
  term.setCursorPos(1,1)
  local File = fs.open("account","w")
  File.writeLine('username = "'..username..'"')
  File.writeLine('password = "'..password..'"')
  File.close()
elseif input == "2" then
  print("Login")
  write("Username:")
  input = read()
  if input == username then
	write("Password:")
	input = read("*")
	if input == password then
	  term.clear()
	  term.setCursorPos(1,1)
	  print("ColtOS")
	  -- Incorrect Pass
	else
	  term.clear()
	  term.setCursorPos(1,1)
	  print("Incorrect!")
	  os.reboot()
	end
  else
	-- Incorrect User
	write("User not found.")
  end
else
  term.clear()
  term.setCursorPos(1,1)
end

Oh and I also indented for you!
Edited on 26 January 2017 - 07:23 PM