Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability for host to change game name #662

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion ClientCore/CnCNet5/NameValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,41 @@ public static string GetValidOfflineName(string name)

if (validName.Length > ClientConfiguration.Instance.MaxNameLength)
return validName.Substring(0, ClientConfiguration.Instance.MaxNameLength);

return validName;
}

/// <summary>
/// Checks if a game name is valid for CnCNet.
/// </summary>
/// <param name="gameName">Game lobby name.</param>
/// <returns>Null if the game name is valid, otherwise a string that tells
/// what is wrong with the name.</returns>
public static string IsGameNameValid(string gameName)
{

if (string.IsNullOrEmpty(gameName))
{
return "Please enter a game name.".L10N("Client:Main:GameNameMissing");
}

char[] disallowedCharacters = { ',', ';' };
if (gameName.IndexOfAny(disallowedCharacters) != -1)
{
return "Game name contains disallowed characters.".L10N("Client:Main:GameNameDisallowedChars");
}

if (gameName.Length > 23)
{
return "Game name is too long.".L10N("Client:Main:GameNameTooLong");
}

if (new ProfanityFilter().IsOffensive(gameName))
{
return "Please enter a less offensive game name.".L10N("Client:Main:GameNameOffensiveText");
}

return null;
}
}
}
16 changes: 7 additions & 9 deletions DXMainClient/DXGUI/Multiplayer/CnCNet/GameCreationWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Rampastring.XNAUI.XNAControls;
using System;
using System.IO;
using ClientCore.CnCNet5;

namespace DTAClient.DXGUI.Multiplayer.CnCNet
{
Expand Down Expand Up @@ -239,17 +240,14 @@ private void BtnLoadMPGame_LeftClick(object sender, EventArgs e)

private void BtnCreateGame_LeftClick(object sender, EventArgs e)
{
string gameName = tbGameName.Text.Replace(";", string.Empty);

if (string.IsNullOrEmpty(gameName))
{
return;
}
var gameName = tbGameName.Text;
var gameNameValid = NameValidator.IsGameNameValid(gameName);

if (new ProfanityFilter().IsOffensive(gameName))
if (!string.IsNullOrEmpty(gameNameValid))
{
XNAMessageBox.Show(WindowManager, "Offensive game name".L10N("Client:Main:GameNameOffensiveTitle"),
"Please enter a less offensive game name.".L10N("Client:Main:GameNameOffensiveText"));
XNAMessageBox.Show(WindowManager, "Invalid game name.".L10N("Client:Main:GameNameInvalid"),
11EJDE11 marked this conversation as resolved.
Show resolved Hide resolved
gameNameValid);
return;
}

Expand All @@ -259,7 +257,7 @@ private void BtnCreateGame_LeftClick(object sender, EventArgs e)
}

GameCreated?.Invoke(this,
new GameCreationEventArgs(gameName,int.Parse(ddMaxPlayers.SelectedItem.Text),
new GameCreationEventArgs(gameName, int.Parse(ddMaxPlayers.SelectedItem.Text),
tbPassword.Text,tunnelHandler.Tunnels[lbTunnelList.SelectedIndex],
ddSkillLevel.SelectedIndex)
);
Expand Down
34 changes: 34 additions & 0 deletions DXMainClient/DXGUI/Multiplayer/GameLobby/CnCNetGameLobby.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class CnCNetGameLobby : MultiplayerGameLobby
private const string MAP_SHARING_DOWNLOAD_REQUEST = "MAPOK";
private const string MAP_SHARING_UPLOAD_REQUEST = "MAPREQ";
private const string MAP_SHARING_DISABLED_MESSAGE = "MAPSDISABLED";
private const string GAME_NAME_CHANGED = "GNC";
private const string CHEAT_DETECTED_MESSAGE = "CD";
private const string DICE_ROLL_MESSAGE = "DR";
private const string CHANGE_TUNNEL_SERVER_MESSAGE = "CHTNL";
Expand Down Expand Up @@ -87,6 +88,7 @@ PrivateMessagingWindow pmWindow
new StringCommandHandler("MM", CheaterNotification),
new StringCommandHandler(DICE_ROLL_MESSAGE, HandleDiceRollResult),
new NoParamCommandHandler(CHEAT_DETECTED_MESSAGE, HandleCheatDetectedMessage),
new StringCommandHandler(GAME_NAME_CHANGED, HandleGameNameChangeMessage),
new StringCommandHandler(CHANGE_TUNNEL_SERVER_MESSAGE, HandleTunnelServerChangeMessage)
};

Expand All @@ -97,6 +99,8 @@ PrivateMessagingWindow pmWindow

AddChatBoxCommand(new ChatBoxCommand("TUNNELINFO",
"View tunnel server information".L10N("Client:Main:TunnelInfoCommand"), false, PrintTunnelServerInformation));
AddChatBoxCommand(new ChatBoxCommand("GAMENAME",
"Change a game's lobby name (game host only).\nExample: \"/gamename 2v2 3v3\"".L10N("Client:Main:ChangeGameNameCommand"), true, s => ChangeGameName(s)));
11EJDE11 marked this conversation as resolved.
Show resolved Hide resolved
AddChatBoxCommand(new ChatBoxCommand("CHANGETUNNEL",
"Change the used CnCNet tunnel server (game host only)".L10N("Client:Main:ChangeTunnelCommand"),
true, (s) => ShowTunnelSelectionWindow("Select tunnel server:".L10N("Client:Main:SelectTunnelServerCommand"))));
Expand Down Expand Up @@ -1561,6 +1565,12 @@ protected override void BanPlayer(int playerIndex)
private void HandleCheatDetectedMessage(string sender) =>
AddNotice(string.Format("{0} has modified game files during the client session. They are likely attempting to cheat!".L10N("Client:Main:PlayerModifyFileCheat"), sender), Color.Red);

private void HandleGameNameChangeMessage(string sender, string newGameName)
{
AddNotice(String.Format("The game host has changed the game name to {0}".L10N("Client:Main:HostGameNameChanged"), newGameName));
channel.UIName = newGameName;
}

private void HandleTunnelServerChangeMessage(string sender, string tunnelAddressAndPort)
{
if (sender != hostName)
Expand Down Expand Up @@ -1882,6 +1892,30 @@ private void DownloadMapByIdCommand(string parameters)

#region Game broadcasting logic

/// <summary>
/// Handles changing the lobby UIName
/// </summary>
/// <param name="gameName">The new name for the hosted game.</param>
private void ChangeGameName(string gameName)
{
var gameNameValid = NameValidator.IsGameNameValid(gameName);

if (!string.IsNullOrEmpty(gameNameValid))
{
XNAMessageBox.Show(WindowManager, "Invalid game name.".L10N("Client:Main:GameNameInvalid"),
11EJDE11 marked this conversation as resolved.
Show resolved Hide resolved
gameNameValid);
return;
}

//update the name and broadcast to everyone
channel.UIName = gameName;
AccelerateGameBroadcasting();

//inform the players in the room
channel.SendCTCPMessage(GAME_NAME_CHANGED + " " + gameName, QueuedMessageType.SYSTEM_MESSAGE, priority: 9);
AddNotice(String.Format("Game name changed to {0}.".L10N("Client:Main:GameNameChanged"), gameName));
}

/// <summary>
/// Lowers the time until the next game broadcasting message.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion DXMainClient/Online/Channel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public Channel(string uiName, string channelName, bool persistent, bool isChatCh

#region Public members

public string UIName { get; }
public string UIName { get; set; }

public string ChannelName { get; }

Expand Down
Loading