Skip to content

Commit 0bfe5ad

Browse files
make SER more friendly for AI
1 parent 04f3634 commit 0bfe5ad

49 files changed

Lines changed: 1413 additions & 526 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Code/ArgumentSystem/Arguments/FloatArgument.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using SER.Code.TokenSystem.Tokens;
66
using SER.Code.TokenSystem.Tokens.ValueTokens;
77
using SER.Code.ValueSystem;
8-
using Random = UnityEngine.Random;
98

109
namespace SER.Code.ArgumentSystem.Arguments;
1110

@@ -33,7 +32,7 @@ public override string InputDescription
3332
if (_minValue.HasValue && _maxValue.HasValue)
3433
{
3534
return $"A number which is at least {_minValue} and most {_maxValue} e.g. " +
36-
$"{Math.Round(Random.Range(_minValue.Value, _maxValue.Value), 2)}";
35+
$"{Math.Round((double)new Random().Next((int)_minValue.Value, (int)_maxValue.Value + 1))}";
3736
}
3837

3938
if (_minValue.HasValue)
@@ -44,7 +43,7 @@ public override string InputDescription
4443
// ReSharper disable once ConvertIfStatementToReturnStatement
4544
if (_maxValue.HasValue)
4645
{
47-
return $"A number which is at most {_maxValue} e.g. {_maxValue - 2f}";
46+
return $"A number which is at most {_maxValue} e.g. {_minValue + 2f}";
4847
}
4948

5049
return "Any number e.g. 2.5";

Code/ArgumentSystem/ProvidedArguments.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using LabApi.Features.Wrappers;
2+
using Respawning.Waves;
23
using SER.Code.ArgumentSystem.Arguments;
34
using SER.Code.ArgumentSystem.BaseArguments;
45
using SER.Code.ArgumentSystem.Structures;
@@ -233,6 +234,11 @@ public Type GetEffectType(string argName)
233234
return GetValue<Type, EffectTypeArgument>(argName);
234235
}
235236

