2017-07-31 04:33:23 +02:00
|
|
|
-- get marp high score file from http://replay.marpirc.net/txt/scores3.htm
|
|
|
|
local dat = {}
|
2022-06-24 20:57:24 +02:00
|
|
|
|
2018-01-14 15:39:37 +01:00
|
|
|
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()
|
2022-06-24 20:57:24 +02:00
|
|
|
local file = 'scores3.htm'
|
2017-07-31 04:33:23 +02:00
|
|
|
|
2022-06-24 20:57:24 +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
|
|
|
|
|
2022-06-24 20:57:24 +02:00
|
|
|
-- scan file for version
|
2017-07-31 04:33:23 +02:00
|
|
|
for line in fh:lines() do
|
2022-06-24 20:57:24 +02:00
|
|
|
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
|
2022-06-24 20:57:24 +02:00
|
|
|
if (not ver) or (ver == dbver) then
|
2017-07-31 04:33:23 +02:00
|
|
|
fh:close()
|
2022-06-24 20:57:24 +02:00
|
|
|
ver = dbver
|
2017-07-31 04:33:23 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2022-06-24 20:57:24 +02:00
|
|
|
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()
|
2022-06-24 20:57:24 +02:00
|
|
|
ver = dbver
|
2017-07-31 04:33:23 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2022-06-24 20:57:24 +02:00
|
|
|
-- clean out previous data and update the version
|
2017-07-31 04:33:23 +02:00
|
|
|
if dbver then
|
2022-06-24 20:57:24 +02:00
|
|
|
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
|
|
|
|
|
2022-06-24 20:57:24 +02:00
|
|
|
fh:seek('set')
|
|
|
|
local buffer = fh:read('a')
|
2017-07-31 04:33:23 +02:00
|
|
|
|
|
|
|
local function gmatchpos()
|
|
|
|
local pos = 1
|
2022-06-24 20:57:24 +02:00
|
|
|
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
|
2022-06-24 20:57:24 +02:00
|
|
|
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
|
2022-06-24 20:57:24 +02:00
|
|
|
|
|
|
|
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
|
2022-06-24 20:57:24 +02:00
|
|
|
url = url .. '\t\n'
|
2017-07-31 04:33:23 +02:00
|
|
|
end
|
2022-06-24 20:57:24 +02:00
|
|
|
|
|
|
|
if (set ~= '') and (lastset ~= set) then
|
2017-07-31 04:33:23 +02:00
|
|
|
local lastdata = data
|
2022-06-24 20:57:24 +02:00
|
|
|
data = url .. plyr .. '\t' .. scr .. '\n'
|
2017-07-31 04:33:23 +02:00
|
|
|
return lastset, lastdata
|
|
|
|
end
|
2022-06-24 20:57:24 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2022-06-24 20:57:24 +02:00
|
|
|
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
|
2022-06-24 20:57:24 +02:00
|
|
|
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
|
2022-06-24 20:57:24 +02:00
|
|
|
query:finalize()
|
|
|
|
|
2017-07-31 04:33:23 +02:00
|
|
|
fh:close()
|
2022-06-24 20:57:24 +02:00
|
|
|
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
|
|
|
|
|
2018-01-14 21:22:05 +01:00
|
|
|
if db then
|
|
|
|
init()
|
|
|
|
end
|
2017-07-31 04:33:23 +02:00
|
|
|
|
|
|
|
function dat.check(set, softlist)
|
2022-06-24 20:57:24 +02:00
|
|
|
if softlist or (not ver) then
|
2017-07-31 04:33:23 +02:00
|
|
|
return nil
|
|
|
|
end
|
2022-06-24 20:57:24 +02:00
|
|
|
|
2017-07-31 04:33:23 +02:00
|
|
|
info = nil
|
2022-06-24 20:57:24 +02:00
|
|
|
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
|
2022-06-24 20:57:24 +02:00
|
|
|
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
|