Module: Console

Includes:
BasicLogging
Defined in:
lib/console.rb

Overview

Functions which create a user-interface in the terminal

Constant Summary collapse

@@won_text =

Some definitions which may change one day, become configurable one day, need to be accessed (one day) from somewhere else or not.

%q"__  __               _       __               __
\ \/ /___  __  __   | |     / /___  ____     / /
 \  / __ \/ / / /   | | /| / / __ \/ __ \   / / 
 / / /_/ / /_/ /    | |/ |/ / /_/ / / / /  /_/  
/_/\____/\__,_/     |__/|__/\____/_/ /_/  (_)"
@@fail_text =
%q"                                   __ 
 __ __            _               |  |
|  |  |___ _ _   | |___ ___ ___   |  |
|_   _| . | | |  | | . |_ -| -_|  |__|
|_| |___|___|  |_|___|___|___|  |__|"
@@win_sequence =
%w"won.. won.. Won.. WOn.. wON.. wON!. woN!! won!! won.!  won.. _on.. __n.. ___.. ___.. w__.. wo_.. " 
@@wait_sequence =
%w"U\  V\  Y\  T\  |\  ¦\  :\  .\  _\  .\  o\  O\ "
@@fail_sequence =
%w":-|\  :-|\  :-[\  :-[\  :-(\  :-(\  8-(\  8-(\  8-(\  8-(\  8-(\  8-(\  8-(\  8-(\  .... |... lo.. Los. LOso LOSe LOSE lOSE loSE lose"
@@points =
0

Constants included from BasicLogging

BasicLogging::DEBUG, BasicLogging::ERROR, BasicLogging::FATAL, BasicLogging::INFO, BasicLogging::Levels, BasicLogging::UNKNOWN, BasicLogging::WARN

Instance Attribute Summary

Attributes included from BasicLogging

#log_level, #target

Instance Method Summary collapse

Methods included from BasicLogging

is_muted?, #log, mute, #set_level, #set_target

Instance Method Details

#a2i(array) ⇒ Object



154
155
156
# File 'lib/console.rb', line 154

def a2i(array)
  UserInput::a2i(array)
end

#ask_new_eventObject



165
166
167
168
# File 'lib/console.rb', line 165

def ask_new_event
  puts "\nEnter number (and return) for more information about an event,\n\t'a' to play with one new, random event,\n\t'q' to quit." 
  UserInput::input()
end

#clearObject



126
127
128
# File 'lib/console.rb', line 126

def clear
  system 'clear'
end

#compare_order(response) ⇒ Object



209
210
211
212
213
214
# File 'lib/console.rb', line 209

def compare_order(response)
  puts "\n... You say: " << bold(response.join(', '))
  devents = @m_events.dup.sort {|f, s| f.year <=> s.year } 
  cmt = "\tThe correct order is:\n" << devents.collect {|ev| ev.disp_index.to_s << ') ' << ev.year.to_s }.join("\n").box()
  wait(@@wait_sequence, 1, cmt)
end

#detail(num) ⇒ Object

show information on an event



190
191
192
193
194
195
196
197
198
199
# File 'lib/console.rb', line 190

def detail(num)
  if(0 != num && num <= @m_events.length)
    det = @m_events.detect{|ev| ev.disp_index == num}.detail
    if(det && !det.empty?)
      puts ("\n" << num.to_s << ') ' << det).box(:color => :green)
    else
      puts " - No details known -"
    end
  end
end

#int_array_input(len, range) ⇒ Object



161
162
163
# File 'lib/console.rb', line 161

def int_array_input(len, range)
  UserInput::int_array_input(len, range ) 
end

#int_input(range = nil) ⇒ Object



158
159
160
# File 'lib/console.rb', line 158

def int_input(range = nil)
  UserInput::int_input(range)
end

#loseObject



176
177
178
# File 'lib/console.rb', line 176

def lose
  wait @@fail_sequence, 1, red(@@fail_text )
end

#new_eventObject

add a new event to the game



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/console.rb', line 132

