Skip to content

Commit

Permalink
Server Multiplayer: Make inactivity and timeout seconds configurable #25
Browse files Browse the repository at this point in the history
  • Loading branch information
MrStahlfelge committed Jan 31, 2021
1 parent ecb6805 commit e42d7e2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
10 changes: 4 additions & 6 deletions server/src/de/golfgl/lightblocks/server/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@
import de.golfgl.lightblocks.server.model.PlayerInfo;

public class Player {
private static final int SECONDS_TIMEOUT = 5;
private static final int SECONDS_INACTIVITY = 25;
private static final int SECONDS_INACTIVITY_WARNING = 10;
private static final String GAME_TIMEOUT_WARNING = "Continue playing or you will be disconnected...";
private static final String GAME_TIMEOUT_WARNING = "Inactive players will be disconnected";
private final LightblocksServer server;
private final WebSocket conn;
public String nickName;
Expand Down Expand Up @@ -106,16 +104,16 @@ public void sendMessageToPlayer(String s) {
public boolean checkTimeOuts() {
long time = System.currentTimeMillis();

if (time - lastGameMessageReceived > (SECONDS_INACTIVITY - SECONDS_INACTIVITY_WARNING) * 1000L) {
if (time - lastGameMessageReceived > (server.serverConfig.secondsInactivity - SECONDS_INACTIVITY_WARNING) * 1000L) {
sendMessageToPlayer(GAME_TIMEOUT_WARNING);
}

if (time - lastMessageReceived > SECONDS_TIMEOUT * 1000L) {
if (time - lastMessageReceived > server.serverConfig.secondsTimeout * 1000L) {
Gdx.app.log("Player", "Timeout: " + (time - lastMessageReceived));
conn.close(4102, "Timeout");
}

if (time - lastGameMessageReceived > SECONDS_INACTIVITY * 1000L) {
if (time - lastGameMessageReceived > server.serverConfig.secondsInactivity * 1000L) {
Gdx.app.log("Player", "Inactive: " + (time - lastGameMessageReceived));
conn.close(4102, "Inactivity");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public class ServerConfiguration {
public String name = "Lighblocks Server";
public boolean enableNsd;
public boolean resetEmptyRooms = true;
public int secondsInactivity = 25;
public int secondsTimeout = 5;
private ServerInfo serverInfo;

public ServerConfiguration(String[] arg) {
Expand Down Expand Up @@ -121,6 +123,8 @@ private void readXml() {
serverInfo.version = LightblocksServer.SERVER_VERSION;

modeType = prefs.getInteger(KEY_XML_GAMEMODES, InitGameParameters.TYPE_MIX);
secondsTimeout = Math.max(secondsTimeout, prefs.getInteger("disconnectTimeoutSeconds", secondsTimeout));
secondsInactivity = Math.max(secondsInactivity, prefs.getInteger("disconnectInactivitySeconds", secondsInactivity));

if (!xmlExisted) {
logger.info("You can set server information by editing server.xml file in the current working directory.");
Expand Down

0 comments on commit e42d7e2

Please sign in to comment.