diff options
Diffstat (limited to 'plugins/data/data_marp.lua')
-rw-r--r-- | plugins/data/data_marp.lua | 186 |
1 files changed, 107 insertions, 79 deletions
diff --git a/plugins/data/data_marp.lua b/plugins/data/data_marp.lua index ed077a8c5bd..09fddf11ef9 100644 --- a/plugins/data/data_marp.lua +++ b/plugins/data/data_marp.lua @@ -1,124 +1,144 @@ -- get marp high score file from http://replay.marpirc.net/txt/scores3.htm local dat = {} + local db = require("data/database") local ver, info local function init() - local filepath - local dbver - local fh - local file = "scores3.htm" - - for path in mame_manager:ui():options().entries.historypath:value():gmatch("([^;]+)") do - filepath = lfs.env_replace(path) .. "/" .. file - fh = io.open(filepath, "r") - if fh then - break - end - end + local file = 'scores3.htm' - local stmt = db.prepare("SELECT version FROM version WHERE datfile = ?") - db.check("reading marp version") - stmt:bind_values(file) - if stmt:step() == db.ROW then - dbver = stmt:get_value(0) - end - stmt:finalize() - - if not fh and dbver then - -- data in database but missing file, just use what we have - ver = dbver - return - elseif not fh then + local fh, filepath, tablename, dbver = db.open_data_file(file) + if not fh then + if dbver then + -- data in database but missing file, just use what we have + ver = dbver + end return - elseif not dbver then - db.exec("CREATE TABLE \"" .. file .. [[" ( - romset VARCHAR NOT NULL, - data CLOB NOT NULL)]]) - db.check("creating marp table") - db.exec("CREATE INDEX \"romset_" .. file .. "\" ON \"" .. file .. "_idx\"(romset)") - db.check("creating marp index") end + -- scan file for version for line in fh:lines() do - local match = line:match("Top Scores from the MAME Action Replay Page %(([%w :]+)%)") + local match = line:match('Top Scores from the MAME Action Replay Page %(([%w :]+)%)') if match then ver = match break end end - - if not ver then + if (not ver) or (ver == dbver) then fh:close() + ver = dbver return end - if ver == dbver then + if not dbver then + db.exec( + string.format( + [[CREATE TABLE "%s" ( + romset VARCHAR NOT NULL, + data CLOB NOT NULL, + PRIMARY KEY(romset));]], + tablename)) + db.check('creating MARP table') + end + + db.exec([[BEGIN TRANSACTION;]]) + if not db.check('starting MARP transaction') then fh:close() + ver = dbver return end + -- clean out previous data and update the version if dbver then - db.exec("DELETE FROM \"" .. file .. "\"") - db.check("deleting marp") - db.exec("DELETE FROM \"" .. file .. "_idx\"") - db.check("deleting marp index") - stmt = db.prepare("UPDATE version SET version = ? WHERE datfile = ?") - db.check("updating marp version") - else - stmt = db.prepare("INSERT INTO version VALUES (?, ?)") - db.check("inserting marp version") + db.exec(string.format([[DELETE FROM "%s";]], tablename)) + if not db.check('deleting previous MARP data') then + db.exec([[ROLLBACK TRANSACTION;]]) + fh:close() + ver = dbver + return + end + end + if not db.set_version(file, ver) then + db.check('updating MARP version') + db.exec([[ROLLBACK TRANSACTION;]]) + fh:close() + ver = dbver + return end - stmt:bind_values(ver, file) - stmt:step() - stmt:finalize() - fh:seek("set") - local buffer = fh:read("a") - db.exec("BEGIN TRANSACTION") - db.check("beginning marp transation") + fh:seek('set') + local buffer = fh:read('a') local function gmatchpos() local pos = 1 - local set, data = "" + local set = '' + local data = '' local function iter() local lastset = set while true do local spos, scr, plyr, stype, ltype - local url = "" - spos, pos, set, stype, scr, plyr, ltype = buffer:find("\n%s*([%w_]*)%-?(%w-) :%s*(%d+) [;:] ([^:]+): [^%[\n]*%[?([%w ]*)[^\n]*", pos) + spos, pos, set, stype, scr, plyr, ltype = buffer:find('\n%s*([%w_]*)%-?(%w-) :%s*(%d+) [;:] ([^:]+): [^%[\n]*%[?([%w ]*)[^\n]*', pos) if not spos then return nil end - if set ~= "" then - if ltype ~= "" then - url = ltype .. "\t\n" - else - url = "" + + local url = '' + if set ~= '' then + if ltype ~= '' then + url = ltype .. '\t\n' + end + url = url .. 'http://replay.marpirc.net/r/' .. set + if stype ~= '' then + url = url .. '-' .. stype end - url = url .. "http://replay.marpirc.net/r/" .. set .. ((stype ~= "") and ("-" .. stype) or "") .. "\t\n" + url = url .. '\t\n' end - if set ~= "" and lastset and lastset ~= set then + + if (set ~= '') and (lastset ~= set) then local lastdata = data - data = url .. plyr .. "\t" .. scr .. "\n" + data = url .. plyr .. '\t' .. scr .. '\n' return lastset, lastdata end - data = data .. ((url ~= "") and ("\n" .. url) or "") .. plyr .. "\t" .. scr .. "\n" + + if url ~= '' then + data = data .. '\n' .. url + end + data = data .. plyr .. '\t' .. scr .. '\n' end end return iter end + local query = db.prepare( + string.format([[INSERT INTO "%s" (romset, data) VALUES (?, ?)]], tablename)) for set, data in gmatchpos() do - stmt = db.prepare("INSERT INTO \"" .. file .. "\" VALUES (?, ?)") - db.check("inserting marp values") - stmt:bind_values(set, data) - stmt:step() - stmt:finalize() + query:bind_values(set, data) + while true do + local status = query:step() + if status == db.DONE then + break + elseif status == db.BUSY then + emu.print_error('Database busy: inserting MARP data') + query:finalize() + db.exec([[ROLLBACK TRANSACTION;]]) + fh:close() + ver = dbver + return + elseif result ~= db.ROW then + db.check('inserting MARP data') + break + end + end + query:reset() end + query:finalize() + fh:close() - db.exec("END TRANSACTION") - db.check("ending marp transation") + db.exec([[COMMIT TRANSACTION;]]) + if not db.check('committing MARP transaction') then + db.exec([[ROLLBACK TRANSACTION;]]) + ver = dbver + end end if db then @@ -126,18 +146,26 @@ if db then end function dat.check(set, softlist) - if softlist or not ver or not db then + if softlist or (not ver) then return nil end + info = nil - local stmt = db.prepare("SELECT data FROM \"scores3.htm\" AS f WHERE romset = ?") - db.check("reading marp data") - stmt:bind_values(set) - if stmt:step() == db.ROW then - info = "#j2\n" .. stmt:get_value(0) + local query = db.prepare([[SELECT data FROM "scores3.htm" WHERE romset = ?;]]) + query:bind_values(set) + while not info do + local status = query:step() + if status == db.ROW then + info = '#j2\n' .. query:get_value(0) + elseif status == db.DONE then + break + elseif status ~= db.BUSY then + db.check('reading MARP data') + break + end end - stmt:finalize() - return info and _("MARPScore") or nil + query:finalize() + return info and _p('plugin-data', 'MARPScore') or nil end function dat.get() |