views:

212

answers:

4

I am developing a small browser based game in asp.net. Think of a game room which has a capaticy of 22 players and players join the room by clicking a button. ( I am saving the number of players in the room in database) I need to call a method when the number of players in the room is 22. The problem is I don't know how to control the number of players in the room. I mean I think like there need to be a bacground code which has to run all the time at the server and that code controls the number and call the function. It's my first web project(school project) and I hope you all can help me.

A: 

You mean to show the number of players in the room to the end-users? You could use AJAX to query a file on the server that returns the number of players online and use it to update a <div> or something. jQuery can help simplify the client side code.

Blindy
The AJAX `Timer` control may be relevant, here. Maybe use an `UpdatePanel`, too.
Brian
actually I can show the number of players in the room to the end-users. What I want to do and can't is when the number of people in the room is 22 I want to call a function(or method) like matchStart(). And this function creates 2 teams (11x2 people) and place the players into these 2 teams and so on. So what I want to do is not a client side action. I don't have to show anything to the end-users. I hope you get it right this time. I know that i explain it too complicated but i dont know how to explain it in easy way.
Emre
But you know when to check for the number of players on the server side: when a user enters the game. Every person coming goes through a central function on the server and in it you check the current number and then call your matchStart() function. Getting the message to your end users might be trickier though because of the html model.
Blindy
A: 

At a very high level when a user tries to join the room, you'd check to see whether the room is already full.

It starts to get complicated when you start to answer the question of who is still in the room. Do you already know all the cases which would cause a person to drop out of a room?

Larsenal
ummm... The user who enters the room cant leave the room. When he joins the room it means he is in the game so thats not too important to drop out of the room but thank anyway=) however I check if the number of player in the room is max or not
Emre
+1  A: 

Presumably you will be calling a web method each time a user enters or leaves a room, right? So you could just update the number of users in the room at that point (make sure you do appropriate locking), and if it is 22, call whatever code you want (since you're already on the server). You may also need to periodically have users ping the server to let you know they are still active, so that you can remove them from the room if they get disconnected or whatever.

bwarner
A: 

The most obvious thing that comes to mind for me is to make the game fall inline with current games.

Have a server (asp.net) and clients (silverlight / flash maybe).

On the server you can manage whats going on by storing things in the application state or in the session state objects.

You can track players progress between sessions using a database (sql server maybe).

You'll have to be aware of things like the application lifecycle ... http://msdn.microsoft.com/en-us/library/ms178473.aspx

I would recommend you use either ashx (web handlers) or WCF for your server based requirements.

On the client (lets say you want to use silverlight because you can still use C#) you can then connect to your server and request the data.

This will help you visualise (i hope) how multiplayer gaming works online.

As suggested above you may find that AJAX works fine for your needs, ajax requests are usually lightweight but remember the full sourceo f your client would effectively be given to each player so you're open to hacking.

Since this is a simple school project though i'm sure u'd be fine.

Wardy
More on my answer :I would suggest using the "Session start" event in global.asax to add to a counter ...Application["UserCount"] = Application["UserCount"]++;Then the other way around for when a session ends ...Application["UserCount"] = Application["UserCount"]--;Then in your page load event ....this.Controls.Add(new LiteralControl("User Count : " + Application["UserCount"].ToString()));You can complicate this by adding more and more functionality this should get you started.
Wardy