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

Parallel AFK timer using os.pullEvent?

Started by ChunkeeMunkee, 30 November 2012 - 11:40 PM
ChunkeeMunkee #1
Posted 01 December 2012 - 12:40 AM
Hi guys,

I'm trying to make an AFK timer for a computer that will be used to make a user profile within my network. I basically want it so that the computer will restart after a set time, so that if a user walks away or logs off whilst registering it won't lock out the terminal for other users.

Here's my code so far:

Spoiler

function splash()
-- Establish splash screen (default terminal state)
term.clear()
term.setCursorPos(1,3)
print[[
.----..--.  .-. .-. .---..-.  .-.
| {_ / {} \ |  `| |/  ___}\ \/ /
| | /  /\  \| |\  |\	 } }  {
`-' `-'  `-'`-' `-' `---'  `--'
	.----..----. .-.	 .--.   .----..-. .-.
   { {__  | {}  }| |	/ {} \ { {__  | {_} |
   .-._} }| .--' | `--./  /\  \.-._} }| { } |
   `----' `-'	`----'`-'  `-'`----' `-' `-'
		.----. .---. .----. .----..----..-. .-.
	   { {__  /  ___}| {}  }| {_  | {_  |  `| |
	   .-._} }\	 }| .-. \| {__ | {__ | |\  |
	   `----'  `---' `-' `-'`----'`----'`-' `-'
	
	
Press any key...]]
os.pullEvent("key")
parallel.waitForAny(register(), AFKtimer())
end
function AFKtimer()
local time = 5
AFKtimer = os.startTimer(time)
print("Timer running...")
while true do
  local event, arg = os.pullEvent()
  if event == "timer" then
	if arg == AFKtimer then
	  print("Timeout!")
	  sleep(2)
	  os.reboot()
	end
  elseif event == "key" then
	print("Timer reset!")
	AFKtimer = os.startTimer(time)
  end
end
end
function register()
-- Introduce the user to the ship's reg process
term.clear()
term.setCursorPos(1,5)
print[[
Hi!
  This is just a bit of intro text about the ship, the registration process, and what's about to happen.
  It'll be really interesting! (Honest!)
]]
sleep(2)
print("Press any key...")
-- Place in a while loop?
while true do
  local event = os.pullEvent()
  if event == "key" then
  break
end
--Gather name and pass
end
end
splash()

I'm hoping to make it so that it'll reset the timer (currently set to 5 for testing) on keystrokes, but currently it's holding the start of the timer until the user presses a key after the second text display.

This is my first foray into parallel processes, and this has me stumped! Any help would be appreciated!

Thanks,
Chris.
Edited on 01 December 2012 - 03:15 AM
Kingdaro #2
Posted 01 December 2012 - 02:11 AM
This line

parallel.waitForAny(register(), AFKtimer())

should be


parallel.waitForAny(register, AFKtimer)
ChunkeeMunkee #3
Posted 01 December 2012 - 04:18 AM
This line

parallel.waitForAny(register(), AFKtimer())

should be


parallel.waitForAny(register, AFKtimer)

Thank you very much, just overlooked the parentheses. Works like a charm now!