timer

I am writing an applet based game that uses some php for mysql read/writes w/ URLConnection. I am trying to figure out what is the best way to implement a timer.

i.e. two players are playing against each other. each one must make move in 2 minutes or points are rewarded to other player.
can i just do the timer in the applet i.e. write to database user turn. read if user has not posted new move give points to new player?

or is their a better method?

The client should handle the game play and report back to the database. If you want to protect against cheating, then you should have a server handle everything and just tell the client what to output.

Have the PHP script keep track of when the last request for an update was made by each player and use the time difference to update.

But you also need the client to represent data correctly, so it should have its own timer that is purely visual. In addition, it will know to make a request after the time limit to see if it indeed has hit the time limit, so that it can refresh its state to the user. To avoid mirrored logic / constants, though, I’d do it like this:

Client boots up, sends ping to the server.
Server initializes game session etc. sends back various config data (including length of a turn).
Client starts the timer, after the turn length has gone by, sends another request to the server,
OR player makes a move within the time limit, client sends the move, if it has actually been too long server sends back error.
etc.

Another way to do this is to have a config file hosted on your server that both the PHP app and the client Java app read from. So when the client starts up it redownloads the config every time, and the server reads the config directly to make various decisions.

I figured out that a client side timer( swing timer) is the best way to accomplish what I want as I can just terminate the game and take away points if the user doesnt move in a set of time and update mysql. But I am running into another problem.

right now i have a column in mysql called startTime and endTime. When I start a game I put a new timedate into startTime.

When a user wants to join a game I select all the games with endTime’s of 0000000 or whatever my default is. Then the user clicks the button and goes into the game. The problem with this is that I can get games in which users created but then left- alt f4 etc… Is their another way to do this without the need for a server?

You can have the client send a disconnect message to the server, call it when the program is closing.
http://download.oracle.com/javase/tutorial/uiswing/events/windowlistener.html

I would also recommend adding a lastUpdate field that keeps track of the last time you heard from the client. You can use it to let other know player(s) know that person is having connection problems or to drop them after a timeout period. I would also have the client send a signal at set intervals just to let the server know it still cares.

Totally unrelated but drawing a flow chart will probably help a lot as it gets more complex.