Designing a Distributed System for an Online Multiplayer Game — Game Server (Part 5–2)

Logic

The game server simulates a drag car race. The gameplay is simple, players can control the number of gears and they should change it at right time.

Simulator

I developed a simulator to balance cars configurations. It helps to categorize the cars in classes easier.

The output of the simulator calculating the maximum parameters, for the best gear changes

Game Loop

The game logic is run in an interval of time, it is called the game loop and the interval depends on the gameplay, for example for a fast-paced game, we need to run the game in 50 frames per second which means a 20ms interval.

Player Inputs

The game client, streams player inputs with the frame number to the game server at a high rate. the udpsocket package receives the inputs, decrypts them, authorizes the user session id, and then proxies the inputs to the game inputs golang channel. The game server caches the inputs to use in the game loop for that frame number.

Player Actions

There is some other type of player inputs that we call player action. These kinds of messages must be delivered correctly and in order, so we use the WebSocket channel to transport them. When users connect to the game server, they must wait until all other players connect too. After that they must change their status to Ready by pushing a button. The player ready message is an example of player action.

Game Events

There are two kinds of events to publish:

Public events

All clients should receive this type of event, like GameStarted, StartCutscene, CountDown. The game server uses the WebSocket channel to broadcast public events.

Internal Events

The game server uses the broker to publish internal events to inform the game manager of its state, like GameServerIsReady or GameFinished. The game manager receives events and logs them into the database.

Pipeline

The game server waits for the players to connect after startup. When all players send the Ready action, the game changes the state to Running and publishes the Cutscene event, then it sleeps for the cutscene duration, afterward it publishes the Countdown events, and in the following, it starts the game loop and listens for the inputs in another goroutine.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sajjad Rad

Sajjad Rad

Currently a software engineer, always an adventurer