Author Topic: Count how many file exists with a certain prefix ?  (Read 923 times)

MetalZelda

  • Hero Member
  • *****
  • Posts: 504
    • View Profile
Re: Count how many file exists with a certain prefix ?
« Reply #15 on: March 13, 2017, 04:54:29 pm »
Yeah i'm completely right with Diarandor's solution, yet, something is unclear because you need to open the concerned save to get the value and write it to another file, maybe using Chris's solution will work, allowing unlimited saves

That also get me to the point where input customisation need to be stored in a file so we can bind custom inputs, even if we are not in a game, but that's something else.

MetalZelda

  • Hero Member
  • *****
  • Posts: 504
    • View Profile
Re: Count how many file exists with a certain prefix ?
« Reply #16 on: March 14, 2017, 11:28:42 am »
I find a solution, but, I'm unable to extract the needed values in order to insert them in a table, io.lines(file) always return an error. (bad argument #1 to 'lines' (string expected, got userdata))

EDIT: it works, but now how can I extract the value I need from this certain file ?

Doing through setfenv, don't know if that's the right way

Code: Lua
  1. function title_screen:do_file(i, name)
  2.   local slot = self.slots[i]
  3.  
  4.   local environment = {
  5.     prop = function()
  6.           slot.max_health = _max_life,
  7.           slot.current_health = _current_life
  8.           slot.player_name = player_name
  9.           slot.tunic = _ability_tunic
  10.           slot.shield = _ability_shield
  11.           slot.sword = _ability_sword
  12.         end,
  13.   }
  14.          
  15.   setmetatable(environment, {
  16.     __index = function()
  17.       return function() end
  18.     end
  19.   })
  20.  
  21.   local chunk = sol.main.load_file(name)
  22.   setfenv(chunk, environment)
  23.   chunk()
  24. end
« Last Edit: March 14, 2017, 12:28:22 pm by MetalZelda »

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1041
    • View Profile
Re: Count how many file exists with a certain prefix ?
« Reply #17 on: March 14, 2017, 02:04:44 pm »
Just use file:lines() instead of io.lines(). io.lines takes a filename, not a file.

setfenv also works (parsing the data file as Lua) but I don't like it so much because it is a function that no longer exists in Lua 5.2. If we switch to a newer Lua version than 5.1 one day, there will be a compatibility issue. Not a big deal, because Lua 5.2 introduces a new (and better) way, but still some manual changes required.

MetalZelda

  • Hero Member
  • *****
  • Posts: 504
    • View Profile
Re: Count how many file exists with a certain prefix ?
« Reply #18 on: March 14, 2017, 06:04:08 pm »
I finally got the file to load and store values in a array, things like hearts display need to be done completely manually.

Thanks for the help

MetalZelda

  • Hero Member
  • *****
  • Posts: 504
    • View Profile
Re: Count how many file exists with a certain prefix ?
« Reply #19 on: March 14, 2017, 07:41:49 pm »
Is it possible to arrange an array in order to display at first the last game played, the modification date of the file might be enough but is it possible through lua ?
100 saves slots should be enough, there are no more lags whe loading all files

Edit: restict to 90 saves, above 90 saves (90 * 90), the surface won't even draw, don't know if it's a bug when a surface above 8100 pixels won't draw
« Last Edit: March 14, 2017, 10:05:10 pm by MetalZelda »

Diarandor

  • Hero Member
  • *****
  • Posts: 758
  • Cats are cool! (ΦωΦ)
    • View Profile
Re: Count how many file exists with a certain prefix ?
« Reply #20 on: March 14, 2017, 11:11:05 pm »
I think 20 or 30 savegames is more than enough. In order to get first the last game played, I think the easiest solution is to save another variable with the index of the last savegame. Each time you save a game you should update the file with your savegame menu info (I think that is what you are already doing, but I am not sure), and at that time you can save this index too.

llamazing

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
Re: Count how many file exists with a certain prefix ?
« Reply #21 on: March 14, 2017, 11:16:35 pm »
You can use a custom sort function to arrange things however you want. The following example assumes that the date is some number that increases over time.

