Esempio n. 1
0
        public void CalculateWinner()
        {
            if (isCompete)
            {
                if (Math.Round(distance1) != Math.Round(distance2))
                {
                    double max = Math.Max(distance1, distance2);
                    double min = Math.Min(distance1, distance2);
                    Console.WriteLine("Max:" + max + "\nMin:" + min);
                    if (max == distance1)
                    {
                        int points = (int)distance1 - (int)distance2;
                        if (points < 20)
                        {
                            points = 20;
                        }
                        if (dbAgent.AddPoints(userName, points) && dbAgent.AddPoints(rivalUserName, 5))
                        {
                            string xmlAnswer = "<Message><Winner>" + userName + "</Winner><Distance>" + distance1 + "</Distance><LoserDistance>" + distance2 + "</LoserDistance></Message>\n";
                            byte[] bAnswer   = System.Text.Encoding.ASCII.GetBytes(xmlAnswer);
                            rivalSocket.Send(bAnswer);
                            userSocket.Send(bAnswer);
                        }
                    }
                    if (max == distance2)
                    {
                        int points = (int)distance2 - (int)distance1;
                        if (points < 20)
                        {
                            points = 20;
                        }
                        if (dbAgent.AddPoints(rivalUserName, points) && dbAgent.AddPoints(userName, 5))
                        {
                            string xmlAnswer = "<Message><Winner>" + rivalUserName + "</Winner><Distance>" + distance2 + "</Distance><LoserDistance>" + distance1 + "</LoserDistance></Message>\n";
                            byte[] bAnswer   = System.Text.Encoding.ASCII.GetBytes(xmlAnswer);
                            rivalSocket.Send(bAnswer);
                            userSocket.Send(bAnswer);
                        }
                    }
                }
                else
                {
                    string xmlAnswer = "<Message><Winner>Tie</Winner></Message>\n";
                    byte[] bAnswer   = System.Text.Encoding.ASCII.GetBytes(xmlAnswer);
                    rivalSocket.Send(bAnswer);
                    userSocket.Send(bAnswer);
                }

                ServerClass.AddingSocketToArryList(userSocket);
                ServerClass.AddingSocketToArryList(rivalSocket);
                IPEndPoint remoteEP = new IPEndPoint(IPAddress.Parse(ServerClass.serverIp), 1323);

                // Create a TCP/IP  socket.
                Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                sender.Connect(remoteEP);
            }
        }
Esempio n. 2
0
        public void SuddnEnd(Socket s)
        {
            string xmlAnswer = "<Message><Type>TheUserQuit</Type></Message>\n";

            byte[] bAnswer = System.Text.Encoding.ASCII.GetBytes(xmlAnswer);
            s.Send(bAnswer);

            ServerClass.AddingSocketToArryList(userSocket);
            ServerClass.AddingSocketToArryList(rivalSocket);
            IPEndPoint remoteEP = new IPEndPoint(IPAddress.Parse(ServerClass.serverIp), 1323);

            // Create a TCP/IP  socket.
            Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            sender.Connect(remoteEP);
        }
        public void FindRival()
        {
            while (true)
            {
                for (int i = 0; i < rivalList.Count; i++)
                {
                    Client clientTemp = rivalList[i];
                    if (clientTemp.timer < 5)
                    {
                        if (clientTemp.timer == 1)
                        {
                            ServerClass.RemoveSocket(clientTemp.socket);
                        }

                        try
                        {
                            if (RivalSearcher[clientTemp.CompetitionType][dbAgent.GetLevel(clientTemp.userName)] == null)
                            {
                                if (RivalSearcher[clientTemp.CompetitionType].Count > 2)
                                {
                                    if (RivalSearcher[clientTemp.CompetitionType][dbAgent.GetLevel(clientTemp.userName) + 1] == null)
                                    {
                                        if (i > 0)
                                        {
                                            if (RivalSearcher[clientTemp.CompetitionType][dbAgent.GetLevel(clientTemp.userName) - 1] == null)
                                            {
                                                RivalSearcher[clientTemp.CompetitionType][dbAgent.GetLevel(clientTemp.userName)] = clientTemp;
                                            }

                                            else
                                            {
                                                Client x = (Client)RivalSearcher[clientTemp.CompetitionType][dbAgent.GetLevel(clientTemp.userName)];
                                                if (clientTemp.socket != x.socket)
                                                {
                                                    Found(clientTemp, x, dbAgent.GetLevel(clientTemp.userName));
                                                }
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    RivalSearcher[clientTemp.CompetitionType][dbAgent.GetLevel(clientTemp.userName)] = clientTemp;
                                }
                            }
                            else
                            {
                                Found(clientTemp, (Client)RivalSearcher[clientTemp.CompetitionType][dbAgent.GetLevel(clientTemp.userName)], dbAgent.GetLevel(clientTemp.userName));
                            }
                        }
                        catch (Exception e) { }
                    }
                    else
                    {
                        string answer  = "<Message><Type>AnswerNotFound</Type></Message>";
                        byte[] bAnswer = System.Text.Encoding.ASCII.GetBytes(answer);
                        clientTemp.socket.Send(bAnswer);

                        Erase(clientTemp, dbAgent.GetLevel(clientTemp.userName));
                        ServerClass.AddingSocketToArryList(clientTemp.socket);
                    }
                    clientTemp.timer = clientTemp.timer + 1;
                }
            }
        }