0

I am newbie in game development, I want to create a multiplayer game server with node.js I want to know, what is the most common techniques and patterns to keep and change game state on the server

My game state size has little size, So I think the best place is memory to store it

I want to scale up and out my server game, so each player can connect to different server

I am worry about simultaneous events that change state of game in different servers, So having single source of truth is somehow complex, because potentially the state on each server can be get out of sync

In nutshell, What is the common approach to build a multiplayer turn based games that server has to implement some logic to control game flow such as changing turn, changing state of game in case in inactivity of the player, ...

I also don't know how to protect state against restating server, May be a redis can save the state, But how to control the game flow after losing the controller

WebMaster
  • 3,050
  • 4
  • 25
  • 77
  • 1
    What's the meaning of "game state", do you mean different stages or steps of a game? It's better to help you if you can describe your game's detail more clearly. – tyChen Feb 16 '21 at 05:00
  • "Game state" is an instance of a class, it is a object. the object has some properties like turn number, players, rule of each player, live count of each player, switching turn, accept command of the player owned turn, Reject commands of other players have not owned turn, skip turn of inactive player and ... – WebMaster Feb 16 '21 at 06:00
  • So it's a game that like monopoly? – tyChen Feb 16 '21 at 08:16
  • Yes, it is similar – WebMaster Feb 16 '21 at 08:28

2 Answers2

1

Firstly, I think it's bettert to start from a little model with central server, the thing about "change state of game in different servers" may not fit your game, even if it's fit, you can do it after the simple try. The first important thing is to make the game run as you wish and as simple as possible.

Then we foucs on the how to build a multiplayer turn based game. For such a game, you need at least several things like below

  • Players infomation
    • total information for static (players number, turn count, players order, position of players, the running player now in this turn)
    • every player's infomation (name, ID, money, others)
  • Map Manager
    • Things will happen in every grid
    • Special events or some other game machenism

After that, here is the central logic of server. We need at least following functions:

  • ProcessNetwork() to receive packages and send infomation to others
    • The ending of a player's operation
    • The events happend to one/many players
    • The interaction of several players
  • CheckEnd() to see if the game is end
  • CheckPlayer() to check if we need to give the operation to next player or if it's next turn.
void run()
{
    while (!bGameEnd)
    {
        ProcessNetwork();
        CheckEnd();
        CheckPlayer();
    }
}

And that is the simplist model of a central controller, you can change it to other ways as long as they can work. After that, you can make it more and more complicated to realised your game.

tyChen
  • 1,404
  • 8
  • 27
  • Thanks for your reply, I have no problem with developing game server itself, my problem is scaling when I have one server (one thread) for each game room, it's easy to manage state as for each room I have only one state, but when I have multiple server for each game room, I have to sync multiple game state in different servers, I am worry about edge cases, like race conditions – WebMaster Feb 17 '21 at 18:04
  • 1
    There is another thing confused me : why do you need to sync multiple game state in different servers. I think every game with some players can be see as a single complete game, if you want to sync state of different games, do you mean all the players play in one game? – tyChen Feb 18 '21 at 02:37
  • I have many game rooms, each rooms has many player, As I want to scale my game for million rooms and 10s players, I have two option, route each player of a room exactly to the same thread or have multiple game state and sync them I know the first approach is good enough but I want to know for second approach more before starting development – WebMaster Feb 18 '21 at 05:52
  • I want to know how can I development my game with second approach and sync states(I can sync with pub sub) my problem is edge cases – WebMaster Feb 18 '21 at 05:54
1

Save the game state in the DB, you can spin up multiple application servers based on the load.

karun
  • 23
  • 2
  • 8