Node.js save gamestate game how?

I am writing a game in javascript, and to prevent cheating I play on the server (this is a board game, like more complex checkers). Since the game is quite complicated, I need to save the game to check the actions of the client.

Is it possible to store memory in memory? Is it smart? Should I do this? If so, how? I do not know how this will work.

I can also store in redis. And such a thing is very familiar to me and requires no explanation. But if I store in redis, the problem is that in each individual case the player will need to get data from redis, interpret and analyze this data to recreate the game from scratch. But since steps occur very often, it seems very silly to me.

What should I do?

+6
javascript database
source share
1 answer

If you really don't want the overhead of I / O, just save the state of the game in the global object with the game key:

var global_gamesate = {} 

Then, on each connection, verify that the game identifier should restore the state of the game:

 var gamestate = global_gamestate[game_id]; 

Presumably, you already have a mechanism for matching client sessions with a game identifier.

Usually the state of the game is small and is unlikely to take up much RAM. Let me be pessimistic and assume that each state of the game takes 500K. Then you can serve two thousand <thousand games <thousand thousand (four <thousand> thousand users, if we accept two users per game) for each gigabyte of RAM on your server.


However, I would like to note that databases such as MySQL already implement caching (which is configurable), so loading the most frequently used data is mostly loaded from RAM with a bit of socket I / O overhead. The advantages of databases are that you can have much more data than you have RAM, because they store the rest on disk.

If your program ever reaches the load, when you start thinking of writing your own disk serialization algorithm to implement the swap file, you basically reinvent the wheel. In this case, I would say that you are using databases.

+4
source share

All Articles