237+
public SpawnableWaveBase GetWaveType(string argName)
238+
{
239+
return GetValue<SpawnableWaveBase, WaveTypeArgument>(argName);
240+
}
241+
236242
public CollectionVariable GetCollectionVariable(string argName)
237243
{
238244
return GetValue<CollectionVariable, CollectionVariableArgument>(argName);

Code/ContextSystem/BaseContexts/LoopContext.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ public abstract class LoopContext : StatementContext, IExtendableStatement, IKey
1515

1616
protected abstract string? Usage { get; }
1717

18-
public string Example =>
19-
$"""
20-
{Usage}
21-
22-
# ========================================
23-
# "break" and "continue" keywords work as usual and you are free to use them inside "{KeywordName}" loops
24-
""";
18+
public string Example => SER.Code.Examples.Example.GetExample($"{KeywordName}KeywordExample") ??
19+
$"""
20+
{Usage}
21+
22+
# ========================================
23+
# "break" and "continue" keywords work as usual and you are free to use them inside "{KeywordName}" loops
24+
""";
2525

2626
protected bool ReceivedContinue;
2727
protected bool ReceivedBreak;

Code/ContextSystem/Contexter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ private static Result TryAddResult(
5353
Stack<StatementContext> statementStack,
5454
List<RunnableContext> contexts
5555
) {
56-
Result rs = $"Invalid context {context}";
56+
Result rs = $"Invalid {context}";
5757

5858
Log.Debug($"Trying to add context {context}");
5959

Code/ContextSystem/Contexts/Control/WaitKeyword.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ public class WaitKeyword : YieldingContext, IKeywordContext
2121

2222
public virtual string[] Arguments => ["<duration>"];
2323

24-
public virtual string? Example =>
25-
"""
26-
# wait for 5 seconds
27-
wait 5s
28-
29-
# Waits using a variable
30-
$duration = 10s
31-
wait $duration
32-
""";
24+
public virtual string? Example => SER.Code.Examples.Example.GetExample($"{KeywordName}KeywordExample") ??
25+
"""
26+
# wait for 5 seconds
27+
wait 5s
28+
29+
# Waits using a variable
30+
$duration = 10s
31+
wait $duration
32+
""";
3333

3434
public override string FriendlyName => $"'{KeywordName}' keyword";
3535

Code/ContextSystem/Contexts/Control/WaitUntilKeyword.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ public class WaitUntilKeyword : YieldingContext, IKeywordContext
2020

2121
public virtual string[] Arguments => ["<condition...>"];
2222

23-
public virtual string? Example =>
24-
"""
25-
# wait until there are no players on the server
26-
wait_until {AmountOf @all} is 0
27-
""";
23+
public virtual string? Example => SER.Code.Examples.Example.GetExample($"{KeywordName}KeywordExample") ??
24+
"""
25+
# wait until there are no players on the server
26+
wait_until {AmountOf @all} is 0
27+
""";
2828

2929
public override string FriendlyName => $"'{KeywordName}' keyword";
3030

Code/Examples/BreachScript.cs

Lines changed: 0 additions & 25 deletions
This file was deleted.

Code/Examples/DiscordServerInfoScript.cs

Lines changed: 0 additions & 54 deletions
This file was deleted.

Code/Examples/DoorRestartScript.cs

Lines changed: 0 additions & 39 deletions
This file was deleted.

Code/Examples/Example.cs

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
using System.Reflection;
1+
using System.Collections.Generic;
2+
using System.IO;
3+
using System.Linq;
4+
using System.Reflection;
25
using JetBrains.Annotations;
36
using SER.Code.ContextSystem.Interfaces;
47
using SER.Code.Helpers.ResultSystem;
@@ -11,35 +14,48 @@ public abstract class Example
1114
public abstract string Name { get; }
1215
public abstract string Content { get; }
1316

17+
private static Dictionary<string, string>? _cachedExamples;
18+
1419
[UsedImplicitly]
15-
public static string Verify()
20+
public static Dictionary<string, string> GetAllExamples()
1621
{
17-
var examples = Assembly.GetExecutingAssembly().GetTypes()
18-
.Where(t => !t.IsAbstract && typeof(Example).IsAssignableFrom(t))
19-
.Select(Activator.CreateInstance)
20-
.Cast<Example>()
21-
.ToArray();
22+
if (_cachedExamples != null) return _cachedExamples;
23+
24+
var assembly = Assembly.GetExecutingAssembly();
25+
var resourceNames = assembly.GetManifestResourceNames()
26+
.Where(n => n.EndsWith(".ser"));
2227

23-
foreach (var example in examples)
28+
var examples = new Dictionary<string, string>();
29+
foreach (var name in resourceNames)
2430
{
25-
if (Script.CreateAnonymous(example.Name, example.Content).Compile().HasErrored(out var error))
26-
{
27-
return new Result(false, $"in example '{example.Name}'") + error;
28-
}
31+
using var stream = assembly.GetManifestResourceStream(name);
32+
if (stream == null) continue;
33+
using var reader = new StreamReader(stream);
34+
string content = reader.ReadToEnd();
35+
string[] parts = name.Split('.');
36+
if (parts.Length < 2) continue;
37+
string fileName = parts[parts.Length - 2];
38+
examples[fileName] = content;
2939
}
3040

31-
var keywords = Assembly.GetExecutingAssembly().GetTypes()
32-
.Where(t => !t.IsAbstract && typeof(IKeywordContext).IsAssignableFrom(t))
33-
.Select(Activator.CreateInstance)
34-
.Cast<IKeywordContext>()
35-
.Where(k => k.Example != null)
36-
.ToArray();
37-
38-
foreach (var keyword in keywords)
41+
return _cachedExamples = examples;
42+
}
43+
44+
public static string? GetExample(string name)
45+
{
46+
return GetAllExamples().TryGetValue(name, out var content) ? content : null;
47+
}
48+
49+
[UsedImplicitly]
50+
public static string Verify()
51+
{
52+
var examples = GetAllExamples();
53+
54+
foreach (var example in examples)
3955
{
40-
if (Script.CreateAnonymous(keyword.KeywordName, keyword.Example!).Compile().HasErrored(out var error))
56+
if (Script.CreateAnonymous(example.Key, example.Value).Compile().HasErrored(out var error))
4157
{
42-
return new Result(false, $"in example of keyword '{keyword.KeywordName}'") + error;
58+
return new Result(false, $"in example '{example.Key}'") + error;
4359
}
4460
}
4561

0 commit comments

Comments
 (0)