summaryrefslogtreecommitdiffstatshomepage
path: root/plugins/timer/init.lua
blob: c0f478363989e0e024acd1de17c52b2c9a2e24a1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888 } /* Comment */
.highlight .err { color: #A61717; background-color: #E3D2D2 } /* Error */
.highlight .k { color: #080; font-weight: bold } /* Keyword */
.highlight .ch { color: #888 } /* Comment.Hashbang */
.highlight .cm { color: #888 } /* Comment.Multiline */
.highlight .cp { color: #C00; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888 } /* Comment.Single */
.highlight .cs { color: #C00; font-weight: bold; background-color: #FFF0F0 } /* Comment.Special */
.highlight .gd { color: #000; background-color: #FDD } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #A00 } /* Generic.Error */
.highlight .gh { color: #333 } /* Generic.Heading */
.highlight .gi { color: #000; background-color: #DFD } /* Generic.Inserted */
.highlight .go { color: #888 } /* Generic.Output */
.highlight .gp { color: #555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666 } /* Generic.Subheading */
.highlight .gt { color: #A00 } /* Generic.Traceback */
.highlight .kc { color: #080; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #080; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #080; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #080 } /* Keyword.Pseudo */
.highlight .kr { color: #080; font-weight: bold } 
-- license:BSD-3-Clause
-- copyright-holders:Carl
require('lfs')
local sqlite3 = require('lsqlite3')
local exports = {}
exports.name = "timer"
exports.version = "0.0.2"
exports.description = "Game play timer"
exports.license = "The BSD 3-Clause License"
exports.author = { name = "Carl" }

local timer = exports

function timer.startplugin()
	local dir = lfs.env_replace(manager:options().entries.homepath:value())
	local timer_db = dir .. "/timer/timer.db"
	local timer_started = false
	local total_time = 0
	local start_time = 0
	local play_count = 0

	local function save()
		total_time = total_time + (os.time() - start_time)

		local db = assert(sqlite3.open(timer_db))
		
		local insert_stmt = assert( db:prepare("INSERT OR IGNORE INTO timer VALUES (?, ?, 0, 0)") )
		insert_stmt:bind_values(emu.romname(), emu.softname())
		insert_stmt:step()
		insert_stmt:reset()
		
		local update_stmt = assert( db:prepare("UPDATE timer SET total_time=?, play_count=? WHERE driver=? AND software=?") )
		update_stmt:bind_values(total_time, play_count,emu.romname(), emu.softname())
		update_stmt:step()
		update_stmt:reset()
				
		assert(db:close() == sqlite3.OK) 
	end


	emu.register_start(function()
		local file
		if timer_started then
			save()
		end
		timer_started = true
		lfs.mkdir(dir .. '/timer')
		local db = assert(sqlite3.open(timer_db))
		local found=false
		db:exec([[select * from sqlite_master where name='timer';]], function(...) found=true return 0 end)
		if not found then 		
			db:exec[[  CREATE TABLE timer (
						driver		VARCHAR(32) PRIMARY KEY,
						software 	VARCHAR(40),
						total_time	INTEGER NOT NULL,
						play_count	INTEGER NOT NULL
					  ); ]] 
		end

		local stmt, row
		stmt = db:prepare("SELECT total_time, play_count FROM timer WHERE driver = ? AND software = ?")
		stmt:bind_values(emu.romname(), emu.softname())
		if (stmt:step() == sqlite3.ROW) then
			row = stmt:get_named_values()
			play_count = row.play_count
			total_time = row.total_time
		else
			play_count = 0
			total_time = 0
		end

		assert(db:close() == sqlite3.OK) 

		start_time = os.time()
		play_count = play_count + 1
	end)

	emu.register_stop(function()
		timer_started = false
		save()
		total_time = 0
		play_count = 0
	end)

	local function sectohms(time)
		local hrs = math.floor(time / 3600)
		local min = math.floor((time % 3600) / 60)
		local sec = time % 60
		return string.format("%03d:%02d:%02d", hrs, min, sec)
	end

	local function menu_populate()
		local time = os.time() - start_time
		return {{ "Current time", "", "off" },
			{ sectohms(time), "", "off" },
			{ "Total time", "", "off" },
			{ sectohms(total_time + time), "", "off" },
			{ "Play Count", "", "off" },
			{ play_count, "", "off" }}
	end

	local function menu_callback(index, event)
		return true
	end

	emu.register_menu(menu_callback, menu_populate, "Timer")
end

return exports