Skip to content

Commit

Permalink
Potential fix for a crash in rare situations where the game host had …
Browse files Browse the repository at this point in the history
…sent a player options message that had 8 players on it despite the channel actually having only 7 players on it. Also improved game broadcasting code.
  • Loading branch information
Rampastring committed Dec 11, 2016
1 parent 2db4bcb commit 0965180
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 47 deletions.
53 changes: 22 additions & 31 deletions DXMainClient/DXGUI/Multiplayer/GameLobby/CnCNetGameLobby.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ namespace DTAClient.DXGUI.Multiplayer.GameLobby
{
public class CnCNetGameLobby : MultiplayerGameLobby
{
const double GAME_BROADCAST_CHECK_INTERVAL = 10.0;
const double INITIAL_TIME = 5.0;
const int MAX_PLAYER_COUNT = 8;
private const double GAME_BROADCAST_INTERVAL = 30.0;
private const double INITIAL_TIME = 20.0;

const string MAP_SHARING_FAIL_MESSAGE = "MAPFAIL";
const string MAP_SHARING_DOWNLOAD_REQUEST = "MAPOK";
const string MAP_SHARING_UPLOAD_REQUEST = "MAPREQ";
const string MAP_SHARING_DISABLED_MESSAGE = "MAPSDISABLED";
private const string MAP_SHARING_FAIL_MESSAGE = "MAPFAIL";
private const string MAP_SHARING_DOWNLOAD_REQUEST = "MAPOK";
private const string MAP_SHARING_UPLOAD_REQUEST = "MAPREQ";
private const string MAP_SHARING_DISABLED_MESSAGE = "MAPSDISABLED";

public CnCNetGameLobby(WindowManager windowManager, string iniName,
TopBar topBar, List<GameMode> GameModes, CnCNetManager connectionManager,
Expand Down Expand Up @@ -84,8 +83,6 @@ public class CnCNetGameLobby : MultiplayerGameLobby

TimeSpan timeSinceGameBroadcast = TimeSpan.Zero;

int timerTicks = 0;

int playerLimit;

bool closed = false;
Expand Down Expand Up @@ -125,16 +122,9 @@ public class CnCNetGameLobby : MultiplayerGameLobby

if (isHost)
{

//PlayerInfo host = new PlayerInfo(ProgramConstants.PLAYERNAME);
//host.Ready = true;
//host.Verified = true;
//Players.Add(host);

RandomSeed = new Random().Next();

timerTicks = 1000000;

// Broadcast about our game after 10 seconds
timeSinceGameBroadcast = TimeSpan.FromSeconds(INITIAL_TIME);
}
else
Expand Down Expand Up @@ -246,7 +236,7 @@ protected override void BtnLeaveGame_LeftClick(object sender, EventArgs e)
if (IsHost)
{
closed = true;
ForceBroadcastGame();
BroadcastGame();
}

Clear();
Expand Down Expand Up @@ -306,7 +296,7 @@ private void Channel_UserAdded(object sender, ChannelUserEventArgs e)
PlayerInfo pInfo = new PlayerInfo(e.User.IRCUser.Name);
Players.Add(pInfo);

if (Players.Count + AIPlayers.Count > MAX_PLAYER_COUNT)
if (Players.Count + AIPlayers.Count > MAX_PLAYER_COUNT && AIPlayers.Count > 0)
AIPlayers.RemoveAt(AIPlayers.Count - 1);

if (sndJoinSound != null)
Expand Down Expand Up @@ -348,6 +338,7 @@ private void RemovePlayer(string playerName)
Players.Remove(pInfo);

CopyPlayerDataToUI();
BroadcastPlayerOptions();
}

if (sndLeaveSound != null)
Expand Down Expand Up @@ -1127,17 +1118,19 @@ protected override void LockGame()

Locked = true;
btnLockGame.Text = "Unlock Game";
AccelerateGameBroadcasting();
}

protected override void UnlockGame(bool announce)
{
connectionManager.SendCustomMessage(new QueuedMessage(
string.Format("MODE {0} -i", channel.ChannelName), QueuedMessageType.INSTANT_MESSAGE, -1));
string.Format("MODE {0} -i", channel.ChannelName), QueuedMessageType.SYSTEM_MESSAGE, 10));

Locked = false;
if (announce)
AddNotice("The game room has been unlocked.");
btnLockGame.Text = "Lock Game";
AccelerateGameBroadcasting();
}

protected override void KickPlayer(int playerIndex)
Expand Down Expand Up @@ -1387,17 +1380,22 @@ public override void Update(GameTime gameTime)
{
timeSinceGameBroadcast += gameTime.ElapsedGameTime;

if (timeSinceGameBroadcast > TimeSpan.FromSeconds(GAME_BROADCAST_CHECK_INTERVAL))
if (timeSinceGameBroadcast > TimeSpan.FromSeconds(GAME_BROADCAST_INTERVAL))
BroadcastGame();
}

base.Update(gameTime);
}

private void ForceBroadcastGame()
/// <summary>
/// Temporarily accelerates game broadcasting by one-third of the normal broadcasting interval.
/// </summary>
private void AccelerateGameBroadcasting()
{
timerTicks = 1000000;
BroadcastGame();
timeSinceGameBroadcast += TimeSpan.FromSeconds(GAME_BROADCAST_INTERVAL / 3.0);

if (timeSinceGameBroadcast > TimeSpan.FromSeconds(GAME_BROADCAST_INTERVAL))
BroadcastGame();
}

private void BroadcastGame()
Expand All @@ -1412,11 +1410,6 @@ private void BroadcastGame()
if (GameMode == null || Map == null)
return;

timerTicks++;

if (timerTicks < 3)
return;

StringBuilder sb = new StringBuilder("GAME ");
sb.Append(ProgramConstants.CNCNET_PROTOCOL_REVISION);
sb.Append(";");
Expand Down Expand Up @@ -1454,8 +1447,6 @@ private void BroadcastGame()
sb.Append(0); // LoadedGameId

broadcastChannel.SendCTCPMessage(sb.ToString(), QueuedMessageType.SYSTEM_MESSAGE, 20);

timerTicks = 0;
}

