public async Task InstallScriptFor(int entryOrGuid, SmartScriptType type, IList <ISmartScriptLine> script)
        {
            using var writeLock = await DatabaseLock.WriteLock();

            await using var model = Database();

            await model.BeginTransactionAsync(IsolationLevel.ReadCommitted);

            foreach (var pair in script.Select(l => (l.ScriptSourceType, l.EntryOrGuid))
                     .Concat(new (int ScriptSourceType, int EntryOrGuid)[] { ((int)type, entryOrGuid) })
Example #2
0
        public async Task InstallConditions(IEnumerable <IConditionLine> conditionLines,
                                            IDatabaseProvider.ConditionKeyMask keyMask,
                                            IDatabaseProvider.ConditionKey?manualKey = null)
        {
            using var writeLock = await DatabaseLock.WriteLock();

            await using var model = new TrinityDatabase();

            var conditions = conditionLines?.ToList() ?? new List <IConditionLine>();
            List <(int SourceType, int?SourceGroup, int?SourceEntry, int?SourceId)> keys = conditions.Select(c =>
                                                                                                             (c.SourceType, keyMask.HasFlag(IDatabaseProvider.ConditionKeyMask.SourceGroup) ? (int?)c.SourceGroup : null,
                                                                                                              keyMask.HasFlag(IDatabaseProvider.ConditionKeyMask.SourceEntry) ? (int?)c.SourceEntry : null,
                                                                                                              keyMask.HasFlag(IDatabaseProvider.ConditionKeyMask.SourceId) ? (int?)c.SourceId : null))
                                                                                           .Union(manualKey.HasValue
                    ? new[]
Example #3
0
        public async Task InstallScriptFor(int entryOrGuid, SmartScriptType type, IEnumerable <ISmartScriptLine> script)
        {
            using var writeLock = await DatabaseLock.WriteLock();

            await using var model = new TrinityDatabase();

            await model.BeginTransactionAsync(IsolationLevel.ReadCommitted);

            await model.SmartScript.Where(x => x.EntryOrGuid == entryOrGuid && x.ScriptSourceType == (int)type).DeleteAsync();

            switch (type)
            {
            case SmartScriptType.Creature:
                await model.CreatureTemplate.Where(p => p.Entry == (uint)entryOrGuid)
                .Set(p => p.AIName, "SmartAI")
                .Set(p => p.ScriptName, "")
                .UpdateAsync();

                break;

            case SmartScriptType.GameObject:
                await model.GameObjectTemplate.Where(p => p.Entry == (uint)entryOrGuid)
                .Set(p => p.AIName, "SmartGameObjectAI")
                .Set(p => p.ScriptName, "")
                .UpdateAsync();

                break;

            case SmartScriptType.Quest:
                var addonExists = await model.QuestTemplateAddon.Where(p => p.Entry == (uint)entryOrGuid).AnyAsync();

                if (!addonExists)
                {
                    await model.QuestTemplateAddon.InsertAsync(() => new MySqlQuestTemplateAddon()
                    {
                        Entry = (uint)entryOrGuid
                    });
                }
                await model.QuestTemplateAddonWithScriptName
                .Where(p => p.Entry == (uint)entryOrGuid)
                .Set(p => p.ScriptName, "SmartQuest")
                .UpdateAsync();

                break;

            case SmartScriptType.AreaTrigger:
                await model.AreaTriggerScript.Where(p => p.Id == entryOrGuid).DeleteAsync();

                await model.AreaTriggerScript.InsertAsync(() => new MySqlAreaTriggerScript()
                {
                    Id = entryOrGuid, ScriptName = "SmartTrigger"
                });

                break;

            case SmartScriptType.AreaTriggerEntity:
                await model.AreaTriggerTemplate.Where(p => p.Id == (uint)entryOrGuid && p.IsServerSide == false)
                .Set(p => p.ScriptName, "SmartAreaTriggerAI")
                .UpdateAsync();

                break;

            case SmartScriptType.AreaTriggerEntityServerSide:
                await model.AreaTriggerTemplate.Where(p => p.Id == (uint)entryOrGuid && p.IsServerSide == true)
                .Set(p => p.ScriptName, "SmartAreaTriggerAI")
                .UpdateAsync();

                break;
            }

            await model.SmartScript.BulkCopyAsync(script.Select(l => new MySqlSmartScriptLine(l)));

            await model.CommitTransactionAsync();
        }
        public async Task InstallScriptFor(int entryOrGuid, SmartScriptType type, IEnumerable <ISmartScriptLine> script)
        {
            using var writeLock = await DatabaseLock.WriteLock();

            await using var model = Database();

            await model.BeginTransactionAsync(IsolationLevel.ReadCommitted);

            await model.SmartScript.Where(x => x.EntryOrGuid == entryOrGuid && x.ScriptSourceType == (int)type).DeleteAsync();

            switch (type)
            {
            case SmartScriptType.Creature:
            {
                uint entry = 0;
                if (entryOrGuid < 0)
                {
                    var template = await model.Creature.Where(p => p.Guid == (uint)-entryOrGuid).FirstOrDefaultAsync();

                    if (template == null)
                    {
                        throw new Exception(
                                  $"Trying to install creature script for guid {-entryOrGuid}, but this guid doesn't exist in creature table, so entry cannot be determined.");
                    }
                    entry = template.Entry;
                }
                else
                {
                    entry = (uint)entryOrGuid;
                }
                await model.CreatureTemplate.Where(p => p.Entry == entry)
                .Set(p => p.AIName, currentCoreVersion.Current.SmartScriptFeatures.CreatureSmartAiName)
                .Set(p => p.ScriptName, "")
                .UpdateAsync();

                break;
            }

            case SmartScriptType.GameObject:
            {
                uint entry = 0;
                if (entryOrGuid < 0)
                {
                    var template = await model.GameObject.Where(p => p.Guid == (uint)-entryOrGuid).FirstOrDefaultAsync();

                    if (template == null)
                    {
                        throw new Exception(
                                  $"Trying to install gameobject script for guid {-entryOrGuid}, but this guid doesn't exist in gameobject table, so entry cannot be determined.");
                    }
                    entry = template.Entry;
                }
                else
                {
                    entry = (uint)entryOrGuid;
                }
                await model.GameObjectTemplate.Where(p => p.Entry == entry)
                .Set(p => p.AIName, currentCoreVersion.Current.SmartScriptFeatures.GameObjectSmartAiName)
                .Set(p => p.ScriptName, "")
                .UpdateAsync();

                break;
            }

            case SmartScriptType.Quest:
                var addonExists = await model.QuestTemplateAddon.Where(p => p.Entry == (uint)entryOrGuid).AnyAsync();

                if (!addonExists)
                {
                    await model.QuestTemplateAddon.InsertAsync(() => new MySqlQuestTemplateAddon()
                    {
                        Entry = (uint)entryOrGuid
                    });
                }
                await model.QuestTemplateAddonWithScriptName
                .Where(p => p.Entry == (uint)entryOrGuid)
                .Set(p => p.ScriptName, "SmartQuest")
                .UpdateAsync();

                break;

            case SmartScriptType.AreaTrigger:
                await model.AreaTriggerScript.Where(p => p.Id == entryOrGuid).DeleteAsync();

                await model.AreaTriggerScript.InsertAsync(() => new MySqlAreaTriggerScript()
                {
                    Id = entryOrGuid, ScriptName = "SmartTrigger"
                });

                break;

            case SmartScriptType.AreaTriggerEntity:
                await model.AreaTriggerTemplate.Where(p => p.Id == (uint)entryOrGuid && p.IsServerSide == false)
                .Set(p => p.ScriptName, "SmartAreaTriggerAI")
                .UpdateAsync();

                break;

            case SmartScriptType.AreaTriggerEntityServerSide:
                await model.AreaTriggerTemplate.Where(p => p.Id == (uint)entryOrGuid && p.IsServerSide == true)
                .Set(p => p.ScriptName, "SmartAreaTriggerAI")
                .UpdateAsync();

                break;
            }

            await model.SmartScript.BulkCopyAsync(script.Select(l => new MySqlSmartScriptLine(l)));

            await model.CommitTransactionAsync();
        }