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
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: 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

Returns:

  • (Boolean)


48
49
50
# File 'lib/hearthstone/log/data/game.rb', line 48

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

#current_turnObject

current turn



33
34
35
# File 'lib/hearthstone/log/data/game.rb', line 33

def current_turn
  @turns.last
end

#filenameObject

A unique filename for this Game



71
72
73
74
75
76
77
# File 'lib/hearthstone/log/data/game.rb', line 71

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



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_hashObject

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_jsonObject

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