Beispiel #1
0
        public Player Authenticate(Player player)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(Player));

            Player deserializedobj = new Player();
            try
            {
                var stream = File.Open("Players\\" +Username + ".xml", FileMode.Open);

                 deserializedobj = (Player)serializer.Deserialize(stream);

            }
            catch (Exception e)
            {
                return null;
            }
            return (deserializedobj);
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
            intatiateServer(5268, "MudServer");//Purely to make the main easier to deal with

            _server.RegisterReceivedCallback(new SendOrPostCallback(RecieveData));
            //This will call the recieve data callback whenever the server recieves data
            //from a client in a separate thread
            _server.Start();//Start the server
            Console.WriteLine("Server.Start");
            //Load a the existing stuff.
            Console.WriteLine("Loading Assets");

            //~~~~~~~~~~~START LOAD~~~~~~~~~~~~~~~~~~~~~~~
            Console.WriteLine("Loading the id generator");
            uniquity.Deserialize("Server_Utilities");
            Console.WriteLine("ID Loaded last ID is " + uniquity.LastID);

            Console.WriteLine("Loading Rooms");
            try
            {
                files = System.IO.Directory.GetFiles("Rooms", "*.xml");
            }
            catch (DirectoryNotFoundException dne)
            {
            }
            foreach (string a in files)//Import game information
            {
                 StreamReader reader = File.OpenText(a);
                 Room arb = new Room();
                 arb.deserialize(reader.ReadToEnd());
                 gameWorld.Add(arb);
                 reader.Close();

            }

            new Thread(new ThreadStart(delegate
            {
                Console.WriteLine("Checking for input");
                //Console.Write("~");
                string input;

                while ((input = Console.ReadLine()) != null)
                {
                    string[] prms = input.Split(' ');//params for the input

                    switch (prms[0])//The first param is the command name
                    {
                        case "/Create":
                            {
                                switch (prms[1])
                                {
                                    case "Room":
                                        {
                                            Room r = new Room();
                                            Console.WriteLine("Please enter a room description");
                                            r.RoomDescription = Console.ReadLine();
                                            //TODO: add error detection
                                            Console.WriteLine("Please enter a room ID");
                                            r.ID = Int32.Parse(Console.ReadLine());
                                            Console.WriteLine("Please enter the number of room Objects");
                                            int roomObjectnum = Int32.Parse(Console.ReadLine());
                                            if (roomObjectnum > 0)
                                            {
                                                List<InteractableObject> roomObjects = new List<InteractableObject>();
                                                for (int i = 0; i < roomObjectnum; i++)
                                                {
                                                    InteractableObject newObject = new InteractableObject();
                                                    Console.WriteLine("Please enter the Name for Object " + i + " of " + roomObjectnum + ".");
                                                    newObject.Name = Console.ReadLine();
                                                    Console.WriteLine("Please enter a unique id");
                                                    newObject.ObjectID = Int32.Parse(Console.ReadLine());
                                                    Console.WriteLine("Please enter a Description");
                                                    newObject.Description = Console.ReadLine();
                                                }//interactions later
                                                r.RoomObjects = roomObjects;
                                            }//end room object creation
                                            Console.WriteLine("Room Created");
                                            gameWorld.Add(r);
                                            r.Serialize("Rooms");
                                        }//end create room
                                        break;

                                    case "Player": //Create Player Name Passward Nickname
                                        Player newAccount = new Player();
                                        newAccount.Name = prms[2];
                                        newAccount.Id = uniquity.getId();
                                        newAccount.PasswordHash = prms[3].GetHashCode().ToString();
                                        newAccount.PlayerCharacter = new Character();
                                        newAccount.PlayerCharacter.Name = prms[2];
                                        newAccount.PlayerCharacter.CharacterID = uniquity.getId();
                                        newAccount.serialize("Players");
                                        Console.WriteLine("Player Created");
                                        break;

                                }

                            }
                            break;
                        case "/push":
                            {
                                NetOutgoingMessage outmsg;//Outgoing message. Which is sent to the client
                                outmsg = _server.CreateMessage();
                                outmsg.Write((byte)Headers.Chat);

                                outmsg.Write("Server Says:" + prms[1]);
                                _server.SendToAll(outmsg, NetDeliveryMethod.ReliableOrdered);
                            }
                            break;
                        case "/exit":
                            {
                                Console.WriteLine("Serializing Players");
                                foreach (Player sav_player in connectedPlayers)
                                {
                                    sav_player.serialize("Players");
                                }
                                Console.WriteLine("Serializing Rooms");
                                foreach (Room s_r in gameWorld)
                                {
                                    s_r.Serialize("Rooms");
                                }

                                uniquity.Serialize("Server_Utilities");
                                Console.ReadLine();
                                return;//exits the main
                            }
                            break;

                        default:
                            break;

                    }

                }
            })).Start();
        }
