start writing results back to disk
I make some effort to preserve order, but deleting results and then readding them appends to the bottom.
This commit is contained in:
parent
0e5f69b099
commit
f9e9b5e53c
|
@ -84,13 +84,18 @@ draw_data = function()
|
||||||
return
|
return
|
||||||
elseif v == nil then
|
elseif v == nil then
|
||||||
Data[t1][t2] = 2 Data[t2][t1] = 0
|
Data[t1][t2] = 2 Data[t2][t1] = 0
|
||||||
|
add_result(t1, t2)
|
||||||
elseif v == 2 then
|
elseif v == 2 then
|
||||||
Data[t1][t2] = 0 Data[t2][t1] = 2
|
Data[t1][t2] = 0 Data[t2][t1] = 2
|
||||||
|
delete_result(t1, t2)
|
||||||
|
add_result(t2, t1)
|
||||||
elseif v == 0 then
|
elseif v == 0 then
|
||||||
Data[t1][t2] = nil Data[t2][t1] = nil
|
Data[t1][t2] = nil Data[t2][t1] = nil
|
||||||
|
delete_result(t2, t1)
|
||||||
else
|
else
|
||||||
error('invalid value')
|
error('invalid value')
|
||||||
end
|
end
|
||||||
|
Global_state.next_save = Current_time + 3
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
@ -101,4 +106,4 @@ draw_data = function()
|
||||||
App.color{r=0, g=0, b=0}
|
App.color{r=0, g=0, b=0}
|
||||||
love.graphics.print(score(Data, t1), l+px, top+y*c+py)
|
love.graphics.print(score(Data, t1), l+px, top+y*c+py)
|
||||||
end
|
end
|
||||||
end
|
end
|
|
@ -1 +1,6 @@
|
||||||
Global_state = {} -- just anything we need across functions
|
-- just anything we need across functions
|
||||||
|
Global_state = {
|
||||||
|
button_handlers = {},
|
||||||
|
next_save = nil, -- if set, timestamp at which to save results to disk
|
||||||
|
results = {}, -- information about order in which to save to disk
|
||||||
|
}
|
|
@ -1,9 +1,11 @@
|
||||||
load_results = function(filename)
|
load_results = function(filename)
|
||||||
results = {}
|
results = {}
|
||||||
|
Global_state.results = {} -- cache ordering for when we modify Data
|
||||||
local f, err = App.open_for_reading(filename)
|
local f, err = App.open_for_reading(filename)
|
||||||
if err then error(err) end
|
if err then error(err) end
|
||||||
for line in f:lines() do
|
for line in f:lines() do
|
||||||
local subject, object = line:match('^%s*(%S+)%s+beat%s+(%S+)%s*$')
|
local subject, object = line:match('^%s*(%S+)%s+beat%s+(%S+)%s*$')
|
||||||
|
table.insert(Global_state.results, {subject, object})
|
||||||
if subject == nil then
|
if subject == nil then
|
||||||
error('incorrect format: '..line)
|
error('incorrect format: '..line)
|
||||||
end
|
end
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
on.update = function()
|
||||||
|
if Global_state.next_save and Global_state.next_save < Current_time then
|
||||||
|
save_results()
|
||||||
|
Global_state.next_save = nil
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1,10 @@
|
||||||
|
save_results = function()
|
||||||
|
if Results_filename == nil then return end
|
||||||
|
print('saving to '..Results_filename)
|
||||||
|
local f, err = App.open_for_writing(Results_filename)
|
||||||
|
if err then error(err) end
|
||||||
|
for _,result in ipairs(Global_state.results) do
|
||||||
|
f:write(('%s beat %s\n'):format(result[1], result[2]))
|
||||||
|
end
|
||||||
|
f:close()
|
||||||
|
end
|
|
@ -0,0 +1,6 @@
|
||||||
|
on.quit = function()
|
||||||
|
if Global_state.next_save then
|
||||||
|
save_results()
|
||||||
|
Global_state.next_save = nil
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1,3 @@
|
||||||
|
add_result = function(from, to)
|
||||||
|
table.insert(Global_state.results, {from, to})
|
||||||
|
end
|
|
@ -0,0 +1,7 @@
|
||||||
|
delete_result = function(from, to)
|
||||||
|
for i=1,#Global_state.results do
|
||||||
|
if eq(Global_state.results[i], {from, to}) then
|
||||||
|
table.remove(Global_state.results, i)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue