1 require 'observer_utils'
6 ByoYomi = Struct.new(:time, :periods)
9 # increment = increment per move
10 # byoyomi.time = time per move after main is elapsed
11 # byoyomi.periods = number of times byoyomi.time has to elapse
12 # for the byoyomi to end
14 # byoyomi and increment don't work together
16 # all times are in seconds
18 def initialize(main, increment, byoyomi = nil, timer_class = Qt::Timer)
20 @increment = increment
22 @byoyomi = byoyomi.dup
23 @byoyomi_time = @byoyomi.time
26 @timer = timer_class.new
27 @timer.single_shot = true
28 @timer.on(:timeout) { tick }
37 @elapsed += @time.elapsed
43 fire :timer => { :main => @main }
49 # milliseconds for the next tick
50 delta = [0, (@count + 1) * 100 - @elapsed].max
65 # if we get here, there must be
66 # a byoyomi, otherwise the timer would
71 @byoyomi.time = @byoyomi_time
72 if @byoyomi.periods <= 0
78 if @main <= 0 and (not @byoyomi)
92 delta = [0, (@count + 1) * 100 - @elapsed - @time.elapsed].max
101 { :byoyomi => @byoyomi.dup }
105 def set_time(milliseconds)
107 @main = milliseconds / 1000
111 @elapsed = milliseconds % 1000