Getting started online

So, I have been working on a card game and it has been going well.
It is a 5 player game and it currently only runs on a single machine which isn’t much fun.

My reason for writing it is that my friends and I used to have card night once a week. Now, we’re all off to college and we would like to play together once a week online.

Now I’m ready to take it online so that we can all play together. The problem is, I’m not sure exactly how to go about this step. I’ve done very little networking work with Java and have been looking on google for a simple multiplayer card game and haven’t found a good tutorial.

So, what would be the best way to do something like this?
Can anyone point me in the direction of a good tutorial? (The game doesn’t need flashy graphics or anything. Just simple cards)

Well, Ive made a few client/ server attempts, got a few working but cant remember getting it going on my old web page.

I do remember starting here and learning about sockets.
Sockets Trail at Sun

Everything on that page will give you a good understanding of what your trying to do. Since its a turn based game your looking at, the standard java api’s will suffice Im sure.

I used and chopped at the knockKnock server example which helped me understand networking basics.

I used 1 thread in my app to check for new connections and 1 thread in my app to handle the game logic. Basically everything a player does sends a message to the host, the host then parses the messages and reacts to it.

Its been a little bit since I messed with my networking apps, but currently Im rewriting my Hearts Single player card game and once I get that up and running on my web site, I will rework the multiplayer hearts and publish that too.

The biggest problem I had was the thread that waited for connections was a blocking thread, meaning that no other code would get passed until a new connection was found. Thats the reason for a multiple thread app. 1 to check for new connections and 1 to handle the game. Start with the link above then if anything you should be able to do better searches to help understand your direction.

Have fun!

Shameless plug:
http://code.google.com/p/kryonet/

Wow, nice looking stuff there. Once I get going with my networking code I’ll take another gander at that.

Thanks for the share!

You can take a look at Game Gardens, which has a lobby system and pretty much automatic network handling for you. It should work great for a card game or other simple online games.

Thanks,

I have a general idea of how I could do this with sockets now. I have one more question. Since it is turn based, will I need to make repeated connections to each client per turn? Or can I connect to each client once in the beginning and that is it?
I would like to include a simple chat also in the application.

My current idea:

  1. I’m thinking each client connects to the server at the beginning. It accepts chat lines from everyone and displays them to everyone. And it only accepts play orders from the client who’s turn it is.

Does this sound like the right idea?

So if input comes in from a client and it begins with a special chat character, display it in everyones chat window. and if input comes in from the client who’s turn it is and the input starts with a special play character, make the move and output the results to the clients. so it would ignore play requests from the other clients.

  1. Or should I be making one connection from everyone to a chat, and a connection that connects only to the player who’s turn it is? So essentially everyone has one connection open, and only the person who’s turn it is has the play connection open?

You need to define a simple, language that you can parse and which will be extended as the
needs present themselves. Each message should include the identity of the sender and
some sort of message-type token. You should be able to parse the message stream without
understanding the particular content of any message.

I made a model very similiar to #1. One connection per client, when client disconnects the connection is gone. I also preceded each message with a code that the server parsed and reacted accordingly.

It seems your on the right track as far as my experience went.

Game Gardens look like a nice solution too. One thing for me though is understanding other peoples code and implementation is a nightmare for myself, more often than not the code is too abstract or high level for me to understand. I’m sure its due to my coding techniques and unorthodox ways of putting stuff together. They would definitely make a veteran cringe in utter terror. :slight_smile:

You can do P2P or client/server. I recommend client/server. Each client connects to the server with a persistent connection.

You don’t have to send strings and check if they start with a special character. You can use something like KryoNet and send whatever objects you want. So you would have a Chat object that has a field “String text”. KryoNet already tells you what player the object came from. A client sends a Chat object to the server, the server then sends an object to each other client so they can add the text to their chat window. You might call this ChatUpdate and it would have “String text” and “int fromPlayerID” which is the KryoNet player ID of the player that sent the chat message.

KryoNet comes with a client/server chat example:
http://code.google.com/p/kryonet/source/browse/trunk/#trunk/kryonet/examples/com/esotericsoftware/kryonet/examples/chat

