public void applicationClose(Socket clientSocket) { User user = null; foreach (User u in Singleton.Singleton.Instance.ListOfUsersLogged) { if (u.Socket == clientSocket) { user = u; break; } } if (user != null) { MessagePacket packet = new MessagePacket(GameProtocol.UserDisconnected(), user.Username); foreach (User aux in Singleton.Singleton.Instance.ListOfUsersLogged) { if (aux == user) { continue; } Othello.Server.SendPacket(aux.Socket, packet.getData()); } Singleton.Singleton.Instance.ListOfUsersLogged.Remove(user); } Console.WriteLine("Client Disconnected!"); clientSocket.Shutdown(SocketShutdown.Both); clientSocket.Close(); }
public void backToLobby() { if (CurrentRoom.FirstUser != null && CurrentRoom.FirstUser.Username == Message) { User currentUser = Singleton.Singleton.Instance.GetUserLoogedByUsername(Message); String PacketMessage = ""; foreach (User u in Singleton.Singleton.Instance.ListOfUsersLogged) { if (u.Username == Message) { continue; } if (CurrentRoom.SecondUser != null && u.Username == CurrentRoom.SecondUser.Username) { MessagePacket mp = new MessagePacket(GameProtocol.BackToLobby(), Message); Othello.Server.SendPacket(u.Socket, mp.getData()); } PacketMessage += u.Username + ":" + u.IsChallenged + ":" + u.InGame + "|"; MessagePacket messagePacket = new MessagePacket(GameProtocol.ChangeUserToOnline(), Message); Othello.Server.SendPacket(u.Socket, messagePacket.getData()); } MessagePacket packet = new MessagePacket(GameProtocol.LoadMainSceneFromGame(), PacketMessage); Othello.Server.SendPacket(currentUser.Socket, packet.getData()); currentUser.InGame = false; CurrentRoom.FirstUser = null; if (CurrentRoom.SecondUser == null) { Singleton.Singleton.Instance.ListOfRooms.Remove(CurrentRoom); } } else { User currentUser = Singleton.Singleton.Instance.GetUserLoogedByUsername(Message); String PacketMessage = ""; foreach (User u in Singleton.Singleton.Instance.ListOfUsersLogged) { if (u.Username == Message) { continue; } if (CurrentRoom.FirstUser != null && u.Username == CurrentRoom.FirstUser.Username) { MessagePacket mp = new MessagePacket(GameProtocol.BackToLobby(), Message); Othello.Server.SendPacket(u.Socket, mp.getData()); } PacketMessage += u.Username + ":" + u.IsChallenged + ":" + u.InGame + "|"; MessagePacket messagePacket = new MessagePacket(GameProtocol.ChangeUserToOnline(), Message); Othello.Server.SendPacket(u.Socket, messagePacket.getData()); } MessagePacket packet = new MessagePacket(GameProtocol.LoadMainSceneFromGame(), PacketMessage); Othello.Server.SendPacket(currentUser.Socket, packet.getData()); currentUser.InGame = false; CurrentRoom.SecondUser = null; if (CurrentRoom.FirstUser == null) { Singleton.Singleton.Instance.ListOfRooms.Remove(CurrentRoom); } } }
public void notifyUsersWithInGameStatus(User User) { MessagePacket packet = new MessagePacket(GameProtocol.ChangeUserToInGame(), User.Username); foreach (User user in Singleton.Singleton.Instance.ListOfUsersLogged) { if (user == User) { continue; } Othello.Server.SendPacket(user.Socket, packet.getData()); } }
public void notifyUsersAfterChallenge(User challengedUser) { MessagePacket packet = new MessagePacket(GameProtocol.ChangeUserToChallenged(), challengedUser.Username); foreach (User user in Singleton.Singleton.Instance.ListOfUsersLogged) { if (user == challengedUser) { continue; } Othello.Server.SendPacket(user.Socket, packet.getData()); } }
public void opponentQuitWhileInGame() { if (CurrentRoom.FirstUser != null && CurrentRoom.FirstUser.Username == Message) { Singleton.Singleton.Instance.ListOfUsersLogged.Remove(CurrentRoom.FirstUser); CurrentRoom.SendGameOver(CurrentRoom.SecondUser, "You won!\n Your opponent got disconnected."); foreach (User user in Singleton.Singleton.Instance.ListOfUsersLogged) { if (user.Username == CurrentRoom.SecondUser.Username) { MessagePacket pack = new MessagePacket(GameProtocol.OpponentQuitWhileInGame(), CurrentRoom.FirstUser.Username); Othello.Server.SendPacket(user.Socket, pack.getData()); continue; } MessagePacket p = new MessagePacket(GameProtocol.UserDisconnected(), CurrentRoom.FirstUser.Username); Othello.Server.SendPacket(user.Socket, p.getData()); } CurrentRoom.FirstUser = null; } else if (CurrentRoom.SecondUser != null && CurrentRoom.SecondUser.Username == Message) { CurrentRoom.SendGameOver(CurrentRoom.FirstUser, "You won!\n Your opponent got disconnected."); Singleton.Singleton.Instance.ListOfUsersLogged.Remove(CurrentRoom.SecondUser); foreach (User user in Singleton.Singleton.Instance.ListOfUsersLogged) { if (user.Username == CurrentRoom.FirstUser.Username) { MessagePacket pack = new MessagePacket(GameProtocol.OpponentQuitWhileInGame(), CurrentRoom.SecondUser.Username); Othello.Server.SendPacket(user.Socket, pack.getData()); continue; } MessagePacket p = new MessagePacket(GameProtocol.UserDisconnected(), CurrentRoom.SecondUser.Username); Othello.Server.SendPacket(user.Socket, p.getData()); } CurrentRoom.SecondUser = null; } if (CurrentRoom.FirstUser == null && CurrentRoom.SecondUser == null) { Singleton.Singleton.Instance.ListOfRooms.Remove(CurrentRoom); } }
public void doLogin(Socket clientSocket) { string[] fields = Message.Split('|'); string username = fields[0]; string password = fields[1]; User user = Singleton.Singleton.Instance.DatabaseConnection.isPasswordRight(username, password); if (user != null) { if (Singleton.Singleton.Instance.isUserLogged(username)) { BasicPacket bp = new BasicPacket(GameProtocol.AlreadyOnlinePacketID()); Othello.Server.SendPacket(clientSocket, bp.getData()); } else { user.InGame = false; user.Socket = clientSocket; Singleton.Singleton.Instance.ListOfUsersLogged.Add(user); //Send to current user logged the list with all the users logged and the status of them string PacketMessage = ""; foreach (User u in Singleton.Singleton.Instance.ListOfUsersLogged) { if (u.Username == username) { continue; } PacketMessage += u.Username + ":" + u.IsChallenged + ":" + u.InGame + "|"; MessagePacket messagePacket = new MessagePacket(GameProtocol.AlertUsersNewUserLoggedID(), username + ":False"); Othello.Server.SendPacket(u.Socket, messagePacket.getData()); } MessagePacket packet = new MessagePacket(GameProtocol.UsersLoggedListPacketID(), PacketMessage); Othello.Server.SendPacket(user.Socket, packet.getData()); } } else { BasicPacket bp = new BasicPacket(GameProtocol.FailedLoginPacketID()); Othello.Server.SendPacket(clientSocket, bp.getData()); } }
public void notifyUsersAfterChallengeRefuse() { string[] splits = Message.Split(':'); MessagePacket mp = new MessagePacket(GameProtocol.ChangeUserToOnline(), splits[0]); foreach (User u in Singleton.Singleton.Instance.ListOfUsersLogged) { if (u.Username == splits[0]) { u.IsChallenged = false; } else if (u.Username == splits[1]) { Othello.Server.SendPacket(u.Socket, new MessagePacket(GameProtocol.UserRefusedChallengePacketID(), splits[0]).getData()); } else { Othello.Server.SendPacket(u.Socket, mp.getData()); } } }
public void opponentQuitAfterEndGame() { if (CurrentRoom.FirstUser != null && CurrentRoom.FirstUser.Username == Message) { Console.WriteLine("Client " + Message + " Disconnected!"); CurrentRoom.FirstUser.Socket.Shutdown(SocketShutdown.Both); CurrentRoom.FirstUser.Socket.Close(); Singleton.Singleton.Instance.ListOfUsersLogged.Remove(CurrentRoom.FirstUser); if (CurrentRoom.FirstUser == null && CurrentRoom.SecondUser == null) { Singleton.Singleton.Instance.ListOfRooms.Remove(CurrentRoom); } else { MessagePacket mp = new MessagePacket(GameProtocol.OpponentQuitAfterEndGame(), Message); Othello.Server.SendPacket(CurrentRoom.SecondUser.Socket, mp.getData()); foreach (User user in Singleton.Singleton.Instance.ListOfUsersLogged) { if (user.Username == CurrentRoom.SecondUser.Username) { continue; } MessagePacket p = new MessagePacket(GameProtocol.UserDisconnected(), CurrentRoom.FirstUser.Username); Othello.Server.SendPacket(user.Socket, p.getData()); } CurrentRoom.FirstUser = null; } } else if (CurrentRoom.SecondUser != null && CurrentRoom.SecondUser.Username == Message) { Console.WriteLine("Client " + Message + " Disconnected!"); CurrentRoom.SecondUser.Socket.Shutdown(SocketShutdown.Both); CurrentRoom.SecondUser.Socket.Close(); Singleton.Singleton.Instance.ListOfUsersLogged.Remove(CurrentRoom.SecondUser); if (CurrentRoom.FirstUser == null && CurrentRoom.SecondUser == null) { Singleton.Singleton.Instance.ListOfRooms.Remove(CurrentRoom); } else { MessagePacket mp = new MessagePacket(GameProtocol.OpponentQuitAfterEndGame(), Message); Othello.Server.SendPacket(CurrentRoom.FirstUser.Socket, mp.getData()); foreach (User user in Singleton.Singleton.Instance.ListOfUsersLogged) { if (user.Username == CurrentRoom.FirstUser.Username) { continue; } MessagePacket p = new MessagePacket(GameProtocol.UserDisconnected(), CurrentRoom.SecondUser.Username); Othello.Server.SendPacket(user.Socket, p.getData()); } CurrentRoom.SecondUser = null; } } if (CurrentRoom.FirstUser == null && CurrentRoom.SecondUser == null) { Singleton.Singleton.Instance.ListOfRooms.Remove(CurrentRoom); } }
public void sendTheChallenge(User challengedUser, string message) { MessagePacket packet = new MessagePacket(GameProtocol.ChallengePacketID(), message); Othello.Server.SendPacket(challengedUser.Socket, packet.getData()); }