aboutsummaryrefslogtreecommitdiff
path: root/forward-please
diff options
context:
space:
mode:
authorNat Lasseter <user@4574.co.uk>2024-05-07 21:27:05 +0100
committerNat Lasseter <user@4574.co.uk>2024-05-07 21:27:05 +0100
commite796ea2d6d3aabe84ff165d8be110506482f895d (patch)
treeb395e9c8d846756f23436ad01b4601e24a6019ba /forward-please
parent18728ccf1262f3cd1425a8277264d18af73a8a26 (diff)
Two levels!
Diffstat (limited to 'forward-please')
-rwxr-xr-xforward-please113
1 files changed, 113 insertions, 0 deletions
diff --git a/forward-please b/forward-please
new file mode 100755
index 0000000..70c6986
--- /dev/null
+++ b/forward-please
@@ -0,0 +1,113 @@
+#!/usr/bin/env ruby
+
+$levels = %w(01-hub2 02-hub3)
+require "./#{$levels.shift}"
+
+puts Level.description
+
+def help
+ puts <<~EOF
+ Commands:
+ f {frame} {iface}: forward a frame to iface
+ h: this help
+ c: click on
+ d: show the level description
+ i: show the interfaces
+ a: show the click frames and actions
+ q: quit
+ EOF
+end
+
+$instructions = []
+
+def forward(frame, iface)
+ $instructions << [frame, $clickframes[frame].to(iface)]
+end
+
+def clickactions
+ puts "Click #{$click}:"
+ puts "Frames:"
+ $clickframes.each do |id, frame|
+ puts " #{id}: #{frame.description} from Interface #{frame.iface}"
+ end
+
+ i = 1
+ puts "Actions:"
+ $instructions.each do |instr|
+ puts " #{i}: Forward frame #{instr[0]} to interface #{instr[1].iface}"
+ end
+end
+
+$click = 1
+
+def click
+ right = 0
+ wrong = 0
+
+ targetframes = []
+ $clickframes.each do |id, frame|
+ targetframes += Level.target(frame)
+ end
+ targetframes.flatten!
+
+ $instructions.each do |id, frame|
+ if targetframes.include?(frame)
+ right += 1
+ targetframes.delete(frame)
+ else
+ wrong += 1
+ end
+ end
+ wrong += targetframes.count
+
+ puts "#{right} out of #{right+wrong} correct"
+
+ $instructions = []
+
+ $click += 1
+ if $click > Level.clicks
+ $click = 1
+ level = $levels.shift
+ if level.nil?
+ puts "Demo all done"
+ exit 0
+ end
+ require "./#{level}"
+ puts Level.description
+ end
+
+ $clickframes = Level.generate
+ clickactions
+end
+
+def handle(cmd)
+ case cmd[0]
+ when ?h
+ help
+ when ?f
+ args = cmd.split[1..]
+ if args.count == 2
+ forward(*args)
+ else
+ puts "f {frame} {iface}"
+ end
+ when ?d
+ puts Level.description
+ when ?i
+ puts Level.interfaces
+ when ?a
+ clickactions
+ when ?c
+ click
+ when ?q
+ exit 0
+ end
+end
+
+$clickframes = Level.generate
+
+clickactions
+loop do
+ print ">: "
+ handle(gets.strip)
+end