Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REG-2230, REG-2231] Implement Validations Within Segments #370

Merged
merged 18 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using RegressionGames.StateRecorder;
using RegressionGames.StateRecorder.BotSegments;
using RegressionGames.StateRecorder.BotSegments.Models;
using StateRecorder.BotSegments.Models;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
Expand All @@ -30,7 +31,7 @@ public class RGSegmentEntry : MonoBehaviour
* <summary>Indicates whether the Segment or Segment List is overridden by a local file.</summary>
*/
public bool isOverride;

/**
* UI component fields
*/
Expand All @@ -48,7 +49,7 @@ public class RGSegmentEntry : MonoBehaviour

[SerializeField]
public GameObject segmentListIndicatorComponent;

[SerializeField]
public GameObject overrideIndicator;

Expand Down Expand Up @@ -83,8 +84,8 @@ public void Start()
resourcePathComponent.gameObject.SetActive(false);
}
}
// set indicator that this Segment is being overriden by a local file, within a build

// set indicator that this Segment is being overriden by a local file, within a build
overrideIndicator.gameObject.SetActive(isOverride);

// assign values to the UI components
Expand Down Expand Up @@ -140,7 +141,8 @@ private void OnPlay()
}

// play the segment
playbackController.SetDataContainer(new BotSegmentsPlaybackContainer(segmentList.segments, sessionId));
playbackController.SetDataContainer(new BotSegmentsPlaybackContainer(new List<BotSegmentList>() {segmentList}, new List<SegmentValidation>(), sessionId));

