redone turtle cli
This commit is contained in:
parent
e04021425a
commit
36f97f09d5
@ -1,62 +0,0 @@
|
||||
package bin;
|
||||
|
||||
import lib.turtle.InvManager;
|
||||
import lib.CLIAppBase;
|
||||
|
||||
using tink.CoreApi;
|
||||
|
||||
@:build(macros.Binstore.includeBin("Turtle", ["turtle", "t"]))
|
||||
class Turtle extends CLIAppBase {
|
||||
public function new() {
|
||||
registerSyncSubcommand("forward", (args) -> {
|
||||
return checkAvailable() && perform(kernel.turtle.Turtle.forward());
|
||||
});
|
||||
|
||||
registerSyncSubcommand("back", (args) -> {
|
||||
return checkAvailable() && perform(kernel.turtle.Turtle.back());
|
||||
});
|
||||
|
||||
registerSyncSubcommand("left", (args) -> {
|
||||
return checkAvailable() && perform(kernel.turtle.Turtle.turnLeft());
|
||||
});
|
||||
|
||||
registerSyncSubcommand("right", (args) -> {
|
||||
return checkAvailable() && perform(kernel.turtle.Turtle.turnRight());
|
||||
});
|
||||
|
||||
registerSyncSubcommand("up", (args) -> {
|
||||
return checkAvailable() && perform(kernel.turtle.Turtle.up());
|
||||
});
|
||||
|
||||
registerSyncSubcommand("down", (args) -> {
|
||||
return checkAvailable() && perform(kernel.turtle.Turtle.down());
|
||||
});
|
||||
|
||||
registerSyncSubcommand("defrag", (args) -> {
|
||||
if (!checkAvailable()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
InvManager.defrag();
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
private function checkAvailable():Bool {
|
||||
if (!kernel.turtle.Turtle.isTurtle()) {
|
||||
handle.write("This is not a turtle!");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private function perform(outcome:Outcome<Noise, String>):Bool {
|
||||
switch outcome {
|
||||
case Success(_):
|
||||
return true;
|
||||
case Failure(error):
|
||||
handle.write(error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
103
src/bin/TurtleCtl.hx
Normal file
103
src/bin/TurtleCtl.hx
Normal file
@ -0,0 +1,103 @@
|
||||
package bin;
|
||||
|
||||
import kernel.turtle.TurtleMutex;
|
||||
import kernel.turtle.Turtle;
|
||||
import lib.turtle.InvManager;
|
||||
import lib.CLIAppBase;
|
||||
|
||||
using tink.CoreApi;
|
||||
|
||||
@:build(macros.Binstore.includeBin("Turtle", ["turtle", "t"]))
|
||||
class TurtleCtl extends CLIAppBase {
|
||||
public function new() {
|
||||
registerAsyncSubcommand("f", (args) -> {
|
||||
return asynPerform(Turtle.forward, parseTimes(args));
|
||||
}, "<times>");
|
||||
|
||||
registerAsyncSubcommand("b", (args) -> {
|
||||
return asynPerform(Turtle.back, parseTimes(args));
|
||||
}, "<times>");
|
||||
|
||||
registerAsyncSubcommand("l", (args) -> {
|
||||
return asynPerform(Turtle.turnLeft, parseTimes(args));
|
||||
}, "<times>");
|
||||
|
||||
registerAsyncSubcommand("r", (args) -> {
|
||||
return asynPerform(Turtle.turnRight, parseTimes(args));
|
||||
}, "<times>");
|
||||
|
||||
registerAsyncSubcommand("u", (args) -> {
|
||||
return asynPerform(Turtle.up, parseTimes(args));
|
||||
}, "<times>");
|
||||
|
||||
registerAsyncSubcommand("d", (args) -> {
|
||||
return asynPerform(Turtle.down, parseTimes(args));
|
||||
}, "<times>");
|
||||
|
||||
registerAsyncSubcommand("defrag", (args) -> {
|
||||
return asynPerform(() -> {
|
||||
// TODO: when defrag can fail return that error
|
||||
InvManager.defrag();
|
||||
return Success(null);
|
||||
}, 1);
|
||||
});
|
||||
|
||||
registerSyncSubcommand("fuel", (args) -> {
|
||||
var lvl = Turtle.getFuelLevel();
|
||||
var limit = Turtle.getFuelLimit();
|
||||
handle.writeLine('${lvl}/${limit} (${(lvl / limit) * 100}%)');
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
private function asynPerform(op:Void->Outcome<Noise, String>, times:Int):Future<Bool> {
|
||||
return new Future<Bool>((wakeup) -> {
|
||||
if (times == 0) {
|
||||
wakeup(false);
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!Turtle.isTurtle()) {
|
||||
handle.write("This is not a turtle!");
|
||||
wakeup(false);
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!handle.claimTurtleMutex()) {
|
||||
handle.writeLine("Failed to claim mutex");
|
||||
wakeup(false);
|
||||
return null;
|
||||
}
|
||||
|
||||
TurtleMutex.runInTThread(() -> {
|
||||
for (i in 0...times) {
|
||||
switch op() {
|
||||
case Success(_):
|
||||
case Failure(failure):
|
||||
handle.writeLine('Failed: $failure');
|
||||
wakeup(false);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
wakeup(true);
|
||||
});
|
||||
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
private function parseTimes(args:Array<String>):Int {
|
||||
if (args.length > 0) {
|
||||
var num = Std.parseInt(args[0]);
|
||||
if (num == null) {
|
||||
handle.writeLine("Failed to parse args");
|
||||
return 0;
|
||||
} else {
|
||||
return num;
|
||||
}
|
||||
} else {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user