mame/plugins/data/data_marp.lua

180 lines
3.7 KiB
Lua
Raw Normal View History

2017-07-31 04:33:23 +02:00
-- get marp high score file from http://replay.marpirc.net/txt/scores3.htm
local dat = {}
local db = require("data/database")
2017-08-27 03:07:21 +02:00
local ver, info
2017-07-31 04:33:23 +02:00
local function init()
local file = 'scores3.htm'
2017-07-31 04:33:23 +02:00
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
2017-07-31 04:33:23 +02:00
return
end
-- scan file for version
2017-07-31 04:33:23 +02:00
for line in fh:lines() do
local match = line:match('Top Scores from the MAME Action Replay Page %(([%w :]+)%)')
2017-07-31 04:33:23 +02:00
if match then
ver = match
break
end
end
if (not ver) or (ver == dbver) then
2017-07-31 04:33:23 +02:00
fh:close()
ver = dbver
2017-07-31 04:33:23 +02:00
return
end
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
2017-07-31 04:33:23 +02:00
fh:close()
ver = dbver
2017-07-31 04:33:23 +02:00
return
end
-- clean out previous data and update the version
2017-07-31 04:33:23 +02:00
if dbver then
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
2017-07-31 04:33:23 +02:00
end
fh:seek('set')
local buffer = fh:read('a')
2017-07-31 04:33:23 +02:00
local function gmatchpos()
local pos = 1
local set = ''
local data = ''
2017-07-31 04:33:23 +02:00
local function iter()
local lastset = set
while true do
local spos, scr, plyr, stype, ltype
spos, pos, set, stype, scr, plyr, ltype = buffer:find('\n%s*([%w_]*)%-?(%w-) :%s*(%d+) [;:] ([^:]+): [^%[\n]*%[?([%w ]*)[^\n]*', pos)
2017-07-31 04:33:23 +02:00
if not spos then
return nil
end
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
2017-07-31 04:33:23 +02:00
end
url = url .. '\t\n'
2017-07-31 04:33:23 +02:00
end
if (set ~= '') and (lastset ~= set) then
2017-07-31 04:33:23 +02:00
local lastdata = data
data = url .. plyr .. '\t' .. scr .. '\n'
2017-07-31 04:33:23 +02:00
return lastset, lastdata
end
if url ~= '' then
data = data .. '\n' .. url
end
data = data .. plyr .. '\t' .. scr .. '\n'
2017-07-31 04:33:23 +02:00
end
end
return iter
end
local query = db.prepare(
string.format([[INSERT INTO "%s" (romset, data) VALUES (?, ?)]], tablename))
2017-07-31 04:33:23 +02:00
for set, data in gmatchpos() do
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()
2017-07-31 04:33:23 +02:00
end
query:finalize()
2017-07-31 04:33:23 +02:00
fh:close()
db.exec([[COMMIT TRANSACTION;]])
if not db.check('committing MARP transaction') then
db.exec([[ROLLBACK TRANSACTION;]])
ver = dbver
end
2017-07-31 04:33:23 +02:00
end
if db then
init()
end
2017-07-31 04:33:23 +02:00
function dat.check(set, softlist)
if softlist or (not ver) then
2017-07-31 04:33:23 +02:00
return nil
end
2017-07-31 04:33:23 +02:00
info = nil
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
2017-07-31 04:33:23 +02:00
end
query:finalize()
return info and _p('plugin-data', 'MARPScore') or nil
2017-07-31 04:33:23 +02:00
end
function dat.get()
return info
end
function dat.ver()
return ver
end
return dat