playbackController.Play();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ namespace RegressionGames.StateRecorder.BotSegments
{
public static class AndKeyFrameCriteriaEvaluator
{
public static bool Matched(bool firstSegment, int segmentNumber, bool botActionCompleted, KeyFrameCriteria criteria)
public static bool Matched(bool firstSegment, int segmentNumber, bool botActionCompleted, bool validationsCompleted, KeyFrameCriteria criteria)
{
if (criteria.data is AndKeyFrameCriteriaData { criteriaList: not null } andCriteria)
{
try
{
return KeyFrameEvaluator.Evaluator.MatchedHelper(firstSegment, segmentNumber, botActionCompleted, BooleanCriteria.And, andCriteria.criteriaList);
return KeyFrameEvaluator.Evaluator.MatchedHelper(firstSegment, segmentNumber, botActionCompleted, validationsCompleted, BooleanCriteria.And, andCriteria.criteriaList);
}
catch (Exception)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
using System.IO.Compression;
using System.Linq;
using Newtonsoft.Json;
using RegressionGames.StateRecorder.BotSegments.JsonConverters;
using RegressionGames.StateRecorder.BotSegments.Models;
using StateRecorder.BotSegments;

namespace RegressionGames.StateRecorder.BotSegments
{
Expand Down Expand Up @@ -66,9 +64,9 @@ public static IOrderedEnumerable<ZipArchiveEntry> OrderZipJsonEntries(IEnumerabl
return entries;
}

public static List<BotSegment> ParseBotSegmentZipFromSystemPath(string zipFilePath, out string sessionId)
public static List<BotSegmentList> ParseBotSegmentZipFromSystemPath(string zipFilePath, out string sessionId)
{
List<BotSegment> results = new();
List<BotSegmentList> results = new();

sessionId = null;

Expand Down Expand Up @@ -100,17 +98,7 @@ public static List<BotSegment> ParseBotSegmentZipFromSystemPath(string zipFilePa
break;
}

foreach (var botSegment in botSegmentList.segments)
{
botSegment.Replay_SegmentNumber = replayNumber++;

if (sessionId == null)
{
sessionId = botSegment.sessionId;
}

results.Add(botSegment);
}
results.Add(botSegmentList);
}
catch (Exception)
{
Expand All @@ -132,7 +120,16 @@ public static List<BotSegment> ParseBotSegmentZipFromSystemPath(string zipFilePa
sessionId = frameData.sessionId;
}

results.Add(frameData);
results.Add(new BotSegmentList()
{
segments = new List<BotSegment>()
{
frameData
},
name = "BotSegmentList for BotSegment - " + frameData.name,
description = "BotSegmentList for BotSegment - " + frameData.description,
validations = new()
});
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,56 +1,130 @@
using System;
using System.Collections.Generic;
using System.Linq;
using RegressionGames.StateRecorder.BotSegments.Models;
using StateRecorder.BotSegments.Models;
using StateRecorder.BotSegments.Models.SegmentValidations;

namespace RegressionGames.StateRecorder.BotSegments
{

public class BotSegmentsPlaybackContainer
{
private readonly List<BotSegment> _botSegments;

private readonly List<BotSegmentList> _botSegmentLists;
private int _botSegmentListIndex = 0;
private int _botSegmentIndex = 0;

/**
* A top-level set of validations to run for an entire sequence of segments
*/
public readonly List<SegmentValidation> Validations;

public readonly string SessionId;

public BotSegmentsPlaybackContainer(IEnumerable<BotSegment> segments, string sessionId = null)
public BotSegmentsPlaybackContainer(IEnumerable<BotSegmentList> segmentLists, IEnumerable<SegmentValidation> validations, string sessionId = null)
{
var replayNumber = 1; // 1 to align with the actual numbers in the recording
_botSegments = new(segments);
_botSegments.ForEach(a => a.Replay_SegmentNumber = replayNumber++);
_botSegmentLists = new(segmentLists);
_botSegmentLists.ForEach(a => a.segments.ForEach(b => b.Replay_SegmentNumber = replayNumber++));
Validations = new(validations);
this.SessionId = sessionId ?? Guid.NewGuid().ToString("n");
}

public void Reset()
{
// sets indexes back to 0
_botSegmentIndex = 0;
_botSegmentListIndex = 0;

// reset all the tracking flags
foreach (var botSegment in _botSegments)
// reset all the tracking flags in the segmentlists / segments
_botSegmentLists.ForEach(a =>
{
botSegment.ReplayReset();
a.segments.ForEach(b => b.ReplayReset()); // This also handles resetting that segments validations
a.validations.ForEach(b => b.ReplayReset());
});

// reset all the top-level sequence validations
foreach (var validation in Validations)
{
validation.ReplayReset();
}

}

public BotSegment DequeueBotSegment()
/**
* Returns the next bot segment and validations to evaluate and also provides the current segmentList level validations
*/
public (BotSegment, List<SegmentValidation>)? DequeueBotSegment()
{
if (_botSegmentIndex < _botSegments.Count)
while (_botSegmentListIndex < _botSegmentLists.Count)
{
return _botSegments[_botSegmentIndex++];
}
var segmentList = _botSegmentLists[_botSegmentListIndex];
if (_botSegmentIndex < segmentList.segments.Count)
{
var segment = segmentList.segments[_botSegmentIndex++];
var segmentListValidations = segmentList.validations;
return (segment, segmentListValidations);
}
else
{
// move to the next segmentlist starting on the 0th segment in that list
_botSegmentIndex = 0;
++_botSegmentListIndex;
}

}

return null;
}

public BotSegment PeekBotSegment()


/**
* <summary>Collects all of the results from the top-level validations and individual bot segments</summary>
*/
public List<SegmentValidationResultSetContainer> GetAllValidationResults()
{
if (_botSegmentIndex < _botSegments.Count)

// First add all the top level results
var results = Validations.Select(validation => validation.data.GetResults()).ToList();

// Then add the validations from bot segment lists and individual bot segment results
foreach (var botSegmentList in _botSegmentLists)
{
// do not update index
return _botSegments[_botSegmentIndex];
results.AddRange(botSegmentList.validations.Select(v => v.data.GetResults()));

foreach (var botSegment in botSegmentList.segments)
{
results.AddRange(botSegment.validations.Select(v => v.data.GetResults()));
}
}

return null;
return results;
}

/**
* <summary>
* This will request to stop all validations in the container, including sequence validations, bot
* segment list validations, and individual bot segment validations.
* </summary>
*/
public void StopAllValidations(int segmentNumber)
{
// First stop the sequence validations
foreach (var validation in Validations)
{
validation.StopValidation(segmentNumber);
}

// Then stop the segment list validations and bot segment validations
foreach (var botSegmentList in _botSegmentLists)
{
botSegmentList.validations.ForEach(v => v.StopValidation(segmentNumber));
foreach (var botSegment in botSegmentList.segments)
{
botSegment.validations.ForEach(v => v.StopValidation(segmentNumber));
}
}
}

}
}
Loading
Loading