summaryrefslogtreecommitdiffstatshomepage
path: root/plugins/timer/init.lua
diff options
context:
space:
mode:
author cracyc <cracyc@users.noreply.github.com>2016-04-07 21:29:40 -0500
committer cracyc <cracyc@users.noreply.github.com>2016-04-07 21:29:40 -0500
commita94eeb628fd9786a4a1d015a7d7fb3a7f40db5a0 (patch)
treec78f8ef0ff1f49be120364ba83d8cc324bfab273 /plugins/timer/init.lua
parent632b10cd0ec304c0367321a5be62164d00b18ff1 (diff)
plugins/timer: add sample plugin for game time [Carl]
Diffstat (limited to 'plugins/timer/init.lua')
-rw-r--r--plugins/timer/init.lua75
1 files changed, 75 insertions, 0 deletions
diff --git a/plugins/timer/init.lua b/plugins/timer/init.lua
new file mode 100644
index 00000000000..307a4d98d9a
--- /dev/null
+++ b/plugins/timer/init.lua
@@ -0,0 +1,75 @@
+-- license:BSD-3-Clause
+-- copyright-holders:Carl
+require('lfs')
+local exports = {}
+exports.name = "timer"
+exports.version = "0.0.1"
+exports.description = "Game play timer"
+exports.license = "The BSD 3-Clause License"
+exports.author = { name = "Carl" }
+
+local dummy = exports
+
+function dummy.startplugin()
+ local timer_path = "timer"
+ local timer_started = false
+ local total_time = 0
+ local start_time = 0
+
+ local function get_filename()
+ local path
+ if emu.softname() ~= "" then
+ path = timer_path .. '/' .. emu.romname() .. "_" .. emu.softname() .. ".time"
+ else
+ path = timer_path .. '/' .. emu.romname() .. ".time"
+ end
+ return path
+ end
+
+ emu.register_start(function()
+ local file
+ if timer_started then
+ total_time = total_time + (os.time() - start_time)
+ os.remove(get_filename()) -- truncate file
+ file = io.open(get_filename(), "w")
+ if not file then
+ lfs.mkdir(timer_path)
+ file = io.open(get_filename(), "w")
+ end
+ if file then
+ file:write(total_time)
+ file:close()
+ end
+ end
+ timer_started = true
+ local file = io.open(get_filename(), "r")
+ if file then
+ total_time = file:read("n")
+ file:close()
+ end
+ start_time = os.time()
+ end)
+
+ local function sectohms(time)
+ local hrs = math.floor(time / 3600)
+ local min = math.floor((time % 3600) / 60)
+ local sec = math.floor(time % 60)
+ return string.format("%02d:%02d:%02d", hrs, min, sec)
+ end
+
+ local function menu_populate()
+ local time = os.time() - start_time
+ return {{ "Current time", "", 32 },
+ { sectohms(time), "", 32 },
+ { "Total time", "", 32 },
+ { sectohms(total_time + time), "", 32 }}
+ end
+
+ local function menu_callback(index, event)
+ return true
+ end
+
+ emu.register_menu(menu_callback, menu_populate, "Timer")
+end
+
+return exports