def new_event
  known_events = @m_events.collect {|ev| ev.title}
  available = $events.collect {|ev| ev unless known_events.include?(ev[0]) }.compact
  if !available || available.empty?
    puts "\n\tNo more events available. Aborting. Bye"
    puts
    return :all_events_done
  end
  clear
  out = ''
  @m_events.each_with_index {|ev, i| out << (i + 1).to_s << ') ' << ev.title << "\n"}
  puts out.box(:left => 2)

  puts "Indicate the event from the previous list, which precedes the following, or '0' (zero) to put it in front:"
  srand(Time.new.usec)
  index = rand(available.length)
  ev = Event.new(available[index]) 
  @m_events << ev
  puts ev.title.box(:color => :yellow)
  return ev
end

#position_by_user(p) ⇒ Object



201
202
203
204
205
206
207
# File 'lib/console.rb', line 201

def position_by_user(p)
  if(p > 0)
    puts "\n... You say: " << bold("after \"" << @m_events[p-1].title << "\"")
  else
    puts "\n... You say: " << bold("Earlier than any of the listed events")
  end
end

#score(pts, all_done = false) ⇒ Object



170
171
172
173
174
# File 'lib/console.rb', line 170

def score(pts, all_done = false)
  intro = (all_done ? 'Final score' : 'Current score')
  debug('all_done? ' << all_done.to_s)
  puts ' (' << intro << ': %i %s)' %[pts, (pts == 1 ? 'point' : 'points')]
end

#show_correct_orderObject



184
185
186
187
# File 'lib/console.rb', line 184

def show_correct_order
  cmt = "\tThe correct order is:\n" << @m_events.collect {|ev| (@m_events.index(ev) + 1).to_s << ') ' << ev.title << ' (' << ev.year.to_s << ')'}.join("\n").box()
  wait(@@wait_sequence, 1, cmt) 
end

#start_gameObject

start with some arbitrary events



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
# File 'lib/console.rb', line 43

def start_game
  available = $events
  clear
  puts bold("\nPut the following events into the correct chronological order (e.g.: 2 3 1).")
  @m_events = Array.new
  out = ''
  $num_events.times do |i|
    srand(Time.new.usec)
    index = rand(available.length)
    ev = Event.new(available.delete_at(index))
    ev.disp_index = (i + 1)
    @m_events << ev
    out << (i + 1).to_s << ') ' << ev.title << "\n"
  end
  puts out.box(:left => 2)
  response = nil

  response = int_array_input($num_events, (1..$num_events) ) 
  if response == :quit_game
    score @@points, true
    exit true
  end

  compare_order(response)
  # good order of the response-array
  if good_order(response)
    @@points += $num_events 
    win
  else
    lose
  end
  score(@@points)

  # Start the loop. Add events as requested.
  until @m_events.length == $MAX_NUM_EVENTS do
    ui = ask_new_event
    if ui == :quit_game
      score @@points, true
      exit true
    end
    if('a' == ui)
      ev = new_event
      # all known events have been handled.
      if ev == :all_events_done
        score(@@points, true) 
        exit true
      end

      # ------> unresolved trouble with 0-based array.indices. <----------
      # 1) exclude new event from the choices (hence -1), allow 0 as 'earlier than any'.
      r = int_input(0..(@m_events.length - 1) )
      if r == :quit_game
        score @@points, true
        exit true
      end
      # User-input can be 0, which complicates the comparison with the
      # 0-based event-array.  

      # 2) in the range of known events, if not 0 (hence 0 is allowed).
      position_by_user(r)

      # 4) 'order' must fit in between the indices of the event-array (0-based, hence -1. Again.)
      ok = good_order(r - 1, ev)

      sort_years(true)
      # 5) show the resulting order as starting from 1, not 0.
      show_correct_order
      # <---------- This should be it. ------------>

      if ok
        @@points += 1
        win
      else
        lose
      end
      score(@@points, @m_events.length == $MAX_NUM_EVENTS) 
    else
      detail( a2i(ui) )
    end
  end

end

#wait(seq, sec, cmt) ⇒ Object



216
217
218
219
220
221
222
# File 'lib/console.rb', line 216

def wait(seq, sec, cmt)
  bi = BusyIndicator.new(false)
  bi.sequence = seq
  bi.run
  sleep sec
  bi.stop(cmt)
end

#winObject



180
181
182
# File 'lib/console.rb', line 180

def win
  wait(@@win_sequence, 1, green(@@won_text) )
end