Class: Hearthstone::Log::Game
- Inherits:
-
Object
- Object
- Hearthstone::Log::Game
- Defined in:
- lib/hearthstone/log/data/game.rb
Instance Attribute Summary collapse
-
#mode ⇒ Object
Returns the value of attribute mode.
-
#players ⇒ Object
readonly
Returns the value of attribute players.
-
#results ⇒ Object
readonly
Returns the value of attribute results.
-
#turns ⇒ Object
readonly
Returns the value of attribute turns.
Instance Method Summary collapse
-
#add_turn(number: nil, player: nil, timestamp: nil) ⇒ Object
proceed to next turn.
-
#completed? ⇒ Boolean
if the game is completed.
-
#current_turn ⇒ Object
current turn.
-
#filename ⇒ Object
A unique filename for this Game.
-
#initialize(mode) ⇒ Game
constructor
A new instance of Game.
-
#player_with_id_or_name(id: nil, name: nil) ⇒ Object
create or get the player, with given id or name.
-
#to_hash ⇒ Object
convert the game into hash.
-
#to_json ⇒ Object
convert the game into JSON.
Constructor Details
#initialize(mode) ⇒ Game
Returns a new instance of Game.
11 12 13 14 15 16 17 18 19 |
# File 'lib/hearthstone/log/data/game.rb', line 11 def initialize(mode) @mode = mode @results = {} @players = [] @turns = [] add_turn(number: 0, player: nil, timestamp: nil) end |
Instance Attribute Details
#mode ⇒ Object
Returns the value of attribute mode.
8 9 10 |
# File 'lib/hearthstone/log/data/game.rb', line 8 def mode @mode end |
#players ⇒ Object (readonly)
Returns the value of attribute players.
9 10 11 |
# File 'lib/hearthstone/log/data/game.rb', line 9 def players @players end |
#results ⇒ Object (readonly)
Returns the value of attribute results.
9 10 11 |
# File 'lib/hearthstone/log/data/game.rb', line 9 def results @results end |
#turns ⇒ Object (readonly)
Returns the value of attribute turns.
9 10 11 |
# File 'lib/hearthstone/log/data/game.rb', line 9 def turns @turns end |
Instance Method Details
#add_turn(number: nil, player: nil, timestamp: nil) ⇒ Object
proceed to next turn
22 23 24 25 26 27 28 29 30 |
# File 'lib/hearthstone/log/data/game.rb', line 22 def add_turn(number: nil, player: nil, timestamp: nil) @turns << GameTurn.new(number: number, player: player, timestamp: ) if player && number is_first_player = (number % 2 == 1) the_player = players.detect {|p| p.first_player == is_first_player} the_player.name = player if the_player end end |
#completed? ⇒ Boolean
if the game is completed
48 49 50 |
# File 'lib/hearthstone/log/data/game.rb', line 48 def completed? self.results.count == 2 end |
#current_turn ⇒ Object
current turn
33 34 35 |
# File 'lib/hearthstone/log/data/game.rb', line 33 def current_turn @turns.last end |
#filename ⇒ Object
A unique filename for this Game
71 72 73 74 75 76 77 |
# File 'lib/hearthstone/log/data/game.rb', line 71 def filename = turns.detect {|t| t. != nil }. rescue Time.now.to_i time = Time.at().strftime("%Y%m%d_%H%M%S") player1 = players.first.name player2 = players.last.name "#{time}_#{mode}_#{player1}_vs_#{player2}" end |
#player_with_id_or_name(id: nil, name: nil) ⇒ Object
create or get the player, with given id or name
38 39 40 41 42 43 44 45 |
# File 'lib/hearthstone/log/data/game.rb', line 38 def player_with_id_or_name(id: nil, name: nil) player = players.detect {|p| (id && p.id == id) || (name && p.name == name) } unless player player = GamePlayer.new(name: name, id: id) players << player end player end |
#to_hash ⇒ Object
convert the game into hash
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/hearthstone/log/data/game.rb', line 53 def to_hash players_hash = players.collect(&:to_hash) turns_hash = turns.collect(&:to_hash) { mode: mode, players: players_hash, turns: turns_hash, results: results } end |
#to_json ⇒ Object
convert the game into JSON
66 67 68 |
# File 'lib/hearthstone/log/data/game.rb', line 66 def to_json JSON.pretty_generate(to_hash) end |