Пример #1
0
		public MainWindow() {
			InitializeComponent();
			messageBox.KeyDown += new KeyEventHandler(messageBox_keyDown);
			player = new VirusPlayer("Player1", rand.Next().ToString(), System.Drawing.Color.Red);
			lobby = new VirusLobby(player);
			lobby.OnBadMessageRecieved += new VirusLobby.TextFunction(BadMessage);
			lobby.OnColorChanged += new VirusLobby.PlayerUpdateColorFunction(ColorUpdated);
			lobby.OnEveryoneReadyChanged += new VirusLobby.BoolFunction(EveryoneReadyUpdated);
			lobby.OnGameMove += new VirusLobby.GameMoveFunction(GameMove);
			lobby.OnNameChanged += new VirusLobby.PlayerUpdateTextFunction(NameUpdated);
			lobby.OnPlayerConnected += new VirusLobby.PlayerUpdateFunction(PlayerConnected);
			lobby.OnPlayerDisconnected += new VirusLobby.PlayerUpdateFunction(PlayerDisconnected);
			lobby.OnStartGame += new VirusLobby.StartFunction(StartGame);
			lobby.OnTextMessageRecieved += new VirusLobby.TextMessageFunction(TextMessageRecieved);
		}
Пример #2
0
 public MainWindow()
 {
     InitializeComponent();
     messageBox.KeyDown += new KeyEventHandler(messageBox_keyDown);
     player              = new VirusPlayer("Player1", rand.Next().ToString(), System.Drawing.Color.Red);
     lobby = new VirusLobby(player);
     lobby.OnBadMessageRecieved   += new VirusLobby.TextFunction(BadMessage);
     lobby.OnColorChanged         += new VirusLobby.PlayerUpdateColorFunction(ColorUpdated);
     lobby.OnEveryoneReadyChanged += new VirusLobby.BoolFunction(EveryoneReadyUpdated);
     lobby.OnGameMove             += new VirusLobby.GameMoveFunction(GameMove);
     lobby.OnNameChanged          += new VirusLobby.PlayerUpdateTextFunction(NameUpdated);
     lobby.OnPlayerConnected      += new VirusLobby.PlayerUpdateFunction(PlayerConnected);
     lobby.OnPlayerDisconnected   += new VirusLobby.PlayerUpdateFunction(PlayerDisconnected);
     lobby.OnStartGame            += new VirusLobby.StartFunction(StartGame);
     lobby.OnTextMessageRecieved  += new VirusLobby.TextMessageFunction(TextMessageRecieved);
 }
Пример #3
0
        private void AwaitCommunicationClient(object client)
        {
            VirusPlayer   player       = (VirusPlayer)client;
            TcpClient     tcpClient    = player.TcpClient;
            NetworkStream clientStream = tcpClient.GetStream();

            while (player.Connected)
            {
                String message = "";

                try {                 //blocks until a client sends a message
                    message = ReadMessage(clientStream);
                }
                catch {                 //a socket error has occured
                    player.Connected = false;
                    break;
                }

                if (message == "")
                {
                    continue;
                }

                MessageType type = VirusLobby.ParseMessageType(message);

                // -- The player should start by initialising
                if (!player.Initialised)
                {
                    if (type != MessageType.Initialise)
                    {
                        player.Connected = false;
                        break;
                    }
                    bool success = TryParseInitialiseMessage(message, out player.ID, out player.Name, out player.Color);
                    if (!success)
                    {
                        player.Connected = false;
                        break;
                    }
                    player.Initialised = true;
                    Players.Add(player);
                    MasterPlayer = player;
                    Connected    = true;
                    if (OnPlayerConnected != null)
                    {
                        OnPlayerConnected(player);
                    }
                    continue;
                }

                // -- Player is already initialised, so the message needs other handling
                HandleMessageClient(message, type);
            }
            //player.TcpClient.Close();
            if (OnPlayerDisconnected != null)
            {
                try {
                    OnPlayerDisconnected(player);
                }
                catch { }
            }
            Players.Clear();
            GameStarted = false;
        }
Пример #4
0
        private void AwaitCommunicationMaster(object client)
        {
            VirusPlayer   player       = (VirusPlayer)client;
            TcpClient     tcpClient    = player.TcpClient;
            NetworkStream clientStream = tcpClient.GetStream();

            while (player.Connected)
            {
                String message = "";

                try {                 //blocks until a client sends a message
                    message = ReadMessage(clientStream);
                }
                catch {                 //a socket error has occured
                    player.Connected = false;
                    break;
                }

                if (message == "")
                {
                    continue;
                }

                MessageType type = VirusLobby.ParseMessageType(message);

                // -- The player should start by initialising
                if (!player.Initialised)
                {
                    if (type != MessageType.Initialise)
                    {
                        player.Connected = false;
                        break;
                    }
                    bool success = TryParseInitialiseMessage(message, out player.ID, out player.Name, out player.Color);
                    if (!success)
                    {
                        player.Connected = false;
                        break;
                    }
                    player.Initialised = true;
                    Players.Add(player);
                    Connected = true;
                    if (OnPlayerConnected != null)
                    {
                        OnPlayerConnected(player);
                    }
                    List <VirusPlayer> list  = new List <VirusPlayer>();
                    List <VirusPlayer> list2 = new List <VirusPlayer>();
                    list.Add(player);
                    foreach (VirusPlayer p in Players)
                    {
                        if (p.ID != player.ID)
                        {
                            SendInitialiseMessage(p, list);                             //send init message to new player about the existing players
                            list2.Add(p);
                        }
                    }
                    SendInitialiseMessage(Player, list);                     //send init message to new player about the host
                    SendInitialiseMessage(player, list2);                    //send init message to existing players about the new player

                    bool allreadyprev = true;
                    foreach (VirusPlayer p in Players)
                    {
                        if (p.ID != player.ID)
                        {
                            if (!p.Ready)
                            {
                                allreadyprev = false;
                            }
                        }
                    }

                    bool allreadynow = true;
                    foreach (VirusPlayer p in Players)
                    {
                        if (!p.Ready)
                        {
                            allreadynow = false;
                        }
                    }

                    if (allreadynow != allreadyprev)
                    {
                        if (OnEveryoneReadyChanged != null)
                        {
                            OnEveryoneReadyChanged(allreadynow);
                        }
                    }

                    continue;
                }

                // -- Player is already initialised, so the message needs other handling
                HandleMessageMaster(player, message, type);
            }
            player.TcpClient.Close();
            if (OnPlayerDisconnected != null)
            {
                try {
                    OnPlayerDisconnected(player);
                }
                catch { }
            }
            Players.Remove(player);
            SendDisconnectMessage(player);
        }