-
-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Consolidate inactive timer check, move logic into containing class
- Loading branch information
Showing
2 changed files
with
74 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 60 additions & 33 deletions
93
DXMainClient/DXGUI/Multiplayer/GameLobby/GameHostInactiveCheck.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |