public static Npc CreateNpc(long id)
        {
            using (var context = new GameDatabaseContext())
            {
                var template = context.Npcs.FirstOrDefault(x => x.Id == id);

                if (template == null)
                    throw new Exception("Creating an NPC with the given Id is invalid.");

                // Load up the quests this NPC will have
                context.Entry(template).Collection(x => x.Quests).Load();
                context.Entry(template).Reference(x => x.ConversationAvailableTemplate).Load();

                foreach (var quest in template.Quests)
                {

                    context.Entry(quest).Collection(x => x.QuestSteps).Load();
                    context.Entry(quest).Collection(x => x.Rewards).Load();

                    // Load up our set of requirements from the table
                    foreach (var x in quest.QuestSteps)
                        context.Entry(x).Collection(a => a.Requirements).Load();

                }

                var npc = new Npc(template);

                return npc;

            }
        }
        public static void OnCharacterCreate(GameClient client, ClientHeroCreatePacket packet)
        {
            Logger.Instance.Info("Attempting to create hero with name {0}", packet.Name);
            bool heroExists;

            using (var context = new GameDatabaseContext())
            {
                // We try to load a hero with a similar name to see if they exist
                heroExists = context.Characters.Any(x => x.Name.ToUpper() == packet.Name.ToUpper());
            }

            if (heroExists)
            {
                client.Send(new ServerHeroCreateResponsePacket(HeroStatus.Invalid));
                return;
            }

            // Create a hero and attach it if it dosen't exist yet

            var hero = new UserHero(client.Account, 0, 0, 0, packet.Name);

            // Save our hero into the database
            using (var context = new GameDatabaseContext())
            {
                context.Accounts.Attach(client.Account);
                context.Entry(client.Account).Collection(a => a.Heroes).Load();
                client.Account.Heroes.Add(hero);
                context.SaveChanges();
            }

            client.Send(new ServerHeroCreateResponsePacket(HeroStatus.OK));

            SendHeroList(client);
        }
Example #3
0
        public IHttpActionResult DeleteItem(int id)
        {
            using (var context = new GameDatabaseContext())
            {
                var item = context.ItemTemplates.FirstOrDefault(x => x.Id == id);

                if (item != null)
                {
                    var entry = context.Entry(item);
                    entry.State = EntityState.Deleted;
                    context.SaveChanges();
                    return Ok();
                }

                return NotFound();
            }
        }
Example #4
0
        /// <summary>
        /// Retrieves a quest that has been created
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public QuestTemplate GetQuest(long id)
        {
            using (var context = new GameDatabaseContext())
            {

                var quest = context.Quests.First(x => x.Id == id);

                context.Entry(quest).Collection(x => x.Rewards).Load();
                context.Entry(quest).Collection(x => x.QuestSteps).Load();

                // Load up our set of requirements from the table
                foreach (var x in quest.QuestSteps)
                    context.Entry(x).Collection(a => a.Requirements).Load();

                return quest;
            }
        }
Example #5
0
        protected override void Save()
        {
            // (:: It might be a good idea to move some of the NPC update logic into the repo, but for now this works)
            using (var db = new GameDatabaseContext())
            {
                var ContentTemplate = this.ContentTemplate as NpcTemplate;

                var originalJob = db.Npcs.Include(j => j.ConversationAvailableTemplate).Include(x => x.Quests)
                    .Single(j => j.Id == ContentTemplate.Id);

                // Update scalar/complex properties
                db.Entry(originalJob).CurrentValues.SetValues(ContentTemplate);

                if (ContentTemplate.ConversationAvailableTemplate != null)
                {
                    var originalDialog =
                    db.DialogTemplates.Single(x => x.Id == ContentTemplate.ConversationAvailableTemplate.Id);
                    originalJob.ConversationAvailableTemplate = originalDialog;
                }

                if (ContentTemplate.Quests != null)
                {

                    List<QuestTemplate> templates = new List<QuestTemplate>();

                    foreach (var quest in ContentTemplate.Quests)
                    {
                        // For each quest we now have, try and find the new, opposing quest
                        var originalQuest = db.Quests.First(x => x.Id == quest.Id);
                        templates.Add(originalQuest);
                    }

                    originalJob.Quests = templates;
                }

                db.SaveChanges();
            }

            base.Save();
        }
        public static void OnCharacterSelect(GameClient client, ClientHeroSelectPacket packet)
        {
            // Ensure this is a valid action
            bool actionCanBePerformed = client.Account.IsOnline && client.HeroEntity == null;

            if (!actionCanBePerformed)
                return;

            // Get the hero the user wanted
            UserHero hero = client.Account.Heroes.FirstOrDefault();

            if (hero == null)
            {
                var response = new ServerHeroSelectResponsePacket(HeroStatus.Invalid);
                client.Send(response);
            }
            else
            {

                using (var context = new GameDatabaseContext())
                {
                    context.Characters.Attach(hero);
                    context.Entry(hero).Collection(a => a.Skills).Load();
                    context.Entry(hero).Collection(a => a.Inventory).Load();
                    context.Entry(hero).Collection(a => a.QuestInfo).Load();
                    context.Entry(hero).Collection(a => a.Flags).Load();

                    // Load up our set of requirements from the table
                    foreach (var x in hero.QuestInfo)
                        context.Entry(x).Collection(a => a.RequirementProgress).Load();

                    context.Entry(hero).Collection(a => a.Equipment).Load();
                }

                // Create an object and assign it the world if everything is okay
                var heroObject = GameObjectFactory.CreateHero(hero, client);
                client.HeroEntity = heroObject;

                var zone = ZoneManager.Instance.FindZone(hero.ZoneId);

                if (zone != null)
                {
                    // Create a response and alert the client that their selection is okay
                    var response = new ServerHeroSelectResponsePacket(HeroStatus.OK);
                    client.Send(response);

                    // Here, we should queue the player for login

                    Logger.Instance.Info("{0} has entered the game.", hero.Name);
                    zone.QueueLogin(heroObject);

                    // Add to global manager
                    ChatManager.Current.Global.Join(client);

                }
                else
                {
                    var response = new ServerHeroSelectResponsePacket(HeroStatus.Unavailable);
                    client.Send(response);

                    Logger.Instance.Error("{0} attempted to enter the game but could not be loaded.", hero.Name);
                }

            }
        }
Example #7
0
        public static void OnLoginRequest(GameClient client, ClientLoginRequestPacket packet)
        {
            Logger.Instance.Info("LoginHandler.OnLoginRequest", client, packet);

            UserAccount account;

            bool loginSuccessful = AuthenticationProvider.Authenticate(packet.Username, packet.Password, out account);

            if (!loginSuccessful)
            {
                client.Send(new ServerLoginResponsePacket(LoginStatus.InvalidCredentials));
                return;
            }

            if (account.IsOnline)
            {
                client.Send(new ServerLoginResponsePacket(LoginStatus.AlreadyLoggedIn));
                return;
            }

            client.Account = account;

            using (var context = new GameDatabaseContext())
            {
                context.Accounts.Attach(account);

                account.IsOnline = true;

                context.SaveChanges();

                // Load heroes
                context.Entry(account).Collection(a => a.Heroes).Load();
            }

            client.Send(new ServerLoginResponsePacket(LoginStatus.OK));

            HeroSelectionHandler.SendHeroList(client);
        }
