Storing Match State History

I would like to know are there examples or suggested ways to store a match state history after a game has ended, for example into a database for analytics, or for a client to be able to later retrieve and replay what happened in the match?

I am doing a turn-based game, so my idea was to use a LinkedList - so during a change to nakama match state i would store an additional field called prevState which contains the whole previous state, all the way to the beginning. Just as an example, match state would look something like:

{
    turn: 2
    attack: 1
    defense: 1
    health: 4
    ...
    prevState: {
        turn: 1,
        attack: 1
        defense: 1
        health: 5
        ...
        prevState: {
           ... 
       } 
    }
}

Would you recommend against doing something like this, would there be performance consequences of keeping a large object like this in memory. After the match ends I would be able to store this match state into a DB and have the entire match history which is what I want to do but not sure if it’s a good idea.

Hi @dragonlobster,

Is your game deterministic? For example, would Player A taking Action X always have the same impact on the game state or is there an element of randomness? If it is deterministic then I would suggest simply storing an array of actions which can later be replayed by the client if they wanted to review the match.

It’s difficult to suggest what the best way to store match state history would be for your game without knowing how it works, but generally speaking however you decide to represent the historical game state you can store this in the Storage Engine when the match ends.

Hi @tom , thanks for your prompt reply. I think an array of the match state would work for me. It is for the most part, deterministic however there is one element of randomness - that is the shuffling of cards during certain parts of the game (it is a turn based card game). However I want to know where would this array go where i can push to it every time state changes? I am on typescript runtime so i was wondering is there a global place to store it while the game is playing. Would i be able to store it in nkruntime.Context for example. Just to clarify i’d like to do this all on the back end - even when i store it into storage engine in the end.

If you’re using server-authoritative matches you can store it as part of the match state itself, e.g. state.actionHistory = [];. If you’re not using authoritative matches, you can simply store it inside the storage engine and append to it every time an action is taken.

With regards to the shuffling, you can likely represent this as an entry in the state history too. Once the shuffle has been performed, store the result as an entry in the state history. You could for example have an entry that looks something like:

state.actionHistory = [..., {
  action: "shuffle",
  cardOrder: [5, 2, 3, 4, 1]
}];

I suspected so, thanks for that