Code examples for simple game servers
Asked Answered
T

3

6

I'd like to build an iPhone game for the Game Center and am currently researching the server part of it all. I learn best by example and I'm having a hard time finding any examples of simple game servers that demonstrate...

  1. How data is formatted and sent to the server and how it is received
  2. How to authenticate the data that is being sent/received to avoid players cheating, etc.
  3. How the game server code is structured along with the DB

I'd like to build the game server using Ruby on Rails, though I'll settle for any examples using any framework just to understand how these concepts work. Does anyone know of any books or online resources that has covered this or shows some example code on how to build a game server?

I would have to think that most multiplayer games on the iPhone require a gamer server, yet I can't seem to find any resources that discuss how to build one. Ever resource I find that discusses building multiplayer games (including Apple's docs) says the same thing, "Developing a game server is outside the scope of this book, but we’ll focus on the client side code necessary to connect to such a server."

Thanks so much in advance for your help!

EDIT: A few more details about what I'm trying to accomplish...

I'm trying to develop a two player (possibly more) game that where the users compete to solve a puzzle faster than the other player. Match making will be handled by the Game Center, but I'll need a game server that will start/manage the games and store the results in a DB. The overall process will be something like...

  1. Game Center pairs up two players
  2. The player data is sent to the server to initialize a new game
  3. The start of the game is synchronized by the server
  4. The game starts and each player attempts to solve the puzzle
  5. When either player finishes, the server is notified
  6. As soon as both players finish, the server responds with the results and the results are stored on the server.
Tarim answered 8/10, 2010 at 17:13 Comment(2)
Can you give us a bit more details? What kind of game are you developing? Do you only need match making? Is the whole game state on the server, or does the iPhone take the job of calculating everything? How important is synchronizing e.g. do you need updates every 10ms? 1000ms? Or is it even a round based game? Also keep in mind that RoR is still a request oriented framework, not something one usually uses for long term socket connections. I recently build one in Node.js, worked out very well.Defraud
Thanks, Ivo, I just added a few more details. I'll look into Node.js for this, as I've been wanting to play with that framework as well. Can you give me any suggestions for figuring out how to do what you did?Tarim
D
4

OK, after reading your edit, Node.js should do the job quite well I think.

Regarding your points:

  1. I don't know how exactly the pairing works(guess you get a pair of keys or so to validate the requests) but it should be hard to implement, regardless of the programming language.

  2. Easy: Setup a server, handle connections, keep a list of running games, for each game keep track of the state(players, time etc.), that should be at maximum(well depending on the message procotol you use..) 150-200 lines

  3. Just send some kind of ready event to each of the players in a given game when you deem that game to be ready(like everyone touched the 'I'm ready' button or so)

  4. OK, some tips here:

    1. Track the remaining/passed time on the server and send it to the players, timers get off pretty quickly(I had 4-5 secs off in just about 3 minutes)

    2. To prevent cheating, validate the moves on the server, that should be fairly easy in case of a puzzle game. You could also check the times between each move everything below 50ms should be suspicious, but that mainly depends on the difficulty of the puzzles.

  5. By checking the moves on the server this is pretty easy too, you don't have to rely on a client sending you a "Look I'm done" message.

  6. Again easy, just send out the events with the accompanying data and log the points into the db.

The most important point is obviously the prevention of cheating, in case of a puzzle game where the solution can be easily solved by a computer, it's impossible to prevent it completely. Although, with the tips above you can make it harder.

But keep in mind, you can't stop people from cheating on the high-scores when the game is simple, building more and more protection into the game just doesn't make sense. Focus on the game-play, make it interesting and simple for humans but complex and unpredictable for computers.

In case of the DB, uh... I suppose you could go with MongoDB for that, but I haven't played around with that under Node yet.

Oh, and if you want a impression what you can do with Node.js here's the game that I've built:
http://github.com/BonsaiDen/NodeGame-Shooter

All the logic runs on the server, the clients just draw the stuff they know about, I had a playable prototype up and running in two evenings.

Defraud answered 8/10, 2010 at 23:22 Comment(1)
This is why I love SO! I really appreciate your effort, I'm gonna start digging in right now!Tarim
D
0

I recently built a client server application, the server side was based on Netty. There are a number of great examples of how things might be able to work on their website. It turned out to perform well also. For a game you might also consider utilising Google ProtoBuffers - an example of this is also on the Netty site.

Doloritas answered 21/10, 2010 at 6:30 Comment(0)
B
0

Just so you know, the GameKit API includes functionality to allow you to send data to the other players in your game. You might want to consider using this instead since it will save you a lot of work building a game server from scratch and you get to make use of Apple's well-tested networking infrastructure.

Blodget answered 6/11, 2010 at 17:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.