From 42f9fdc0b88dd819a61593ed053d6b0713c59e45 Mon Sep 17 00:00:00 2001 From: Nat Lasseter Date: Fri, 10 May 2024 10:31:08 +0100 Subject: One frame at a time --- 01-hub2.rb | 10 +++------- 02-hub3.rb | 10 +++------- 03-switch3.rb | 29 +++++++++++++---------------- forward-please | 48 ++++++++++++++++++------------------------------ 4 files changed, 37 insertions(+), 60 deletions(-) diff --git a/01-hub2.rb b/01-hub2.rb index 7ecd8a1..d5b0ba1 100644 --- a/01-hub2.rb +++ b/01-hub2.rb @@ -19,13 +19,9 @@ $levels << Level.new do end def generate - frames = {} - @interfaces.each do |iface| - next if rand < 0.3 - frames[@count.to_s] = Frame.new(iface, @count) - @count += 1 - end - frames + frame = Frame.new(interfaces.sample, @count) + @count += 1 + frame end def click diff --git a/02-hub3.rb b/02-hub3.rb index 5379bb4..f3c6e3d 100644 --- a/02-hub3.rb +++ b/02-hub3.rb @@ -21,13 +21,9 @@ $levels << Level.new do end def generate - frames = {} - @interfaces.each do |iface| - next if rand < 0.3 - frames[@count.to_s] = Frame.new(iface, @count) - @count += 1 - end - frames + frame = Frame.new(interfaces.sample, @count) + @count += 1 + frame end def click diff --git a/03-switch3.rb b/03-switch3.rb index 7cbdc2f..f8809d7 100644 --- a/03-switch3.rb +++ b/03-switch3.rb @@ -33,25 +33,22 @@ $levels << Level.new do end def generate - frames = {} - @interfaces.each do |iface| - next if rand < 0.3 + iface = interfaces.sample - if rand < 0.4 || @fib[iface].nil? - src = Frame2.gen_addr - @next_fib[iface] = src - else - src = @fib[iface] - end + if rand < 0.4 || @fib[iface].nil? + src = Frame2.gen_addr + @next_fib[iface] = src + else + src = @fib[iface] + end - dst = nil - dst = (@fib.values.compact - [src]).sample if rand < 0.5 - dst = Frame2.gen_addr if dst.nil? + dst = nil + dst = (@fib.values.compact - [src]).sample if rand < 0.5 + dst = Frame2.gen_addr if dst.nil? - frames[@count.to_s] = Frame2.new(iface, src, dst) - @count += 1 - end - frames + frame = Frame2.new(iface, src, dst) + @count += 1 + frame end def click diff --git a/forward-please b/forward-please index ebb6d4b..e8e7933 100755 --- a/forward-please +++ b/forward-please @@ -14,9 +14,9 @@ $level = $levels.shift def help puts <<~EOF Commands: - f {frame} {iface}: forward a frame to iface + f {iface}: forward the frame to iface h: this help - c: click on + n: next frame d: show the level description a: show the click frames and actions q: quit @@ -25,35 +25,27 @@ end $instructions = [] -def forward(frame, iface) - if !$clickframes.keys.include?(frame) - puts "#{frame} is not a frame in this click" - return - end - +def forward(iface) if !$level.interfaces.include?(iface) puts "#{iface} is not an interface in this device" return end - $instructions << [frame, $clickframes[frame].to(iface)] + $instructions << $frame.to(iface) end def clickactions - puts "Click #{$click}:" + puts "Frame #{$click}:" puts " Interfaces:" puts " #{$level.interfaces.join(", ")}" - puts " Frames:" - $clickframes.each do |id, frame| - puts " #{id}: #{frame.description} from Interface #{frame.iface}" - end + puts " Frame:" + puts " #{$frame.description} from Interface #{$frame.iface}" - i = 1 puts " Actions:" - $instructions.each do |instr| - puts " #{i}: Forward frame #{instr[0]} to interface #{instr[1].iface}" + $instructions.each_with_index do |instr, idx| + puts " #{idx + 1}: Forward frame to interface #{instr.iface}" end end @@ -63,13 +55,9 @@ def click right = 0 wrong = 0 - targetframes = [] - $clickframes.each do |id, frame| - targetframes += $level.target(frame) - end - targetframes.flatten! + targetframes = $level.target($frame) - $instructions.each do |id, frame| + $instructions.each do |frame| if targetframes.include?(frame) right += 1 targetframes.delete(frame) @@ -98,24 +86,24 @@ def click puts end - $clickframes = $level.generate + $frame = $level.generate clickactions end def handle(cmd) case cmd.split[0] when ?f - args = cmd.split[1..] - if args.count == 2 - forward(*args) + arg = cmd.split[1] + if arg.nil? + puts "f {iface}" else - puts "f {frame} {iface}" + forward(arg) end when ?d puts $level.description when ?a clickactions - when ?c + when ?n click when ?q exit 0 @@ -128,7 +116,7 @@ end puts $level.description puts -$clickframes = $level.generate +$frame = $level.generate clickactions loop do -- cgit v1.2.1