#endregion
Expand Down
18 changes: 9 additions & 9 deletions DXMainClient/DXGUI/Multiplayer/GameLobby/GameLobbyBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace DTAClient.DXGUI.Multiplayer.GameLobby
/// </summary>
public abstract class GameLobbyBase : XNAWindow
{
protected const int PLAYER_COUNT = 8;
protected const int MAX_PLAYER_COUNT = 8;
protected const int PLAYER_OPTION_VERTICAL_MARGIN = 12;
protected const int PLAYER_OPTION_HORIZONTAL_MARGIN = 3;
protected const int PLAYER_OPTION_CAPTION_Y = 6;
Expand Down Expand Up @@ -423,11 +423,11 @@ protected void LbMapList_SelectedIndexChanged(object sender, EventArgs e)
/// </summary>
protected void InitPlayerOptionDropdowns()
{
ddPlayerNames = new XNAClientDropDown[PLAYER_COUNT];
ddPlayerSides = new XNAClientDropDown[PLAYER_COUNT];
ddPlayerColors = new XNAClientDropDown[PLAYER_COUNT];
ddPlayerStarts = new XNAClientDropDown[PLAYER_COUNT];
ddPlayerTeams = new XNAClientDropDown[PLAYER_COUNT];
ddPlayerNames = new XNAClientDropDown[MAX_PLAYER_COUNT];
ddPlayerSides = new XNAClientDropDown[MAX_PLAYER_COUNT];
ddPlayerColors = new XNAClientDropDown[MAX_PLAYER_COUNT];
ddPlayerStarts = new XNAClientDropDown[MAX_PLAYER_COUNT];
ddPlayerTeams = new XNAClientDropDown[MAX_PLAYER_COUNT];

int playerOptionVecticalMargin = GameOptionsIni.GetIntValue(Name, "PlayerOptionVerticalMargin", PLAYER_OPTION_VERTICAL_MARGIN);
int playerOptionHorizontalMargin = GameOptionsIni.GetIntValue(Name, "PlayerOptionHorizontalMargin", PLAYER_OPTION_HORIZONTAL_MARGIN);
Expand All @@ -447,7 +447,7 @@ protected void InitPlayerOptionDropdowns()

string randomColor = GameOptionsIni.GetStringValue("General", "RandomColor", "255,255,255");

for (int i = PLAYER_COUNT - 1; i > -1; i--)
for (int i = MAX_PLAYER_COUNT - 1; i > -1; i--)
{
var ddPlayerName = new XNAClientDropDown(WindowManager);
ddPlayerName.Name = "ddPlayerName" + i;
Expand Down Expand Up @@ -1091,7 +1091,7 @@ protected virtual void CopyPlayerDataToUI()
}

// Unused player slots
for (int ddIndex = Players.Count + AIPlayers.Count; ddIndex < PLAYER_COUNT; ddIndex++)
for (int ddIndex = Players.Count + AIPlayers.Count; ddIndex < MAX_PLAYER_COUNT; ddIndex++)
{
XNADropDown ddPlayerName = ddPlayerNames[ddIndex];
ddPlayerName.AllowDropDown = false;
Expand All @@ -1114,7 +1114,7 @@ protected virtual void CopyPlayerDataToUI()
ddPlayerTeams[ddIndex].AllowDropDown = false;
}

if (allowOptionsChange && Players.Count + AIPlayers.Count < PLAYER_COUNT)
if (allowOptionsChange && Players.Count + AIPlayers.Count < MAX_PLAYER_COUNT)
ddPlayerNames[Players.Count + AIPlayers.Count].AllowDropDown = true;

MapPreviewBox.UpdateStartingLocationTexts();
Expand Down
1 change: 0 additions & 1 deletion DXMainClient/DXGUI/Multiplayer/GameLobby/LANGameLobby.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ namespace DTAClient.DXGUI.Multiplayer.GameLobby
{
public class LANGameLobby : MultiplayerGameLobby
{
private const int MAX_PLAYER_COUNT = 8;
private const double DROPOUT_TIMEOUT = 20.0;
private const double GAME_BROADCAST_INTERVAL = 10.0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ public override void Initialize()

InitPlayerOptionDropdowns();

ReadyBoxes = new XNACheckBox[PLAYER_COUNT];
ReadyBoxes = new XNACheckBox[MAX_PLAYER_COUNT];

int readyBoxX = GameOptionsIni.GetIntValue(Name, "PlayerReadyBoxX", 7);
int readyBoxY = GameOptionsIni.GetIntValue(Name, "PlayerReadyBoxY", 4);

for (int i = 0; i < PLAYER_COUNT; i++)
for (int i = 0; i < MAX_PLAYER_COUNT; i++)
{
XNACheckBox chkPlayerReady = new XNACheckBox(WindowManager);
chkPlayerReady.Name = "chkPlayerReady" + i;
Expand Down Expand Up @@ -630,6 +630,9 @@ protected override void CopyPlayerDataFromUI(object sender, EventArgs e)

protected override void CopyPlayerDataToUI()
{
if (Players.Count + AIPlayers.Count > MAX_PLAYER_COUNT)
return;

base.CopyPlayerDataToUI();

if (IsHost)
Expand All @@ -650,7 +653,7 @@ protected override void CopyPlayerDataToUI()
ReadyBoxes[aiId + Players.Count].Checked = true;
}

for (int i = AIPlayers.Count + Players.Count; i < PLAYER_COUNT; i++)
for (int i = AIPlayers.Count + Players.Count; i < MAX_PLAYER_COUNT; i++)
{
ReadyBoxes[i].Checked = false;
}
Expand Down
2 changes: 1 addition & 1 deletion DXMainClient/DXGUI/Multiplayer/GameLobby/SkirmishLobby.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ private void LoadSettings()
return;
}

if (AIPlayers.Count < PLAYER_COUNT - 1)
if (AIPlayers.Count < MAX_PLAYER_COUNT - 1)
AIPlayers.Add(aiPlayer);
}

Expand Down
4 changes: 2 additions & 2 deletions DXMainClient/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.0.2.5")]
[assembly: AssemblyFileVersion("2.0.2.5")]
[assembly: AssemblyVersion("2.0.2.6")]
[assembly: AssemblyFileVersion("2.0.2.6")]

0 comments on commit 0965180

Please sign in to comment.