aboutsummaryrefslogtreecommitdiff
path: root/forward-please.rb
blob: c9190798727d3610eeb10a495afebc655e6be0aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
require './01-hub2.rb'

$level = L1_Hub2

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 "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

def click
  right = 0
  wrong = 0

  targetframes = []
  $clickframes.each do |id, frame|
    targetframes += $level.target(frame)
  end

  $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 = []
  $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