Class: RTG::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/rtg/server.rb

Constant Summary collapse

TIME_LIMIT =
60

Instance Method Summary collapse

Instance Method Details

#initObject



5
6
7
8
9
10
11
12
13
# File 'lib/rtg/server.rb', line 5

def init
  @words = YAML.load_file(File.expand_path("words.yml", __dir__))["method"]
  @words << YAML.load_file(File.expand_path("words.yml", __dir__))["class"]
  @words << YAML.load_file(File.expand_path("words.yml", __dir__))["library"]
  @words.flatten!
  @time = 0
  @type_count = 0
  @typo_count = 0
end

#startObject



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
# File 'lib/rtg/server.rb', line 15

def start
  init
  word = get_word
  inputted = []

  Screen.wait_screen
  print "\r\e[9A"

  while input = getkey
    if input == Keyevent::SPACE
      break
    elsif input == Keyevent::ETX
      exit(0)
    end
  end

  Screen.clean_screen(9)

  while @time < TIME_LIMIT
    begin
      Timeout.timeout(1) do
        loop do
          Screen.print_time_bar(TIME_LIMIT - @time)
          Screen.main_screen(inputted.join.colorize(:white) + word.join)
          print "\r\e[11A"

          input = getkey

          if input == Keyevent::ETX
            exit(0)
          elsif input == word.first
            inputted << word.shift
            @type_count += 1

            if word.empty?
              word = get_word
              inputted = []
            end
          else
            @typo_count += 1
            print "\a"
          end
        end
      end
    rescue Timeout::Error
      @time += 1
    end
  end

  Screen.clean_screen(11)
  Screen.result_screen(@type_count, @typo_count)

  while input = getkey
    case input
      when /Q/
        break
      when Keyevent::ETX
        exit(0)
    end
  end
end