2016-04-07 21:29:40 -05:00
|
|
|
-- license:BSD-3-Clause
|
2021-11-06 05:20:59 +11:00
|
|
|
-- copyright-holders:Vas Crabb
|
2023-04-07 06:20:40 +10:00
|
|
|
-- TODO: track time properly across soft reset and state load
|
2021-11-06 05:20:59 +11:00
|
|
|
local exports = {
|
|
|
|
|
name = 'timer',
|
|
|
|
|
version = '0.0.3',
|
|
|
|
|
description = 'Game play timer',
|
|
|
|
|
license = 'BSD-3-Clause',
|
|
|
|
|
author = { name = 'Vas Crabb' } }
|
2016-04-07 21:29:40 -05:00
|
|
|
|
2016-04-07 21:42:42 -05:00
|
|
|
local timer = exports
|
2016-04-07 21:29:40 -05:00
|
|
|
|
2023-04-07 06:20:40 +10:00
|
|
|
local reset_subscription, stop_subscription
|
|
|
|
|
|
2016-04-07 21:42:42 -05:00
|
|
|
function timer.startplugin()
|
2016-04-07 21:29:40 -05:00
|
|
|
local total_time = 0
|
|
|
|
|
local start_time = 0
|
2016-04-08 13:18:04 -05:00
|
|
|
local play_count = 0
|
2021-11-06 05:20:59 +11:00
|
|
|
local emu_total = emu.attotime()
|
2016-04-07 21:29:40 -05:00
|
|
|
|
2021-11-06 05:20:59 +11:00
|
|
|
local reference = 0
|
|
|
|
|
local lastupdate
|
2021-11-08 11:51:29 +11:00
|
|
|
local highlight -- hacky - workaround for the menu not remembering the selected item if its ref is nullptr
|
2016-11-08 09:52:10 +01:00
|
|
|
|
2016-04-08 08:33:12 -05:00
|
|
|
|
2016-04-07 21:29:40 -05:00
|
|
|
local function sectohms(time)
|
2021-11-06 05:20:59 +11:00
|
|
|
local hrs = time // 3600
|
|
|
|
|
local min = (time % 3600) // 60
|
2016-04-08 13:18:04 -05:00
|
|
|
local sec = time % 60
|
2021-11-06 05:20:59 +11:00
|
|
|
return string.format(_p('plugin-timer', '%03d:%02d:%02d'), hrs, min, sec)
|
2016-04-07 21:29:40 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local function menu_populate()
|
2021-10-22 03:46:00 +11:00
|
|
|
lastupdate = os.time()
|
2021-11-06 05:20:59 +11:00
|
|
|
local refname = (reference == 0) and _p('plugin-timer', 'Wall clock') or _p('plugin-timer', 'Emulated time')
|
|
|
|
|
local time = (reference == 0) and (lastupdate - start_time) or manager.machine.time.seconds
|
|
|
|
|
local total = (reference == 0) and (total_time + time) or (manager.machine.time + emu_total).seconds
|
2021-10-22 03:46:00 +11:00
|
|
|
return
|
2021-11-06 05:20:59 +11:00
|
|
|
{
|
|
|
|
|
{ _p("plugin-timer", "Reference"), refname, (reference == 0) and 'r' or 'l' },
|
|
|
|
|
{ '---', '', '' },
|
|
|
|
|
{ _p("plugin-timer", "Current time"), sectohms(time), "off" },
|
|
|
|
|
{ _p("plugin-timer", "Total time"), sectohms(total), "off" },
|
2022-03-23 20:27:30 +11:00
|
|
|
{ _p("plugin-timer", "Play Count"), tostring(play_count), "off" } },
|
2021-11-08 11:51:29 +11:00
|
|
|
highlight,
|
2021-10-22 03:46:00 +11:00
|
|
|
"idle"
|
2016-04-07 21:29:40 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local function menu_callback(index, event)
|
2021-11-06 05:20:59 +11:00
|
|
|
if (index == 1) and ((event == 'left') or (event == 'right') or (event == 'select')) then
|
|
|
|
|
reference = reference ~ 1
|
|
|
|
|
return true
|
|
|
|
|
end
|
2021-11-08 11:51:29 +11:00
|
|
|
highlight = index
|
2021-10-22 03:46:00 +11:00
|
|
|
return os.time() > lastupdate
|
2016-04-07 21:29:40 -05:00
|
|
|
end
|
|
|
|
|
|
2021-11-06 05:20:59 +11:00
|
|
|
|
2023-04-07 06:20:40 +10:00
|
|
|
reset_subscription = emu.add_machine_reset_notifier(
|
|
|
|
|
function ()
|
2021-11-06 05:20:59 +11:00
|
|
|
if emu.romname() ~= '___empty' then
|
|
|
|
|
start_time = os.time()
|
|
|
|
|
local persister = require('timer/timer_persist')
|
|
|
|
|
total_time, play_count, emu_total = persister:start_session()
|
|
|
|
|
end
|
|
|
|
|
end)
|
|
|
|
|
|
2023-04-07 06:20:40 +10:00
|
|
|
stop_subscription = emu.add_machine_stop_notifier(
|
|
|
|
|
function ()
|
2021-11-06 05:20:59 +11:00
|
|
|
if emu.romname() ~= '___empty' then
|
|
|
|
|
local persister = require('timer/timer_persist')
|
|
|
|
|
persister:update_totals(start_time)
|
|
|
|
|
end
|
|
|
|
|
end)
|
|
|
|
|
|
2021-10-31 12:31:16 +11:00
|
|
|
emu.register_menu(menu_callback, menu_populate, _p("plugin-timer", "Timer"))
|
2016-04-07 21:29:40 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
return exports
|