Class: Hearthstone::Models::Game
- Inherits:
-
Object
- Object
- Hearthstone::Models::Game
- Defined in:
- lib/hearthstone/models/game.rb
Instance Attribute Summary collapse
-
#entities ⇒ Object
readonly
Returns the value of attribute entities.
-
#players ⇒ Object
readonly
Returns the value of attribute players.
-
#store ⇒ Object
readonly
Returns the value of attribute store.
-
#turn ⇒ Object
Returns the value of attribute turn.
Instance Method Summary collapse
-
#add_player(id: nil, name: nil, first_player: nil, hero_id: nil, hero_card_id: nil, hero_power_id: nil, hero_power_card_id: nil) ⇒ Object
Events.
- #apply_damage(id: nil, amount: 0, card_id: nil, player_id: nil) ⇒ Object (also: #damage)
- #card_added_to_deck(player_id: nil, id: nil, card_id: nil) ⇒ Object
- #card_attached(attachment_id: nil, target_id: nil) ⇒ Object
- #card_destroyed(player_id: nil, id: nil, card_id: nil) ⇒ Object (also: #card_discarded)
- #card_drawn(player_id: nil, id: nil, card_id: nil) ⇒ Object
- #card_played(player_id: nil, id: nil, card_id: nil) ⇒ Object
- #card_put_in_play(player_id: nil, id: nil, card_id: nil) ⇒ Object
- #card_received(player_id: nil, id: nil, card_id: nil) ⇒ Object
- #card_revealed(id: nil, card_id: nil, player_id: nil) ⇒ Object
- #card_with_card_id(card_id) ⇒ Object
-
#entity_with_id(id, card_id: nil) ⇒ Object
Accessors.
-
#initialize(store = CardStore.new) ⇒ Game
constructor
A new instance of Game.
- #open_card(id: nil, card_id: nil) ⇒ Object
- #player_with_id(id) ⇒ Object
-
#to_s ⇒ Object
Public.
Constructor Details
#initialize(store = CardStore.new) ⇒ Game
Returns a new instance of Game.
14 15 16 17 18 |
# File 'lib/hearthstone/models/game.rb', line 14 def initialize(store=CardStore.new) @store = store @entities = {} @players = [] end |
Instance Attribute Details
#entities ⇒ Object (readonly)
Returns the value of attribute entities.
11 12 13 |
# File 'lib/hearthstone/models/game.rb', line 11 def entities @entities end |
#players ⇒ Object (readonly)
Returns the value of attribute players.
11 12 13 |
# File 'lib/hearthstone/models/game.rb', line 11 def players @players end |
#store ⇒ Object (readonly)
Returns the value of attribute store.
11 12 13 |
# File 'lib/hearthstone/models/game.rb', line 11 def store @store end |
#turn ⇒ Object
Returns the value of attribute turn.
12 13 14 |
# File 'lib/hearthstone/models/game.rb', line 12 def turn @turn end |
Instance Method Details
#add_player(id: nil, name: nil, first_player: nil, hero_id: nil, hero_card_id: nil, hero_power_id: nil, hero_power_card_id: nil) ⇒ Object
Events
22 23 24 25 26 27 |
# File 'lib/hearthstone/models/game.rb', line 22 def add_player(id: nil, name: nil, first_player: nil, hero_id: nil, hero_card_id: nil, hero_power_id: nil, hero_power_card_id: nil) hero = entity_with_id(hero_id, card_id: hero_card_id) hero_power = entity_with_id(hero_power_id, card_id: hero_power_card_id) player = Player.new(id: id, name: name, first_player: first_player, hero: hero, hero_power: hero_power) self.players << player end |
#apply_damage(id: nil, amount: 0, card_id: nil, player_id: nil) ⇒ Object Also known as: damage
70 71 72 73 |
# File 'lib/hearthstone/models/game.rb', line 70 def apply_damage(id: nil, amount: 0, card_id: nil, player_id: nil) target = entity_with_id(id) target.damaged = amount end |
#card_added_to_deck(player_id: nil, id: nil, card_id: nil) ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/hearthstone/models/game.rb', line 37 def card_added_to_deck(player_id: nil, id: nil, card_id: nil) entity = entity_with_id(id, card_id: card_id) entity.card = card_with_card_id(card_id) player = player_with_id(player_id) raise "Player #{player_id} not found!" unless player player.move_card(entity, :deck) end |
#card_attached(attachment_id: nil, target_id: nil) ⇒ Object
64 65 66 67 68 |
# File 'lib/hearthstone/models/game.rb', line 64 def card_attached(attachment_id: nil, target_id: nil) target = entity_with_id(target_id) = entity_with_id() target.attach() end |
#card_destroyed(player_id: nil, id: nil, card_id: nil) ⇒ Object Also known as: card_discarded
86 87 88 89 90 91 92 |
# File 'lib/hearthstone/models/game.rb', line 86 def card_destroyed(player_id: nil, id: nil, card_id: nil) player = player_with_id(player_id) target = entity_with_id(id) raise "Player #{player_id} not found!" unless player player.move_card(target, :graveyard) end |
#card_drawn(player_id: nil, id: nil, card_id: nil) ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/hearthstone/models/game.rb', line 55 def card_drawn(player_id: nil, id: nil, card_id: nil) entity = entity_with_id(id, card_id: card_id) entity.card = card_with_card_id(card_id) player = player_with_id(player_id) raise "Player #{player_id} not found!" unless player player.move_card(entity, :hand) end |
#card_played(player_id: nil, id: nil, card_id: nil) ⇒ Object
76 77 78 79 80 81 82 83 84 |
# File 'lib/hearthstone/models/game.rb', line 76 def card_played(player_id: nil, id: nil, card_id: nil) player = player_with_id(player_id) target = entity_with_id(id) target.card = card_with_card_id(card_id) raise "Player #{player_id} not found!" unless player #binding.pry player.move_card(target, :play) end |
#card_put_in_play(player_id: nil, id: nil, card_id: nil) ⇒ Object
95 96 97 98 99 100 101 102 |
# File 'lib/hearthstone/models/game.rb', line 95 def card_put_in_play(player_id: nil, id: nil, card_id: nil) entity = entity_with_id(id, card_id: card_id) entity.card = card_with_card_id(card_id) player = player_with_id(player_id) raise "Player #{player_id} not found!" unless player player.move_card(entity, :play) end |
#card_received(player_id: nil, id: nil, card_id: nil) ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/hearthstone/models/game.rb', line 46 def card_received(player_id: nil, id: nil, card_id: nil) entity = entity_with_id(id, card_id: card_id) entity.card = card_with_card_id(card_id) player = player_with_id(player_id) raise "Player #{player_id} not found!" unless player player.move_card(entity, :hand) end |
#card_revealed(id: nil, card_id: nil, player_id: nil) ⇒ Object
33 34 35 |
# File 'lib/hearthstone/models/game.rb', line 33 def card_revealed(id: nil, card_id: nil, player_id: nil) entity_with_id(id).card = card_with_card_id(card_id) end |
#card_with_card_id(card_id) ⇒ Object
119 120 121 122 123 124 125 126 |
# File 'lib/hearthstone/models/game.rb', line 119 def card_with_card_id(card_id) card = nil if card_id && card_id != "" card = self.store.card_with_id(card_id) raise "Card #{card_id} not found!" unless card end card end |
#entity_with_id(id, card_id: nil) ⇒ Object
Accessors
106 107 108 109 110 111 112 113 |
# File 'lib/hearthstone/models/game.rb', line 106 def entity_with_id(id, card_id: nil) entity = entities[id] unless entity entity = Entity.new(id: id, card: card_with_card_id(card_id)) entities[id] = entity end entity end |
#open_card(id: nil, card_id: nil) ⇒ Object
29 30 31 |
# File 'lib/hearthstone/models/game.rb', line 29 def open_card(id: nil, card_id: nil) entity_with_id(id).card = card_with_card_id(card_id) end |
#player_with_id(id) ⇒ Object
115 116 117 |
# File 'lib/hearthstone/models/game.rb', line 115 def player_with_id(id) self.players.detect{|p| p.id == id} end |
#to_s ⇒ Object
Public
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/hearthstone/models/game.rb', line 130 def to_s player1 = self.players.first player2 = self.players.last """======================= Turn ##{turn} ===================== #{player1.name} (#{player1.hero.current_health}) ====================================================== #{player1.play.collect(&:to_s).join("\n")} ---------------------------------------------------- #{player2.play.collect(&:to_s).join("\n")} ====================================================== #{player2.name} (#{player2.hero.current_health}) ====================================================== """ end |