Disclaimer: I didn't implement such a thing by myself. since it is a little time consuming. so maybe someone has a better solution.
My understanding how to achieve time traveling.
You start with your base state:
{
players: {}
}
Now you add a player on server tick 1145
state looks like this after that
{
players: {
"420": {
name: "Player 1"
}
}
}
Now you add a player on server tick 5000
state looks like this after that
{
players: {
"420": {
name: "Player 1"
},
"125": {
name: "Player 2"
}
}
}
so what you have to do is: save somewhere the base state. and every action you do on the state.
so example:
{
baseState: {
players: {}
},
actions: [
{
type: "AddPlayer",
data: {
id: "420",
name: "Player 1"
tick: 1145
},
{
type: "AddPlayer",
data: {
id: "125",
name: "Player 2"
}
tick: 5000
}
]
}
So when the functions which mutate the state of the room are pure functions.
You can just recalculate the state with all actions to a specific tick.
if we are on tick 12'000 and we want to rewind 10 seconds. we calculate 12'000 - 10'000 so we get 2'000
now we just recalculate all actions in the correct order again which happen to the tick 2'000
so we would get this state:
{
players: {
"420": {
name: "Player 1"
}
}
}
this is the basic stuff. their can be alot of performance optimisation.
like after 2 hours of game time you probably dont want to recalculate the hole state from tick 0.
so you could save a snapshot of the state all 100'000 ticks
hope this helps.