Are you sure you want to report the post shown below? This will send an email to the ATVO administrators. Please include a short reason for reporting.
Users reporting for no reason may be locked out.
using System;
using ATVO.ThemesSDK;
using ATVO.ThemeEditor.ThemeModels;
using ATVO.ThemeEditor.Scripting.DotNET;
using ATVO.ThemesSDK.Data.Results;
using System.Collections.Generic;
using System.Linq;
namespace Scripts
{
public class AverageLaptime : IScript
{
public object Execute(ThemeContentItem item, object value, string parameter, ISimulation sim)
{
var result = (IEntitySessionResult)value;
var lastLapIndex = result.Laps.Count - 1;
var laptimes = new List<float>();
// Loop backwards 4 laps starting from the most recent lap
for (int i = lastLapIndex; i <= lastLapIndex - 4; i--)
{
// Stop if we reached the first lap
if (i < 0)
break;
var lap = result.Laps[i];
if (lap.Time > 0) // invalid laps have -1 laptime
{
laptimes.Add(lap.Time);
}
}
// Calculate the average over the valid laps
var averageLaptime = laptimes.Average();
return averageLaptime.ToString();
}
}
}
var averageLaptime = result.Laps // Laps list of this result
.Select(l => l.Time) // Select the laptime
.Reverse() // Reverse so we start at the last lap
.Where(t => t > 0) // Take only valid laps where the time > 0
.Take(4) // Take up to 4 laps
.Average(); // Average
return averageLaptime.ToString();