Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

After sessionStorage.js is included in the JS environment, the contents of the playerX_sessionStorage.js file are loaded and then pumped in via sessionStorage.fromJSON(data) (where 'data' is the string representation of the contents of the playerX_sessionStorage.js file).

That's it.

Future development: lcoalStorage

If the sessionStorage idea proposed above gets accepted, then an obvious next-step would be to implement localStorage.

localStorage works in pretty much the same way as sessionStorage with some notable differences:

  • It persists across multiple games (whereas sessionStorage is just for a single game)
  • It is scoped to a script (whereas sessionStorage is scoped to a player)
  • Each time data is added/changed, the associated localStorage objects all need updating (whereas sessionStorage files only get updated when saving a game)

I would assume that we wouldn't want localStorage data being sent across the intertubes during a network game, which is fine - the localStorage for an AI script (eg. NullBot) would be taken from the client machine on which the script is running. If the same script is running on other machines elsewhere, then they will use their own local (client machine) localStorage data file.

In order to enable localStorage, a new JS API function would need exposing, something like:

Code Block
setLocalStorage(fromPlayer,key,val)

Inside the localStorage.setItem() method, it would do the normal stuff - in other words update its own internal data object. But then it would also call setLocalStorage(me,key,JSON.stringify(val))

When setLocalStorage is called, the C++ code would do something like this:

Code Block
// simplified pseudo code
 
function setLocalStorage(fromPlayer,key,val) {
  // work out what script called setLocalStorage (eg. nullbot_1.1.js):
  var playerEngine = getEngineForPlayer(fromPlayer);
  var playerScript = playerEngine.scriptFileName;
  // update all other players where applicable:
  for (var i=0; i<maxPlayers; i++) {
    // no need to update the script that triggered setLocalStorage():
    if (i != fromPlayer) {
      // is this player's engine running same script as the calling script?
      var engine = getEngineForPlayer(i);
      if (engine.scriptFileName == playerScript) {
        // update it's localStorage with the new data
        engine.globalObject.localStorage.setItem(key,val,false,true);
      }
    }
  }
  // now persist the localStorage data to file on disk
  var jsonData = playerEngine.globalObject.localStorage.toJSON();
  var fileName = "path/to/config/folder/"+playerScript+"_localStorage.js";
  createOrReplaceFile(filename,jsonData);
}

Obviously there will be a load of stuff I'm not aware of in the way things work in the C++ environment, and there's probably several alternate approaches that could be used, but hopefully the pseudo-code above illustrates the macroscopic elements that would be required?

So, what do you think?