private void CommandRecieved(object sender, CommandEventArgs e)
        {
            //When a user connects set their client username
            if (e.Command.CommandType == CommandType.UserConnected)
            {
                string username = e.Command.Data.Split(':')[2];
                Console.WriteLine("Checking for : " + username);
                bool nameAvailability = CheckUsernameAvailability(e.Command.SenderIP, e.Command.SenderPort, username);
                if (nameAvailability)
                {
                    string data = CheckForClientData(username);

                    SetClientUsername(e.Command.SenderIP, e.Command.SenderPort, username);
                    SetClientData(e.Command.SenderIP, e.Command.SenderPort, data);
                    SendUserStats(e.Command.SenderIP, e.Command.SenderPort, data);
                    AnswerUsernameRequest(e.Command.SenderIP, e.Command.SenderPort, nameAvailability);
                    e.Command.Data += (":" + data);
                    SendCommandToAll(e.Command);
                }
                else if (nameAvailability == false)
                {
                    AnswerUsernameRequest(e.Command.SenderIP, e.Command.SenderPort, nameAvailability);
                }

            }

            //User asks to disconnect
            if (e.Command.CommandType == CommandType.UserDisconnectRequest)
            {
                int index = FindClientID(e.Command.SenderIP, e.Command.SenderPort);
                string clientDetails = clientList[index].IP.ToString() + ":" + clientList[index].Port.ToString() + ":" + clientList[index].Username;
                Console.WriteLine("User {0}:{1} ({2}) has disconnected ({3}/{4})", e.Command.SenderIP, e.Command.SenderPort, clientList[index].Username, DateTime.Now.ToShortTimeString(), DateTime.Now.ToLongDateString());
                clientList[index].Disconnect();
                clientList.RemoveAt(index);
                Command cmd = new Command(CommandType.UserDisconnected, IPAddress.Broadcast);
                cmd.SenderName = e.Command.SenderName;
                cmd.SenderIP = e.Command.SenderIP;
                cmd.SenderPort = e.Command.SenderPort;
                cmd.Data = clientDetails;
                SendCommandToAll(cmd);

            }

            //Reply to client list request
            if (e.Command.CommandType == CommandType.ClientListRequest)
            {
                SendClientList(e.Command.SenderIP, e.Command.SenderPort);
            }

            //Sends message commands to all connected clients
            if (e.Command.CommandType == CommandType.Message)
            {
                if (e.Command.TargetIP.Equals(IPAddress.Broadcast))
                {
                    SendCommandToAll(e.Command);
                }
                else
                {
                    SendCommandToClient(e.Command);
                }
            }

            //Pass challenge request to challenged user
            if (e.Command.CommandType == CommandType.ChallengeRequest)
            {
                int challengerID = FindClientID(e.Command.SenderIP, e.Command.SenderPort);
                e.Command.Data = clientList[challengerID].Wins + ":" + clientList[challengerID].Losses;
                SendCommandToClient(e.Command);
            }

            //Pass challenge response to challenger
            if (e.Command.CommandType == CommandType.ChallengeResponse)
            {
                SendCommandToClient(e.Command);
            }

            //Handle game start request
            if (e.Command.CommandType == CommandType.GameStartRequest)
            {
                IPAddress client2IP = IPAddress.Parse(e.Command.Data.Split(':')[0]);
                int client2Port = int.Parse(e.Command.Data.Split(':')[1]);
                int client1ID = FindClientID(e.Command.SenderIP, e.Command.SenderPort);
                int client2ID = FindClientID(client2IP, client2Port);
                BattleshipsGame game = new BattleshipsGame(clientList[client1ID], clientList[client2ID]);

                Command cmd = new Command(CommandType.GameIDInform, e.Command.SenderIP, activeGames.Count.ToString());
                cmd.SenderName = "server";
                cmd.SenderIP = serverIP;
                cmd.SenderPort = serverPort;
                cmd.TargetPort = e.Command.SenderPort;
                SendCommandToClient(cmd);

                cmd.TargetIP = client2IP;
                cmd.TargetPort = client2Port;
                SendCommandToClient(cmd);

                activeGames.Add(game);
            }

            //Handle ShipPlacementRequest
            if (e.Command.CommandType == CommandType.GameShipRequest)
            {
                activeGames[int.Parse(e.Command.Data.Split(':')[0])].CommandRecieved(e);
            }
            //Handle GameShotRequest
            if (e.Command.CommandType == CommandType.GameShotRequest)
            {
                activeGames[int.Parse(e.Command.Data.Split(':')[0])].CommandRecieved(e);
            }
            //Handle GameOverInform
            if (e.Command.CommandType == CommandType.GameOverInform)
            {
                activeGames[int.Parse(e.Command.Data.Split(':')[0])].GameOverMessageCount++;
                if (activeGames[int.Parse(e.Command.Data.Split(':')[0])].GameOverMessageCount >= 2)
                {
                    PostGameStatisticsUpdate(activeGames[int.Parse(e.Command.Data.Split(':')[0])].Clients);
                    activeGames.RemoveAt(int.Parse(e.Command.Data.Split(':')[0]));
                    GC.Collect();
                }
            }

            //Handle user data request
            if (e.Command.CommandType == CommandType.UserDataRequest)
            {
                int ID = FindClientID(e.Command.TargetIP, e.Command.TargetPort);
                string data = CheckForClientData(clientList[ID].Username);
                Command cmd = new Command(CommandType.UserDataInform, e.Command.SenderIP, data);
                cmd.SenderIP = e.Command.TargetIP;
                cmd.SenderPort = e.Command.TargetPort;
                cmd.SenderName = clientList[ID].Username; //Client username and IPEndpoint data is stored as command sender data
                cmd.TargetPort = e.Command.SenderPort;
                SendCommandToClient(cmd);
            }
        }
