Tabletop Game Server/Client guidance needed. lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- TomTom · 2022-07-16 19:42 Hi. I have close to non knowledge about networking. I know that there are some protocols, ports, etc. but I have no idea what they are exactly. But... I would like to now. I have this project on my mind, I don't know if its big or not. So, I would like to create a client/server app for playing Four Against Darkness (Tabletop RPG, dungeon crawling game in form of book, I would paste a link but I dont know if Im allowed to do that here). What I have in mind. There's this one app that is server and client app at the same time. User runs the app and can choose if he/she wants to host a game session or join session hosted by someone else. In case user wants to host a session he/she sets some properties (Game Rules, maybe password, set of image tiles which will be used to display dungeo, etc). If user decides to join a game session he/she enters the IP of the host (password if needed) and joins the session. Oh and The Host also can play the game he/she is hosting. Users/Players can send text messages visible to all in session and can send simple commands like changing position of character in dungeon, casting a spell or attacking, rolling a dice. So I'm guessing this would be just sending also text messages converted to integer when needed. When every player sends his/hers action, then the server calculates everything - how dungeon looks - when rooms is empty/monsters are dead then player roll 2d6 dice and result of that roll is the number of room in the pool of rooms) - fight results, - events - etc When everything is calculated server sends all this to players (how many monsters the slayed, how much damage they recived etc). So, How do I approach this? What should I use (what components, what protocols)? Where can I read about stuff that can help me in this project? I know I need to learn a lot but I don't know where to start. What I think I need to now. How to connect to other PC in the network/internet. Send/Recive data (I don't know what kind of data it will be. Propably arrays containg information about action_type, some random integers). Can You guide me where could I start learning all that could help me in this? Thanks in advance ---------------------------------------------------------------------- MarkMLl · 2022-07-16 20:26 Gut feeling: don't start messing around with low-level network protocols unless you really have to. For that matter, do not automatically assume that you need to do any sort of conventional programming yourself. Instead, research what is already available for distributed Multi-User Dungeons (MUDs). You might find that there is already generic higher- level software, to which you would need to describe your game in the same way that e.g. Zork was written in a descriptive language. In any event, keep us updated :-) MarkMLl Xref to older thread: https://forum.lazarus.freepascal.org/index.php/topic,55335.0.html ---------------------------------------------------------------------- TomTom · 2022-07-16 22:20 As a total newbie what I think would be suffucient for my needs. 1. What protocol to use? My best guess TCP/IP 2. Know how to connect 2-4 users where one of them is host and player at the same time 3. Assuming I have done code for dungeon building, how to send information between users how this dungeon looks? Dungeon is build while game progress and consists of rooms (can be a corridor). After clearing a room (defeating monsters, solving a puzzle etc) one of the players roll 2d6 for a number of next room. Rooms (can be of corridor type) are stored in an array so rolled number is the index of that array. When shape/type of next room is determined  player can decide where to place it (entrance must be connected to exit of current room). Each room can be also an 2D array (1 for wall, 0 for floor). 4. How to send/recieve simple data such as strings/integers. ---------------------------------------------------------------------- af0815 · 2022-07-17 06:21 For communication, look at first for chat programs written with Lazarus. This a good start, because it shows how the component works. indy,lnet and synapse are some high level limbs for this ---------------------------------------------------------------------- TomTom · 2022-07-17 08:54 I'm looking at examples right now and one thing makes me worried. For example in this one https://stackoverflow.com/questions/26629513/how- to-set-up-a-simple-connection-between-tidtcpclient-and-tidtcpserver- to-send#26633619 there is TIDSync mentioned. I don't have it and I can't find it. There is also TidNotify (or sth like that) mentioned in other post. I don't have it either.  What I installed using Online Package Manager: Indy 10 Synapse LNet And I cant(don't know how to) use IDSync/TidNotify ... Quote from: af0815 on July 17, 2022, 08:21:30 amFor communication, look at first for chat programs written with Lazarus. This a good start, because it shows how the component works. indy,lnet and synapse are some high level limbs for this ---------------------------------------------------------------------- Handoko · 2022-07-17 13:14 I bookmarked some useful links when I was doing research about creating network/online games. I haven't studied them, but I believe these would be useful to you: https://www.pascalgamedevelopment.com/showthread.php?32328-Best- approach-for-simple-network-game https://forum.lazarus.freepascal.org/index.php/topic,49655.msg360570.h tml#msg360570 https://forum.lazarus.freepascal.org/index.php/topic,38866.0.html https://forum.lazarus.freepascal.org/index.php/topic,38622.msg263693.h tml#msg263693 https://forum.lazarus.freepascal.org/index.php/topic,28575.msg178951.h tml#msg178951 https://forum.lazarus.freepascal.org/index.php/topic,48565.msg349808.h tml#msg349808 https://forum.lazarus.freepascal.org/index.php/topic,45276.msg319977.h tml#msg319977 https://forum.lazarus.freepascal.org/index.php/topic,53538.msg396120.h tml#msg396120 ---------------------------------------------------------------------- Warfley · 2022-07-17 18:25 There are a few things you need to consider when designing such a network protocol. Do you want to have a dedicated server, player hosted or peer to peer (from your first post it looks like you want a player hosted game) and the question if the players are in a local network or you need to connect over the internet. Generally I would recommend you a dedicated server approach, the dedicated server can than be hosted by one of the players, but this generally makes it easier to have game clients and game server logic separated. If the host wants to join the game, they are just another client for the server. Also for the beginning I would recommend you for now to focus on local network, as this is much easier for a beginner. Then the question is requirements you have to the handling of the connection and the flow of the game. Is you game very structured, e.g. turn based (e.g. chess where each player can only continue after the other has made their move) or is it a more chaotic real time where the game is going forth if the players react or not. Then how are your delay requirements and can you deal with package loss, e.g. in chess delay doesn't matter and if your move "get's lost" you get into trouble, while in a first person shooter, delay is crucial, and if one shot of you is lost it doesn't matter as much as you can just shoot again. From this you can then select your protocols. general rule of thumb: Low latency -> UDP, structured and reliable -> TCP. You can also of course use higher level protocols, e.g. you could use Websockets if you want a TCP based event driven but connection based flow (so where each client has one persistent connection), or HTTP if you have a stateless request-response flow (for most games not realy fitted as most games the server might want to initiate communication and in HTTP the server can only send data to the client as a response to a request from the client) From my understanding tabletop games are usually more a structured game, where a situation occurs and every player choses their moves and then the  reacts to those moves. So you could go with a very simpe TCP based system where each client connects to the server and then when it's their turn, the server just awaits data from them, and because no one else is able to do anything during this players phase, the server can pretty much just sit and wait for that players client to make their move. This is rather easy to build with raw TCP. But you might want to have multiple channels for connections anyway, for example if you want to have a chat function, best to create a new connection for the chat which simplifies dealing with distinquishing between the different kinds of data (with different formats). Lastly there is the question on how much you trust your participants. If you don't a naive approach (like described above) might open the door for exploitation, e.g. looking at the example above, a malicious player could literally just waste time by not sending any data, while all other clients have to wait. So you might need additional security mechanisms. For the beginning I would keep it simple, start with a local network TCP based apporach and look how far you come. Simple TCP communication can be done using the "ssockets" unit, which provides everything you need to get started with a TCP client-server architecture. A very simple server with ssockets looks like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project1; {$mode objfpc}{$H+} uses  classes, Generics.Collections, ssockets; type  TPlayerMove = record    // Player move information  end;   TConnectionList = specialize TThreadList;   { TGameHandler }   TGameHandler = class(TThread)  private    FClients: TConnectionList;     function ConnectedPlayers: Integer;    function GetConnection(AIndex: Integer): TInetSocket;  protected    procedure Execute; override;  public    constructor Create;    destructor Destroy; override;    procedure ClientConnected(Sender: TObject; Connection: TSocketStream);  end; const MAX_PLAYERS = 4;const MIN_PLAYERS = 2; { TGameHandler } function TGameHandler.ConnectedPlayers: Integer;var  clients: specialize TList;begin  clients := FClients.LockList;  try    Result := clients.Count;  finally    FClients.UnlockList;  end;end; function TGameHandler.GetConnection(AIndex: Integer): TInetSocket;var  clients: specialize TList;begin  clients := FClients.LockList;  try    Result := clients[AIndex];  finally    FClients.UnlockList;  end;end; procedure TGameHandler.Execute;var  Player: TInetSocket;  NextMove: TPlayerMove;begin  while ConnectedPlayers < MIN_PLAYERS do  Sleep(100); // Wait for enough players to start  // Game loop here  // e.g.  Player := GetConnection(0); // get first player  Player.Read(NextMove, SizeOf(NextMove)); // get next move from player, will wait until information is sent  // Do something with next move  // etcend; constructor TGameHandler.Create; var  clients: specialize TList;begin         clients := FClients.LockList;  try  while clients.Count > 0 do    begin      clients[0].Free;      clients.Delete(0);    end;  finally    FClients.UnlockList;  end;  FClients := TConnectionList.Create;  inherited Create;  FreeOnTerminate := True;end; destructor TGameHandler.Destroy;begin  FClients.Free;  inherited Destroy;end; procedure TGameHandler.ClientConnected(Sender: TObject; Connection: TSocketStream);begin  if ConnectedPlayers < MAX_PLAYERS then    Connection.Free  else    FClients.Add(Connection); // Additional code to handle new players hereend;    var  Server: TInetServer;  GameHandler: TGameHandler;begin  GameHandler := TGameHandler.Create;  GameHandler.FreeOnTerminate := True;  Server := TInetServer.Create('0.0.0.0', 1337); // 0 IP -> All ips can be bound, port 1337  try    Server.OnConnect:=GameHandler.ClientConnected;    Server.StartAccepting;  finally    Server.Free;  end;end. ---------------------------------------------------------------------- TomTom · 2022-07-23 19:52 Wow thank You for such detailed answer . It helps Yes, I guess LAN will do for my project (some Virtual LAN software could be used with this? I mean similar to Hamachi...?) Game is turn based, there is no need for haste. Game is/could be a little bit like classic pen &paper RPG (Dungeons&Dragons etc.). Purpose of my project  is to provide a GUI for players. 4AD (Four Against Darkness) is based on d6 rolls. Everything is made with results of rolling a six sided dice and comparing the result with various tables. For example. At start player rolls 2d6  and gets 2 digits, 3 and 4 so that forms number 34. Player looks in proper table in book that room nr 34 has this shape and he draws it on graph paper. Also player can decide where to put the room on the map (connect to other existing room). Then player rolls 1d6 for content of this room. Player gets 4. 4 in other table means there is a swarm of wasps in this room.  Then player rolls 2d6 to know how many wasps there are etc. etc. All these rolls are sent (somehow) to Server. Server use all this rolls results to generate  image of dungeon (topdown 2d Image). Players decide what to do, attack or try sth else.  If the attack then their statistic are compared to statistics of monsters in the room to see if the attack was a success (players are attacking one after another, but I need to figure out if this will work for my project). Then server sends image of dungeon to players, and sends back the information if the succeded in their actions (attack or sneak/escape etc). So server needs to generate and send image (PNG?) and simple text for example in log window to show results of actions. I hope all that makes sense . I need to figure out many aspects, how to translate this Pen&Paper game to virtual tabletop game because 4AD was actually designed as a solo game with just a "possibility" to play with others, but the rules for multiplayer version are just roughly descirbed in the book (I can make my own rules for that I guess). Generating images, writing code for rules, drawing from the tables etc., all that  that I can do myself (I guess). What I need is to learn how to send all this to 4 people connected to the game, and to learn all that, I need to start very simple, with just connecting/disconnecting and sending/reciving text messages. So thank You for your answer . Quote from: Warfley on July 17, 2022, 08:25:53 pmThere are a few things you need to consider when designing such a network protocol. Do you want to have a dedicated server, player hosted or peer to peer (from your first post it looks like you want a player hosted game) and the question if the players are in a local network or you need to connect over the internet. Generally I would recommend you a dedicated server approach, the dedicated server can than be hosted by one of the players, but this generally makes it easier to have game clients and game server logic separated. If the host wants to join the game, they are just another client for the server. Also for the beginning I would recommend you for now to focus on local network, as this is much easier for a beginner. Then the question is requirements you have to the handling of the connection and the flow of the game. Is you game very structured, e.g. turn based (e.g. chess where each player can only continue after the other has made their move) or is it a more chaotic real time where the game is going forth if the players react or not. Then how are your delay requirements and can you deal with package loss, e.g. in chess delay doesn't matter and if your move "get's lost" you get into trouble, while in a first person shooter, delay is crucial, and if one shot of you is lost it doesn't matter as much as you can just shoot again. From this you can then select your protocols. general rule of thumb: Low latency -> UDP, structured and reliable -> TCP. You can also of course use higher level protocols, e.g. you could use Websockets if you want a TCP based event driven but connection based flow (so where each client has one persistent connection), or HTTP if you have a stateless request-response flow (for most games not realy fitted as most games the server might want to initiate communication and in HTTP the server can only send data to the client as a response to a request from the client) From my understanding tabletop games are usually more a structured game, where a situation occurs and every player choses their moves and then the  reacts to those moves. So you could go with a very simpe TCP based system where each client connects to the server and then when it's their turn, the server just awaits data from them, and because no one else is able to do anything during this players phase, the server can pretty much just sit and wait for that players client to make their move. This is rather easy to build with raw TCP. But you might want to have multiple channels for connections anyway, for example if you want to have a chat function, best to create a new connection for the chat which simplifies dealing with distinquishing between the different kinds of data (with different formats). Lastly there is the question on how much you trust your participants. If you don't a naive approach (like described above) might open the door for exploitation, e.g. looking at the example above, a malicious player could literally just waste time by not sending any data, while all other clients have to wait. So you might need additional security mechanisms. For the beginning I would keep it simple, start with a local network TCP based apporach and look how far you come. Simple TCP communication can be done using the "ssockets" unit, which provides everything you need to get started with a TCP client-server architecture. A very simple server with ssockets looks like this: ---------------------------------------------------------------------- PierceNg · 2022-07-27 08:53 Just saw these on Reddit's r/pascal that might help. RNL - a UDP-based network library for real-time applications and gamesNot Quake - Example of real-time multi-player shooter using Castle Engine and RNL