Class: Hearthstone::Log::Game

Inherits:
Object
  • Object
show all
Defined in:
lib/hearthstone/log/data/game.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#modeObject

Returns the value of attribute mode.



8
9
10
# File 'lib/hearthstone/log/data/game.rb', line 8

def mode
  @mode
end

#playersObject (readonly)

Returns the value of attribute players.



9
10
11
# File 'lib/hearthstone/log/data/game.rb', line 9

def players
  @players
end

#resultsObject (readonly)

Returns the value of attribute results.



9
10
11
# File 'lib/hearthstone/log/data/game.rb', line 9

def results
  @results
end

#turnsObject (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: timestamp)
end

#completed?Boolean

if the game is completed

Returns:

  • (Boolean)


42
43
44
# File 'lib/hearthstone/log/data/game.rb', line 42

def completed?
  self.results.count == 2
end

#current_turnObject

current turn



27
28
29
# File 'lib/hearthstone/log/data/game.rb', line 27

def current_turn
  @turns.last
end

#filenameObject

A unique filename for this Game



65
66
67
68
69
70
71
# File 'lib/hearthstone/log/data/game.rb', line 65

def filename
  timestamp = turns.detect {|t| t.timestamp != nil }.timestamp rescue Time.now.to_i
  time = Time.at(timestamp).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_hashObject

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_jsonObject

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