Skip to content

Commit

Permalink
add snake.api.move
Browse files Browse the repository at this point in the history
  • Loading branch information
Dylan Kendal committed Aug 3, 2016
1 parent 3a6562d commit a29ef32
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 19 deletions.
27 changes: 27 additions & 0 deletions fixture/vcr_cassettes/snake_move.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[
{
"request": {
"body": "{\"turn\":0,\"snakes\":[{\"url\":\"localhost:4000\",\"name\":\"Snek\",\"coords\":[[0,0]]}],\"food\":[],\"board\":[[{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"}],[{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"}],[{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"}],[{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"}],[{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"}],[{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"}],[{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"}],[{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"}],[{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"}],[{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"},{\"state\":\"empty\"}]]}",
"headers": {
"content-type": "application/json"
},
"method": "post",
"options": [],
"request_body": "",
"url": "http://localhost:4000/move"
},
"response": {
"body": "{\"taunt\":\"down\",\"move\":\"down\"}",
"headers": {
"server": "Cowboy",
"date": "Wed, 03 Aug 2016 02:29:11 GMT",
"content-length": "30",
"content-type": "application/json; charset=utf-8",
"cache-control": "max-age=0, private, must-revalidate",
"x-request-id": "flkustkkula3vt4mfv4d15oinfisch02"
},
"status_code": 200,
"type": "ok"
}
}
]
3 changes: 3 additions & 0 deletions lib/battle_snake/move.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
defmodule BattleSnake.Move do
defstruct [:move, :taunt]
end
17 changes: 9 additions & 8 deletions lib/battle_snake_server/snake/api.ex
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
defmodule BattleSnakeServer.Snake.Api do
alias BattleSnake.Snake
alias BattleSnake.{Snake, Move}

use HTTPoison.Base

@spec load(BattleSnakeServer.Snake, BattleSnakeServer.Game) :: BattleSnake.Snake
def load(snake, game) do
url = snake.url <> "/start"
response = post! url, payload(game), headers
Poison.decode!(response.body, as: %Snake{})
Poison.decode!(response.body, as: %Snake{url: snake.url})
end

def load(snake, game) do
url = snake.url <> "/start"
response = post! url, payload(game), headers
Poison.decode!(response.body, as: %Snake{})
def move(snake, world) do
url = snake.url <> "/move"
payload = Poison.encode!(world)
response = post! url, payload, headers
Poison.decode!(response.body, as: %Move{})
end

def payload(game) do
%{
Poison.encode! %{
game_id: game.id,
height: game.height,
width: game.width,
} |> Poison.encode!
}
end

def headers() do
Expand Down
45 changes: 34 additions & 11 deletions test/battle_snake_server/snake/api_test.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule BattleSnakeServer.Snake.ApiTest do
alias BattleSnake.{World, Move}
alias BattleSnakeServer.Game
alias BattleSnakeServer.Snake.Api

Expand All @@ -8,22 +9,44 @@ defmodule BattleSnakeServer.Snake.ApiTest do
setup do
Api.start

snake = %BattleSnakeServer.Snake{url: "localhost:4000"}
snake_form = %BattleSnakeServer.Snake{url: "localhost:4000"}

snake = %BattleSnake.Snake{
url: "localhost:4000",
color: "#6699ff",
head_url: "",
name: "Snek",
taunt: "gotta go fast",
}

game = %Game{id: "sup", width: 20, height: 20}

%{snake: snake, game: game}
world = %World{
width: 10,
height: 10,
snakes: [
%{snake| coords: [[0,0]]}
]
}

%{
game: game,
snake: snake,
snake_form: snake_form,
world: world,
}
end

test "#load", %{game: game, snake: snake} do
test "#load", %{game: game, snake: snake, snake_form: snake_form} do
use_cassette "snake start" do
snake = Api.load(snake, game)

%BattleSnake.Snake{
color: "#6699ff",
head_url: "",
name: "Snek",
taunt: "gotta go fast",
} = snake
assert Api.load(snake_form, game) == snake
end
end

test "#move", %{world: world, snake: snake} do
use_cassette "snake move" do
actual = Api.move(snake, world)
assert actual == %Move{move: "down", taunt: "down"}
end
end
end

0 comments on commit a29ef32

Please sign in to comment.