Skip to content

Commit

Permalink
Add auto desync kick
Browse files Browse the repository at this point in the history
  • Loading branch information
maxsupermanhd committed Jul 20, 2023
1 parent ba3ea74 commit 9870bea
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 3 deletions.
1 change: 1 addition & 0 deletions lib/gamelib/gtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,7 @@ void recvPlayerGameTime(NETQUEUE queue)
{
NETsetPlayerConnectionStatus(CONNECTIONSTATUS_DESYNC, queue.index);
}
ingame.DesyncCounter[queue.index]++;
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,7 @@ bool loadConfig()
pie_EnableFog(false);
}
war_setAutoLagKickSeconds(iniGetInteger("hostAutoLagKickSeconds", war_getAutoLagKickSeconds()).value());
war_setAutoDesyncKickSeconds(iniGetInteger("hostAutoDesyncKickSeconds", war_getAutoDesyncKickSeconds()).value());
war_setDisableReplayRecording(iniGetBool("disableReplayRecord", war_getDisableReplayRecording()).value());
war_setMaxReplaysSaved(iniGetInteger("maxReplaysSaved", war_getMaxReplaysSaved()).value());
war_setOldLogsLimit(iniGetInteger("oldLogsLimit", war_getOldLogsLimit()).value());
Expand Down Expand Up @@ -768,6 +769,7 @@ bool saveConfig()
iniSetBool("autosaveEnabled", autosaveEnabled);
iniSetBool("fog", pie_GetFogEnabled());
iniSetInteger("hostAutoLagKickSeconds", war_getAutoLagKickSeconds());
iniSetInteger("hostAutoDesyncKickSeconds", war_getAutoDesyncKickSeconds());
iniSetBool("disableReplayRecord", war_getDisableReplayRecording());
iniSetInteger("maxReplaysSaved", war_getMaxReplaysSaved());
iniSetInteger("oldLogsLimit", war_getOldLogsLimit());
Expand Down
2 changes: 2 additions & 0 deletions src/multibot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
#include "mapgrid.h"
#include "multirecv.h"
#include "transporter.h"
#include "warzoneconfig.h"
#include "multiint.h"

#include <vector>
#include <algorithm>
Expand Down
51 changes: 48 additions & 3 deletions src/multiplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ static bool sendDataCheck2();
void startMultiplayerGame();

// ////////////////////////////////////////////////////////////////////////////
// Auto Lag Kick Handling
// Auto Kick Handling

#define LAG_INITIAL_LOAD_GRACEPERIOD 60
#define LAG_CHECK_INTERVAL 1000
Expand Down Expand Up @@ -180,12 +180,56 @@ static void autoLagKickRoutine()
ingame.LagCounter[i] = 0;
}
else if (ingame.LagCounter[i] >= (LagAutoKickSeconds - 3)) {
std::string msg = astringf("Auto-kicking player %" PRIu32 " (\"%s\") in %u seconds.", i, getPlayerName(i), (LagAutoKickSeconds - ingame.LagCounter[i]));
std::string msg = astringf("Auto-kicking player %" PRIu32 " (\"%s\") in %u seconds. (lag)", i, getPlayerName(i), (LagAutoKickSeconds - ingame.LagCounter[i]));
debug(LOG_INFO, "%s", msg.c_str());
sendTextMessage(msg.c_str());
}
else if (ingame.LagCounter[i] % 15 == 0) { // every 15 seconds
std::string msg = astringf("Auto-kicking player %" PRIu32 " (\"%s\") in %u seconds.", i, getPlayerName(i), (LagAutoKickSeconds - ingame.LagCounter[i]));
std::string msg = astringf("Auto-kicking player %" PRIu32 " (\"%s\") in %u seconds. (lag)", i, getPlayerName(i), (LagAutoKickSeconds - ingame.LagCounter[i]));
debug(LOG_INFO, "%s", msg.c_str());
sendTextMessage(msg.c_str());
}
}
}

