/// <summary>
        /// Determines whether the given username and password are valid to authenticate with.
        /// </summary>
        /// <param name="username">The username to challenge</param>
        /// <param name="password">The password to challenge</param>
        bool AreCredentialsValid(string username, string password)
        {
            // Get our context
            var context = new ServerContext();
            var account = context.Accounts.FirstOrDefault(x => x.Username.ToLower() == username.ToLower() && x.EditorAllowed);

            if (account == null)
                return false;

            return account.Password  == password;
        }
Example #2
0
        private List<EditorTemplateEntry> GetAllItems(NetConnection connection)
        {
            var entries = new List<EditorTemplateEntry>();
            var context = new ServerContext();

            foreach (var itemTemplate in context.ItemTemplates)
            {
                var locked = _contentLockManager.AnyoneHasLock(connection, itemTemplate.Id, ContentType.Item);
                entries.Add(new EditorTemplateEntry(itemTemplate.Id, itemTemplate.Name, itemTemplate.VirtualCategory,
                                                    ContentType.Item, locked));
            }
            return entries;
        }
Example #3
0
        static void Main(string[] args)
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();

            // Setup some stuff, because why not
            Console.Title = "Inspire Server";
            Console.WindowWidth = 100;

            //Logger.Instance.Log(Level.Info, "The lobby server has succesfully been started.");

            _editorService = new EditorService();

            // Add services
            _serviceContainer.RegisterService(_authenticationService);
            _serviceContainer.RegisterService(_editorService);
            _serviceContainer.RegisterService(_chatService);

            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            ClientNetworkManager.Instance.Update();

            // Create a map simulator for each map we need
            using (var context = new ServerContext())
                context.MapTemplates.ToList().ForEach(x => _serviceContainer.MapSimulators.Add(new MapSimulator(x)));

            stopwatch.Stop();
            Logger.Instance.Log(Level.Info, "Succesfully started game loop in " + stopwatch.Elapsed.Seconds + "s");

            var proxy = new MapPacketProxy(_serviceContainer);

            while (true)
            {

                ClientNetworkManager.Instance.Update();
                _serviceContainer.PerformUpdates();

                foreach (var simulator in _serviceContainer.MapSimulators)
                {
                    simulator.ServerServiceContainer.PerformUpdates();
                }

                Thread.Sleep(1);

            }

            Console.ReadLine();
        }
        private void ProcessLoginRequest(LoginRequestPacket obj)
        {
            var username = obj.Username;
            var password = obj.Password;

            if (AreCredentialsValid(username, password))
            {

                Logger.Instance.Log(Level.Info, username + " has succesfully authenticated");

                var packet = new LoginResultPacket(LoginResultPacket.LoginResult.Succesful);
                ClientNetworkManager.Instance.SendPacket(packet, obj.Sender);

                // Grab that account
                using (var context = new ServerContext())
                {
                    var account = context.Accounts.FirstOrDefault(x => x.Username.ToLower() == obj.Username);
                    var character = context.Characters.FirstOrDefault(x => x.AccountId == account.AccountId);

                    if(character == null)
                        throw new Exception("A character could not be found under this slot. This should never happen.");

                    // Introduce the entity into the simulation
                    var entity = EntityFactory.CreateCharacter(character, obj.Sender);
                    var mapSimulator = ((ServerServiceContainer) ServiceContainer).MapSimulators[character.MapId];
                    mapSimulator.AddEntity(entity);

                }

            }

            else
            {
                // Reject the user if they aren't able to authenticate

                var packet = new LoginResultPacket(LoginResultPacket.LoginResult.Failed);
                ClientNetworkManager.Instance.SendPacket(packet, obj.Sender);
            }
        }
Example #5
0
        private void Handler(ContentSaveRequestPacket contentSaveRequestPacket)
        {
            var result = RequestResult.Succesful;

            var locked = !_contentLockManager.HasLock(contentSaveRequestPacket.Sender, contentSaveRequestPacket.ContentObject.Id,
                                                contentSaveRequestPacket.ContentType);
            if (!locked)
            {
                try
                {

                    // Create our context and use it
                    using (var context = new ServerContext())
                    {
                        switch (contentSaveRequestPacket.ContentType)
                        {
                            case ContentType.Item:
                                var item =
                                    context.ItemTemplates.Attach(contentSaveRequestPacket.ContentObject as ItemTemplate);
                                context.Entry(item).State = EntityState.Modified;
                                context.SaveChanges();
                                break;
                            case ContentType.Skill:
                                var skill =
                                    context.SkillTemplates.Attach(
                                        contentSaveRequestPacket.ContentObject as SkillTemplate);
                                context.Entry(skill).State = EntityState.Modified;
                                context.SaveChanges();
                                break;
                            case ContentType.Map:
                                var map = context.MapTemplates.Attach(contentSaveRequestPacket.ContentObject as MapTemplate);
                                context.Entry(map).State = EntityState.Modified;
                                context.SaveChanges();
                                break;

                            default:
                                result = RequestResult.Failed;
                                return;
                        }
                    }

                }
                catch (Exception exception)
                {
                    // Log the error and eat it
                    Logger.Instance.Log(Level.Error,
                                        "The content of type " + contentSaveRequestPacket.ContentType +
                                        " failed to save: " + exception);
                    result = RequestResult.Failed;
                }
            }
            else
            {
                // If it's locked, it's considered a failure
                result = RequestResult.Failed;
            }

            // Send the result either way back to the client
            var packet = new ContentSaveResultPacket(result);
            ClientNetworkManager.Instance.SendPacket(packet, contentSaveRequestPacket.Sender);
        }
Example #6
0
 private object GetSkillByID(int id)
 {
     var context = new ServerContext();
     var skill = context.SkillTemplates.FirstOrDefault(x => x.Id == id);
     return skill;
 }
Example #7
0
 private object GetMapByID(int id)
 {
     var context = new ServerContext();
     var map = context.MapTemplates.FirstOrDefault(x => x.Id == id);
     return map;
 }
Example #8
0
 private object GetItemByID(int id)
 {
     var context = new ServerContext();
     var item = context.ItemTemplates.FirstOrDefault(x => x.Id == id);
     return item;
 }