/
batch.rb
94 lines (79 loc) · 1.88 KB
/
batch.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#gems/clock/lib/clock.rb
class Clock
CHECK = 10 #frames
MAX_ALLOWED_MH = 0.5 #sec
TICK_IN_SCENES = [Scene_Map, Scene_Battle]#, Scene_Menu]
attr_accessor :started_at, :mh, :last_ticked_at
class << self
def start!
@current = new
end
def flush!
@current = nil
end
def load(arr)
if arr.to_a.any?
start!
@current.started_at = arr[0]
@current.mh = arr[1]
@current.last_ticked_at = arr[2]
end
end
def to_save
return [] unless @current
[@current.started_at, @current.mh, @current.last_ticked_at]
end
def seconds_in_game
@current && @current.seconds_in_game
end
def tick
@current && @current.tick
end
end
def initialize
@started_at = Time.now.to_f
@mh, @ticked = 0, 0
end
def tick
if @ticked % CHECK == 0
prev = @last_ticked_at
@last_ticked_at = Time.now.to_f
if prev && (@last_ticked_at - prev) > MAX_ALLOWED_MH
@mh += @last_ticked_at - prev
end
end
@ticked += 1
end
def seconds_in_game
(Time.now.to_f - @started_at - @mh).to_i
end
end
#gems/clock/lib/clock/patch.rb
module Clock::Patch
end
#gems/clock/lib/clock/patch/scenes_patch.rb
Clock::TICK_IN_SCENES.each do |scene_klass|
scene_klass.class_eval do
alias update_for_clock update
def update
update_for_clock
Clock.tick
end
end
end
#gems/clock/lib/clock/patch/data_manager_patch.rb
module DataManager
instance_eval do
alias make_save_contents_for_clock make_save_contents
def make_save_contents
make_save_contents_for_clock.tap do |contents|
contents[:clock] = Clock.to_save
end
end
alias extract_save_contents_for_clock extract_save_contents
def extract_save_contents(contents)
extract_save_contents_for_clock contents
Clock.load contents[:clock]
end
end
end