-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #187 from SSchulze1989/feature/point-formula
Feature/point formula
- Loading branch information
Showing
23 changed files
with
361 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/iRLeagueApiCore.Services/ResultService/Calculation/FormulaParameters.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using iRLeagueApiCore.Services.ResultService.Models; | ||
|
||
namespace iRLeagueApiCore.Services.ResultService.Calculation; | ||
internal record FormulaParameter(string[] Aliases, string Description, Func<SessionCalculationData, ResultRowCalculationData, object> valueFunc); | ||
|
||
public static class FormulaParameters | ||
{ | ||
internal static IEnumerable<FormulaParameter> Parameters { get; } = new List<FormulaParameter>() | ||
{ | ||
new(["pos", "position"], "Finish position", (_, row) => row.FinishPosition), | ||
new(["start", "start_position"], "Starting position", (_, row) => row.StartPosition), | ||
new(["irating"], "Irating at the start of the session", (_, row) => row.OldIrating), | ||
new(["sof", "strength_of_field"], "SOF - Strength of field (Irating)", (session, _) => session.Sof), | ||
new(["count", "driver_count"], "Number of drivers/teams in the result", (session, _) => session.ResultRows.Count()), | ||
new(["flap", "fastest_lap"], "Personal fastest lap", (_, row) => row.FastestLapTime.TotalSeconds), | ||
new(["qlap", "qualy_lap"], "Personal qualy lap", (_, row) => row.QualifyingTime.TotalSeconds), | ||
new(["avglap", "avg_lap"], "Personal avg. lap", (_, row) => row.AvgLapTime.TotalSeconds), | ||
new(["flapsession", "session_fastest_lap"], "Fastest lap in the session", (session, _) => session.FastestLap.TotalSeconds), | ||
new(["qlapsession", "session_fastest_qualy_lap"], "Fastest qaly lap in the session", (session, _) => session.FastestQualyLap.TotalSeconds), | ||
new(["avglapsession", "session_fastest_avg_lap"], "Fastest avg. lap in the session", (session, _) => session.FastestAvgLap.TotalSeconds), | ||
}; | ||
|
||
internal static IDictionary<string, FormulaParameter> ParameterDict => Parameters | ||
.SelectMany(x => x.Aliases.Select(y => (name: y, parameter: x))) | ||
.ToDictionary(k => k.name, v => v.parameter); | ||
|
||
public static IEnumerable<(string[] aliases, string description)> ParameterInfo => Parameters.Select(x => (x.Aliases, x.Description)); | ||
} |
43 changes: 43 additions & 0 deletions
43
src/iRLeagueApiCore.Services/ResultService/Calculation/FormulaPointRule.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
| ||
using iRLeagueApiCore.Services.ResultService.Models; | ||
using MySqlX.XDevAPI.Relational; | ||
using NCalc; | ||
|
||
namespace iRLeagueApiCore.Services.ResultService.Calculation; | ||
internal class FormulaPointRule : CalculationPointRuleBase | ||
{ | ||
public string Formula { get; } | ||
public bool AllowNegativePoints { get; } | ||
private static IDictionary<string, FormulaParameter> _parameters = FormulaParameters.ParameterDict; | ||
|
||
public FormulaPointRule(string formula, bool allowNegativePoints) | ||
{ | ||
Formula = formula; | ||
AllowNegativePoints = allowNegativePoints; | ||
} | ||
|
||
public override IReadOnlyList<T> ApplyPoints<T>(SessionCalculationData session, IReadOnlyList<T> rows) | ||
{ | ||
// prepare parameters | ||
var e = new NCalc.Expression(Formula, EvaluateOptions.IterateParameters); | ||
foreach (var parameter in _parameters) | ||
{ | ||
e.Parameters[parameter.Key] = rows.Select(row => parameter.Value.valueFunc.Invoke(session, row)).ToArray(); | ||
} | ||
// calculate | ||
if (e.Evaluate() is not IList<object> points) | ||
{ | ||
return rows; | ||
} | ||
// assign points to rows | ||
foreach (var (row, rowPoints) in rows.Zip(points)) | ||
{ | ||
row.RacePoints = Convert.ToDouble(rowPoints); | ||
if (!AllowNegativePoints) | ||
{ | ||
row.RacePoints = Math.Max(row.RacePoints, 0); | ||
} | ||
} | ||
return rows; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 4 additions & 2 deletions
6
src/iRLeagueApiCore.Services/ResultService/Calculation/UseResultPointsPointRule.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.