Skip to content

Commit

Permalink
add timer (beta)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmichotte committed Jun 15, 2015
1 parent 218b27e commit 8d419fd
Show file tree
Hide file tree
Showing 13 changed files with 175 additions and 14 deletions.
12 changes: 9 additions & 3 deletions app/app_delegate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ def applicationDidFinishLaunching(notification)

# load cards into database if needed
DatabaseGenerator.init_database(@splash) do
@splash.window.orderOut(self)
@splash = nil

NSApp.mainMenu = MainMenu.new.menu

Expand All @@ -37,14 +35,19 @@ def applicationDidFinishLaunching(notification)
@opponent.showWindow(self)
@opponent.window.orderFrontRegardless

@timer_hud = TimerHud.new
@timer_hud.showWindow(self)
@timer_hud.window.orderFrontRegardless

Game.instance.player_tracker = @player
Game.instance.opponent_tracker = @opponent
Game.instance.timer_hud = @timer_hud

if Configuration.remember_last_deck
last_deck_played = Configuration.last_deck_played
unless last_deck_played.nil?
name, version = last_deck_played.split('#')
deck = Deck.where(:name => name).and(:version).eq(version).first
deck = Deck.where(:name => name).and(:version).eq(version).first
if deck
@player.show_deck(deck.playable_cards, deck.name)
Game.instance.with_deck(deck)
Expand Down Expand Up @@ -77,6 +80,9 @@ def applicationDidFinishLaunching(notification)
Hearthstone.instance.start
end

@splash.window.orderOut(self)
@splash = nil

VersionChecker.check

NSNotificationCenter.defaultCenter.observe 'AppleLanguages_changed' do |_|
Expand Down
33 changes: 29 additions & 4 deletions app/controllers/huds/text_hud.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,34 @@ class TextHud < NSView
attr_accessor :text, :font_size

def font_size
@font_size ||= 16
@font_size ||= 14
end

def text=(value)
@text = value
setNeedsDisplay true
end

def flash
if @flash_count == 4
@flash_count = 0
@is_flashing = false
setNeedsDisplay true
return
end

@flash_count ||= 0
@flash_count += 1
@is_flashing = true

Dispatch::Queue.concurrent.after (0.24) do
Dispatch::Queue.main.async do
setNeedsDisplay true
flash
end
end
end

def drawRect(rect)
super.tap do
return if text.nil?
Expand All @@ -24,13 +44,18 @@ def drawRect(rect)
ratio = 1.0
end

font_size = (14.0 / ratio).round
stroke = Configuration.count_color_border
if @is_flashing and @flash_count % 2 == 0
stroke = :red.nscolor
end

size = (font_size / ratio).round
style = NSMutableParagraphStyle.new
style.alignment = NSCenterTextAlignment
name = text.attrd.paragraph_style(style)
.font('Belwe Bd BT'.nsfont(font_size))
.font('Belwe Bd BT'.nsfont(size))
.stroke_width(-1.5)
.stroke_color(Configuration.count_color_border)
.stroke_color(stroke)
.foreground_color(Configuration.count_color)
name.drawInRect self.bounds,
options: NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesDeviceMetrics
Expand Down
64 changes: 64 additions & 0 deletions app/controllers/huds/timer_hud.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
class TimerHud < Hud

def init
super.tap do
@layout = TimerHudLayout.new
self.window = @layout.window
self.window.delegate = self

@label = @layout.get(:label)
end
end

def game_start
@label.text = nil
@player_mulligan = @opponent_mulligan = false
end

def game_end
@label.text = nil
if @timer
@timer.invalidate
end
end

def mulligan_done(who)
who == :player ? @player_mulligan = true : @opponent_mulligan = true
end

def restart(player)
return unless Configuration.show_timer

@current_player = player

if @timer
@timer.invalidate
end
@seconds = 90
@timer = NSTimer.scheduledTimerWithTimeInterval(1,
target: self,
selector: 'fire:',
userInfo: nil,
repeats: true)

if (!@player_mulligan and @opponent_mulligan) or (!@opponent_mulligan and @player_mulligan) or (!@player_mulligan and !@opponent_mulligan and @seconds < 85)
@opponent_mulligan = @player_mulligan = true
end

end

private
def fire(_)
return if !@player_mulligan and !@opponent_mulligan

@seconds -= 1 if @seconds > 0

seconds = 90 - @seconds
if @seconds < 10
@label.flash
end

@label.text = '%02d:%02d' % [(seconds / 60) % 60, seconds % 60]
end

end
38 changes: 38 additions & 0 deletions app/controllers/huds/timer_hud_layout.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class TimerHudLayout < MK::WindowLayout

def layout
wframe = [[400, 120], [200, 80]]
frame = NSUserDefaults.standardUserDefaults.objectForKey 'HSTimer'
if frame
wframe = NSRectFromString(frame)
end