To structure your gameplay, the server keeps the state of the game. To start, it probably shuffles some cards, and gives each player some cards. After setting up the state, you probably want to send a StartGame object to each player, eg so the client knows what cards are in their hand.

You generally don’t want to send information the client doesn’t need to know. Eg, if the player has some face down cards and they aren’t supposed to know what they are, don’t send what the cards are until the player draws them. If you do, someone could hack the client to make those cards always visible. By not sending the hidden information, you prevent cheating. That said, if you don’t care to worry about preventing cheating (eg you are playing with friends) then if it makes development easier to send hidden information, go ahead.

After sending StartGame, the server might send a StartTurn object to each player. It would have “int turnPlayerID” which is the ID of the player with the current turn. Each client would disable most of their UI so they can’t play cards and highlight the current turn player. The client with the turnPlayerID would enable its UI and allow the user to play a card (or whatever). The client would then send a PlayCard object to the server with the necessary identifying information.

When the server receives a PlayCard object it should check to make sure it is from the current turn player. Then it determines any actions necessary when the card is played and sends objects to the clients so they can update their UIs. It then sends the next player a StartTurn message (assuming the first player is done).

Because your game is turn based, you could support persisting the state of the game between turns. If a connection is lost or all the players otherwise leave, the server could hold on to the game state. When the players come back, the StartGame object would restore the game to how it was before, rather than to a new game state.

You could take this even further to make your turn based game playable on mobile phones. The server is exactly the same except instead of having persistent connections, you could do the communication over something like HTTP. This way the clients are normally not connected and they poll for updates. I have a project for that too, also based on Kryo so you can send objects over HTTP, and it works on the desktop and Android:
http://code.google.com/p/legion/

You might want to structure your server to handle multiple games at once. Eg, you could have a lobby where players go when they first connect. They organize and start a game from there, are removed from the lobby, and then the game code runs just for those players. This way not all players are in the same game, and the server can be hosting multiple games. Building a lobby is a bit boring though, so you might want to add this after your game works. Then again, if you are just going to play with friends you may not care to host more than one game per server.

Here is an online multiplayer card game that uses KryoNet:
http://www.ttafo.com/afo.html

Thank you much Nate!

I will definitely be looking into KryoNet. Sounds pretty good.

It would be nice to have some of those additional features. Especially the lobby in time.

A couple more things:

So when going about this, which should I start with: the client or the server? I’m thinking the server?

How will I test this? I don’t happen to have 5-6 computers laying around. I do have 2. Can I connect multiple clients from the same machine? (Something I would want to eliminate for distribution) Heck could I even run a client on the same computer as the server?

Also, I’m getting a bit ahead of myself, but what about having a user log in? I mean for now, in the client, I can just have it ask for a name so that it can be displayed and people know who’s who but in the event this becomes something more, how would KryoNet work with users logging in with a user name and password? I guess I don’t need to know how so much as if I go with KryoNet, is this still a possibilty?

I would build both at the same time.

[quote]How will I test this? I don’t happen to have 5-6 computers laying around. I do have 2. Can I connect multiple clients from the same machine? (Something I would want to eliminate for distribution) Heck could I even run a client on the same computer as the server?
[/quote]
The server runs on a specific port. The clients automatically choose an open port and connect to the server through this port. You can run multiple clients on the same machine as the server. To stop this you could prevent the server from accepting more than one connection from the same IP address. A better way, but more easily circumvented, might be to lock a file or open a socket on the client. The second client that runs would check this before allowing connection to the server.

[quote]Also, I’m getting a bit ahead of myself, but what about having a user log in? I mean for now, in the client, I can just have it ask for a name so that it can be displayed and people know who’s who but in the event this becomes something more, how would KryoNet work with users logging in with a user name and password? I guess I don’t need to know how so much as if I go with KryoNet, is this still a possibilty?
[/quote]
Logging in isn’t much different than just registering a name. The client would ask for the name and password and send it to the server, which would either respond with a JoinLobby object (or whatever) or an InvalidPassword object.