Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm writing a browser game in php and Sql. I'm also using Javascript - Ajax and Mysql.

I'm stack on the battle system because I want to Synchronize the turns of the players in the battle.

What I doing is to put two players on a battle. So from the first turn a countDown of 60sec will start. What I am thinking is to use a server function to check the countdown, not to the client side. This because I think that in that case a bad synchronization will came, instead if it is the server to count down.

But.. how can I let the server do all this stuff?

share|improve this question
    
web sockets. I don't know of other alternatives to manage server-side events without polling (http push does not count) –  FxIII Sep 1 '11 at 6:54
add comment

2 Answers

I am not a php programmer (.NET developer), but if you're using Ajax it seems to me that the best thing to do is to use javascript to keep track of a client side counter. Then every 5 or 10 seconds or so you could make an ajax call to get the official server time to see how many seconds have actually passed and update your counter accordingly.

On the server side whenever a request comes in you could check to see how long it has been since the other client has made a request. If one of the clients haven't responded in a while then you can assume that client has closed their browser and instead of updating the counter you can notify the client that's still connected of this status change.

share|improve this answer
    
Thanks for your answer, but let's imagine if my browser is slow to load the page.. What I want is that when the battle starts both the players can have the same time to begin the battle. In that case, if my browser is really slow to load the page, I'll start my battle with the counter to an half of the time.. =) Or maybe I can make pool requests every second until each players are ready.. And only after start the game. What do you think? –  tendril89 Aug 31 '11 at 21:55
    
If you add an onLoad event to the bottom of your page body then it should be the case that the javascript doesn't get loaded until the page is finished (or nearly finished) loading. If you use that javascript to indicate a ready status then you have a way of making sure that both players pages have loaded before beginning the countdown. –  Landon Aug 31 '11 at 22:20
    
Example: Player A's browser is fast. A makes an ajax call and a session variable (or whatever) is set. Client A is told to wait 10 seconds (server time). After 7 seconds Client B finally loads and calls the server. B is told to wait approximately 3 seconds. A's 10 seconds are up at roughly the same time that B's 3 seconds are up and both clients are told to begin the countdown... This is all just theory of course, but it might be worth a shot. –  Landon Aug 31 '11 at 22:24
add comment

You need to coordinate the turns on the server, I'd think of some entity that represents fight state, something like

  1. GET /fight => Returns { player: 1 }
  2. Player 1 POSTs to /fight he acknowledges it's his turn, timestamp starts
  3. Player 1 POSTs his action(s) to /fight, server computes the actions and updates the fight state, clears the timestamp and now it's player 2's turn
  4. Player 2 is GETTing /fight (some pool maybe), then, suddenly the app receives { player: 2 } (hey, it's my turn!)
  5. Player 2 POSTs he acknowledges it's his turn, timestamp starts
  6. Player 2 does not play, as Player 1 is pooling the server for changes on the fight state, after 60 seconds it will be player 1's turn. Back to step 1.

You could use something like Socket.io/Comet for real time communication, but cleverly sending timestamp information to the clients may suit you and save some requests. Most of these decisions depend on your gameplay.

General advice is - don't worry about the technology if it doesn't work on paper (this example could be easily implemented in any language/db pair), it's easier to abstract when you don't have to care about storage or requests. If it gets too complicated, try to simplify it and then build more complex processes on top of the simpler ones.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.