Skip to content

Commit

Permalink
Consolidate inactive timer check, move logic into containing class
Browse files Browse the repository at this point in the history
  • Loading branch information
devo1929 committed Feb 15, 2024
1 parent 6213cf2 commit 012fdf5
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 74 deletions.
55 changes: 14 additions & 41 deletions DXMainClient/DXGUI/Multiplayer/GameLobby/CnCNetGameLobby.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public CnCNetGameLobby(WindowManager windowManager, string iniName,
this.gameCollection = gameCollection;
this.cncnetUserData = cncnetUserData;
this.pmWindow = pmWindow;
gameHostInactiveCheck = new GameHostInactiveCheck(WindowManager);

ctcpCommandHandlers = new CommandHandlerBase[]
{
Expand Down Expand Up @@ -132,8 +133,7 @@ public CnCNetGameLobby(WindowManager windowManager, string iniName,

private MapSharingConfirmationPanel mapSharingConfirmationPanel;

private XNATimerControl gameHostActivityTimer;
private GameHostInactiveCheck gameHostInactiveCheck;
private readonly GameHostInactiveCheck gameHostInactiveCheck;

/// <summary>
/// The SHA1 of the latest selected map.
Expand All @@ -158,7 +158,7 @@ public override void Initialize()
IniNameOverride = nameof(CnCNetGameLobby);
base.Initialize();

MouseMove += (sender, args) => RefreshInactiveCheck();
MouseMove += (sender, args) => gameHostInactiveCheck.Reset();

btnChangeTunnel = FindChild<XNAClientButton>(nameof(btnChangeTunnel));
btnChangeTunnel.LeftClick += BtnChangeTunnel_LeftClick;
Expand All @@ -169,15 +169,7 @@ public override void Initialize()
gameBroadcastTimer.Enabled = false;
gameBroadcastTimer.TimeElapsed += GameBroadcastTimer_TimeElapsed;

gameHostInactiveCheck = new GameHostInactiveCheck();
gameHostInactiveCheck.CloseInactiveGame += GameHostInactiveCheck_CloseInactiveGame;
gameHostInactiveCheck.SendInactiveGameWarningMessage += GameHostInactiveCheck_SendInactiveGameWarningMessage;

gameHostActivityTimer = new XNATimerControl(WindowManager);
gameHostActivityTimer.AutoReset = true;
gameHostActivityTimer.Interval = TimeSpan.FromSeconds(1.0);
gameHostActivityTimer.Enabled = false;
gameHostActivityTimer.TimeElapsed += GameHostActivityTimer_TimeElapsed;
gameHostInactiveCheck.CloseEvent += GameHostInactiveCheckCloseEvent;

tunnelSelectionWindow = new TunnelSelectionWindow(WindowManager, tunnelHandler);
tunnelSelectionWindow.Initialize();
Expand All @@ -193,7 +185,6 @@ public override void Initialize()
mapSharingConfirmationPanel.MapDownloadConfirmed += MapSharingConfirmationPanel_MapDownloadConfirmed;

WindowManager.AddAndInitializeControl(gameBroadcastTimer);
WindowManager.AddAndInitializeControl(gameHostActivityTimer);

globalContextMenu = new GlobalContextMenu(WindowManager, connectionManager, cncnetUserData, pmWindow);
AddChild(globalContextMenu);
Expand Down Expand Up @@ -238,11 +229,7 @@ public void SetUp(Channel channel, bool isHost, int playerLimit,
RandomSeed = new Random().Next();
RefreshMapSelectionUI();
btnChangeTunnel.Enable();
if (ClientConfiguration.Instance.InactiveHostKickEnabled)
{
gameHostActivityTimer.Enabled = true;
gameHostActivityTimer.Start();
}
StartInactiveCheck();
}
else
{
Expand All @@ -262,29 +249,16 @@ public void SetUp(Channel channel, bool isHost, int playerLimit,

private void TunnelHandler_CurrentTunnelPinged(object sender, EventArgs e) => UpdatePing();

private void GameHostActivityTimer_TimeElapsed(object sender, EventArgs e) => gameHostInactiveCheck.Start();
private void GameHostInactiveCheckCloseEvent(object sender, EventArgs e) => LeaveGameLobby();

private void GameHostInactiveCheck_SendInactiveGameWarningMessage(object sender, EventArgs e)
public void StartInactiveCheck()
{
XNAMessageBox hostInactiveWarningMessageBox = new XNAMessageBox(
WindowManager,
ClientConfiguration.Instance.InactiveHostWarningTitle,
ClientConfiguration.Instance.InactiveHostWarningMessage,
XNAMessageBoxButtons.OK
);
hostInactiveWarningMessageBox.OKClickedAction = InactiveMessageBox_OKClicked;
hostInactiveWarningMessageBox.Show();
if (!ClientConfiguration.Instance.InactiveHostKickEnabled)
return;
gameHostInactiveCheck.Start();
}

private void GameHostInactiveCheck_CloseInactiveGame(object sender, EventArgs e) => LeaveGameLobby();

private void InactiveMessageBox_OKClicked(XNAMessageBox messageBox) => gameHostInactiveCheck.Reset();

public void RefreshInactiveCheck()
{
if (gameHostActivityTimer.Enabled)
gameHostInactiveCheck.Reset();
}
public void StopInactiveCheck() => gameHostInactiveCheck.Stop();

public void OnJoined()
{
Expand Down Expand Up @@ -415,8 +389,7 @@ public void LeaveGameLobby()
{
if (IsHost)
{
gameHostActivityTimer.Enabled = false;
gameHostActivityTimer.Pause();
StopInactiveCheck();
closed = true;
BroadcastGame();
}
Expand Down Expand Up @@ -1258,7 +1231,7 @@ protected override void GameProcessExited()
BroadcastPlayerOptions();
BroadcastPlayerExtraOptions();

gameHostActivityTimer.Resume();
StartInactiveCheck();

if (Players.Count < playerLimit)
UnlockGame(true);
Expand Down Expand Up @@ -1328,7 +1301,7 @@ protected override void StartGame()
HandleCheatDetectedMessage(ProgramConstants.PLAYERNAME);
}

gameHostActivityTimer.Pause();
StopInactiveCheck();

base.StartGame();
}
Expand Down
93 changes: 60 additions & 33 deletions DXMainClient/DXGUI/Multiplayer/GameLobby/GameHostInactiveCheck.cs
Original file line number Diff line number Diff line change
@@ -1,54 +1,81 @@
using ClientCore;
using Rampastring.Tools;
using System;
using System;
using System.Timers;
using ClientCore;
using ClientGUI;
using Rampastring.XNAUI;

namespace DTAClient.DXGUI.Multiplayer.GameLobby
{
class GameHostInactiveCheck
public class GameHostInactiveCheck
{
private int secondsElapsed;
private bool inactiveGameWarningMessageSent = false;
private bool closeInactiveGameEventSent = false;
private readonly WindowManager windowManager;
private readonly Timer timer;
private bool warningShown;
private DateTime startDttm;
private static int WarningSeconds => ClientConfiguration.Instance.InactiveHostWarningMessageSeconds;
private static int CloseSeconds => WarningSeconds + ClientConfiguration.Instance.InactiveHostKickSeconds;

public event EventHandler SendInactiveGameWarningMessage;
public event EventHandler CloseInactiveGame;
public event EventHandler CloseEvent;

public GameHostInactiveCheck(WindowManager windowManager)
{
this.windowManager = windowManager;
timer = CreateTimer();
}


private Timer CreateTimer()
{
var _timer = new Timer();
_timer.AutoReset = true;
_timer.Interval = 1000;
_timer.Elapsed += TimerOnElapsed;
return _timer;
}

private void TimerOnElapsed(object sender, ElapsedEventArgs e)
{
double secondsElapsed = (DateTime.UtcNow - startDttm).TotalSeconds;

if (secondsElapsed > WarningSeconds && !warningShown)
ShowWarning();

if (secondsElapsed > CloseSeconds)
SendCloseEvent();
}

public void Start()
{
secondsElapsed++;

if (secondsElapsed > ClientConfiguration.Instance.InactiveHostWarningMessageSeconds &&
!inactiveGameWarningMessageSent)
{
SendHostInactiveEvent();
}

if (secondsElapsed > ClientConfiguration.Instance.InactiveHostKickSeconds &&
inactiveGameWarningMessageSent &&
!closeInactiveGameEventSent)
{
SendCloseGameEvent();
}
Reset();
timer.Start();
}


public void Reset()
{
secondsElapsed = 0;
closeInactiveGameEventSent = false;
inactiveGameWarningMessageSent = false;
startDttm = DateTime.UtcNow;
warningShown = false;
}

private void SendCloseGameEvent()
public void Stop() => timer.Stop();

private void SendCloseEvent()
{
CloseInactiveGame?.Invoke(this, null);
closeInactiveGameEventSent = true;
Stop();
CloseEvent?.Invoke(this, null);
}

private void SendHostInactiveEvent()
private void ShowWarning()
{
SendInactiveGameWarningMessage?.Invoke(this, null);
inactiveGameWarningMessageSent = true;
secondsElapsed = 0;
warningShown = true;
XNAMessageBox hostInactiveWarningMessageBox = new XNAMessageBox(
windowManager,
ClientConfiguration.Instance.InactiveHostWarningTitle,
ClientConfiguration.Instance.InactiveHostWarningMessage,
XNAMessageBoxButtons.OK
);
hostInactiveWarningMessageBox.OKClickedAction = box => Reset();
hostInactiveWarningMessageBox.Show();
}
}
}

0 comments on commit 012fdf5

Please sign in to comment.