Module: MCLimit

Defined in:
lib/mc-limit/gui.rb,
lib/mc-limit/core.rb,
lib/mc-limit/admin.rb,
lib/mc-limit/winfns.rb,
lib/mc-limit/version.rb

Defined Under Namespace

Modules: Win Classes: AdminApp, AdminDialog, GUI

Constant Summary collapse

DEFAULT_MINUTES =

The default number of minutes of game play to allow per day when env not set

30
MINIMUM_MINUTES =

The minimum number of remaining minutes needed to start the game

1
REMAINING_FILE =
'remaining.yml'
GAME_COMMAND =
'javaw.exe -Xms512m -Xmx1024m -cp "%APPDATA%\.minecraft\bin\*" -Djava.library.path="%APPDATA%\.minecraft\bin\natives" net.minecraft.client.Minecraft'
VERSION =
"1.0.0"

Class Method Summary collapse

Class Method Details

.adminObject



54
55
56
# File 'lib/mc-limit/admin.rb', line 54

def self.admin
  AdminApp.new.main_loop
end

.admin_passwordObject



38
39
40
# File 'lib/mc-limit/core.rb', line 38

def self.admin_password
  ENV['MC_LIMIT_ADMIN_PASSWORD']
end

.check_admin_password(parent) ⇒ Object



31
32
33
34
35
36
# File 'lib/mc-limit/admin.rb', line 31

def self.check_admin_password( parent )
  raise 'Administration password has not been set!' if MCLimit.admin_password.nil?
  password = Wx::PasswordEntryDialog.new( parent, 'Enter administration password' )
  raise 'Password required!' unless Wx::ID_OK == password.show_modal
  raise 'Incorrect password!' unless password.get_value == MCLimit.admin_password
end

.default_minutesObject



42
43
44
# File 'lib/mc-limit/core.rb', line 42

def self.default_minutes
  Float( ENV['DEFAULT_MC_LIMIT'] || DEFAULT_MINUTES )
end

.disappoint(title, body) ⇒ Object



17
18
19
20
# File 'lib/mc-limit/core.rb', line 17

def self.disappoint( title, body )
  GUI.error( body, title )
  exit 1
end

.game_commandObject



22
23
24
# File 'lib/mc-limit/core.rb', line 22

def self.game_command
  ENV['MC_LIMIT_COMMAND'] || GAME_COMMAND
end

.launchObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/mc-limit/core.rb', line 90

def self.launch
  GUI.new.main_loop do
    remaining = MCLimit.remaining_minutes
    pid = MCLimit.run( MCLimit.game_command, remaining )

    start = Time.now
    Process.waitpid( pid, 0 )
    finish = Time.now

    consumed = ( finish - start ) / 60
    remaining = [ 0, remaining - consumed ].sort.last
    MCLimit.update_remaining_minutes( remaining )
  end
end

.remaining_fileObject



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/mc-limit/core.rb', line 26

def self.remaining_file
  if ENV['MC_LIMIT_FILE'].nil?
    if RUBY_PLATFORM =~ /mingw/
      File.join( ENV['APPDATA'], '.mc-limit', REMAINING_FILE )
    else
      File.join( ENV['HOME'], '.mc-limit', REMAINING_FILE )
    end
  else
    ENV['MC_LIMIT_FILE']
  end
end

.remaining_minutes(date = Date.today) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/mc-limit/core.rb', line 46

def self.remaining_minutes( date = Date.today )
  FileUtils.mkdir_p( File.dirname( MCLimit.remaining_file ) )
  yaml = YAML.load_file( MCLimit.remaining_file )
  ( yaml[:date] == date ) ? Float(yaml[:remaining]) : default_minutes
rescue
  default_minutes
end

.run(command, time_limit) ⇒ Object



85
86
87
88
# File 'lib/mc-limit/core.rb', line 85

def self.run( command, time_limit )
  validate_sufficient time_limit
  pid = timeout_pid( Process.spawn( command ), time_limit )
end

.stop_minecraft(pids) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/mc-limit/core.rb', line 60

def self.stop_minecraft(pids)
  if RUBY_PLATFORM =~ /mingw/
    Win.close_process(pids, 'Minecraft')
  else
    pids.each { |pid| kill(:QUIT, pid) }
  end
end

.timeout_pid(pid, minutes) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/mc-limit/core.rb', line 68

def self.timeout_pid(pid, minutes)
  Thread.new do
    sleep minutes * 60
    pids = [ pid ]
    Sys::ProcTable.ps do |process|
      pids << process.pid if pids.include? process.ppid
    end
    MCLimit.stop_minecraft(pids)
  end
  pid
end

.update_remaining_minutes(minutes) ⇒ Object



54
55
56
57
58
# File 'lib/mc-limit/core.rb', line 54

def self.update_remaining_minutes( minutes )
  yaml = { :date => Date.today, :remaining => minutes }.to_yaml
  FileUtils.mkdir_p( File.dirname( MCLimit.remaining_file ) )
  File.open( MCLimit.remaining_file, 'wt' ) { |f| f.write yaml }
end

.validate_sufficient(time_limit) ⇒ Object



80
81
82
83
# File 'lib/mc-limit/core.rb', line 80

def self.validate_sufficient( time_limit )
  return if time_limit >= MINIMUM_MINUTES
  disappoint( 'Sorry', 'No more Minecraft allowed today!' )
end