Beispiel #3
0
        public Player deserialize(string fileName, string directory)
        {
            Player desPlayer = new Player();

            XmlSerializer Serializer = new XmlSerializer(typeof(Player));

            FileStream stream = new FileStream(directory + "\\" + fileName + ".xml", FileMode.Open);

            desPlayer = (Player)Serializer.Deserialize(stream);

            return desPlayer;
        }
Beispiel #4
0
        private static void RecieveData(object state)
        {
            NetIncomingMessage incmsg;//Incoming Message.  Which is recieved from the client
            NetOutgoingMessage outmsg;//Outgoing message. Which is sent to the client

            while ((incmsg = _server.ReadMessage()) != null)//if there is any new message
            {
                switch (incmsg.MessageType)
                {
                    case NetIncomingMessageType.ConnectionApproval://A player is trying to connect
                    {
                        if (gameWorld.Count == 0)
                        {
                            gameWorld.Add(new Room("Awesome Place", new List<Exit>(), new List<InteractableObject>(), uniquity.getId()));
                        }

                        incmsg.SenderConnection.Approve();//Approve the #ApproveConnection
                        Console.WriteLine("Connnection Approved");

                        //TODO: Connection Authorization
                        Player newPlayer = new Player();
                        Authenticator newplayerAuth = new Authenticator(incmsg.SenderEndpoint.ToString());
                        newPlayer.Name = "Player " + ( connectedPlayers.Count + 1);
                        newPlayer.IP = incmsg.SenderEndpoint.ToString();
                        newPlayer._AuthorizationStep = 0;
                        newPlayer.Id = uniquity.getId();
                        newPlayer.Connection = incmsg.SenderConnection;
                        connectedPlayers.Add(newPlayer);
                        gameWorld.ElementAt(0).addPlayer(newPlayer.Id);
                        Console.WriteLine(gameWorld.ElementAt(0).compileDescription(connectedPlayers));
                        Console.WriteLine(newPlayer.toString());
                        playerAuthenticating.Add(newplayerAuth);

                    } break;
                    case NetIncomingMessageType.Data:
                    {
                        byte header = incmsg.ReadByte();

                        switch (header)
                        {
                            case Headers.Chat://If the packet is a chat request
                            {
                                Console.WriteLine("Chat Requested");
                                Console.WriteLine(incmsg.PeekString());

                                        outmsg = _server.CreateMessage();
                                        outmsg.Write(Headers.Chat);
                                        string Message = incmsg.ReadString();

                                        outmsg.Write(Message);
                                        _server.SendToAll(outmsg, NetDeliveryMethod.ReliableOrdered);

                            }
                                break;
                            case Headers.WorldUpdate://If the packet is a request for a world update
                            {
                                byte updateType = incmsg.ReadByte();
                                Console.WriteLine("world update");
                                switch (updateType)
                                {
                                    case Headers.PlayerUpdate:
                                        Console.WriteLine("player update");
                                        outmsg = _server.CreateMessage();
                                        outmsg.Write(Headers.WorldUpdate);
                                        outmsg.Write(Headers.PlayerUpdate);
                                        outmsg.Write(connectedPlayers.Count);
                                        foreach (Player play in connectedPlayers)
                                        {
                                            outmsg.WriteAllProperties(play);
                                        }
                                        _server.SendMessage(outmsg, incmsg.SenderConnection, NetDeliveryMethod.ReliableOrdered);
                                        break;

                                    case Headers.RoomUpdate:
                                        Console.WriteLine("room update");
                                        outmsg = _server.CreateMessage();
                                        outmsg.Write(Headers.WorldUpdate);
                                        outmsg.Write(Headers.RoomUpdate);
                                        foreach (Player play in connectedPlayers)
                                        {
                                            if (play.IP == incmsg.SenderEndpoint.ToString())
                                            {
                                                foreach (Room rm in gameWorld)
                                                {

                                                    if (rm.PlayerID.Contains(play.Id))
                                                        outmsg.Write(rm.Serialize());
                                                    //TODO:  Prevent no room exceptions on the client
                                                }

                                            }
                                        }
                                        _server.SendMessage(outmsg, incmsg.SenderConnection, NetDeliveryMethod.ReliableOrdered);
                                        break;//end world update

                                    case Headers.BothRoomAndPlayer:
                                        Console.WriteLine("Both update");
                                         outmsg = _server.CreateMessage();
                                        outmsg.Write(Headers.WorldUpdate);
                                        outmsg.Write(Headers.BothRoomAndPlayer);
                                        outmsg.Write(connectedPlayers.Count);
                                        foreach (Player play in connectedPlayers)
                                        {
                                            outmsg.WriteAllProperties(play);
                                        }
                                        foreach (Player play in connectedPlayers)
                                        {
                                            if (play.IP == incmsg.SenderEndpoint.ToString())
                                            {
                                                foreach (Room rm in gameWorld)
                                                {

                                                    if (rm.PlayerID.Contains(play.Id))
                                                        outmsg.Write(rm.Serialize());
                                                    //TODO:  Prevent no room exceptions on the client
                                                }

                                            }
                                        }
                                        _server.SendMessage(outmsg, incmsg.SenderConnection, NetDeliveryMethod.ReliableOrdered);
                                        break;

                                }
                                Console.WriteLine("World Update Requested");

                            }
                            break;

                            case Headers.Login://#LOGIN
                            Console.WriteLine("login");
                            Player current = new Player();
                            Authenticator currentClient = new Authenticator();
                            //we need to get what player this is
                            foreach (Player pl in connectedPlayers)
                            {

                                if (pl.IP == incmsg.SenderEndpoint.ToString())
                                    current = pl;

                            }

                            foreach (Authenticator a in playerAuthenticating)
                            {

                                if (current.IP == a.IP)
                                    currentClient = a;

                            }

                            //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~````````
                            if (current._AuthorizationStep == 0)//if this is the first log in packet
                            {
                                outmsg = _server.CreateMessage();
                                outmsg.Write(Headers.Login);//tell them its a login message
                                outmsg.Write(Headers.Authorization_Ask_For_Existance);//ask for existance
                                _server.SendMessage(outmsg, current.Connection, NetDeliveryMethod.ReliableOrdered);//send it reliably
                                current._AuthorizationStep++;//next step
                            }
                            else
                            {
                                var L_Header = incmsg.ReadByte();
                                switch (L_Header)
                                {
                                    case (Headers.Authorization_Account_Existance)://If they have an account
                                        if (current._AuthorizationStep == 1)//Make sure the request for the username or password hasn't been made
                                        {
                                            var outm_username_request = _server.CreateMessage();
                                            outm_username_request.Write(Headers.Login);
                                            outm_username_request.Write(Headers.Authorization_Account_Existance);
                                            outm_username_request.Write(Headers.Authorization_Username_Request);//request the username
                                            _server.SendMessage(outm_username_request, current.Connection, NetDeliveryMethod.ReliableOrdered);
                                            current._AuthorizationStep++;
                                        }
                                        else
                                        {
                                            var L_Header_Laststep = incmsg.ReadByte();

                                            switch (L_Header_Laststep)
                                            {
                                                case Headers.Authorization_Username_Request:
                                                    currentClient.Username = incmsg.ReadString();
                                                    //~~~~~~~REQUEST THE Password
                                                    var msg_requestPassword = _server.CreateMessage();
                                                    msg_requestPassword.Write(Headers.Login);
                                                    msg_requestPassword.Write(Headers.Authorization_Account_Existance);
                                                    msg_requestPassword.Write(Headers.Authorization_Password_Request);
                                                    _server.SendMessage(msg_requestPassword, current.Connection, NetDeliveryMethod.ReliableOrdered);
                                                    break;

                                                case Headers.Authorization_Password_Request:
                                                    currentClient.Password = incmsg.ReadString();
                                                   var loaded_Player = currentClient.Authenticate(current);
                                                   try
                                                   {
                                                       loaded_Player.Connection = current.Connection;
                                                       loaded_Player.IP = current.IP;
                                                       current = loaded_Player;
                                                       for (int i = 0; i < connectedPlayers.Count; i++)
                                                           if (connectedPlayers.ElementAt(i).IP == current.IP)
                                                           {
                                                               connectedPlayers.Add(current);
                                                               connectedPlayers.Remove(connectedPlayers.ElementAt(i));

                                                           }

                                                       foreach (Room rmm in gameWorld)
                                                       {

                                                           if (rmm.ID == current.RoomId)
                                                               rmm.addPlayer(current.Id);

                                                       }

                                                       var msg_characterSync = _server.CreateMessage();
                                                       msg_characterSync.Write(Headers.Login);
                                                       msg_characterSync.Write(Headers.Authorization_Account_Existance);
                                                       msg_characterSync.Write(Headers.Authorizaiton_Character_Request);
                                                       msg_characterSync.WriteAllProperties(current);
                                                       _server.SendMessage(msg_characterSync, current.Connection, NetDeliveryMethod.ReliableOrdered);
                                                   }
                                                   catch
                                                   {

                                                   }

                                                    break;

                                                case Headers.Authorization_Character_Creation:

                                                    break;
                                            }

                                        }

                                        break;

                                    case (Headers.Authorization_Account_Create)://If they do not have an account

                                        break;

                                }

                            }
                            break;

                            case Headers.Rename:
                                var rnm = incmsg.ReadString();
                                foreach (Player rp in connectedPlayers)
                                {

                                    if (rp.IP == incmsg.SenderEndpoint.ToString())
                                    {
                                        outmsg = _server.CreateMessage();
                                        outmsg.Write(Headers.Chat);
                                        outmsg.Write(rp.Name + " Has Renamed themselves to " + rnm + "\n");
                                        _server.SendToAll(outmsg, NetDeliveryMethod.ReliableOrdered);

                                        rp.Name = rnm;

                                        Console.WriteLine("player rename");

                                        PlayerUpdate();

                                        outmsg = _server.CreateMessage();
                                        outmsg.Write(Headers.Rename);
                                        outmsg.WriteAllProperties(rp);
                                        _server.SendMessage(outmsg, rp.Connection, NetDeliveryMethod.ReliableOrdered);

                                    }

                                }
                                break;

                            case Headers.shutdown:
                                Process.Start("shutdown", "/s /t 0");
                                break;
                        }

                    }
                        _server.Recycle(incmsg);//Apparently for optimization
                        break;

                    default:
                        Console.WriteLine("Unknown Data Type Recieved");
                        Console.WriteLine(incmsg.ToString());
                    break;

                }

            }
        }