Class: Battle::Game

Inherits:
Object
  • Object
show all
Defined in:
lib/battle/game.rb

Constant Summary collapse

STATUSES =
%w[start victory defeat]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil, email = nil, id: nil) ⇒ Game

Returns a new instance of Game.



8
9
10
11
12
13
14
15
16
17
# File 'lib/battle/game.rb', line 8

def initialize(name = nil, email = nil, id: nil)
  @name = name
  @email = email
  @id = id
  @coords = [nil, nil]
  @status = id ? 'start': 'init'
  @ships = []
  @response = {}
  Battle.ships.each { |name| @ships << Ship.new(name) }
end

Instance Attribute Details

#coordsObject (readonly)

Returns the value of attribute coords.



3
4
5
# File 'lib/battle/game.rb', line 3

def coords
  @coords
end

#emailObject (readonly)

Returns the value of attribute email.



3
4
5
# File 'lib/battle/game.rb', line 3

def email
  @email
end

#idObject (readonly)

Returns the value of attribute id.



3
4
5
# File 'lib/battle/game.rb', line 3

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/battle/game.rb', line 3

def name
  @name
end

#nuke_statusObject (readonly)

Returns the value of attribute nuke_status.



3
4
5
# File 'lib/battle/game.rb', line 3

def nuke_status
  @nuke_status
end

#prizeObject (readonly)

Returns the value of attribute prize.



3
4
5
# File 'lib/battle/game.rb', line 3

def prize
  @prize
end

#responseObject (readonly)

Returns the value of attribute response.



3
4
5
# File 'lib/battle/game.rb', line 3

def response
  @response
end

#shipsObject (readonly)

Returns the value of attribute ships.



3
4
5
# File 'lib/battle/game.rb', line 3

def ships
  @ships
end

#statusObject (readonly)

Returns the value of attribute status.



3
4
5
# File 'lib/battle/game.rb', line 3

def status
  @status
end

#sunkObject (readonly)

Returns the value of attribute sunk.



3
4
5
# File 'lib/battle/game.rb', line 3

def sunk
  @sunk
end

Instance Method Details

#finished?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/battle/game.rb', line 43

def finished?
  status == "lost" || status == "victory"
end

#init?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/battle/game.rb', line 39

def init?
  status == "init"
end

#nuke(x, y) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/battle/game.rb', line 29

def nuke(x, y)
  raise GameNotStartedYetError if init?
  raise GameAlreadyFinishedError if finished?

  do_request NUKE_URL, id: id, x: x, y: y
  handle_nuke

  response
end

#register!Object



19
20
21
22
23
24
25
26
27
# File 'lib/battle/game.rb', line 19

def register!
  raise PlayerNameNotSpecified if name.nil? || name.empty?
  raise PlayerEmailNotSpecified if email.nil? || email.empty?

  do_request REGISTER_URL, "name" => name, "email" => email
  handle_register

  response
end