qstbak/Questionable/Questionable.Validation.Validators/DialogueChoiceValidator.cs
2025-10-09 07:47:19 +10:00

80 lines
2.2 KiB
C#

using System.Collections.Generic;
using Questionable.Functions;
using Questionable.Model;
using Questionable.Model.Questing;
namespace Questionable.Validation.Validators;
internal sealed class DialogueChoiceValidator : IQuestValidator
{
private readonly ExcelFunctions _excelFunctions;
public DialogueChoiceValidator(ExcelFunctions excelFunctions)
{
_excelFunctions = excelFunctions;
}
public IEnumerable<ValidationIssue> Validate(Quest quest)
{
foreach (var x in quest.AllSteps())
{
if (x.Step.DialogueChoices.Count == 0)
{
continue;
}
foreach (DialogueChoice dialogueChoice in x.Step.DialogueChoices)
{
ExcelRef prompt = dialogueChoice.Prompt;
if (prompt != null)
{
ValidationIssue validationIssue = Validate(quest, x.Sequence, x.StepId, dialogueChoice.ExcelSheet, prompt, "Prompt");
if (validationIssue != null)
{
yield return validationIssue;
}
}
ExcelRef answer = dialogueChoice.Answer;
if (answer != null)
{
ValidationIssue validationIssue2 = Validate(quest, x.Sequence, x.StepId, dialogueChoice.ExcelSheet, answer, "Answer");
if (validationIssue2 != null)
{
yield return validationIssue2;
}
}
}
}
}
private ValidationIssue? Validate(Quest quest, QuestSequence sequence, int stepId, string? excelSheet, ExcelRef excelRef, string label)
{
if (excelRef.Type == ExcelRef.EType.Key)
{
if (!_excelFunctions.GetRawDialogueText(quest, excelSheet, excelRef.AsKey()).HasValue)
{
return new ValidationIssue
{
ElementId = quest.Id,
Sequence = sequence.Sequence,
Step = stepId,
Type = EIssueType.InvalidExcelRef,
Severity = EIssueSeverity.Error,
Description = $"{label} invalid: {excelSheet} → {excelRef.AsKey()}"
};
}
}
else if (excelRef.Type == ExcelRef.EType.RowId && !_excelFunctions.GetRawDialogueTextByRowId(excelSheet, excelRef.AsRowId()).HasValue)
{
return new ValidationIssue
{
ElementId = quest.Id,
Sequence = sequence.Sequence,
Step = stepId,
Type = EIssueType.InvalidExcelRef,
Severity = EIssueSeverity.Error,
Description = $"{label} invalid: {excelSheet} → {excelRef.AsRowId()}"
};
}
return null;
}
}