static void autoDesyncKickRoutine()
{
if (!NetPlay.isHost)
{
return;
}

int DesyncAutoKickSeconds = war_getAutoDesyncKickSeconds();
if (DesyncAutoKickSeconds <= 0)
{
return;
}

const std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now();
if (std::chrono::duration_cast<std::chrono::milliseconds>(now - ingame.lastDesyncCheck) < LagCheckInterval)
{
return;
}

ingame.lastDesyncCheck = now;
uint32_t playerCheckLimit = (!ingame.TimeEveryoneIsInGame.has_value()) ? MAX_CONNECTED_PLAYERS : MAX_PLAYERS;
for (uint32_t i = 0; i < playerCheckLimit; ++i)
{
if (ingame.DesyncCounter[i] >= DesyncAutoKickSeconds) {
std::string msg = astringf("Auto-kicking player %" PRIu32 " (\"%s\") because of desync. (Timeout: %u seconds)", i, getPlayerName(i), DesyncAutoKickSeconds);
debug(LOG_INFO, "%s", msg.c_str());
sendTextMessage(msg.c_str());
wz_command_interface_output("WZEVENT: lag-kick: %u %s\n", i, NetPlay.players[i].IPtextAddress);
kickPlayer(i, "Your timeline diviated too far.", ERROR_CONNECTION, false);
ingame.DesyncCounter[i] = 0;
}
else if (ingame.DesyncCounter[i] >= (DesyncAutoKickSeconds - 3)) {
std::string msg = astringf("Auto-kicking player %" PRIu32 " (\"%s\") in %u seconds. (desync)", i, getPlayerName(i), (DesyncAutoKickSeconds - ingame.DesyncCounter[i]));
debug(LOG_INFO, "%s", msg.c_str());
sendTextMessage(msg.c_str());
}
else if (ingame.DesyncCounter[i] % 2 == 0) { // every 2 seconds
std::string msg = astringf("Auto-kicking player %" PRIu32 " (\"%s\") in %u seconds. (desync)", i, getPlayerName(i), (DesyncAutoKickSeconds - ingame.DesyncCounter[i]));
debug(LOG_INFO, "%s", msg.c_str());
sendTextMessage(msg.c_str());
}
Expand Down Expand Up @@ -385,6 +429,7 @@ bool multiPlayerLoop()
if (NetPlay.isHost)
{
autoLagKickRoutine();
autoDesyncKickRoutine();
}

// if player has won then process the win effects...
Expand Down
2 changes: 2 additions & 0 deletions src/multiplay.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ struct MULTIPLAYERINGAME
{
UDWORD PingTimes[MAX_CONNECTED_PLAYERS]; // store for pings.
int LagCounter[MAX_CONNECTED_PLAYERS];
int DesyncCounter[MAX_CONNECTED_PLAYERS];
bool VerifiedIdentity[MAX_CONNECTED_PLAYERS]; // if the multistats identity has been verified.
bool localOptionsReceived; // used to show if we have game options yet..
bool localJoiningInProgress; // used before we know our player number.
Expand All @@ -116,6 +117,7 @@ struct MULTIPLAYERINGAME
std::chrono::steady_clock::time_point startTime;
optional<std::chrono::steady_clock::time_point> endTime;
std::chrono::steady_clock::time_point lastLagCheck;
std::chrono::steady_clock::time_point lastDesyncCheck;
optional<std::chrono::steady_clock::time_point> lastSentPlayerDataCheck2[MAX_CONNECTED_PLAYERS] = {};
std::chrono::steady_clock::time_point lastPlayerDataCheck2;
bool muteChat[MAX_CONNECTED_PLAYERS] = {false};
Expand Down
16 changes: 16 additions & 0 deletions src/warzoneconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ struct WARZONE_GLOBALS
JS_BACKEND jsBackend = (JS_BACKEND)0;
bool autoAdjustDisplayScale = true;
int autoLagKickSeconds = 60;
int autoDesyncKickSeconds = 10;
bool disableReplayRecording = false;
int maxReplaysSaved = MAX_REPLAY_FILES;
int oldLogsLimit = MAX_OLD_LOGS;
Expand Down Expand Up @@ -470,6 +471,21 @@ void war_setAutoLagKickSeconds(int seconds)
warGlobs.autoLagKickSeconds = seconds;
}

int war_getAutoDesyncKickSeconds()
{
return warGlobs.autoDesyncKickSeconds;
}

void war_setAutoDesyncKickSeconds(int seconds)
{
seconds = std::max(seconds, 0);
if (seconds > 0)
{
seconds = std::max(seconds, 60);
}
warGlobs.autoDesyncKickSeconds = seconds;
}

bool war_getDisableReplayRecording()
{
return warGlobs.disableReplayRecording;
Expand Down
2 changes: 2 additions & 0 deletions src/warzoneconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ bool war_getAutoAdjustDisplayScale();
void war_setAutoAdjustDisplayScale(bool autoAdjustDisplayScale);
int war_getAutoLagKickSeconds();
void war_setAutoLagKickSeconds(int seconds);
int war_getAutoDesyncKickSeconds();
void war_setAutoDesyncKickSeconds(int seconds);
bool war_getDisableReplayRecording();
void war_setDisableReplayRecording(bool disable);
int war_getMaxReplaysSaved();
Expand Down

0 comments on commit 9870bea

Please sign in to comment.