turn-based game - timer problem
Alo I have a client/server turn-based game where the players (4) on each table have 30 sec to make a move. What is the best design: Right now I have 3 solution: 1. Timer on client - The timer is activated on the client. And when the time is zero, send a message to the server, which notify the other players. Problem: What if the current client logout? 2. Timer on server - The server send a message every second to all players at the table. Problem: What if I have many tables (50) running at the same time. 4*50=200 mesages every second just for uptate timers. 3. Timer on client & server. The timer is activated on the client and the server send a mesage every 5 or 10 second to synchronized the timer on the client. Problem: Can't think of anything right now. Any other solutions? Thx in advance.
I'd say Server, but, consider it as a permissable event. Here's a mock log:
2006 11 21 15 30 00: Server to Client: Start Turn
2006 11 21 15 30 10: Client to Server: Move character to 1, 1
2006 11 21 15 30 11: Server to Client: Move permitted.
2006 11 21 15 30 20: Client to Server: Move character to 2, 2
2006 11 21 15 30 21: Server to Client: Move permitted.
2006 11 21 15 30 29: Client to Server: Move character to 3, 3
2006 11 21 15 30 31: Server to Client: Move permitted.
2006 11 21 15 30 35: Client to Server: Move character to 4, 4
2006 11 21 15 30 36: Server to Client: Move denied.
Basically, don't run timers. Just do checks against time as events occur. If anything, give the Client a 30 second timer that stops sending events.
2006 11 21 15 30 00: Server to Client: Start Turn
2006 11 21 15 30 10: Client to Server: Move character to 1, 1
2006 11 21 15 30 11: Server to Client: Move permitted.
2006 11 21 15 30 20: Client to Server: Move character to 2, 2
2006 11 21 15 30 21: Server to Client: Move permitted.
2006 11 21 15 30 29: Client to Server: Move character to 3, 3
2006 11 21 15 30 31: Server to Client: Move permitted.
2006 11 21 15 30 35: Client to Server: Move character to 4, 4
2006 11 21 15 30 36: Server to Client: Move denied.
Basically, don't run timers. Just do checks against time as events occur. If anything, give the Client a 30 second timer that stops sending events.
william bubel
What I'd do (which I think is basically the same) is for the server to broadcast within that game when the 30 seconds starts and ends, and have the clients keep a timer locally for display purposes and stopping late events from getting sent in most cases. Remember that there will be some lag, so you might want to make the move timeout on the server 31 sec (or the client-side on 29s). This will stop people thinking they've made a 'last second' move only to see it rejected.
You can also put server timestamps on the back of any other messages sent within a game, so the clients can resync when other game events happen.
You can also put server timestamps on the back of any other messages sent within a game, so the clients can resync when other game events happen.
Bob's method is probably the best - the server starts and stops the timers, and you let the client keep time in between. This is appropriate when the players take turns like in internet poker. Things get more complicated if you have all players moving at the same time, as differing latency between clients can cause the timers to start at different times to different players. However, you do some clever stuff to get the client timers to begin together. First send a few generic messages to and from each of the clients to the server to test average latency. Then when you're ready to begin you send a message to each client stating "timer beginning in X seconds" where the X varies for each client. Now although each client will receive this message at a different time due to lag, the server can make sure each client timer starts roughly around the same time.
Yeah, the best answer has pretty much been offered, setup the timer with the server, notify each client that the timer has begun, then count down locally. Do not do a turn update until the player has made their move or the timer has hit zero (in the first case it would be a client->server communication and in the second a server defaulting.)
Here's an additional thing answering something you also mentioned.. what if the client times out or loses connection somehow during the timer? I would implement a way for the server to communicate with each client, including the one being timed (communicate about every 5 seconds or so), to detect disconnections. Notify/do whatever in case of disconnections.
Here's an additional thing answering something you also mentioned.. what if the client times out or loses connection somehow during the timer? I would implement a way for the server to communicate with each client, including the one being timed (communicate about every 5 seconds or so), to detect disconnections. Notify/do whatever in case of disconnections.
Quote:
Original post by ferr
Here's an additional thing answering something you also mentioned.. what if the client times out or loses connection somehow during the timer? I would implement a way for the server to communicate with each client, including the one being timed (communicate about every 5 seconds or so), to detect disconnections. Notify/do whatever in case of disconnections.
Ping.
Every 5 seconds would probably be overkill, but at the top of each player's turn would be a perfect time to make this check. To define a Ping, it's a simple Response Request event to which the log would look like this:
2006 11 22 12 45 00: Server to Client: Ping.
2006 11 22 12 45 01: Client to Server: Pong.
If a Pong isn't received by the next player's turn, than the Pinged player has timed out. So:
2006 11 22 12 46 00: Server to Client: Start Turn.
2006 11 22 12 46 00: Server to Client: Ping.
2006 11 22 12 46 30: Server to All: Client Timed out.
2006 11 22 12 46 30: Server to Next Client: Start Turn.
william bubel
Thx for all the answers. I will go for the solution where the server send start & end time and where the clients counts down.
If I have 50 tables on the server and server know each table start & stop time should I make a new separate thread which loop through all the tables and check if the 30 sec has gone for any tables. If the time is gone notify all on the table about a new turn but if the player has make a move within the 30 sec. reset the time for that table?
If I have 50 tables on the server and server know each table start & stop time should I make a new separate thread which loop through all the tables and check if the 30 sec has gone for any tables. If the time is gone notify all on the table about a new turn but if the player has make a move within the 30 sec. reset the time for that table?
The timer stuff should be in your 'main game loop' – whatever that means to you. In this case the 'main game' is actually 50 minigames, but so what? ... you can just do
I don't think threading is necessary for this (processing each table is going to be very low computational overhead). If calculating the result of a move is a complex operation, you might want to farm that out to a thread pool, but try it with everything in one thread first, because it's much easier to understand and debug.
while(true){ for(int minigame = 0 to 49) update_minigame(games);}
I don't think threading is necessary for this (processing each table is going to be very low computational overhead). If calculating the result of a move is a complex operation, you might want to farm that out to a thread pool, but try it with everything in one thread first, because it's much easier to understand and debug.
The server makes the decisions, the clients are slave to it.
The server sends Start and Stop signals and wont accept any orders
after a fixed interval (ping ave based) after it sends the Stop signal.
The clients can run their own local clocks but when they get the Stop
thats it.
Irregularities will happen, but if the turns are long enif (like 30 seconds) then ordinary lag differences will matter litte (and basic compensation by the server
will be sufficient).
The server sends Start and Stop signals and wont accept any orders
after a fixed interval (ping ave based) after it sends the Stop signal.
The clients can run their own local clocks but when they get the Stop
thats it.
Irregularities will happen, but if the turns are long enif (like 30 seconds) then ordinary lag differences will matter litte (and basic compensation by the server
will be sufficient).
--------------------------------------------[size="1"]Ratings are Opinion, not Fact
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement