Skip to content

Commit c305bb5

Browse files
add translators to PropInfo for translating value objects
1 parent 1a6af19 commit c305bb5

2 files changed

Lines changed: 9 additions & 3 deletions

File tree

Code/ValueSystem/PlayerValue.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,12 @@ public enum PlayerProperty
9595
IsDummy,
9696
}
9797

98-
public class Info<T>(Func<Player, T> handler, string? description)
99-
: PropInfo<Player, T>(handler, description) where T : Value;
98+
public class Info<T>(Func<Player, T> handler, string? description)
99+
: PropInfo<Player, T>(handler, description) where T : Value
100+
{
101+
public override Func<object, object>? Translator { get; } =
102+
obj => obj is PlayerValue { Players.Length: 1 } val ? val.Players[0] : obj;
103+
}
100104

101105
public static readonly Dictionary<PlayerProperty, PropInfo> PropertyInfoMap = new()
102106
{

Code/ValueSystem/Value.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,11 @@ public class PropInfo<TIn, TOut>(Func<TIn, TOut> handler, string? description) :
6161
where TOut : Value
6262
{
6363
public override Func<TIn, Value> Func => handler;
64-
64+
public virtual Func<object, object>? Translator { get; } = null;
65+
6566
public override TryGet<Value> GetValue(object obj)
6667
{
68+
if (Translator is not null) obj = Translator(obj);
6769
if (obj is not TIn inObj) return $"Provided value is not of type {typeof(TIn).AccurateName}";
6870
return handler(inObj);
6971
}

0 commit comments

Comments
 (0)