Files
crtr/Assets/Cryville/Crtr/JudgeAction.cs
2023-05-20 16:31:23 +08:00

52 lines
1.8 KiB
C#

using Cryville.Common;
using Cryville.Common.Pdt;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Cryville.Crtr {
public abstract class JudgeAction {
public static JudgeAction Construct(HashSet<string> a, string k) {
if (a.Remove("pass")) {
return new Pass(a, from i in k.Split(',') select new Identifier(i.Trim()));
}
else if (a.Remove("score")) {
return new Score(a, k);
}
throw new FormatException("Invalid judge action format.");
}
public readonly HashSet<string> annotations;
public JudgeAction(IEnumerable<string> a) {
annotations = a.ToHashSet();
}
public virtual void Optimize(PdtEvaluatorBase etor, PdtExpression value) { etor.Optimize(value); }
public abstract bool Execute(IJudge judge, JudgeEvent ev, float time, PdtExpression exp, int depth);
public class Pass : JudgeAction {
readonly Identifier[] _targets;
public Pass(IEnumerable<string> a, IEnumerable<Identifier> k) : base(a) {
_targets = k.ToArray();
}
public override bool Execute(IJudge judge, JudgeEvent ev, float time, PdtExpression exp, int depth) {
return judge.Pass(ev, time, _targets, depth);
}
}
public class Score : JudgeAction {
readonly ScoreOperation _op;
public Score(IEnumerable<string> a, string k) : base(a) {
_op = new ScoreOperation(k);
}
public Score(ScoreOperation op) : base(Enumerable.Empty<string>()) {
_op = op;
}
public override void Optimize(PdtEvaluatorBase etor, PdtExpression value) {
base.Optimize(etor, value);
if (_op.op != default(Identifier)) PdtExpression.PatchCompound(_op.name.Key, _op.op.Key, value);
}
public override bool Execute(IJudge judge, JudgeEvent ev, float time, PdtExpression exp, int depth) {
judge.UpdateScore(_op, exp);
return false;
}
}
}
}