using System;
using Amphora;
using System.Collections.Generic;
namespace Ariadne.UnitTests
{
///
/// Waits a second, then two more, logging between.
///
public partial class WaitingScript : Arc
{
[Scratch]
public readonly List Log = new List();
protected override void Run(ArcTests.Gates gates)
{
Log.Add("start");
first:
Wait(TimeSpan.FromSeconds(1));
Log.Add("after one second");
second:
Wait(TimeSpan.FromSeconds(2));
Log.Add("end");
}
}
///
/// Waits for no time at all, then for a negative time.
///
public partial class NoTimeWaitScript : Arc
{
[Scratch]
public readonly List Log = new List();
protected override void Run(ArcTests.Gates gates)
{
none:
Wait(TimeSpan.Zero);
Log.Add("through zero");
behind:
Wait(TimeSpan.FromSeconds(-5));
Log.Add("through negative");
}
}
///
/// Waits once each trip round a loop, with a local counting the trips.
///
public partial class LoopWaitScript : Arc
{
[Scratch]
public readonly List Log = new List();
protected override void Run(ArcTests.Gates gates)
{
for (int trip = 0; trip < 3; trip++)
{
rest:
Wait(TimeSpan.FromSeconds(1));
Log.Add("trip " + trip);
}
}
}
///
/// Waits for a span held in a local, which is only read on the way in.
///
public partial class SpanLocalScript : Arc
{
[Scratch]
public readonly List Log = new List();
protected override void Run(ArcTests.Gates gates)
{
TimeSpan span = TimeSpan.FromSeconds(1.5);
rest:
Wait(span);
Log.Add("rested");
}
}
///
/// Waits only on one side of an if, and in a nested block.
///
public partial class BranchWaitScript : Arc
{
[Scratch]
public readonly List Log = new List();
protected override void Run(ArcTests.Gates gates)
{
Log.Add("start");
if (gates.Open.Contains("slow"))
{
slow:
Wait(TimeSpan.FromSeconds(1));
Log.Add("slow");
}
else
{
Log.Add("quick");
}
Log.Add("end");
}
}
///
/// Waits for as long as a passage that pauses says.
///
public partial class AskWaitScript : Arc
{
[Scratch]
public readonly List Log = new List();
private TimeSpan Ask(ArcTests.Gates gates)
{
ask:
while (!gates.Open.Contains("answer"))
{
Yield();
}
return TimeSpan.FromSeconds(2);
}
protected override void Run(ArcTests.Gates gates)
{
first:
Wait(Ask(gates));
Log.Add("done");
}
}
///
/// Waits for the total of two answers and a local read after both.
///
public partial class AskSumWaitScript : Arc
{
[Scratch]
public readonly List Log = new List();
private TimeSpan Ask(ArcTests.Gates gates, string gate)
{
ask:
while (!gates.Open.Contains(gate))
{
Yield();
}
return TimeSpan.FromSeconds(1);
}
protected override void Run(ArcTests.Gates gates)
{
TimeSpan extra = TimeSpan.FromSeconds(0.5);
first:
Wait(Ask(gates, "a") + Ask(gates, "b") + extra);
Log.Add("done");
}
}
///
/// Jumps back to a step that waits.
///
public partial class JumpWaitScript : Arc
{
[Scratch]
public readonly List Log = new List();
protected override void Run(ArcTests.Gates gates)
{
int laps = 0;
again:
Wait(TimeSpan.FromSeconds(1));
laps++;
Log.Add("lap " + laps);
if (laps < 2)
{
goto again;
}
}
}
///
/// Waits for longer than the game time can count.
///
public partial class ForeverWaitScript : Arc
{
[Scratch]
public readonly List Log = new List();
protected override void Run(ArcTests.Gates gates)
{
forever:
Wait(TimeSpan.MaxValue);
Log.Add("never");
}
}
///
/// Waits for a span it is given, then logs.
///
public partial class SpanWaitScript : Arc
{
[Scratch]
public readonly List Log = new List();
public Duration Span;
protected override void Run(ArcTests.Gates gates)
{
rest:
Wait(Span);
Log.Add("woke");
}
}
///
/// Offers a passage for other arcs to run, which waits a second.
///
public partial class WaitWorkerScript : Arc
{
[Scratch]
public readonly List Log = new List();
public void Pace(ArcTests.Gates gates)
{
rest:
Wait(TimeSpan.FromSeconds(1));
Log.Add("rested");
}
protected override void Run(ArcTests.Gates gates)
{
}
}
///
/// Runs the passage of another arc that waits, which is never ticked itself.
///
public partial class WaitClientScript : Arc
{
public WaitWorkerScript Worker = new WaitWorkerScript();
protected override void Run(ArcTests.Gates gates)
{
pace:
Worker.Pace(gates);
}
}
}