Class: KAG::Stats::Main

Inherits:
Hash
  • Object
show all
Includes:
Singleton
Defined in:
lib/kag/stats/main.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Hash

#random_key, #shuffle, #shuffle!

Constructor Details

#initialize(hash = nil) ⇒ Main

Returns a new instance of Main.



6
7
8
9
# File 'lib/kag/stats/main.rb', line 6

def initialize(hash=nil)
  super
  self.merge!(self._load)
end

Class Method Details

.add_stat(stat, increment = 1) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/kag/stats/main.rb', line 40

def self.add_stat(stat,increment = 1)
  stat = stat.to_sym
  s = KAG::Stats::Main.instance
  s[stat] = 0 unless s[stat]
  s[stat] = s[stat].to_i + increment.to_i
  s.save
end

.subtract_stat(stat, decrement = 1) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/kag/stats/main.rb', line 48

def self.subtract_stat(stat,decrement = 1)
  stat = stat.to_sym
  s = KAG::Stats::Main.instance
  if s[stat]
    s[stat] = s[stat].to_i - decrement.to_i
  else
    s[stat] = 0
  end
  s.save
end

Instance Method Details

#_loadObject



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/kag/stats/main.rb', line 11

def _load
  unless File.exists?("data/index.json")
    return {}
  end
  f = ::IO.read("data/index.json")
  if f and !f.empty?
    SymbolTable.new(JSON.parse(f))
  else
    SymbolTable.new
  end
end

#reloadObject



23
24
25
26
# File 'lib/kag/stats/main.rb', line 23

def reload
  puts "Reloading stats.main file..."
  self.merge!(self._load)
end

#saveObject



34
35
36
37
38
# File 'lib/kag/stats/main.rb', line 34

def save
  File.open("data/index.json","w") do |f|
    f.write(self.to_json)
  end
end

#store(key, val) ⇒ Object

Sets the value of the given key to val.



29
30
31
32
# File 'lib/kag/stats/main.rb', line 29

def store(key, val)
  super(key,val)
  self.save
end