locked
Multiplayer card game design using .NET and C# RRS feed

  • Question

  • User1758761245 posted

    Hello.

    I am designing an online multiplayer card game that people can play with each other. For the user interface i am thinking of using a .NET web application (with ajax, jquery, etc) but i think there has to be a way to keep the game running in the background on the server side and process user input and data even if there is no postback. I am thinking of using a windows forms application (or console) that will run on the server, querying the database every second and taking action where it needs to (based on the data). The user interface (web side) posts back every second via Ajax and receives the next action that has to be displayed, action that has been processed by the background software. It also updates the database with the current player action.

    This can work i'm sure but i don't know if its optimal. Since the communication between server and client is stateless, i can't think of a better way to "keep the data flow". This can lead to a lot of partial postbacks every second and can stress out the server/database. 

    Please excuse my explanation, i don't know how to better put this. To my knowledge, this has to have both a web form and a windows form side. I would like to know if there is a better approach to this problem and/or design. Thank you!

    Monday, November 10, 2014 9:30 AM

Answers

All replies

  • User-821857111 posted

    You should look at using SignalR for this: http://www.asp.net/signalr

    Monday, November 10, 2014 10:48 AM
  • User1758761245 posted

    Thanks for the response Mike.  After watching a presentation about  SingalR i don't think that would help me in this situation. Sure, it will make my game more responsive due to it's crazy real-time communication with the server, but that is not my issue. Here is what i have in mind:

    1. Client web app updates the DB with a user action (say a button click) and keeps querying the DB every second for a response or a change in the data (via Ajax).

    2. Server software keeps querying the DB reading the new user action and it makes a decision about it. After the processing is done it updates the DB with a result.

    3. Client web app gets the new result and it changes the UI accordingly. The process is then repeated.

    Why am i not doing all of this in the web app itself? Because if a user doesn't act in time, or doesn't act at all (say he lost his internet connection)  the game is basically stuck now.  Therefore i thought a server side software could keep things going independently from the client (which is what a multiplayer game should do). 

    Isn't there a way for a web app to communicate directly with a windows forms app? Databases are for storing data, not for exchanging messages between the two :) Or maybe there is a better way to do this without using a server side software?

    Monday, November 10, 2014 12:40 PM
  • User-821857111 posted

    SignalR does exactly what you want, based on your description so far. SignalR can broadcast changes in data to all connected clients all the time. It is about the best way for web apps to initiate communication with clients, whether they are browsers, Console apps, windows forms or wpf apps. 

    Monday, November 10, 2014 2:27 PM
  • User1758761245 posted

    I am looking into it now. It seems that websockets are the answer and SignalR uses those to communicate. Is it the best/easiest technology to use in my case? I will come back if i have more questions. Thanks again Mike!

    Monday, November 10, 2014 3:06 PM
  • User-821857111 posted

    Is it the best/easiest technology to use in my case?
    I believe it is.

    Monday, November 10, 2014 4:47 PM
  • User1758761245 posted

    SignalR is great for real time communication between server and client app. It will make my game lightning fast and super responsive but i still don't know what should i use as a back-end software for processing data. I can't rely on clients to keep the game going so i'm going to need a back-end program that listens to the web app, receives input and then, after processing it, somehow being able to broadcast a stream of data to the clients (web app). I heard i could use WCF for that. What's your opinion? Thanks!

    Tuesday, November 11, 2014 8:34 AM
  • User-821857111 posted

    I don't understand why you think you need a separate application. The web app should be capable of processing input.

    Tuesday, November 11, 2014 8:48 AM
  • User1758761245 posted

    I don't understand why you think you need a separate application. The web app should be capable of processing input.

    Thanks for the answer Mike. Let me explain why i think i need a separate app for this. I am designing a poker card game. There will be multiple tables with 10 seats on each table. Imagine players sitting down and playing. Each one of them will have to act within 30 seconds otherwise they will be automatically seated out and the action will be passed to the next player. What happens if all players are sitting out and there is no one active at a table? Cards still need to be distributed, blinds still needs to be taken and hands still needs to keep rolling because... this is poker. How is a web app going to handle this? Sure, when you have a postback (aka user clicks, JS sripts, etc) things get to be processed on the server, but the game will get stuck if there will be no one to act (a typical scenario is when there are two players and both don't show up in time when a tournament starts, or they lose connection). For that i imagined i need a console app or some similar software that will keep running on the web server and getting the game rolling even without user interaction (would be perfect if it could listen to the web app - get user requests - without going through the database). Don't get me wrong, i am no newbie to developing asp.net, its just that i don't see how can this be accomplished just with aspx pages. What do you think Mike? Am i missing something? 

    Tuesday, November 11, 2014 11:13 AM
  • User-821857111 posted

    If the game needs to continue without any user interaction then yes, a separate "engine" will be needed. Something like a service, I would have thought.

    Tuesday, November 11, 2014 12:00 PM
  • User1758761245 posted

    Mikesdotnetting

    If the game needs to continue without any user interaction then yes, a separate "engine" will be needed. Something like a service, I would have thought.

    WCF service? That can run on the web server and process the web app requests but what about the other way around? How can it send data to other clients (web apps)? I mean how can other players be notified about one's action?

    Tuesday, November 11, 2014 1:51 PM
  • User-821857111 posted

    WWCF works on the same basis as a web app - request and response. I was talking about a Windows service.

    Tuesday, November 11, 2014 2:47 PM
  • User1758761245 posted

    Can a windows service broadcast data to a web app without the web app asking for it? Sorry, but this is a new territory for me. Can you please explain a bit what you had in mind, or paste a link here that might help me in that matter? Thanks!

    Tuesday, November 11, 2014 4:45 PM
  • User-821857111 posted

    Yes - the Windows service can do that via SignalR.

    http://weblog.west-wind.com/posts/2013/Sep/04/SelfHosting-SignalR-in-a-Windows-Service

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 11, 2014 5:25 PM
  • User1758761245 posted

    That looks awesome. I found this article http://www.asp.net/signalr/overview/deployment/tutorial-signalr-self-host that self-hosts SignalR in a console application as well. I tried the sample code after changing some lines and it works! Clients get messages from the server without interacting. Thanks Mike! 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 12, 2014 4:32 AM