/// <summary> /// Helper method for the event NameRequested /// </summary> /// <param name="pclient">The client</param> private void OnNameRequest(PirateClient pclient) { Contract.Ensures(pclient.Name == game.PlayerName); var playerName = game.PlayerName; pclient.SetName(playerName); }
/// <summary> /// Set the name of the specified player. /// </summary> /// <param name="pclient">The player to set the name for.</param> /// <param name="name">The name </param> public void SetPlayerName(PirateClient pclient, string name) { Contract.Requires(pclient != null && name != null && this.Clients.ContainsKey(pclient.Socket)); lock (Players) { if (!String.IsNullOrEmpty(this.Clients[pclient.Socket].Name)) { if (this.Players.ContainsKey(this.Clients[pclient.Socket].Name)) { this.Players.Remove(this.Clients[pclient.Socket].Name); } } this.Players.Add(name, pclient.Socket); } pclient.SetName(name); Console.WriteLine("Set name for " + pclient.Socket.RemoteEndPoint + " to " + name); UpdateBroadcastInfo(); }
private static void OnNameRequest(PirateClient pclient) { var playerName = string.Empty; while (String.IsNullOrEmpty(playerName)) { Console.Write("Select a name [a-zA-Z0-9] (3 - 12 chars): "); playerName = Console.ReadLine(); if(playerName == null || !Regex.IsMatch(playerName, @"^[a-zA-Z0-9]{3,12}$")) { Console.WriteLine("Invalid name specified, try again..."); playerName = string.Empty; } } Console.Title = "Player: " + playerName; pclient.SetName(playerName); }