diff --git a/0003-draw_data b/0003-draw_data index 7e8872a..f9a3de9 100644 --- a/0003-draw_data +++ b/0003-draw_data @@ -84,13 +84,18 @@ draw_data = function() return elseif v == nil then Data[t1][t2] = 2 Data[t2][t1] = 0 + add_result(t1, t2) elseif v == 2 then Data[t1][t2] = 0 Data[t2][t1] = 2 + delete_result(t1, t2) + add_result(t2, t1) elseif v == 0 then Data[t1][t2] = nil Data[t2][t1] = nil + delete_result(t2, t1) else error('invalid value') end + Global_state.next_save = Current_time + 3 end, }) end @@ -101,4 +106,4 @@ draw_data = function() App.color{r=0, g=0, b=0} love.graphics.print(score(Data, t1), l+px, top+y*c+py) end -end +end \ No newline at end of file diff --git a/0011-Global_state b/0011-Global_state index 2195237..aaedc60 100644 --- a/0011-Global_state +++ b/0011-Global_state @@ -1 +1,6 @@ -Global_state = {} -- just anything we need across functions \ No newline at end of file + -- 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 +} \ No newline at end of file diff --git a/0020-load_results b/0020-load_results index 718f818..a75bb08 100644 --- a/0020-load_results +++ b/0020-load_results @@ -1,9 +1,11 @@ load_results = function(filename) results = {} + Global_state.results = {} -- cache ordering for when we modify Data local f, err = App.open_for_reading(filename) if err then error(err) end for line in f:lines() do local subject, object = line:match('^%s*(%S+)%s+beat%s+(%S+)%s*$') + table.insert(Global_state.results, {subject, object}) if subject == nil then error('incorrect format: '..line) end diff --git a/0023-on.update b/0023-on.update new file mode 100644 index 0000000..63f17a4 --- /dev/null +++ b/0023-on.update @@ -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 \ No newline at end of file diff --git a/0024-save_results b/0024-save_results new file mode 100644 index 0000000..50ca820 --- /dev/null +++ b/0024-save_results @@ -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 \ No newline at end of file diff --git a/0025-on.quit b/0025-on.quit new file mode 100644 index 0000000..516b0c2 --- /dev/null +++ b/0025-on.quit @@ -0,0 +1,6 @@ +on.quit = function() + if Global_state.next_save then + save_results() + Global_state.next_save = nil + end +end \ No newline at end of file diff --git a/0026-add_result b/0026-add_result new file mode 100644 index 0000000..9ea43f4 --- /dev/null +++ b/0026-add_result @@ -0,0 +1,3 @@ +add_result = function(from, to) + table.insert(Global_state.results, {from, to}) +end \ No newline at end of file diff --git a/0027-delete_result b/0027-delete_result new file mode 100644 index 0000000..53c0cc4 --- /dev/null +++ b/0027-delete_result @@ -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 \ No newline at end of file