Code: Lua
  1. local function sort_by_date(a, b)
  2.         return a.date > b.date --descending order
  3.         --return a.date < b.date --ascending order
  4. end
  5.  
  6. local example_data = {
  7.         { date = 56, name = "second" },
  8.         { date = 99, name = "first" },
  9.         { date = 21, name = "third" },
  10. }
  11.  
  12. print"Original Order:"
  13. for i,data in ipairs(example_data) do
  14.         print(string.format("%i - date: %f, name: %s", i, data.date or 0, data.name))
  15. end
  16.  
  17. table.sort(example_data, sort_by_date)
  18.  
  19. print"\nSorted by Date:"
  20. for i,data in ipairs(example_data) do
  21.         print(string.format("%i - date: %f, name: %s", i, data.date or 0, data.name))
  22. end

MetalZelda

  • Hero Member
  • *****
  • Posts: 504
    • View Profile
Re: Count how many file exists with a certain prefix ?
« Reply #22 on: March 15, 2017, 12:04:56 pm »
You can use a custom sort function to arrange things however you want. The following example assumes that the date is some number that increases over time.

Code: Lua
  1. local function sort_by_date(a, b)
  2.         return a.date > b.date --descending order
  3.         --return a.date < b.date --ascending order
  4. end
  5.  
  6. local example_data = {
  7.         { date = 56, name = "second" },
  8.         { date = 99, name = "first" },
  9.         { date = 21, name = "third" },
  10. }
  11.  
  12. print"Original Order:"
  13. for i,data in ipairs(example_data) do
  14.         print(string.format("%i - date: %f, name: %s", i, data.date or 0, data.name))
  15. end
  16.  
  17. table.sort(example_data, sort_by_date)
  18.  
  19. print"\nSorted by Date:"
  20. for i,data in ipairs(example_data) do
  21.         print(string.format("%i - date: %f, name: %s", i, data.date or 0, data.name))
  22. end

This might be the only solution, yet, the only parameter needed here is the file's last modifcation date which will be stored in file.date, then this will be fairly possible with your function

This is how infos are retrieved from a savegame file

Code: Lua
  1. function title_screen:do_file(i, name)
  2.   local file = sol.file.open(name)
  3.   local slot = { dungeon = {} }
  4.  
  5.   -- This should be where the last modification date will be stored
  6.   -- slot.date = ???
  7.  
  8.   local font, font_size = sol.language.get_dialog_font()
  9.   slot.player_name_text = sol.text_surface.create{
  10.     font = font,
  11.     font_size = font_size,
  12.   }
  13.  
  14.   for line in chunk:lines() do
  15.     -- Player stats
  16.     if line:match("player_name")then
  17.           slot.player_name_text:set_text(line:sub(16, -2))
  18.         end
  19.         if line:match("_ability_shield") then
  20.           slot.shield = line:sub(19)
  21.         end
  22.         if line:match("_ability_sword") then
  23.           slot.sword = line:sub(18)
  24.         end
  25.         if line:match("_ability_tunic") then
  26.           slot.tunic = line:sub(18)
  27.         end
  28.         -- Game stats
  29.         if line:match("_current_life") then
  30.           slot.current_life = line:sub(17)
  31.         end
  32.         if line:match("_max_life") then
  33.           slot.max_life = line:sub(13)
  34.         end
  35.         if line:match("hero_mode") then
  36.           slot.hero_mode = line:sub(13)
  37.         end
  38.         if line:match("finished_game") then
  39.           slot.finished_game = line:sub(17)
  40.         end
  41.        
  42.         -- Dungeon state
  43.         if line:match("dungeon_1_finished") then
  44.           slot.dungeon[1] = true
  45.         end
  46.        -- blablabla
  47.   end
  48.  
  49.   file:close()
  50.   slot.savegame_number = i
  51.   self.slots[i] = slot
  52. end

The file organisation is done somewhere else when another function, more explainatory, is called, here, i just want the last modification date
« Last Edit: March 15, 2017, 12:10:56 pm by MetalZelda »

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1041
    • View Profile
Re: Count how many file exists with a certain prefix ?
« Reply #23 on: March 15, 2017, 01:44:55 pm »
There is probably a shorter way to parse the savegame files. Here is an example of the string.gmatch official doc:
Code: Lua
  1. -- The next example collects all pairs key=value from the given string into a table:
  2.      t = {}
  3.      s = "from=world, to=Lua"
  4.      for k, v in string.gmatch(s, "(%w+)=(%w+)") do
  5.        t[k] = v
  6.      end
  7.  
This example can be adapted to include string values (enclosed inside double quotes) and it should just work.