Page 1 of 1

#PB_Localstorage seems not to work

Posted: Wed Aug 12, 2020 11:58 pm
by AMpos
why doesnt work this code?

Code: Select all

Procedure Callback(Status, Filename$, File, SizeRead)
    Select Status
      Case #PB_Status_Saved
        ; File correctly saved
        
      Case #PB_Status_Error
        ; File saving has failed
    EndSelect
  EndProcedure

  If CreateFile(0, "Text.txt", @Callback(),#PB_LocalStorage)      ; we create a new text file
    For a=1 To 10
      WriteStringN(0, "TEST")
    Next
;     ExportFile(0, "text/plain")
    CloseFile(0)                      
  EndIf
  
  ReadFile(0,"Text.txt",@Callback(),#PB_LocalStorage)
  Debug ReadString(0)
  CloseFile(0)

Re: #PB_Localstorage seems not to work

Posted: Thu Aug 13, 2020 12:16 am
by Peter
you can only read the file after it has been saved:

Code: Select all

EnableExplicit

Procedure ReadFileCallback(Status, Filename$, File, Size)
  
  Protected a
  
  Select Status
      
    Case #PB_Status_Loaded
      
      Debug "#PB_Status_Loaded"
      
      For a = 1 To 10
        Debug ReadString(0)
      Next
      
      CloseFile(0)
      
    Case #PB_Status_Error
      
      Debug "#PB_Status_Error"
      
  EndSelect
  
EndProcedure

Procedure CreateFileCallback(Status, Filename$, File, SizeRead)
  
  Select Status
      
    Case #PB_Status_Saved
      
      Debug "#PB_Status_Saved"
      
      ReadFile(0, "Text.txt", @ReadFileCallback(), #PB_LocalStorage) ; <- here!
      
    Case #PB_Status_Error
      
      Debug "#PB_Status_Error"
      
  EndSelect
  
EndProcedure

Define a

If CreateFile(0, "Text.txt", @CreateFileCallback(), #PB_LocalStorage)
  
  For a = 1 To 10
    WriteStringN(0, "TEST" + Str(a))
  Next
  
  CloseFile(0)
  
EndIf
Greetings ... Peter

Re: #PB_Localstorage seems not to work

Posted: Thu Aug 13, 2020 6:52 pm
by KianV
Peter wrote:you can only read the file after it has been saved
This took me a while to get my head around too.
The file operations run out of sync with the top-to-bottom flow of your program.
If you put a couple of 'Debug' statements in your original code :

Code: Select all

Procedure Callback(Status, Filename$, File, SizeRead)
    Select Status
      Case #PB_Status_Saved
        ; File correctly saved
       Debug "The file is now saved"      ;HERE
      Case #PB_Status_Error
        ; File saving has failed
    EndSelect
  EndProcedure

  If CreateFile(0, "Text.txt", @Callback(),#PB_LocalStorage)      ; we create a new text file
    For a=1 To 10
      WriteStringN(0, "TEST")
    Next
;     ExportFile(0, "text/plain")
    CloseFile(0)                     
  EndIf
 
  Debug "Now read the file"             ; AND HERE
  ReadFile(0,"Text.txt",@Callback(),#PB_LocalStorage)
  Debug ReadString(0)
  CloseFile(0)
you will see that it starts to 'Read' the file before it has finished the 'Save' operation, which is still going on in the background.
Hence the need for Callback routines.

As always, Peter's example shows you how to do it properly.

Re: #PB_Localstorage seems not to work

Posted: Thu Aug 13, 2020 10:29 pm
by AMpos
I'm having a hard time with this...

I got it working ith Fred code... but with a single output/input file. I place my readstrings loop inside the readfilecallback() and it works but...

the problem is that my original code wants to write and read 7 files (each file has a player name and scores). And I can not have it working.

Something like this:

Code: Select all

Dim name$(7)

Procedure Callback(Status, Filename$, File, SizeRead)
    Select Status
      Case #PB_Status_Saved
        ; File correctly saved
       
      Case #PB_Status_Error
        ; File saving has failed
    EndSelect
    
EndProcedure

For j=0 To 7
  If CreateFile(0, "p"+Str(j), @Callback(),#PB_LocalStorage)      ; we create a new text file
    WriteStringN(0, "Player Name "+Str(j))
    ;write scores blah blah blah
    CloseFile(0)                     
  EndIf
Next

For j=0 To 7
  ReadFile(0,"p"+Str(j),@Callback(),#PB_LocalStorage)
  name$(j)=ReadString(0):Debug name$(j)
  ;read scores blah blah blah
  CloseFile(0)
Next

PS: I know it is not working. This code is just "what I want to do", not "how to do it"

The idea is that my program will save all the player names and scores every time they change, so if the browser crash or the app reloads, it will start reading the last saved player names and scores, to continue were it was left:

Code: Select all

START:  PSEUDO CODE
readscores()
fillscreen()
repeat ;main loop
  if score=change
   savesccores():fillscreen()
  endif
forever


Re: #PB_Localstorage seems not to work

Posted: Thu Aug 13, 2020 11:31 pm
by Peter
Although I don't understand why you want to save data for seven players in a single browser session:
You can save the data for these seven players in a single file. There is no need to create seven files.

Re: #PB_Localstorage seems not to work

Posted: Thu Aug 13, 2020 11:36 pm
by AMpos
Because sometimes, if you open another program/tab on your mobile, when you come back to your previous tab, it is loaded again, so scores will be lost.

I "can not" record all of this in a single file because I dont know how many scores has each player.

You can see my program here:

https://www.frikland.com/sb/CVC.htm

BTW, tonite I made a bingo :)

https://www.frikland.com/sb/bingo.htm

Re: #PB_Localstorage seems not to work

Posted: Thu Aug 13, 2020 11:47 pm
by Peter
Alternatively you can use my Preferences module, which is operated exactly like in PureBasic: https://github.com/spiderbytes/Preferences

Re: #PB_Localstorage seems not to work

Posted: Fri Aug 14, 2020 8:10 am
by AMpos
Peter wrote:Alternatively you can use my Preferences module, which is operated exactly like in PureBasic: https://github.com/spiderbytes/Preferences
I will give it a shot. Thank you.

Re: #PB_Localstorage seems not to work

Posted: Fri Aug 14, 2020 8:16 am
by KianV
AMpos wrote:I "can not" record all of this in a single file because I dont know how many scores has each player.
Could you not just write out each score accompanied by a byte with the player number?
The player names could also be held in the file as strings.

Code: Select all

PSEUDOCODE:

CreateFile

For j=0 To 6
WriteStringN(playername$(j)
Next j

For j=1 to numberofscores
WriteByte(playernumber)
WriteWord(scorevalue)
Next j

CloseFile
The data could then be read back in a similar fashion.

Alternatively you could record a byte for each player to say how many scores are recorded, giving a file in the form:

#player1name$ #howmanyscoresforP1 #P1score1 #P1score2 #P1score3 ........ #player2name$ #howmanyscoresforP2 #P2score1 #P2score2 #P2score3 ...