Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tArgs = { ... }
- --usage string
- usageString = "Clock & Timer\nParameters:\n\'clock\' to display current time,\n\'timer <seconds>\' to set timer\n"
- --magical constants
- --clock update in seconds
- clockUpdate = 1
- --low timer value in ticks
- timerLow = 40
- --current server time on script startup
- startupTime = os.time()
- --resume table
- resumeT = {}
- --operation mode
- resumeT["mode"] = ""
- --precision in ticks (hardcoded)
- resumeT["precision"] = 20
- --timer goal setting in ticks
- resumeT["timerGoal"] = 0
- --last saved timer value in ticks
- resumeT["timerVal"] = 0
- --alarm side (hardcoded)
- resumeT["alarmSide"] = "top"
- function ReadResume()
- if not fs.exists("resume.f") then
- print("Trying to resume without resume file, aborting...")
- return
- end
- local file = fs.open("resume.f","r")
- local sT = file.readAll()
- resumeT = textutils.unserialize(sT)
- file.close()
- return
- end
- function WriteResume()
- local file = fs.open("resume.f","w")
- local sT = textutils.serialize(resumeT)
- file.write(sT)
- file.close()
- return
- end
- --kiloticks in 24hr format as args
- function KiloTicksPassed(from, to)
- if (from > to) then
- --midnight
- to = to+24.0
- end
- return (to-from)
- end
- function TimerFinish()
- --message
- term.setCursorPos(1,5)
- print("ALARM")
- --alarm
- rs.setOutput(resumeT["alarmSide"], true)
- --delete resume.f
- fs.delete("resume.f")
- print("\nPress any key...")
- os.pullEvent("key")
- rs.setOutput(resumeT["alarmSide"], false)
- return
- end
- --returns time as days:H:mm:ss
- function PrintReadableTime(kiloticks)
- return(math.floor(kiloticks/24.0)..":"..textutils.formatTime(kiloticks%24, true)..":"..(kiloticks%24*50))
- end
- if fs.exists("resume.f") then
- --resuming
- ReadResume()
- elseif #tArgs < 1 then
- --running by hand without args
- print( usageString )
- end
- if (tArgs[1] == "clock") then
- resumeT["mode"] = "clock"
- end
- if (tArgs[1] == "timer") then
- if #tArgs < 2 then
- print(usageString)
- return
- else
- resumeT["mode"] = "timer"
- resumeT["timerGoal"] = tArgs[2]*20
- end
- end
- WriteResume()
- if (resumeT["mode"] == "clock") then
- while (true) do
- term.clear()
- term.setCursorPos(1,1)
- currentTime = textutils.formatTime(os.time(), true)
- print(currentTime)
- sleep(clockUpdate)
- end
- end
- if (resumeT["mode"] == "timer") then
- local ticksToGo = resumeT["timerGoal"] - resumeT["timerVal"]
- --check if time interval has already passed
- if (ticksToGo < timerLow) and (KiloTicksPassed(startupTime,os.time())*1000>ticksToGo) then
- TimerFinish()
- return
- end
- local curTime = 0.0
- while (true) do
- curTime = os.time()
- WriteResume()
- ticksToGo = resumeT["timerGoal"] - resumeT["timerVal"]
- if ticksToGo < resumeT["precision"] then
- TimerFinish()
- return
- else
- term.clear()
- term.setCursorPos(1,1)
- print("Passed:")
- print(PrintReadableTime(resumeT["timerVal"]/1000).." or "..resumeT["timerVal"]/20.." seconds")
- print("Remaining:")
- print(PrintReadableTime(ticksToGo).." or "..(ticksToGo/20).." seconds")
- -- os.startTimer(resumeT["precision"])
- -- os.pullEvent(timer)
- sleep(resumeT["precision"]/20)
- resumeT["timerVal"] = resumeT["timerVal"] + KiloTicksPassed(curTime,os.time())*1000
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement