/// <summary>
        /// Fills the placeholders of a code
        /// </summary>
        /// <param name="code">Code to fill</param>
        /// <param name="data">Export Data</param>
        /// <returns>Filled Code</returns>
        public async Task <string> FillPlaceholders(string code, ExportObjectData data)
        {
            // Check Data
            if (!data.ExportData.ContainsKey(ExportConstants.ExportDataObject))
            {
                return(code);
            }

            KortistoNpc npc = data.ExportData[ExportConstants.ExportDataObject] as KortistoNpc;

            if (npc != null)
            {
                // Replace Dialog Placeholders
                return(await FillDialogPlaceholders(code, npc));
            }

            ExportDialogFunctionCode dialogFunction = data.ExportData[ExportConstants.ExportDataObject] as ExportDialogFunctionCode;

            if (dialogFunction != null)
            {
                // Replace Dialog Function
                GoNorthProject project = await _cachedDbAccess.GetUserProject();

                return(await RenderDialogFunction(project, dialogFunction));
            }

            return(code);
        }
        /// <summary>
        /// Fills the daily routine placeholders
        /// </summary>
        /// <param name="code">Code</param>
        /// <param name="npc">Npc</param>
        /// <param name="data">Export Data</param>
        /// <returns>Filled code</returns>
        private async Task <string> FillDailyRoutinePlaceholders(string code, KortistoNpc npc, ExportObjectData data)
        {
            GoNorthProject project = await _cachedDbAccess.GetUserProject();

            ExportTemplate dailyRoutineFunctionTemplate = await _defaultTemplateProvider.GetDefaultTemplateByType(project.Id, TemplateType.ObjectDailyRoutineFunction);

            code = await ExportUtil.BuildPlaceholderRegex(Placeholder_DailyRoutine_Events, ExportConstants.ListIndentPrefix).ReplaceAsync(code, async m => {
                string eventList = await ReplaceEventList(project, data);
                return(ExportUtil.IndentListTemplate(eventList, m.Groups[1].Value));
            });

            code = await ExportUtil.BuildPlaceholderRegex(Placeholder_DailyRoutine_Functions, ExportConstants.ListIndentPrefix).ReplaceAsync(code, async m => {
                string functionList = await ReplaceFunctionList(project, npc);
                return(ExportUtil.IndentListTemplate(functionList, m.Groups[1].Value));
            });

            code = ExportUtil.BuildPlaceholderRegex(Placeholder_DailyRoutine_Function, ExportConstants.ListIndentPrefix).Replace(code, m => {
                return(ExportUtil.IndentListTemplate(dailyRoutineFunctionTemplate.Code, m.Groups[1].Value));
            });

            code = ExportUtil.RenderPlaceholderIfTrue(code, Placeholder_HasDailyRoutine_Start, Placeholder_HasDailyRoutine_End, npc.DailyRoutine != null && npc.DailyRoutine.Count > 0);

            code = ExportUtil.BuildRangePlaceholderRegex(Placeholder_DailyRoutine_Events_Start, Placeholder_DailyRoutine_Events_End).Replace(code, m => {
                return(ExportUtil.TrimEmptyLines(BuildEventList(m.Groups[1].Value, project, npc)));
            });

            code = await ExportUtil.BuildRangePlaceholderRegex(Placeholder_DailyRoutine_Functions_Start, Placeholder_DailyRoutine_Functions_End).ReplaceAsync(code, async m => {
                string functionList = await BuildFunctionList(m.Groups[1].Value, project, npc);
                return(ExportUtil.TrimEmptyLines(functionList));
            });

            return(code);
        }
        /// <summary>
        /// Renders an daily routine event list
        /// </summary>
        /// <param name="context">Template context</param>
        /// <param name="callerContext">Caller context</param>
        /// <param name="arguments">Arguments to render the list with</param>
        /// <returns>Rendered daily routine event list</returns>
        private async ValueTask <object> RenderDailyRoutineEventList(TemplateContext context, ScriptNode callerContext, ScriptArray arguments)
        {
            List <ScribanExportDailyRoutineEvent> eventList = ScribanParsingUtil.GetArrayFromScribanArgument <ScribanExportDailyRoutineEvent>(arguments, 0);

            if (eventList == null)
            {
                _errorCollection.AddInvalidParameter(ScribanErrorUtil.FormatScribanSpan(callerContext.Span));
                return("<<DID NOT PROVIDE VALID EVENT LIST>>");
            }

            GoNorthProject curProject = await _exportCachedDbAccess.GetUserProject();

            ExportTemplate eventListTemplate = await _defaultTemplateProvider.GetDefaultTemplateByType(curProject.Id, TemplateType.ObjectDailyRoutineEventList);

            ExportObjectData objectData = _exportObjectData.Clone();

            if (objectData.ExportData.ContainsKey(ExportConstants.ExportDataObject) && objectData.ExportData[ExportConstants.ExportDataObject] is KortistoNpc)
            {
                ((KortistoNpc)objectData.ExportData[ExportConstants.ExportDataObject]).DailyRoutine = eventList.Select(e => e.OriginalEvent).ToList();
            }

            ExportPlaceholderFillResult fillResult = await _templatePlaceholderResolver.FillPlaceholders(TemplateType.ObjectDailyRoutineEventList, eventListTemplate.Code, objectData, eventListTemplate.RenderingEngine);

            _errorCollection.Merge(fillResult.Errors);

            return(ScribanOutputUtil.IndentMultilineCode(context, fillResult.Code));
        }
        /// <summary>
        /// Resolved the placeholders for a single daily routine event
        /// </summary>
        /// <param name="code">Code to resolve the placeholders in</param>
        /// <param name="npc">Npc to which the event belongs</param>
        /// <param name="dailyRoutineEvent">Daily routine to use for resolving the placeholders</param>
        /// <param name="errorCollection">Error collection</param>
        /// <returns>Code with resolved placeholders</returns>
        public async Task <string> ResolveDailyRoutineEventContentPlaceholders(string code, KortistoNpc npc, KortistoNpcDailyRoutineEvent dailyRoutineEvent, ExportPlaceholderErrorCollection errorCollection)
        {
            GoNorthProject project = await _cachedDbAccess.GetUserProject();

            ExportTemplate dailyRoutineFunctionTemplate = await _defaultTemplateProvider.GetDefaultTemplateByType(project.Id, TemplateType.ObjectDailyRoutineFunction);

            List <DailyRoutineFunction> functions = await this.RenderDailyRoutineFunctions(dailyRoutineEvent, npc, errorCollection);

            string finalCode = string.Empty;

            foreach (DailyRoutineFunction curFunction in functions)
            {
                if (!string.IsNullOrEmpty(finalCode))
                {
                    finalCode += Environment.NewLine;
                }

                string functionCode = code;
                if (dailyRoutineFunctionTemplate.RenderingEngine == ExportTemplateRenderingEngine.Scriban)
                {
                    functionCode = await ReplaceEventFunction(functionCode, curFunction, errorCollection);
                }
                functionCode = FillFunctionCode(functionCode, curFunction);
                finalCode   += functionCode;
            }

            return(finalCode);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Loads the inventory
        /// </summary>
        /// <param name="parsedTemplate">Parsed scriban template</param>
        /// <param name="inputNpc">Input npc</param>
        /// <returns>List of items</returns>
        private async Task<List<ScribanExportInventoryItem>> LoadInventory(Template parsedTemplate, KortistoNpc inputNpc)
        {
            if(inputNpc.Inventory == null || !inputNpc.Inventory.Any())
            {
                return new List<ScribanExportInventoryItem>();
            }
            

            GoNorthProject project = await _exportCachedDbAccess.GetUserProject();
            ExportSettings exportSettings = await _exportCachedDbAccess.GetExportSettings(project.Id);

            List<ScribanExportInventoryItem> inventoryItems = new List<ScribanExportInventoryItem>();
            List<StyrItem> items = await _exportCachedDbAccess.GetItemsById(inputNpc.Inventory.Select(i => i.ItemId).ToList());
            foreach(KortistoInventoryItem curItem in inputNpc.Inventory)
            {
                StyrItem loadedItem = items.FirstOrDefault(i => i.Id == curItem.ItemId);
                if(loadedItem == null)
                {
                    continue;
                }

                ScribanExportInventoryItem exportItem = FlexFieldValueCollectorUtil.BuildFlexFieldValueObject<ScribanExportInventoryItem>(null, parsedTemplate, loadedItem, exportSettings, _errorCollection);
                exportItem.Quantity = curItem.Quantity;
                exportItem.IsEquipped = curItem.IsEquipped;
                inventoryItems.Add(exportItem);
            }

            return inventoryItems;
        }
Exemplo n.º 6
0
        /// <summary>
        /// Returns the npc use
        /// </summary>
        /// <param name="flexFieldObject">Flex field object</param>
        /// <param name="parsedData">Parsed data</param>
        /// <param name="errorCollection">Error Collection</param>
        /// <returns>Value Object</returns>
        private async Task <IFlexFieldExportable> GetNpc(FlexFieldObject flexFieldObject, UseItemActionData parsedData, ExportPlaceholderErrorCollection errorCollection)
        {
            if (_isPickNpc)
            {
                KortistoNpc npc = await _cachedDbAccess.GetNpcById(parsedData.NpcId);

                if (npc == null)
                {
                    errorCollection.AddDialogItemNotFoundError();
                    return(null);
                }

                return(npc);
            }
            else if (_isPlayer)
            {
                GoNorthProject curProject = await _cachedDbAccess.GetUserProject();

                KortistoNpc npc = await _cachedDbAccess.GetPlayerNpc(curProject.Id);

                if (npc == null)
                {
                    errorCollection.AddNoPlayerNpcExistsError();
                    return(null);
                }

                return(npc);
            }

            return(flexFieldObject);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Renders an skill List
        /// </summary>
        /// <param name="context">Template context</param>
        /// <param name="callerContext">Caller context</param>
        /// <param name="arguments">Arguments to render the list with</param>
        /// <returns>Rendered skill list</returns>
        private async ValueTask <object> RenderSkillList(TemplateContext context, ScriptNode callerContext, ScriptArray arguments)
        {
            if (!_exportObjectData.ExportData.ContainsKey(ExportConstants.ExportDataObject) || !(_exportObjectData.ExportData[ExportConstants.ExportDataObject] is KortistoNpc))
            {
                _errorCollection.AddInvalidParameter(ScribanErrorUtil.FormatScribanSpan(callerContext.Span));
                return("<<NO VALID NPC CONTEXT>>");
            }

            List <ScribanExportSkill> skillList = ScribanParsingUtil.GetArrayFromScribanArgument <ScribanExportSkill>(arguments, 0);

            if (skillList == null)
            {
                _errorCollection.AddInvalidParameter(ScribanErrorUtil.FormatScribanSpan(callerContext.Span));
                return("<<DID NOT PROVIDE VALID SKILL LIST>>");
            }

            GoNorthProject curProject = await _exportCachedDbAccess.GetUserProject();

            ExportTemplate skillListTemplate = await _defaultTemplateProvider.GetDefaultTemplateByType(curProject.Id, TemplateType.ObjectSkillList);

            ExportObjectData objectData = _exportObjectData.Clone();

            ((KortistoNpc)objectData.ExportData[ExportConstants.ExportDataObject]).Skills = ConvertSkillsToKortistoNpcSkill(skillList);

            ExportPlaceholderFillResult fillResult = await _templatePlaceholderResolver.FillPlaceholders(TemplateType.ObjectSkillList, skillListTemplate.Code, objectData, skillListTemplate.RenderingEngine);

            _errorCollection.Merge(fillResult.Errors);

            return(ScribanOutputUtil.IndentMultilineCode(context, fillResult.Code));
        }
Exemplo n.º 8
0
        /// <summary>
        /// Renders an attribute List
        /// </summary>
        /// <param name="context">Template context</param>
        /// <param name="callerContext">Caller context</param>
        /// <param name="arguments">Arguments to render the list with</param>
        /// <returns>Rendered attribute list</returns>
        private async ValueTask <object> RenderAttributeList(TemplateContext context, ScriptNode callerContext, ScriptArray arguments)
        {
            List <ScribanFlexFieldField> fieldList = ScribanParsingUtil.GetArrayFromScribanArgument <ScribanFlexFieldField>(arguments, 0);

            if (fieldList == null)
            {
                _errorCollection.AddInvalidParameter(ScribanErrorUtil.FormatScribanSpan(callerContext.Span));
                return("<<DID NOT PROVIDE VALID ATTRIBUTE LIST>>");
            }

            GoNorthProject curProject = await _exportCachedDbAccess.GetUserProject();

            ExportTemplate attributeListTemplate = await _defaultTemplateProvider.GetDefaultTemplateByType(curProject.Id, TemplateType.ObjectAttributeList);

            ExportObjectData objectData = _exportObjectData.Clone();

            if (objectData.ExportData.ContainsKey(ExportConstants.ExportDataObject) && objectData.ExportData[ExportConstants.ExportDataObject] is IFlexFieldExportable)
            {
                ((IFlexFieldExportable)objectData.ExportData[ExportConstants.ExportDataObject]).Fields = ConvertListToFlexFieldList(fieldList);
            }

            ExportPlaceholderFillResult fillResult = await _templatePlaceholderResolver.FillPlaceholders(TemplateType.ObjectAttributeList, attributeListTemplate.Code, objectData, attributeListTemplate.RenderingEngine);

            _errorCollection.Merge(fillResult.Errors);

            return(ScribanOutputUtil.IndentMultilineCode(context, fillResult.Code));
        }
        /// <summary>
        /// Renders the inventory based on the shared template
        /// </summary>
        /// <param name="data">Export Data</param>
        /// <param name="indent">Indentation</param>
        /// <returns>Item List</returns>
        private async Task <string> RenderInventory(ExportObjectData data, string indent)
        {
            GoNorthProject project = await _cachedDbAccess.GetUserProject();

            ExportTemplate inventoryTemplate = await _defaultTemplateProvider.GetDefaultTemplateByType(project.Id, TemplateType.ObjectInventory);

            ExportPlaceholderFillResult fillResult = await _placeholderResolver.FillPlaceholders(TemplateType.ObjectInventory, inventoryTemplate.Code, data, inventoryTemplate.RenderingEngine);

            _errorCollection.Merge(fillResult.Errors);

            return(ExportUtil.IndentListTemplate(fillResult.Code, indent));
        }
        /// <summary>
        /// Loads a template
        /// </summary>
        /// <param name="templateName">Template name</param>
        /// <param name="callerSpan">Caller span</param>
        /// <returns>Code of the template</returns>
        private async Task <string> LoadTemplate(string templateName, SourceSpan callerSpan)
        {
            GoNorthProject project = await _cachedDbAccess.GetUserProject();

            IncludeExportTemplate includeExportTemplate = await _cachedDbAccess.GetIncludeTemplateByName(project.Id, templateName);

            if (includeExportTemplate == null)
            {
                _errorCollection.AddIncludeTemplateNotFoundError(templateName, ScribanErrorUtil.FormatScribanSpan(callerSpan));
                return("<<INCLUDE TEMPLATE NOT FOUND>>");
            }

            return(includeExportTemplate.Code);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Loads the skills
        /// </summary>
        /// <param name="parsedTemplate">Parsed scriban template</param>
        /// <param name="inputNpc">Input npc</param>
        /// <returns>List of skills</returns>
        private async Task <List <ScribanExportSkill> > LoadSkills(Template parsedTemplate, KortistoNpc inputNpc)
        {
            if (inputNpc.Skills == null || !inputNpc.Skills.Any())
            {
                return(new List <ScribanExportSkill>());
            }

            GoNorthProject project = await _exportCachedDbAccess.GetUserProject();

            ExportSettings exportSettings = await _exportCachedDbAccess.GetExportSettings(project.Id);

            List <EvneSkill> skills = await _exportCachedDbAccess.GetSkillsById(inputNpc.Skills.Select(i => i.SkillId).ToList());

            return(skills.Select(s => FlexFieldValueCollectorUtil.BuildFlexFieldValueObject <ScribanExportSkill>(null, parsedTemplate, s, exportSettings, _errorCollection)).ToList());
        }
Exemplo n.º 12
0
        /// <summary>
        /// Returns the currently valid npc for the action
        /// </summary>
        /// <param name="parsedData">Parsed data</param>
        /// <param name="flexFieldObject">Flex field object</param>
        /// <returns>Npc</returns>
        private async Task <KortistoNpc> GetNpc(MoveNpcActionData parsedData, FlexFieldObject flexFieldObject)
        {
            if (_isPickedNpc)
            {
                return(await _cachedDbAccess.GetNpcById(parsedData.ObjectId));
            }

            if (_isPlayer)
            {
                GoNorthProject project = await _cachedDbAccess.GetUserProject();

                return(await _cachedDbAccess.GetPlayerNpc(project.Id));
            }

            return(flexFieldObject as KortistoNpc);
        }
        /// <summary>
        /// Returns the npc to use
        /// </summary>
        /// <param name="flexFieldObject">Flex field object to which the dialog belongs</param>
        /// <param name="errorCollection">Error Collection</param>
        /// <returns>Value Object</returns>
        private async Task <IFlexFieldExportable> GetNpc(FlexFieldObject flexFieldObject, ExportPlaceholderErrorCollection errorCollection)
        {
            if (_isPlayer)
            {
                GoNorthProject curProject = await _cachedDbAccess.GetUserProject();

                flexFieldObject = await _cachedDbAccess.GetPlayerNpc(curProject.Id);

                if (flexFieldObject == null)
                {
                    errorCollection.AddNoPlayerNpcExistsError();
                    return(null);
                }
            }

            return(flexFieldObject);
        }
        /// <summary>
        /// Collects the values for an export
        /// </summary>
        /// <param name="templateType">Template type</param>
        /// <param name="parsedTemplate">Parsed scriban template</param>
        /// <param name="scriptObject">Scriban script object to fill</param>
        /// <param name="data">Export Data</param>
        /// <returns>Task</returns>
        public override async Task CollectValues(TemplateType templateType, Template parsedTemplate, ScriptObject scriptObject, ExportObjectData data)
        {
            IFlexFieldExportable exportable = data.ExportData[ExportConstants.ExportDataObject] as IFlexFieldExportable;

            if (exportable == null)
            {
                return;
            }

            _languageKeyGenerator.SetErrorCollection(_errorCollection);

            GoNorthProject project = await _cachedDbAccess.GetUserProject();

            ExportSettings exportSettings = await _cachedDbAccess.GetExportSettings(project.Id);

            scriptObject.AddOrUpdate(ObjectKey, FlexFieldValueCollectorUtil.ExtractScribanFields(exportable, exportSettings, _errorCollection));
            scriptObject.AddOrUpdate(ExportConstants.ScribanLanguageKeyName, _languageKeyGenerator);
        }
Exemplo n.º 15
0
        /// <summary>
        /// Fills the language key Placeholders
        /// </summary>
        /// <param name="code">Code to fill</param>
        /// <param name="flexFieldObject">Flex Field Object</param>
        /// <param name="referencedLanguageKeys">Referenced language keys</param>
        /// <returns>Filled Code</returns>
        private async Task <string> FillLanguageKeyPlaceholders(string code, IFlexFieldExportable flexFieldObject, List <LanguageKey> referencedLanguageKeys)
        {
            GoNorthProject project = await _cachedDbAccess.GetUserProject();

            List <LanguageKey> languageKeys = await _languageKeyDbAccess.GetLanguageKeysByGroupId(project.Id, flexFieldObject.Id);

            ExportSettings exportSettings = await _cachedDbAccess.GetExportSettings(project.Id);

            code = ExportUtil.RenderPlaceholderIfTrue(code, Placeholder_Has_Referenced_LanguageKeys_Start, Placeholder_Has_Referenced_LanguageKeys_End, referencedLanguageKeys != null && referencedLanguageKeys.Any());
            code = ExportUtil.BuildPlaceholderRegex(Placeholder_ObjectName).Replace(code, flexFieldObject.Name);
            code = ExportUtil.BuildRangePlaceholderRegex(Placeholder_LanguageKeys_Start, Placeholder_LanguageKeys_End).Replace(code, m => {
                return(BuildLanguageKeyList(m.Groups[1].Value, languageKeys, exportSettings));
            });
            code = ExportUtil.BuildRangePlaceholderRegex(Placeholder_Referenced_LanguageKeys_Start, Placeholder_Referenced_LanguageKeys_End).Replace(code, m => {
                return(BuildLanguageKeyList(m.Groups[1].Value, referencedLanguageKeys, exportSettings));
            });

            return(code);
        }
Exemplo n.º 16
0
        /// <summary>
        /// Collects the values for an export
        /// </summary>
        /// <param name="templateType">Template type</param>
        /// <param name="parsedTemplate">Parsed scriban template</param>
        /// <param name="scriptObject">Scriban script object to fill</param>
        /// <param name="data">Export Data</param>
        /// <returns>Task</returns>
        public override async Task CollectValues(TemplateType templateType, Template parsedTemplate, ScriptObject scriptObject, ExportObjectData data)
        {
            KortistoNpc inputNpc = data.ExportData[ExportConstants.ExportDataObject] as KortistoNpc;

            if (inputNpc == null)
            {
                return;
            }

            _languageKeyGenerator.SetErrorCollection(_errorCollection);

            GoNorthProject project = await _exportCachedDbAccess.GetUserProject();

            TaleDialog dialog = await _taleDbAccess.GetDialogByRelatedObjectId(inputNpc.Id);

            ExportDialogData parsedDialog = await ParseDialog(inputNpc, project, dialog);

            ScribanExportDialog exportDialog = await BuildExportDialog(parsedDialog, inputNpc);

            scriptObject.AddOrUpdate(ExportConstants.ScribanDialogKey, exportDialog);
            scriptObject.AddOrUpdate(DialogFunctionRenderer.DialogFunctionName, new DialogFunctionRenderer(_templatePlaceholderResolver, _exportCachedDbAccess, _defaultTemplateProvider, _errorCollection, data));
        }
Exemplo n.º 17
0
        /// <summary>
        /// Collects the values for an export
        /// </summary>
        /// <param name="templateType">Template type</param>
        /// <param name="parsedTemplate">Parsed scriban template</param>
        /// <param name="scriptObject">Scriban script object to fill</param>
        /// <param name="data">Export Data</param>
        /// <returns>Task</returns>
        public override async Task CollectValues(TemplateType templateType, Template parsedTemplate, ScriptObject scriptObject, ExportObjectData data)
        {
            InputObject inputObject = data.ExportData[ExportConstants.ExportDataObject] as InputObject;

            if (inputObject == null)
            {
                return;
            }

            _languageKeyGenerator.SetErrorCollection(_errorCollection);


            GoNorthProject project = await _exportCachedDbAccess.GetUserProject();

            ExportSettings exportSettings = await _exportCachedDbAccess.GetExportSettings(project.Id);

            ExportClass exportObject = BuildExportObject(parsedTemplate, inputObject, exportSettings);

            scriptObject.Add(GetObjectKey(), exportObject);
            scriptObject.Add(ExportConstants.ScribanLanguageKeyName, _languageKeyGenerator);
            scriptObject.Add(FlexFieldAttributeListRenderer.AttributeListFunctionName, new FlexFieldAttributeListRenderer(_templatePlaceholderResolver, _exportCachedDbAccess, _defaultTemplateProvider, _errorCollection, data));
            AddAdditionalScriptObjectValues(templateType, parsedTemplate, scriptObject, data);
        }
Exemplo n.º 18
0
        /// <summary>
        /// Renders an state machine function
        /// </summary>
        /// <param name="context">Template context</param>
        /// <param name="callerContext">Caller context</param>
        /// <param name="arguments">Arguments to render the function with</param>
        /// <returns>Rendered state machine function</returns>
        private async ValueTask <object> RenderStateMachineFunction(TemplateContext context, ScriptNode callerContext, ScriptArray arguments)
        {
            if (arguments.Count != 1 || !(arguments[0] is ScribanExportStateFunction))
            {
                _errorCollection.AddInvalidParameter(ScribanErrorUtil.FormatScribanSpan(callerContext.Span));
                return("<<DID NOT PROVIDE VALID STATE MACHINE FUNCTION>>");
            }
            ScribanExportStateFunction exportFunction = (ScribanExportStateFunction)arguments[0];

            GoNorthProject curProject = await _exportCachedDbAccess.GetUserProject();

            ExportTemplate eventFunctionTemplate = await _defaultTemplateProvider.GetDefaultTemplateByType(curProject.Id, TemplateType.ObjectStateFunction);

            ExportObjectData objectData = new ExportObjectData();

            objectData.ExportData.Add(ExportConstants.ExportDataObject, exportFunction.ToStateFunction());

            ExportPlaceholderFillResult fillResult = await _templatePlaceholderResolver.FillPlaceholders(TemplateType.ObjectStateFunction, eventFunctionTemplate.Code, objectData, eventFunctionTemplate.RenderingEngine);

            _errorCollection.Merge(fillResult.Errors);

            return(ScribanOutputUtil.IndentMultilineCode(context, fillResult.Code));
        }
        /// <summary>
        /// Resolves the placeholders for a single daily routine event
        /// </summary>
        /// <param name="code">Code to resolve the placeholders in</param>
        /// <param name="npc">Npc to which the event belongs</param>
        /// <param name="dailyRoutineEvent">Daily routine to use for resolving the placeholders</param>
        /// <returns>Code with resolved placeholders</returns>
        public async Task <string> ResolveDailyRoutineEventPlaceholders(string code, KortistoNpc npc, KortistoNpcDailyRoutineEvent dailyRoutineEvent)
        {
            GoNorthProject defaultProject = await _cachedDbAccess.GetUserProject();

            MiscProjectConfig projectConfig = await _cachedDbAccess.GetMiscProjectConfig();

            code = ExportUtil.RenderPlaceholderIfTrue(code, Placeholder_HasMovementTargetStart, Placeholder_HasMovementTargetEnd, dailyRoutineEvent.MovementTarget != null && !string.IsNullOrEmpty(dailyRoutineEvent.MovementTarget.Name));
            code = ExportUtil.RenderPlaceholderIfTrue(code, Placeholder_HasNoMovementTargetStart, Placeholder_HasNoMovementTargetEnd, dailyRoutineEvent.MovementTarget == null || string.IsNullOrEmpty(dailyRoutineEvent.MovementTarget.Name));
            code = ExportUtil.RenderPlaceholderIfTrue(code, Placeholder_HasMovementTargetExportNameStart, Placeholder_HasMovementTargetExportNameEnd, dailyRoutineEvent.MovementTarget != null && !string.IsNullOrEmpty(dailyRoutineEvent.MovementTarget.ExportName));
            code = ExportUtil.RenderPlaceholderIfTrue(code, Placeholder_HasNoMovementTargetExportNameStart, Placeholder_HasNoMovementTargetExportNameEnd, dailyRoutineEvent.MovementTarget == null || string.IsNullOrEmpty(dailyRoutineEvent.MovementTarget.ExportName));
            code = ResolveDailyRoutineEventHasScript(code, dailyRoutineEvent.ScriptType != ExportConstants.ScriptType_None);
            code = ExportUtil.RenderPlaceholderIfTrue(code, Placeholder_HasNodeGraphScriptStart, Placeholder_HasNodeGraphScriptEnd, dailyRoutineEvent.ScriptType == ExportConstants.ScriptType_NodeGraph);
            code = ExportUtil.RenderPlaceholderIfTrue(code, Placeholder_HasCodeScriptStart, Placeholder_HasCodeScriptEnd, dailyRoutineEvent.ScriptType == ExportConstants.ScriptType_Code);
            code = ExportUtil.RenderPlaceholderIfTrue(code, Placeholder_HasTargetStateStart, Placeholder_HasTargetStateEnd, !string.IsNullOrEmpty(dailyRoutineEvent.TargetState));
            code = ExportUtil.RenderPlaceholderIfTrue(code, Placeholder_HasNoTargetStateStart, Placeholder_HasNoTargetStateEnd, string.IsNullOrEmpty(dailyRoutineEvent.TargetState));
            code = ExportUtil.RenderPlaceholderIfTrue(code, Placeholder_IsEnabledByDefaultStart, Placeholder_IsEnabledByDefaultEnd, dailyRoutineEvent.EnabledByDefault);
            code = ExportUtil.RenderPlaceholderIfTrue(code, Placeholder_IsDisabledByDefaultStart, Placeholder_IsDisabledByDefaultEnd, !dailyRoutineEvent.EnabledByDefault);

            code = ExportUtil.BuildPlaceholderRegex(Placeholder_EventId).Replace(code, dailyRoutineEvent.EventId);
            code = ExportUtil.BuildPlaceholderRegex(Placeholder_EarliestTime_Hours).Replace(code, dailyRoutineEvent.EarliestTime != null ? dailyRoutineEvent.EarliestTime.Hours.ToString() : string.Empty);
            code = ExportUtil.BuildPlaceholderRegex(Placeholder_EarliestTime_Minutes).Replace(code, dailyRoutineEvent.EarliestTime != null ? dailyRoutineEvent.EarliestTime.Minutes.ToString() : string.Empty);
            code = ExportUtil.BuildPlaceholderRegex(Placeholder_EarliestTime_TotalMinutes).Replace(code, dailyRoutineEvent.EarliestTime != null ? (dailyRoutineEvent.EarliestTime.Hours * projectConfig.MinutesPerHour + dailyRoutineEvent.EarliestTime.Minutes).ToString() : string.Empty);
            code = ExportUtil.BuildPlaceholderRegex(Placeholder_LatestTime_Hours).Replace(code, dailyRoutineEvent.LatestTime != null ? dailyRoutineEvent.LatestTime.Hours.ToString() : string.Empty);
            code = ExportUtil.BuildPlaceholderRegex(Placeholder_LatestTime_Minutes).Replace(code, dailyRoutineEvent.LatestTime != null ? dailyRoutineEvent.LatestTime.Minutes.ToString() : string.Empty);
            code = ExportUtil.BuildPlaceholderRegex(Placeholder_LatestTime_TotalMinutes).Replace(code, dailyRoutineEvent.LatestTime != null ? (dailyRoutineEvent.LatestTime.Hours * projectConfig.MinutesPerHour + dailyRoutineEvent.LatestTime.Minutes).ToString() : string.Empty);
            code = ExportUtil.BuildPlaceholderRegex(Placeholder_MovementTargetName).Replace(code, dailyRoutineEvent.MovementTarget != null && dailyRoutineEvent.MovementTarget.Name != null ? dailyRoutineEvent.MovementTarget.Name : string.Empty);
            code = ExportUtil.BuildPlaceholderRegex(Placeholder_MovementTargetExportName).Replace(code, dailyRoutineEvent.MovementTarget != null && dailyRoutineEvent.MovementTarget.ExportName != null ? dailyRoutineEvent.MovementTarget.ExportName : string.Empty);
            code = ExportUtil.BuildPlaceholderRegex(Placeholder_MovementTargetExportNameOrName).Replace(code, dailyRoutineEvent.MovementTarget != null && !string.IsNullOrEmpty(dailyRoutineEvent.MovementTarget.ExportName) ? dailyRoutineEvent.MovementTarget.ExportName : (dailyRoutineEvent.MovementTarget != null && dailyRoutineEvent.MovementTarget.Name != null ? dailyRoutineEvent.MovementTarget.Name : string.Empty));
            code = await ExportUtil.BuildPlaceholderRegex(Placeholder_ScriptFunctionName).ReplaceAsync(code, async m => {
                return(await _dailyRoutineFunctionNameGenerator.GetNewDailyRoutineStepFunction(defaultProject.Id, npc.Id, dailyRoutineEvent.EventId));
            });

            code = ExportUtil.BuildPlaceholderRegex(Placeholder_ScriptName).Replace(code, dailyRoutineEvent.ScriptName != null ? dailyRoutineEvent.ScriptName : string.Empty);
            code = ExportUtil.BuildPlaceholderRegex(Placeholder_TargetState).Replace(code, dailyRoutineEvent.TargetState != null ? dailyRoutineEvent.TargetState : string.Empty);

            return(code);
        }
Exemplo n.º 20
0
        /// <summary>
        /// Collects the values for an export
        /// </summary>
        /// <param name="templateType">Template type</param>
        /// <param name="parsedTemplate">Parsed scriban template</param>
        /// <param name="scriptObject">Scriban script object to fill</param>
        /// <param name="data">Export Data</param>
        /// <returns>Task</returns>
        public override async Task CollectValues(TemplateType templateType, Template parsedTemplate, ScriptObject scriptObject, ExportObjectData data)
        {
            IFlexFieldExportable flexFieldObject = data.ExportData[ExportConstants.ExportDataObject] as IFlexFieldExportable;

            if (flexFieldObject == null)
            {
                return;
            }

            List <LanguageKey> referencedLanguageKeys = null;

            if (data.ExportData.ContainsKey(ExportConstants.ExportDataReferencedLanguageIds))
            {
                referencedLanguageKeys = data.ExportData[ExportConstants.ExportDataReferencedLanguageIds] as List <LanguageKey>;
            }

            if (referencedLanguageKeys == null)
            {
                referencedLanguageKeys = new List <LanguageKey>();
            }

            GoNorthProject project = await _exportCachedDbAccess.GetUserProject();

            List <LanguageKey> languageKeys = await _languageKeyDbAccess.GetLanguageKeysByGroupId(project.Id, flexFieldObject.Id);

            ExportSettings exportSettings = await _exportCachedDbAccess.GetExportSettings(project.Id);

            languageKeys = languageKeys.OrderBy(l => GetLanguageKeySortOrder(l, flexFieldObject)).ToList();

            ScribanExportLanguageFile languageFileData = new ScribanExportLanguageFile();

            languageFileData.Object                 = FlexFieldValueCollectorUtil.ConvertScribanFlexFieldObject(flexFieldObject, exportSettings, _errorCollection);
            languageFileData.LanguageKeys           = ConvertLanguageKeysToScriban(languageKeys, exportSettings);
            languageFileData.ReferencedLanguageKeys = ConvertLanguageKeysToScriban(referencedLanguageKeys, exportSettings);

            scriptObject.AddOrUpdate(LanguageObjectKey, languageFileData);
        }
        /// <summary>
        /// Maps the daily routine data of an npc
        /// </summary>
        /// <param name="cachedDbAccess">Gecachter Datenzugriff</param>
        /// <param name="dailyRoutineFunctionNameGenerator">Daily routine function name generator</param>
        /// <param name="npc">Npc</param>
        /// <param name="dailyRoutine">Daily routine data</param>
        /// <returns>Mapped daily routine events</returns>
        public static async Task <List <ScribanExportDailyRoutineEvent> > MapNpcDailyRoutineEvents(IExportCachedDbAccess cachedDbAccess, IDailyRoutineFunctionNameGenerator dailyRoutineFunctionNameGenerator, KortistoNpc npc, List <KortistoNpcDailyRoutineEvent> dailyRoutine)
        {
            if (dailyRoutine == null || !dailyRoutine.Any())
            {
                return(new List <ScribanExportDailyRoutineEvent>());
            }

            GoNorthProject project = await cachedDbAccess.GetUserProject();

            MiscProjectConfig projectConfig = await cachedDbAccess.GetMiscProjectConfig();

            ExportSettings exportSettings = await cachedDbAccess.GetExportSettings(project.Id);

            List <ScribanExportDailyRoutineEvent> mappedEvents = new List <ScribanExportDailyRoutineEvent>();

            foreach (KortistoNpcDailyRoutineEvent curEvent in dailyRoutine)
            {
                ScribanExportDailyRoutineEvent convertedEvent = await ConvertDailyRoutineEvent(dailyRoutineFunctionNameGenerator, npc, curEvent, project, projectConfig, exportSettings);

                mappedEvents.Add(convertedEvent);
            }

            return(mappedEvents);
        }
        /// <summary>
        /// Exports the code snippet additional functions
        /// </summary>
        /// <param name="renderedFunctions">Rendered function cache</param>
        /// <param name="objectExportSnippet">Object export snippet to export</param>
        /// <param name="flexFieldObject">Flex field object</param>
        /// <param name="listIndent">Indent of the code</param>
        /// <returns>Result of the snippet render</returns>
        private async Task <string> ExportCodeSnippetAdditionalFunctions(Dictionary <string, List <ExportSnippetFunction> > renderedFunctions, ObjectExportSnippet objectExportSnippet, FlexFieldObject flexFieldObject, string listIndent)
        {
            if (objectExportSnippet == null)
            {
                return(string.Empty);
            }

            GoNorthProject project = await _cachedDbAccess.GetUserProject();

            ExportTemplate functionContentTemplate = await _defaultTemplateProvider.GetDefaultTemplateByType(project.Id, TemplateType.ObjectExportSnippetFunction);

            List <ExportSnippetFunction> functions = await RenderFunctions(renderedFunctions, objectExportSnippet, flexFieldObject);

            List <ExportSnippetFunction> additionalFunctions = functions.Skip(1).ToList();

            string functionList = string.Empty;

            foreach (ExportSnippetFunction curFunction in additionalFunctions)
            {
                functionList += await RenderExportSnippetFunction(functionContentTemplate, curFunction);
            }

            return(ExportUtil.IndentListTemplate(functionList, listIndent));
        }
        /// <summary>
        /// Renders an daily routine function list
        /// </summary>
        /// <param name="context">Template context</param>
        /// <param name="callerContext">Caller context</param>
        /// <param name="arguments">Arguments to render the list with</param>
        /// <returns>Rendered daily routine function list</returns>
        private async ValueTask <object> RenderDailyRoutineFunctionList(TemplateContext context, ScriptNode callerContext, ScriptArray arguments)
        {
            List <ScribanExportDailyRoutineFunction> functionList = ScribanParsingUtil.GetArrayFromScribanArgument <ScribanExportDailyRoutineFunction>(arguments, 0);

            if (functionList == null)
            {
                _errorCollection.AddInvalidParameter(ScribanErrorUtil.FormatScribanSpan(callerContext.Span));
                return("<<DID NOT PROVIDE VALID FUNCTION LIST>>");
            }

            GoNorthProject curProject = await _exportCachedDbAccess.GetUserProject();

            ExportTemplate functionListTemplate = await _defaultTemplateProvider.GetDefaultTemplateByType(curProject.Id, TemplateType.ObjectDailyRoutineFunctionList);

            ExportObjectData objectData = new ExportObjectData();

            objectData.ExportData.Add(ExportConstants.ExportDataObject, functionList.Select(f => f.ToDailyRoutineFunction()).ToList());

            ExportPlaceholderFillResult fillResult = await _templatePlaceholderResolver.FillPlaceholders(TemplateType.ObjectDailyRoutineFunctionList, functionListTemplate.Code, objectData, functionListTemplate.RenderingEngine);

            _errorCollection.Merge(fillResult.Errors);

            return(ScribanOutputUtil.IndentMultilineCode(context, fillResult.Code));
        }
        /// <summary>
        /// Fills the Flex Field Placeholders
        /// </summary>
        /// <param name="code">Code to fill</param>
        /// <param name="flexFieldObject">Flex Field Object</param>
        /// <param name="objectType">Object Type</param>
        /// <param name="data">Export Data</param>
        /// <returns>Filled Code</returns>
        private async Task <string> FillFlexFieldPlaceholders(string code, IFlexFieldExportable flexFieldObject, string objectType, ExportObjectData data)
        {
            GoNorthProject project = await _exportCachedDbAccess.GetUserProject();

            ExportSettings exportSettings = await _exportCachedDbAccess.GetExportSettings(project.Id);

            HashSet <string> usedFields = new HashSet <string>();

            code = ExportUtil.BuildPlaceholderRegex(BuildFinalPlaceholder(Placeholder_Name)).Replace(code, flexFieldObject.Name);
            code = ExportUtil.RenderPlaceholderIfFuncTrue(code, BuildFinalPlaceholder(Placeholder_Field_Value_Equals_Start), BuildFinalPlaceholder(Placeholder_Field_Value_Equals_End), m => {
                string fieldName = m.Groups[1].Value;
                FlexField field  = FindFlexField(flexFieldObject, fieldName);
                if (field == null)
                {
                    return(false);
                }
                return(m.Groups[2].Value == field.Value);
            });
            code = ExportUtil.RenderPlaceholderIfFuncTrue(code, BuildFinalPlaceholder(Placeholder_Field_Value_NotEquals_Start), BuildFinalPlaceholder(Placeholder_Field_Value_NotEquals_End), m => {
                string fieldName = m.Groups[1].Value;
                FlexField field  = FindFlexField(flexFieldObject, fieldName);
                if (field == null)
                {
                    return(true);
                }
                return(m.Groups[2].Value != field.Value);
            });
            code = ExportUtil.RenderPlaceholderIfFuncTrue(code, BuildFinalPlaceholder(Placeholder_FlexField_HasField_Start), BuildFinalPlaceholder(Placeholder_FlexField_HasField_End), m => {
                string fieldName = m.Groups[1].Value;
                FlexField field  = FindFlexField(flexFieldObject, fieldName);
                return(field != null);
            });
            code = ExportUtil.RenderPlaceholderIfFuncTrue(code, BuildFinalPlaceholder(Placeholder_FlexField_NotHasField_Start), BuildFinalPlaceholder(Placeholder_FlexField_NotHasField_End), m => {
                string fieldName = m.Groups[1].Value;
                FlexField field  = FindFlexField(flexFieldObject, fieldName);
                return(field == null);
            });
            code = ExportUtil.RenderPlaceholderIfFuncTrue(code, BuildFinalPlaceholder(Placeholder_FlexField_FieldIsBlank_Start), BuildFinalPlaceholder(Placeholder_FlexField_FieldIsBlank_End), m => {
                string fieldName = m.Groups[1].Value;
                FlexField field  = FindFlexField(flexFieldObject, fieldName);
                if (field == null)
                {
                    return(true);
                }

                return(string.IsNullOrEmpty(field.Value));
            });
            code = ExportUtil.RenderPlaceholderIfFuncTrue(code, BuildFinalPlaceholder(Placeholder_FlexField_FieldIsNotBlank_Start), BuildFinalPlaceholder(Placeholder_FlexField_FieldIsNotBlank_End), m => {
                string fieldName = m.Groups[1].Value;
                FlexField field  = FindFlexField(flexFieldObject, fieldName);
                if (field == null)
                {
                    return(false);
                }

                return(!string.IsNullOrEmpty(field.Value));
            });
            code = await ExportUtil.BuildPlaceholderRegex(BuildFinalPlaceholder(Placeholder_Name_LangKey)).ReplaceAsync(code, async m => {
                // Run in function to only create language key if required
                string key = await _languageKeyGenerator.GetFlexFieldNameKey(flexFieldObject.Id, flexFieldObject.Name, objectType);
                return(key);
            });

            code = ExportUtil.BuildPlaceholderRegex(BuildFinalPlaceholder(Placeholder_Field_Value)).Replace(code, m => {
                string fieldName = m.Groups[1].Value;
                FlexField field  = FindFlexField(flexFieldObject, fieldName);
                if (field != null)
                {
                    usedFields.Add(field.Id);
                    return(EscapedFieldValue(field, exportSettings));
                }

                _errorCollection.AddErrorFlexField(fieldName, flexFieldObject.Name);
                return("<<" + flexFieldObject.Name + "[" + fieldName + "] MISSING>>");
            });
            code = ExportUtil.BuildPlaceholderRegex(BuildFinalPlaceholder(Placeholder_Field_LangKey)).Replace(code, m => {
                string fieldName = m.Groups[1].Value;
                FlexField field  = FindFlexField(flexFieldObject, fieldName);
                if (field != null && (field.FieldType == ExportConstants.FlexFieldType_String || field.FieldType == ExportConstants.FlexFieldType_Option))
                {
                    usedFields.Add(field.Id);
                    return(BuildFlexFieldLangKey(flexFieldObject, field, objectType));
                }

                _errorCollection.AddErrorFlexField(fieldName, flexFieldObject.Name);
                return("<<" + flexFieldObject.Name + "[" + fieldName + "] MISSING>>");
            });

            code = ExportUtil.RenderPlaceholderIfFuncTrue(code, BuildFinalPlaceholder(Placeholder_HasUnusedFields_Start), BuildFinalPlaceholder(Placeholder_HasUnusedFields_End), m => {
                return(flexFieldObject.Fields.Where(f => !usedFields.Contains(f.Id)).Any());
            });
            code = ExportUtil.BuildRangePlaceholderRegex(BuildFinalPlaceholder(Placeholder_UnusedFields_Start), BuildFinalPlaceholder(Placeholder_UnusedFields_End)).Replace(code, m => {
                List <FlexField> fieldsToUse = flexFieldObject.Fields.Where(f => !usedFields.Contains(f.Id)).ToList();
                return(BuildFlexFieldList(m.Groups[1].Value, fieldsToUse, flexFieldObject, exportSettings, objectType));
            });
            code = ExportUtil.BuildRangePlaceholderRegex(BuildFinalPlaceholder(Placeholder_AllFields_Start), BuildFinalPlaceholder(Placeholder_AllFields_End)).Replace(code, m => {
                return(BuildFlexFieldList(m.Groups[1].Value, flexFieldObject.Fields, flexFieldObject, exportSettings, objectType));
            });
            code = ExportUtil.BuildRangePlaceholderRegex(Placeholder_FlexFieldList_Start, Placeholder_FlexFieldList_End).Replace(code, m => {
                return(BuildFlexFieldList(m.Groups[1].Value, flexFieldObject.Fields, flexFieldObject, exportSettings, objectType));
            });
            code = await ExportUtil.BuildPlaceholderRegex(BuildFinalPlaceholder(Placeholder_UnusedFields), ExportConstants.ListIndentPrefix).ReplaceAsync(code, async m => {
                return(await RenderAttributeList(project, FilterInvalidFlexFields(flexFieldObject.Fields.Where(f => !usedFields.Contains(f.Id)).ToList()), data, m.Groups[1].Value));
            });

            code = await ExportUtil.BuildPlaceholderRegex(BuildFinalPlaceholder(Placeholder_AllFields), ExportConstants.ListIndentPrefix).ReplaceAsync(code, async m => {
                return(await RenderAttributeList(project, FilterInvalidFlexFields(flexFieldObject.Fields), data, m.Groups[1].Value));
            });


            return(code);
        }