Developing a Turn Based Multiplayer Server Prototype for Poker - FujiNetWIFI/fujinet-firmware GitHub Wiki

Goal

To provide a server in which multiple participants can easily connect, to reflect information needed for a turn based game, such as Poker.

Scope

This is a prototype. Produce just as much as is needed to emit the required data to each connected client, to maintain game state for multiple games.

Language

For the case of portability, I am specifying the language to be C. It should be possible to do this without any large external libraries, e.g. just libsock.

Types of Information

These are the types of data that the server needs to support. These are specified as C data structures.

Player

There will be a max of 255 possible players on this prototype server, and only the amount of information we need to be able to send a message to a given player (the open file descriptor to the player socket), and a displayed name should be kept.

Enumeration

Player numbers are assigned an entry in this array upon connection, and this is used to reference a particular player. An entry in this table is zeroed when the player is disconnected.

typedef struct _player
{
    int sock_fd;             // Socket file descriptor
    char displayedName[32];  // Displayed Name
    unsigned char game;      // Current game # or 0 if no game active.
} Player;

Player player[255]; // 255 maximum players

Game

Essentially a simple identifier delineating groups of players. Entries in this table last for a single game session, and are marked free when done.

typedef struct _game
{
   char title[32];     // Title for game
   char maxPlayers;    // Max # of players (2-8)
   char availPlayers;  // # of player seats available (2-8)
   bool locked;        // TRUE = no more players can join this game.
   bool inprogress;    // Is game in progress?
}

Game game[16]; // 16 maximum games

Enumeration

As with Player, games are assigned in first come, first serve, and last as long as there are participants. Once the last participant leaves, the game field is zeroed and is available for use.

Message

This is the most basic element. A message can be:

  • a PLAYER message registering a player with the system.
  • a GAMES message listing the available games.
  • a PLAYERS message listing the available players.
  • a JOIN message to attach a player to a game.
  • a CHAT message between a specific user or all users in a game.
  • a YOURTURN message from the server indicating when it is a player's turn.
  • a MYTURN message from the player indicating the desired change to game state.
  • a RESULT message from the server indicating the result of processing server game logic

Example session

First, the client establishes a connection to TCP port 6502, and is greeted by the server banner.

server

Connected to Game Server IRATA.ONLINE
Please register with PLAYER <playername>

player

PLAYER ThomasCherryhomes

server

PLAYER ThomasCherryhomes logged in.

Notice that every server reply starts with the same word as the player request. This should be guaranteed, and no other messages should follow a request, except the reply.

To get the player list:

player

PLAYERS

The player list is returned as

  • PLAYERS,Number of players on server
  • Player #, Player Name,Game Number

If a player is not in a game, their room is 0.

server

PLAYERS,6
1,BobBarker,1
2,JoeyLawrence,1
3,Bubba,1
4,Oogy,2
5,Feargal,2
6,Noobi,2

Similarly, a player can get a list of games with the GAMES command.

The values returned:

  • GAMES,number of active games
  • Game Number,Game Name,Max Players, Available Players,Locked?

player

GAMES

server

GAMES,2
1,Weekly ABBUC Poker Game,8,0,0,1
2,Texas Hold'em Speedrun,8,2,0,0 

To join a game, specify its number.

player

JOIN,1

If a game is locked, the JOIN reply will indicate GAMELOCKED. If a game is full, the join reply will indicate GAMEFULL. GAMELOCKED takes precedence over GAMEFULL.

server

JOIN,GAMELOCKED

player

JOIN,2

Otherwise, the JOIN will indicate SUCCESS, and you will start to receive messages from the game room.

server

JOIN,SUCCESS
⚠️ **GitHub.com Fallback** ⚠️