Class: TikTakTiokiClient::Game

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, http_client) ⇒ Game

Returns a new instance of Game.



30
31
32
33
# File 'lib/tik_tak_tioki_client/game.rb', line 30

def initialize(data, http_client)
  self.data = data
  self.http_client = http_client || HttpClient.new
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



8
9
10
# File 'lib/tik_tak_tioki_client/game.rb', line 8

def data
  @data
end

#http_clientObject

Returns the value of attribute http_client.



8
9
10
# File 'lib/tik_tak_tioki_client/game.rb', line 8

def http_client
  @http_client
end

Class Method Details

.all_joinablesObject



18
19
20
21
22
# File 'lib/tik_tak_tioki_client/game.rb', line 18

def self.all_joinables
  client = HttpClient.new
  response = client.get("/join")
  response.body
end

.create!Object



12
13
14
15
16
# File 'lib/tik_tak_tioki_client/game.rb', line 12

def self.create!
  client = HttpClient.new
  response = client.post("/game")
  Game.new(response.body, client)
end

.join!(game_name) ⇒ Object



24
25
26
27
28
# File 'lib/tik_tak_tioki_client/game.rb', line 24

def self.join!(game_name)
  client = HttpClient.new
  response = client.post("/join", data: { name: game_name })
  Game.new(response.body, client)
end

Instance Method Details

#fetchObject



35
36
37
38
# File 'lib/tik_tak_tioki_client/game.rb', line 35

def fetch
  response = http_client.get("/game", query_params: { player_token: player_token })
  response.body
end

#inspectObject



53
54
55
56
57
58
# File 'lib/tik_tak_tioki_client/game.rb', line 53

def inspect
  fields = %i[name player_role state created_at updated_at]
  inspection = fields.map { |field| "#{field}: #{public_send(field)}" }.join(", ")
  inspection << ", board: " << board.join("-")
  "#<#{self.class.name} #{inspection}>"
end

#make_move(field) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/tik_tak_tioki_client/game.rb', line 44

def make_move(field)
  data = {
    next_move_token: next_move_token,
    field: field
  }
  response = http_client.post("/move", data: data, query_params: { player_token: player_token })
  self.data = response.body
end

#update!Object



40
41
42
# File 'lib/tik_tak_tioki_client/game.rb', line 40

def update!
  self.data = fetch
end