Example #8
0
        private static void PersistPlayer(Player player)
        {
            using (var context = new GameDatabaseContext())
            {
                var hero = context.Characters.First(x => x.UserHeroId == player.UserId);

                // Load quest info
                context.Entry(hero).Collection(x => x.QuestInfo).Load();

                foreach (var x in hero.QuestInfo)
                {
                    context.Entry(x).Collection(a => a.RequirementProgress).Load();

                    var l = x.RequirementProgress.ToList();
                    foreach (var n in l)
                        context.Entry(n).State = EntityState.Deleted;

                }

                context.Entry(hero).Collection(x => x.Inventory).Load();
                context.Entry(hero).Collection(x => x.Equipment).Load();
                context.Entry(hero).Collection(x => x.Flags).Load();

                hero.Name = player.Name;
                hero.PositionX = (int)player.Position.X;
                hero.PositionY = (int)player.Position.Y;
                hero.ZoneId = player.Zone.Id;

                // Persist stats
                hero.Hitpoints = (int)player.CharacterStats[(int)StatTypes.Hitpoints].CurrentValue;
                hero.Strength = (int)player.CharacterStats[(int)StatTypes.Strength].CurrentValue;
                hero.Intelligence = (int)player.CharacterStats[(int)StatTypes.Intelligence].CurrentValue;
                hero.Dexterity = (int)player.CharacterStats[(int)StatTypes.Dexterity].CurrentValue;
                hero.Luck = (int)player.CharacterStats[(int)StatTypes.Luck].CurrentValue;
                hero.Vitality = (int)player.CharacterStats[(int)StatTypes.Vitality].CurrentValue;
                hero.MaximumHitpoints = (int)player.CharacterStats[(int)StatTypes.Hitpoints].MaximumValue;
                hero.Mind = (int)player.CharacterStats[StatTypes.Mind].CurrentValue;

                hero.SkillResource = (int)player.CharacterStats[StatTypes.SkillResource].CurrentValue;
                hero.MaximumSkillResource = (int)player.CharacterStats[StatTypes.SkillResource].MaximumValue;

                hero.Experience = player.Experience;
                hero.Level = player.Level;

                hero.Flags = player.Flags;

                //TODO: Need better tracking code here, incrementing the row needlessly here

                // For now though, we don't care...
                hero.QuestInfo.ToList().ForEach(r => context.UserQuestInfo.Remove(r));

                context.SaveChanges();
                //context.SaveChanges();

                foreach (var entry in player.QuestLog)
                {

                    var temp = new List<UserQuestRequirements>();

                    for (int index = 0; index < entry.Progress.Count; index++)
                    {
                        var x = entry.Progress[index];

                        var questRequirementProgress = new UserQuestRequirements()
                        {
                            Progress = x
                        };

                        if (entry.QuestInfo.RequirementProgress.Count - 1 >= index)
                            questRequirementProgress.UserQuestRequirementId =
                                entry.QuestInfo.RequirementProgress[index].QuestInfo.UserQuestInfoId;

                        temp.Add(questRequirementProgress);
                    }

                    var quest = new UserQuestInfo()
                   {
                       QuestId = entry.QuestInfo.QuestId,
                       State = entry.State,
                       UserQuestInfoId = entry.QuestInfo.UserQuestInfoId,
                       UserHero = hero,
                       RequirementProgress = temp,
                       QuestProgress = entry.QuestInfo.QuestProgress
                   };

                    hero.QuestInfo.Add(quest);

                }

                hero.Inventory.ToList().ForEach(r => context.UserItems.Remove(r));

                foreach (var inventorySlot in player.Backpack.Storage)
                {
                    var x = new UserItem(inventorySlot.Value.Item.Id, inventorySlot.Value.Amount, inventorySlot.Key);
                    hero.Inventory.Add(x);
                }

                hero.Equipment.ToList().ForEach(r => context.UserEquipments.Remove(r));

                foreach (var equipmentPiece in player.Equipment)
                {
                    if (equipmentPiece == null)
                        continue;

                    var x = new UserEquipment()
                    {
                        ItemId = equipmentPiece.Id,
                        Slot = equipmentPiece.Slot,
                        UserEquipmentId = 0
                    };

                    hero.Equipment.Add(x);
                }

                context.Entry(hero).State = EntityState.Modified;

                // Flush
                context.SaveChanges();

            }
        }