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 |
# 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: ) end |
#completed? ⇒ Boolean
if the game is completed
42 43 44 |
# File 'lib/hearthstone/log/data/game.rb', line 42 def completed? self.results.count == 2 end |
#current_turn ⇒ Object
current turn
27 28 29 |
# File 'lib/hearthstone/log/data/game.rb', line 27 def current_turn @turns.last end |
#filename ⇒ Object
A unique filename for this Game
65 66 67 68 69 70 71 |
# File 'lib/hearthstone/log/data/game.rb', line 65 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
32 33 34 35 36 37 38 39 |
# File 'lib/hearthstone/log/data/game.rb', line 32 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
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/hearthstone/log/data/game.rb', line 47 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
60 61 62 |
# File 'lib/hearthstone/log/data/game.rb', line 60 def to_json JSON.pretty_generate(to_hash) end |