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); } }
public CompetitionHandler(Socket s1, Socket s2, string usName1, string usName2, ServerClass server1) { userSocket = s1; userName = usName1; rivalSocket = s2; rivalUserName = usName2; dbAgent = new DataBaseClass(); serverAgent = server1; }
static void Main(string[] args) { FindingRivalHandle findRivalHAgent = new FindingRivalHandle(); ServerClass sv = new ServerClass(findRivalHAgent); Thread serverThread = new Thread(new ThreadStart(sv.SelectLoop)); Thread competitionThread = new Thread(new ThreadStart(findRivalHAgent.FindRival)); competitionThread.Start(); serverThread.Start(); }
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 bool AddToRivalList(Socket s, string userName, string competitionType, ServerClass s1)//הפעולה של הסרבר { try { Client c = new Client(); c.socket = s; c.userName = userName; c.serverAgent = s1; if (competitionType == "Time") { c.CompetitionType = 0; } else { c.CompetitionType = 1; } rivalList.Add(c); return(true); } catch (Exception e) { Console.WriteLine(e.ToString()); return(false); } }
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; } } }