Beispiel #1
0
 public void ObjectStringConverter_ConversionTests()
 {
     NetworkMessage input = new NetworkMessage { Content = _messageContent };
     NetworkMessage output = _converter.StringToObject(_converter.ObjectToString(input)) as NetworkMessage;
     Assert.That(input.Content.Equals(output.Content), "input.Content was " + input.Content + ", but output.Content was " +
         output.Content);
 }
Beispiel #2
0
        public void ReceiveNetworkMessage(IPAddress senderAddress, NetworkMessage message)
        {
            Match match = _nameRegex.Match(message.Content);
            if (match.Success)
            {
                if (_host == null)
                {
                    //Only the host keeps track of clients and ids and etc
                    return;
                }

                string clientPlayerName = match.Groups[1].Value;
                if (_clientAddressesAndMonikers.ContainsKey(senderAddress))
                {
                    _clientAddressesAndMonikers[senderAddress][NAME] = clientPlayerName;
                }
                else
                {
                    var monikers = new List<object> {
                        clientPlayerName,
                        null
                    };
                    _clientAddressesAndMonikers.Add(senderAddress, monikers);
                }
                //Make a list of player names and send to clients
                string list = _mainMenu.PlayerName;
                foreach (var moniker in _clientAddressesAndMonikers)
                {
                    list = list + "|" + moniker.Value[NAME];
                }
                PlayerList.Configuration = new[] {list};
                NewPlayerListUpdate = true;
                _host.SendObjectTCP(PlayerList);
                return;
            }

            match = _yourIDRegex.Match(message.Content);
            if (match.Success)
            {
                int id = int.Parse(match.Groups[1].Value);
                while (PlayerManager.Players.Count < id + 1)
                {
                    PlayerManager.AddPlayer(new Player(1, 1, Color.Red));
                }
                MainPlayerID = id;
                MainPlayer.Bank = 50000;
                MainPlayer.IsDead = true;
                return;
            }

            match = _changeScreenRegex.Match(message.Content);
            if (match.Success)
            {
                if (match.Groups[1].Value.Equals(Screen.Upgrade.ToString()))
                {
                    ChangeToScreen(Screen.Upgrade);
                    return;
                }
                if (match.Groups[1].Value.Equals(Screen.InGame.ToString()))
                {
                    ChangeToScreen(Screen.InGame);
                    return;
                }
            }

            if (_host != null && message.Content.Equals("Ready"))
            {
                ShoppingPlayers.Remove(senderAddress);
                if (ShoppingPlayers.Count == 0)
                {
                    _upgradeAndWaitScreen.EnableTheReadyButton();
                }
            }
        }