tmedwards / sugarcube-2

SugarCube is a free (gratis and libre) story format for Twine/Twee.
https://www.motoslave.net/sugarcube/2/
BSD 2-Clause "Simplified" License
177 stars 41 forks source link

Serialize all save slots and deserialize them without loading a specific slot #305

Closed sizhongbin closed 2 months ago

sizhongbin commented 3 months ago

Is your feature request related to a problem? I'm working on a game with cloud save feature based on Supabase database. The process in my plan should be like: :: Start User login with useid and password. If successd, goto :: LoadGame

:: LoadGame

  1. User select one of the three save slots and load it from localStorage. Or, upload/download all the three save slots to/from database.
    • Load from localStorage: Save.slots.load(slot)
    • Upload: Serialize all the 3 slots as a JSON string like: {"slot0":"Slot 0 Serialized String","slot1":"Slot 1 Serialized String","slot2":"Slot 2 Serialized String"}, and INSERT/UPDATE the JSON to database with userid.
    • Download: SELECT the JSON by userid from database and deserialize all the 3 slots. Cover the current localStorage. Then goto :: LoadGame again. Now user can select one of the three save slots that just downloaded and deserialized to localStorage.

The problem is that serialize/deserialize feature only works on the current slot. And a load will be immediately processed after deserialize.

Describe the solution you'd like. Save.serializeAll(): Return a JSON string like: {"slot0":"Slot 0 Serialized String","slot1":"Slot 1 Serialized String","slot2":"Slot 2 Serialized String"} Save.deserializeAll(string): Deserializes the given save string created via Save.serializeAll() and cover the current localStorage.

Describe alternatives you've considered. Maybe some other solutions using the current Serialization API?

Additional context. Nope.

tmedwards commented 3 months ago

Unless I'm mistaken, this is already part of v2.37.0.

sizhongbin commented 3 months ago

Unless I'm mistaken, this is already part of v2.37.0.

Thanks for quick reply. I've checked the 2.37.0 document. Looks like the serialize ion API is replaced by base64. But I cannot find any difference except the Promise return. I'm still only able to encode the current save slot or load it immediately. I cannot encode/decode ALL the slots. Did I miss something?

tmedwards commented 3 months ago

Yes. Check out the documentation on Save.browser.export() and Save.browser.import().

sizhongbin commented 3 months ago

Yes. Check out the documentation on Save.browser.export() and Save.browser.import().

Oh i figured out. Yes the import/export can backup/restore all the slots to/from a save file. But unfortunately, what i need is a JSON string so that i can SELECT/INSERT/UPDATE it in postgresSQL easily.

I guess I should create my own save/load function instead of using the brower save API, so that I can set/get the JSON string I want to/from localStorage and do something before loading.

tmedwards commented 3 months ago

I suppose I could add .exportToBase64() and .importFromBase64() methods or something similar.

tmedwards commented 3 months ago

Made some changes:

The latter two are what you should need.

sizhongbin commented 3 months ago

Great! Thanks a lot. I'll try it soon.