Jump to content




Help optimizing my code?

help

5 replies to this topic

#1 DarkEyeDragon

  • Members
  • 31 posts
  • LocationBelgium

Posted 13 June 2015 - 08:51 PM

http://pastebin.com/T2SejYff --Pastebin link.
--Disable the terminating of programs
os.pullEvent = os.pullEventRaw
--------------------------------------
loginValue = false
value = 0
goToRoot = false
--Startup Function-
function startup()
  term.clear()
  term.setCursorPos(1,1)
  print("Booting System...")
  while value < 17 do
    value = value + 1
    paintutils.drawPixel(value,2, colors.red)
    sleep(0)
  end
  paintutils.drawLine(1,2,17,2, colors.green)
  term.setCursorPos(5,2)
  print("Complete")
  paintutils.drawPixel(0,0,colors.black)
  term.setCursorPos(1,4)
end
--Login Fucntion--
function login()
  loggedInAs = null
  if loginValue == false then
    print("Username:")
    local username = read()
    print("Password:")
    local password = read("*")
    if username == "Dark" and password == "Dark" then
	  print("Logged in as ", username)
	  loggedInAs = username
	  loginValue = true
    else
	  print("Invalid password.")
	  sleep(2)
	  startup()
    end
  else
    print("logging in as: " , username)
  end
end
----------------------
-- end of functions --
----------------------
startup()
print("Available actions: terminal, login, logout, root")
while goToRoot == false do
  input = read()
  if input == "terminal" then
    print("Enabeling AE Terminal")
    login()
  end
  if input == "login" then
    login()
  end
  if input == "logout" then
    loginValue = false
    print("Successfully logged out.")
  end
  if input == "root" then
    if loginValue == false then
	  print("Root access requires you to be logged in.")
	  login()
    end
    if loginValue == true then
	   goToRoot = true
    end
  end
end
startup()

How could I improve this code? I am quite sure there are way 'cleaner' and easier ways of doing this. Any suggestions?

#2 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 14 June 2015 - 02:01 AM

  • You should undo your change to os.pullEvent() as the script ends.
  • Don't forget about "elseif"!
  • Implement variable localisation.
  • You could simply use the "break" keyword instead of checking your "goToRoot" variable.
  • "loggedInAs" doesn't appear to serve any purpose.
  • "terminal" and "login" both do the same thing.
  • Your "while value < 17 do" loop would be better phrased as "for value = 1, 17 do", ditching the "value = 0" and "value = value + 1" lines.
  • "<variable> == true" is redundant; just use "<variable>". If it equals true, then that means it was already true!
  • "not <variable>" is a neater method of writing "<variable> == false".

Edited by Bomb Bloke, 14 June 2015 - 02:01 AM.


#3 DarkEyeDragon

  • Members
  • 31 posts
  • LocationBelgium

Posted 14 June 2015 - 08:12 AM

View PostBomb Bloke, on 14 June 2015 - 02:01 AM, said:

  • You should undo your change to os.pullEvent() as the script ends.
  • Don't forget about "elseif"!
  • Implement variable localisation.
  • You could simply use the "break" keyword instead of checking your "goToRoot" variable.
  • "loggedInAs" doesn't appear to serve any purpose.
  • "terminal" and "login" both do the same thing.
  • Your "while value < 17 do" loop would be better phrased as "for value = 1, 17 do", ditching the "value = 0" and "value = value + 1" lines.
  • "<variable> == true" is redundant; just use "<variable>". If it equals true, then that means it was already true!
  • "not <variable>" is a neater method of writing "<variable> == false".
Thanks. I'll be get that fixed. You're a great help to nubs like me :P

#4 DarkEyeDragon

  • Members
  • 31 posts
  • LocationBelgium

Posted 14 June 2015 - 08:28 AM

View PostBomb Bloke, on 14 June 2015 - 02:01 AM, said:

  • You should undo your change to os.pullEvent() as the script ends.
  • Don't forget about "elseif"!
  • Implement variable localisation.
  • You could simply use the "break" keyword instead of checking your "goToRoot" variable.
  • "loggedInAs" doesn't appear to serve any purpose.
  • "terminal" and "login" both do the same thing.
  • Your "while value < 17 do" loop would be better phrased as "for value = 1, 17 do", ditching the "value = 0" and "value = value + 1" lines.
  • "<variable> == true" is redundant; just use "<variable>". If it equals true, then that means it was already true!
  • "not <variable>" is a neater method of writing "<variable> == false".

Ok i altered the script. Theres 1 issue i noticed now. Previously the var value would stay equal to 17 as long as the computer was on. This draws a progressbar. But once the system was completely start up it wouldn't have to fill the progress bar again. Because the value was already 17. So when i would use the clear command it would trigger the startup() function. The progress bar would already be filled when its drawn again. Now that I changed it to "for value = 1,17 do" it would refill the progress bar every time. Would in that case using a variable to store the value in be a better option?

Edited by DarkEyeDragon, 14 June 2015 - 08:32 AM.


#5 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 14 June 2015 - 08:36 AM

Yes, I suppose it would. I'd assumed you'd've wanted the bar to fill every time.

#6 DarkEyeDragon

  • Members
  • 31 posts
  • LocationBelgium

Posted 15 June 2015 - 11:47 AM

View PostBomb Bloke, on 14 June 2015 - 08:36 AM, said:

Yes, I suppose it would. I'd assumed you'd've wanted the bar to fill every time.

Thats fine. Its not like it has any function. Its more of an astetic thingy. Thanks for you help though! Finally starting to get my around in coding :P





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users