aboutsummaryrefslogtreecommitdiff
path: root/src/ch/epfl
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2016-05-11 17:39:14 +0200
committerPacien TRAN-GIRARD2016-05-11 17:39:14 +0200
commita43d1f0013c7f2cb7595a1fe1a5cfd99a441d0b4 (patch)
treebe3d5ae3b065da758c272a68dbc1138a3c72a81f /src/ch/epfl
parente4b599c9bb3f2c1bfcebadc832f9d291ab8e4e93 (diff)
downloadxblast-a43d1f0013c7f2cb7595a1fe1a5cfd99a441d0b4.tar.gz
Implement delay management
Diffstat (limited to 'src/ch/epfl')
-rw-r--r--src/ch/epfl/xblast/server/Server.java20
1 files changed, 13 insertions, 7 deletions
diff --git a/src/ch/epfl/xblast/server/Server.java b/src/ch/epfl/xblast/server/Server.java
index 0989d55..6d8db2c 100644
--- a/src/ch/epfl/xblast/server/Server.java
+++ b/src/ch/epfl/xblast/server/Server.java
@@ -20,6 +20,8 @@ import java.util.*;
20public class Server { 20public class Server {
21 21
22 public static final int DEFAULT_PORT = 2016; 22 public static final int DEFAULT_PORT = 2016;
23
24 private static final long REFRESH_RATE = (long) (50 * 1E6); // nanosecond
23 private static final int DEFAULT_EXPECTED_CLIENTS = PlayerID.values().length; 25 private static final int DEFAULT_EXPECTED_CLIENTS = PlayerID.values().length;
24 26
25 private static class Channel { 27 private static class Channel {
@@ -133,6 +135,14 @@ public class Server {
133 135
134 } 136 }
135 137
138 private static void delay(long ns) {
139 try {
140 Thread.sleep((long) (ns / 1E6), (int) (ns % 1E6));
141 } catch (InterruptedException e) {
142 e.printStackTrace();
143 }
144 }
145
136 private final Channel channel; 146 private final Channel channel;
137 private final int expectedClients; 147 private final int expectedClients;
138 148
@@ -160,15 +170,11 @@ public class Server {
160 GameState gameState = GameState.DEFAULT_GAME_STATE; 170 GameState gameState = GameState.DEFAULT_GAME_STATE;
161 171
162 while (!gameState.isGameOver()) { 172 while (!gameState.isGameOver()) {
173 long computationStartTime = System.nanoTime();
174
163 gameState = updateGameState(updateGameState(gameState)); 175 gameState = updateGameState(updateGameState(gameState));
164 // TODO: send updated game state to clients
165 176
166 try { 177 delay(REFRESH_RATE - (computationStartTime - System.nanoTime()));
167 Thread.sleep(10000);
168 // TODO: adapt sleeping time
169 } catch (InterruptedException e) {
170 e.printStackTrace();
171 }
172 } 178 }
173 } 179 }
174 180