Beispiel #2
0
        private void CommandRecieved(object sender, CommandEventArgs e)
        {
            //When a user connects set their client username
            if (e.Command.CommandType == CommandType.UserConnected)
            {
                string username = e.Command.Data.Split(':')[2];
                Console.WriteLine("Checking for : " + username);
                bool nameAvailability = CheckUsernameAvailability(e.Command.SenderIP, e.Command.SenderPort, username);
                if (nameAvailability)
                {
                    string data = CheckForClientData(username);

                    SetClientUsername(e.Command.SenderIP, e.Command.SenderPort, username);
                    SetClientData(e.Command.SenderIP, e.Command.SenderPort, data);
                    SendUserStats(e.Command.SenderIP, e.Command.SenderPort, data);
                    AnswerUsernameRequest(e.Command.SenderIP, e.Command.SenderPort, nameAvailability);
                    e.Command.Data += (":" + data);
                    SendCommandToAll(e.Command);
                }
                else if (nameAvailability == false)
                {
                    AnswerUsernameRequest(e.Command.SenderIP, e.Command.SenderPort, nameAvailability);
                }
            }

            //User asks to disconnect
            if (e.Command.CommandType == CommandType.UserDisconnectRequest)
            {
                int    index         = FindClientID(e.Command.SenderIP, e.Command.SenderPort);
                string clientDetails = clientList[index].IP.ToString() + ":" + clientList[index].Port.ToString() + ":" + clientList[index].Username;
                Console.WriteLine("User {0}:{1} ({2}) has disconnected ({3}/{4})", e.Command.SenderIP, e.Command.SenderPort, clientList[index].Username, DateTime.Now.ToShortTimeString(), DateTime.Now.ToLongDateString());
                clientList[index].Disconnect();
                clientList.RemoveAt(index);
                Command cmd = new Command(CommandType.UserDisconnected, Command.BroadcastAddress);
                cmd.SenderName = e.Command.SenderName;
                cmd.SenderIP   = e.Command.SenderIP;
                cmd.SenderPort = e.Command.SenderPort;
                cmd.Data       = clientDetails;
                SendCommandToAll(cmd);
            }

            //Reply to client list request
            if (e.Command.CommandType == CommandType.ClientListRequest)
            {
                SendClientList(e.Command.SenderIP, e.Command.SenderPort);
            }

            //Sends message commands to all connected clients
            if (e.Command.CommandType == CommandType.Message)
            {
                if (e.Command.TargetIP.Equals(Command.BroadcastAddress))
                {
                    SendCommandToAll(e.Command);
                }
                else
                {
                    SendCommandToClient(e.Command);
                }
            }

            //Pass challenge request to challenged user
            if (e.Command.CommandType == CommandType.ChallengeRequest)
            {
                int challengerID = FindClientID(e.Command.SenderIP, e.Command.SenderPort);
                e.Command.Data = clientList[challengerID].Wins + ":" + clientList[challengerID].Losses;
                SendCommandToClient(e.Command);
            }

            //Pass challenge response to challenger
            if (e.Command.CommandType == CommandType.ChallengeResponse)
            {
                SendCommandToClient(e.Command);
            }

            //Handle game start request
            if (e.Command.CommandType == CommandType.GameStartRequest)
            {
                IPAddress       client2IP   = IPAddress.Parse(e.Command.Data.Split(':')[0]);
                int             client2Port = int.Parse(e.Command.Data.Split(':')[1]);
                int             client1ID   = FindClientID(e.Command.SenderIP, e.Command.SenderPort);
                int             client2ID   = FindClientID(client2IP, client2Port);
                BattleshipsGame game        = new BattleshipsGame(clientList[client1ID], clientList[client2ID]);

                Command cmd = new Command(CommandType.GameIDInform, e.Command.SenderIP, activeGames.Count.ToString());
                cmd.SenderName = "server";
                cmd.SenderIP   = serverIP;
                cmd.SenderPort = serverPort;
                cmd.TargetPort = e.Command.SenderPort;
                SendCommandToClient(cmd);

                cmd.TargetIP   = client2IP;
                cmd.TargetPort = client2Port;
                SendCommandToClient(cmd);

                activeGames.Add(game);
            }

            //Handle ShipPlacementRequest
            if (e.Command.CommandType == CommandType.GameShipRequest)
            {
                activeGames[int.Parse(e.Command.Data.Split(':')[0])].CommandRecieved(e);
            }
            //Handle GameShotRequest
            if (e.Command.CommandType == CommandType.GameShotRequest)
            {
                activeGames[int.Parse(e.Command.Data.Split(':')[0])].CommandRecieved(e);
            }
            //Handle GameOverInform
            if (e.Command.CommandType == CommandType.GameOverInform)
            {
                activeGames[int.Parse(e.Command.Data.Split(':')[0])].GameOverMessageCount++;
                if (activeGames[int.Parse(e.Command.Data.Split(':')[0])].GameOverMessageCount >= 2)
                {
                    PostGameStatisticsUpdate(activeGames[int.Parse(e.Command.Data.Split(':')[0])].Clients);
                    activeGames.RemoveAt(int.Parse(e.Command.Data.Split(':')[0]));
                    GC.Collect();
                }
            }

            //Handle user data request
            if (e.Command.CommandType == CommandType.UserDataRequest)
            {
                int     ID   = FindClientID(e.Command.TargetIP, e.Command.TargetPort);
                string  data = CheckForClientData(clientList[ID].Username);
                Command cmd  = new Command(CommandType.UserDataInform, e.Command.SenderIP, data);
                cmd.SenderIP   = e.Command.TargetIP;
                cmd.SenderPort = e.Command.TargetPort;
                cmd.SenderName = clientList[ID].Username; //Client username and IPEndpoint data is stored as command sender data
                cmd.TargetPort = e.Command.SenderPort;
                SendCommandToClient(cmd);
            }
        }