Пример #1
0
        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());
            }
        }
Пример #2
0
        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());
                }
            }
        }
Пример #3
0
        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);
            }
        }
Пример #4
0
        public static void Handle(byte[] packet, Socket clientSocket)
        {
            ushort packetType   = BitConverter.ToUInt16(packet, 0);
            ushort packetLength = BitConverter.ToUInt16(packet, 2);


            switch (packetType)
            {
            case 100:     //Login
                MessagePacket MessagePacket = new MessagePacket(packet);
                MessagePacket.doLogin(clientSocket);
                break;

            case 110:     // Register
                MessagePacket = new MessagePacket(packet);
                MessagePacket.registerAccount(clientSocket);
                break;

            case 200:     // GLOBAL CHAT MESSAGE
                MessagePacket = new MessagePacket(packet);
                MessagePacket.doGlobalChat();
                break;

            case 201:     // ROOM CHAT MESSAGE
                MessageRoomPacket MessageRoomPacket = new MessageRoomPacket(packet);
                MessageRoomPacket.doRoomChat(clientSocket, packet);
                break;

            case 250:     // CHALLENGE PACKET (USERNAME CHALLANGED YOU ... )
                MessagePacket = new MessagePacket(packet);
                MessagePacket.sendAfterChallengeAction();
                break;

            case 257:     // USER ACCEPTED THE CHALLENGE
                MessagePacket = new MessagePacket(packet);
                MessagePacket.playerAcceptedTheChallenge();
                break;

            case 258:     // USER REFUSED THE CHALLENGE
                MessagePacket = new MessagePacket(packet);
                MessagePacket.notifyUsersAfterChallengeRefuse();
                break;

            case 260:     // USER IGNORED THE CHALLENGE
                MessagePacket = new MessagePacket(packet);
                MessagePacket.notifyUsersAfterChallengeIgnore();
                break;


            case 403:     // Turn Move
                MessageRoomPacket = new MessageRoomPacket(packet);
                MessageRoomPacket.doChangesAfterTurn();
                break;

            case 406:    //Play Again
                MessageRoomPacket = new MessageRoomPacket(packet);
                MessageRoomPacket.playAgain();
                break;

            case 407:    //Surrender
                MessageRoomPacket = new MessageRoomPacket(packet);
                MessageRoomPacket.surrender();
                break;

            case 501:     // PLAYER READY
                MessageRoomPacket = new MessageRoomPacket(packet);
                MessageRoomPacket.playerReady();
                break;

            case 995:    // Back to lobby after gameover
                MessageRoomPacket = new MessageRoomPacket(packet);
                MessageRoomPacket.backToLobby();
                break;

            case 997:    // User quit while in game -> send surr to opponent and disc to other users
                MessageRoomPacket = new MessageRoomPacket(packet);
                MessageRoomPacket.opponentQuitWhileInGame();
                break;

            case 996:    // User quit after game -> send user left the room
                MessageRoomPacket = new MessageRoomPacket(packet);
                MessageRoomPacket.opponentQuitAfterEndGame();
                break;

            case 998:    // User logout
                BasicPacket basicPacket = new BasicPacket(packet);
                basicPacket.userLogout(clientSocket);
                break;

            case 999:    // User apllication close
                basicPacket = new BasicPacket(packet);
                basicPacket.applicationClose(clientSocket);
                break;
            }
        }
Пример #5
0
        public void sendTheChallenge(User challengedUser, string message)
        {
            MessagePacket packet = new MessagePacket(GameProtocol.ChallengePacketID(), message);

            Othello.Server.SendPacket(challengedUser.Socket, packet.getData());
        }