Question

Hello I am creating a card game and using System.Net and System.Net.Sockets classes. What I am having a hard time doing is figuring out how I am going to distinguish who did what and who to send that packet of information to. For example here is the order of things that would be happening.

  1. User logs into program and Credentials are checked against a MySQL Database.

  2. The user gets to create a game ID or game # that is also checked against a database to see if there is already a game ID matching and allows them to join the game or if there is no game then inserting data into the database and essentially waiting for another player to join it (think server browser if you will for online games).

  3. Once the user has either joined a game in progress or has created a game it calls a Async BeginConnect().

  4. The user then plays the game and the data like ("played card") or ("looses life") etc is sent to the only other player in the current game.

My question is how to only send that information to the correct players. The game will only support 2 players 1 on 1 but there could be multiple games going on at one given time. For example there could be 5 games playing each supporting 2 players. What is the best way for me to keep track of these sockets so that I can distinguish between the game ID's.

-> user#1 is playing in game with ID #1 against user#2
--> user#1 "plays a card"
---> Only send this information to user#2

I appreciate the help in advance and please let me know if I left something out that would be fruitful in answering my question.

UPDATE
I have continued to keep looking at examples how others are handling keeping track of individual sockets for each user or in this case Game Room. Should I create a class for lets say a ClientSocket and assign it properties?

Was it helpful?

Solution

You can keep track by adding each connection to a key value list in each client e.g. (pseudo code):

Dictionary<string gameId, socket gameIdConnection> gameConnections;

You know of each move to which gameId it belongs, so you can get the associated connection/socket with it and just write the packet to that socket.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top