aboutsummaryrefslogtreecommitdiff
path: root/forward-please
blob: e8e7933fb170dffa8b9ba2d1d3e25cfac410ccee (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env ruby

$levels = []

%w(
  01-hub2 02-hub3
  03-switch3
).each do |file|
  require "./#{file}"
end

$level = $levels.shift

def help
  puts <<~EOF
    Commands:
      f {iface}: forward the frame to iface
      h: this help
      n: next frame
      d: show the level description
      a: show the click frames and actions
      q: quit
  EOF
end

$instructions = []

def forward(iface)
  if !$level.interfaces.include?(iface)
    puts "#{iface} is not an interface in this device"
    return
  end

  $instructions << $frame.to(iface)
end

def clickactions
  puts "Frame #{$click}:"

  puts "  Interfaces:"
  puts "    #{$level.interfaces.join(", ")}"

  puts "  Frame:"
  puts "    #{$frame.description} from Interface #{$frame.iface}"

  puts "  Actions:"
  $instructions.each_with_index do |instr, idx|
    puts "    #{idx + 1}: Forward frame to interface #{instr.iface}"
  end
end

$click = 1

def click
  right = 0
  wrong = 0

  targetframes = $level.target($frame)

  $instructions.each do |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 = []

  $level.click

  $click += 1
  if $click > $level.clicks
    $click = 1
    $level = $levels.shift
    if $level.nil?
      puts "Demo all done"
      exit 0
    end
    puts
    puts $level.description
    puts
  end

  $frame = $level.generate
  clickactions
end

def handle(cmd)
  case cmd.split[0]
  when ?f
    arg = cmd.split[1]
    if arg.nil?
      puts "f {iface}"
    else
      forward(arg)
    end
  when ?d
    puts $level.description
  when ?a
    clickactions
  when ?n
    click
  when ?q
    exit 0
  when nil
  else
    help
  end
end

puts $level.description
puts

$frame = $level.generate
clickactions

loop do
  print ">: "
  handle(gets.strip)
end