Server and client responsibilities

Server and client

I botched this onebefore. I wanted to put in so much magic that everything just understood by itself where it was going. It ended up being hard to follow and direct.

Supported scenarios

  • Be a server with no players
  • Be a client with one or many players
  • Be a server and a client with one or many players

Codewise, I previously used a single point for all messaging. And a single point for all connections. I've divided it up into Server and ServerConnection. The Server is the connection that the server uses to communicate with the clients. The ServerConnection is the connection that any client code uses to send messages to the server.

I also shared all code in the same node. Both server and client, so I could have it in the same place. Some times, they don't match up in that way.

Separation of responsibility

To create the layers independently one needs to be clear what should exist on each layer. After a lot of implementing, I sat down and thought about how it really should be. I usually plan before I code, but some times a problem seems to be trivial and you don't. :)

This is my take on the responsibilities of the different layers.

Client

  • Client keeps track off player profiles.
  • Client matches controllers to players.
  • Client sends a request for a player to join the session to the server.
  • Client has an UI.
  • Client sends commands from UI and game to server.
  • Client changes scenes.
  • Client process movement input and send to server.
  • Client keeps a copy off individual player data. Inventory, position, stats, etc.
  • Client keeps a copy off data that is not for players, but needed to show graphics, etc. Enemies, houses, etc.

Server

  • Server creates a new game session.
  • Server detemines if a player from a client can join the session.
  • Server creates all data related to the session and syncs it's to client.
  • Server creates instances of levels / dungeons when players enter them.
  • Server runs logic for events and commands and report result to client. I.e buy, sell, start conversation, open door.

Example implementation

ClientPlayerNode

  • Takes input from the controllers / input sources assigned to a player.
  • Sends messages to server with input (for movement x/y) and commands generated by UI / button input. Buy, sell, attack, jump, etc.
  • Takes data from ServerPlayerNode.

ServerPlayerNode

  • Receives messages for movement
  • Assign velocity to physics objects from movement messages
  • Syncs data to ClientPlayerNode

ServerLogicNode

  • Contains logic for specific commands. For instance ShopServerLogicNode handles everything that goes on in a shop. Buy, sell, trade, steal, etc.
  • Receives command from ClientPlayerNode and reports back results.
  • Can handle commands that spans over time. Like hammering a sword on an anvil. Reports back when the elapsed time is done.

ClientSharedDataNode

  • Contains data that is shared between players on the client. Enemies, houses, world data, etc.
  • Gets synced from GameSessionDataNode.

GameSessionDataNode

  • Contains all data related to the session.
  • Syncs shared data to ClientSharedDataNode.
  • Save / load game saves and state.