frame(wframe)
identifier 'HSTimer'
title 'Timer'._

# transparent all the things \o|
opaque false
has_shadow false
background_color :black.nscolor(Configuration.window_transparency)

locked = Configuration.windows_locked

if locked
mask = NSBorderlessWindowMask
else
mask = NSTitledWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask | NSBorderlessWindowMask
end
style_mask mask
level NSScreenSaverWindowLevel

add TextHud, :label do
font_size 28
constraints do
width.equals(:superview).minus(10)
height.equals(:superview).minus(10)
end
end
end

end
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ def options
end
}
},
:show_timer => 'Show timers'._
}
end

Expand Down
9 changes: 8 additions & 1 deletion app/hearthstone/game.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ class Game

Log = Motion::Log

attr_accessor :player_tracker, :opponent_tracker, :start_date, :end_date
attr_accessor :player_tracker, :opponent_tracker
attr_accessor :timer_hud
attr_accessor :start_date, :end_date

def self.instance
Dispatch.once { @instance ||= new }
Expand Down Expand Up @@ -208,11 +210,14 @@ def game_end

player_tracker.game_end
opponent_tracker.game_end
timer_hud.game_end
end

def turn_start(player, turn)
log(player, "turn : #{turn}")
@current_turn = turn

timer_hud.restart(player)
end

## player events
Expand Down Expand Up @@ -275,6 +280,7 @@ def player_hand_discard(card_id, turn)
def player_mulligan(card_id)
log(:player, "mulligan #{card_id} (#{card(card_id)})")
player_tracker.mulligan(card_id)
timer_hud.mulligan_done(:player)
end

def player_back_to_hand(card_id, turn)
Expand Down Expand Up @@ -355,6 +361,7 @@ def opponent_hand_discard(card_id, from, turn)
def opponent_mulligan(from)
log(:opponent, "mulligan (from: #{from})")
opponent_tracker.mulligan
timer_hud.mulligan_done(:opponent)
end

def opponent_play_to_hand(card_id, turn, id)
Expand Down
5 changes: 3 additions & 2 deletions app/utilities/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def is_cyrillic_or_asian
flash_color fixed_window_names reset_on_end card_layout count_color
count_color_border hand_count_window show_get_cards show_card_on_hover
in_hand_as_played use_hearthstats hearthstats_token show_notifications
remember_last_deck last_deck_played skin)
remember_last_deck last_deck_played skin show_timer)

KDefaults = {
:flash_color => [55, 189, 223],
Expand All @@ -34,7 +34,8 @@ def is_cyrillic_or_asian
:use_hearthstats => false,
:show_notifications => true,
:remember_last_deck => true,
:skin => :hearthstats
:skin => :hearthstats,
:show_timer => true
}

def method_missing(symbol, *args)
Expand Down
4 changes: 3 additions & 1 deletion resources/de.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,6 @@
"Show notifications" = "Show notifications";
"Remember last played deck" = "Remember last played deck";
"Default" = "Default";
"Skin" = "Skin";
"Skin" = "Skin";

"Show timers" = "Show timers";
4 changes: 3 additions & 1 deletion resources/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,6 @@
"Show notifications" = "Show notifications";
"Remember last played deck" = "Remember last played deck";
"Default" = "Default";
"Skin" = "Skin";
"Skin" = "Skin";

"Show timers" = "Show timers";
4 changes: 3 additions & 1 deletion resources/fr.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,6 @@
"Show notifications" = "Voir les notifications";
"Remember last played deck" = "Se souvenir du dernier deck joué";
"Default" = "Défaut";
"Skin" = "Skin";
"Skin" = "Skin";

"Show timers" = "Voir la minuterie";
4 changes: 3 additions & 1 deletion resources/it.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,6 @@
"Show notifications" = "Mostra notifiche";
"Remember last played deck" = "Ricorda l'ultimo mazzo giocato";
"Default" = "Default";
"Skin" = "Skin";
"Skin" = "Skin";

"Show timers" = "Show timers";
10 changes: 10 additions & 0 deletions spec/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -251,4 +251,14 @@
value = Configuration.skin
value.should == :default
end

it 'should accept a "show_timer" option' do
value = Configuration.show_timer
value.should == true

should.not.raise(ArgumentError) { Configuration.show_timer = false }
value = Configuration.show_timer
value.should == false
end

end
1 change: 1 addition & 0 deletions versions.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
- Correct a bug where the last deck was deleted on right-click instead of the selected one
- Add a verification for the config.log file
- Improve deck manager opening
- Add turn timer (beta). There is a know bug with the first turn which starts too soon.

#### 0.11
##### HearthStats support
Expand Down

0 comments on commit 8d419fd

Please sign in to comment.