Class: Timequiz

Inherits:
Object
  • Object
show all
Includes:
BasicLogging
Defined in:
lib/timequiz.rb

Overview

Main program class. All action is within the initialize() method, or, more precisely in the start_game() method which has to be defined by an interface-module.

Constant Summary

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BasicLogging

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

Constructor Details

#initialize(*args) ⇒ Timequiz

Returns a new instance of Timequiz.



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/timequiz.rb', line 114

def initialize(*args)
  # adjust log-level, if given.
  # Else use what you know.
  options = ArgParser.parse(*args)
  set_level (BasicLogging::DEBUG if options.debug)
  #    should we add only a new event to the list  
  debug('options are : ' << options.to_s)
  add_ok = game_ok = false
  if(options.file)
    debug('shall use non-standard events file')
    add_ok, game_ok = verify_prepare(options.file)
  else
    game_ok = require_relative 'events'
  end
  if(options.add)
    if(add_ok)
      Adder::add(options)
      exit true
    elsif(options.file)
      error('cannot add events to ' << options.file)
      exit false
    else
      error('PSE start the program with -h or --help to see an option-overview')
      exit false
    end
  end

  # start the chosen interface
  if(game_ok)
    start_game 
  else
    error("Cannot play with the events in " << options.file)
    error("PSE verify that there are at least 3 events defined!") 
    exit false
  end
end

Class Method Details

.called_as?(executable = nil) ⇒ Boolean

Find out, how the program has been called. In juin 2017 this can be ‘timequiz’ or ‘timequizGtk’. Write a Qt-interface, create a symbolic link ‘timequizQt’ in ../../bin and add a when-clause to the case-when structure, right below the method (in class-scope). Returns true if the given executable file is named the same as the one called, false if they are not the same. Returns the name of the called executable, if no argument is given.

Returns:

  • (Boolean)


43
44
45
46
# File 'lib/timequiz.rb', line 43

def self.called_as?(executable = nil)
  return File::basename($0) == executable.strip if executable
  return File::basename($0) 
end

Instance Method Details

#verify_prepare(file) ⇒ Object



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

def verify_prepare(file)
  if(file && !file.empty? && !file.end_with?('.rb'))
     file << ".rb"
  end
  begin
    ofl = File.open(file, 'a')
    if !File.readable?(file)
      error('The file ' << file << ' cannot be read! Aborting')
      exit false
    end
    if(File.empty?(file)  )
      info("The file " << file << " is empty. You must add some events to it.")
      if(File.writable?(file) )
        orig_file = File.dirname(__FILE__) << File::Separator << 'events.rb'
        debug('orig_file is ' << orig_file)
        File.open(orig_file, 'r') do |ifl|
          ofl.puts("# Events defined for the Timequiz game")
          ofl.puts("# Lines starting with '#' are comments.\n\n# EXAMPLES:")
          3.times do 
            line = ""
            line = ifl.readline.strip until line.start_with?('$')
            ofl.puts('#' << line)
          end
          ofl.puts("\n# Add your own events below this line\n#" << '_' * 60)
        end
        ofl.close
      end
      add_ok = require file 
    else
      add_ok = require file 
      if(add_ok)
        return add_ok, $events.length >= 3
      end
    end
  rescue IOError => ex
    ofl.close
    error('Cannot work with the given file: ' << ex.message)
    exit false
  end
end