how to connect two people in your website how to connect two people in your website asp.net asp.net

how to connect two people in your website


In my understanding we have two logic entities:

Games: involve interactivity between two players in private

Players: Visitors (anonymous) of a web site

I will start with Players because it’s easier. A visitor lands on your site and there is a need to (uniquely) identify him. The easiest solution is a Guid that can be used as a session parameter or as a session cookie (my suggestion). Guid is not a nullable type so any Guid of 32 zeros will be our undefined Guid.

Having your GUIDed visitors, you need a key/value collection that will connect them to games.

Scenario 1:Each visitor can be a player for only one game at a time. A Dictionary<player, game> can do the job and visitors who are not players can easily be traced (game = undefined Guid)

Scenario 2:Each visitor can be a player for many games at the same time. A Dictionary<player, List<game>> is the solution but game = undefinedGuid will become List.Count = 0

Now let’s see what you can do with games. First of all you can use GUIDs to identify your games. This means that your players dictionary will be Dictionary<Guid, Guid> for scenario 1 or Dictionary<Guid, List<Guild>> for scenario 2. Obviously you will need a key/value collection for the games, let’s say in the form of Dictionary<gameGuid, gameDetails>. GameDetails must be a class holding the necessary information that can define the interactivity between the players. In other worlds this class must include the role of each player (role 1: the one who asks or role 2: the one who is guessing) and the messages they exchange as a key/value collection where key is the player Guid and value is a string message.

To summarize you will need two static dictionaries defined in your global.asax, one for the players and one for the games. You will also need a GameDetails class similar to this (basic concept implementation):

class GameDatails{  public Guid Role1 { get; set; } // holds the guid of the player who asks  public Guid  Role2 { get; set; } // holds the guid of the player who guesses  public List<KeyValuePair<Guid, string>> Messages; // holds the player/message pairs  public GameDetails(Guid role1, Guid role2)  {    this.Role1 = role1;    this.Role2 = role2;    this.Message = new List<KeyValuePair<Guid, string>>();  }}

Adding and removing players is easy as well as games (players are connected to games).

There are a lot of other things you can do (ie the one who guesses quits and you randomly assign another player to continue etc).

More or less this is also the way to make an asp.net chat with private rooms. It may be helpful for you to find and check a good sample of a simple asp.net chat script, see the logic and the implementation and adapt them to the above. In addition you can extend the chat script to support private rooms and have two applications instead of one.

Needless to say that asp.net is more than enough for your project. What you have to take in account is that if you cannot control you application pool recycling you will also need a persistence layer otherwise you may loose your dictionaries.

If you need more help just let me know.


1- how I can connect two players randomly to let them play together although they are not registered users ( they are just guests to the website )?

There is nothing magical about a registered user. ASP.NET can track state of anonymous users.

2- how to make them playing in private game, i mean each two players are playing their own game?

You just need to keep track of which two users are playing together. That's as easy as storing their SessionIDs

3- What would I need to use? Is it enough to use ASP.Net? Do I need silverlight?

Don't go Silverlight, you don't need it to accomplish this, and I believe Microsoft is abandoning it in favor of HTML5. Plus, its market share is around 60%, whereas Flash is around 100%, so less people are going to be able to use your site if you go the Silverlight plug-in route.

As others have said, your idea can be acomplished with a combination of client side AJAX, and ASP.NET server side. I recommend jQuery for your client side code, it makes JavaScript and DOM manipulation a breeze.

One last thing, check out http://browserquest.mozilla.org/ for and example of what you can accomplish without plugins on today's browser technology.


I think if two people are accessing your website they can access the data separately and play game independently

To control number of concurrent users in a site you have to create a Application State variable and store two selected user ids to it.

Every time a user try to play game, you have to check if its ID exists in Application State variable or not , if exists allow him otherwise not.

Of course you have to write lot of code to

  1. add/delete userid to Application State variable in Global.ascx file
  2. implement authentication to know who the user is and user validations.
  3. if you want them to sign out automatically after some time (idle/fixed) you have to write code in Session_End event of Global.ascx file.

Let me know more details if you want specific answer.

Create a class GameSession { int gameid; string player1; string player2}

suppose there is only one game and 2 users max

User Registration Process:if a first user enters to your website, check user if he is registered check his userid pwd and get his userid from table, if he is guest generate random key (guid) or ask him for the unique key (uniquenickname).

State Management:create object of this class , assign gameid = 1 and assign player1 = user's unique key.Create collection of GameSession object say GameSessions, add GameSession to GameSessionsStore GameSessions to Application State.

Next time if another user logs in, follow the same User Registration Process. Check for empty slot (empty player2) in GameSessions, if found assign this user to first or selected empty slot of GameSessions. Now this GameSession is ready to serve.

Save the object back to Application State.

Allow user to play the game

Write negative cases for above scenarios.

This way you can do this.