From ce49a4bcf15eeb94e076ed76dbe5d18fbe5443de Mon Sep 17 00:00:00 2001 From: Bijan Date: Sun, 14 Jan 2018 22:38:46 -0500 Subject: [PATCH 1/4] Generate gameplays resources --- lib/platform/products/gameplay.ex | 5 +- lib/platform/products/products.ex | 114 +++++++++++++++--- .../controllers/gameplay_controller.ex | 42 +++++++ lib/platform_web/router.ex | 1 + lib/platform_web/views/gameplay_view.ex | 17 +++ .../20170826154100_create_games.exs | 8 -- .../20180115032842_create_gameplays.exs | 16 +++ test/platform/products/products_test.exs | 60 +++++++++ .../controllers/gameplay_controller_test.exs | 79 ++++++++++++ 9 files changed, 317 insertions(+), 25 deletions(-) create mode 100644 lib/platform_web/controllers/gameplay_controller.ex create mode 100644 lib/platform_web/views/gameplay_view.ex create mode 100644 priv/repo/migrations/20180115032842_create_gameplays.exs create mode 100644 test/platform_web/controllers/gameplay_controller_test.exs diff --git a/lib/platform/products/gameplay.ex b/lib/platform/products/gameplay.ex index d6adf32..7eeedf9 100644 --- a/lib/platform/products/gameplay.ex +++ b/lib/platform/products/gameplay.ex @@ -9,10 +9,11 @@ defmodule Platform.Products.Gameplay do use Ecto.Schema import Ecto.Changeset - alias Platform.Products.Gameplay alias Platform.Products.Game + alias Platform.Products.Gameplay alias Platform.Accounts.Player + schema "gameplays" do belongs_to :game, Game belongs_to :player, Player @@ -26,6 +27,6 @@ defmodule Platform.Products.Gameplay do def changeset(%Gameplay{} = gameplay, attrs) do gameplay |> cast(attrs, [:game_id, :player_id, :player_score]) - |> validate_required([:game_id, :player_id, :player_score]) + |> validate_required([:player_score]) end end diff --git a/lib/platform/products/products.ex b/lib/platform/products/products.ex index 747f443..3f9117e 100644 --- a/lib/platform/products/products.ex +++ b/lib/platform/products/products.ex @@ -22,10 +22,6 @@ defmodule Platform.Products do Repo.all(Game) end - def list_gameplays do - Repo.all(Gameplay) - end - @doc """ Gets a single game. @@ -43,11 +39,6 @@ defmodule Platform.Products do def get_game!(id), do: Repo.get!(Game, id) def get_game_by_slug!(slug), do: Repo.get_by!(Game, slug: slug) - def get_gameplays_by_id!(id) do - query = from gp in "gameplays", where: gp.game_id == ^id, select: [:player_id, :player_score] - Repo.all(query) - end - @doc """ Creates a game. @@ -66,12 +57,6 @@ defmodule Platform.Products do |> Repo.insert() end - def create_gameplay(attrs \\ %{}) do - %Gameplay{} - |> Gameplay.changeset(attrs) - |> Repo.insert() - end - @doc """ Updates a game. @@ -118,4 +103,103 @@ defmodule Platform.Products do def change_game(%Game{} = game) do Game.changeset(game, %{}) end + + @doc """ + Returns the list of gameplays. + + ## Examples + + iex> list_gameplays() + [%Gameplay{}, ...] + + """ + def list_gameplays do + Repo.all(Gameplay) + end + + @doc """ + Gets a single gameplay. + + Raises `Ecto.NoResultsError` if the Gameplay does not exist. + + ## Examples + + iex> get_gameplay!(123) + %Gameplay{} + + iex> get_gameplay!(456) + ** (Ecto.NoResultsError) + + """ + def get_gameplay!(id), do: Repo.get!(Gameplay, id) + + def get_gameplays_by_id!(id) do + query = from gp in "gameplays", where: gp.game_id == ^id, select: [:player_id, :player_score] + Repo.all(query) + end + + @doc """ + Creates a gameplay. + + ## Examples + + iex> create_gameplay(%{field: value}) + {:ok, %Gameplay{}} + + iex> create_gameplay(%{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def create_gameplay(attrs \\ %{}) do + %Gameplay{} + |> Gameplay.changeset(attrs) + |> Repo.insert() + end + + @doc """ + Updates a gameplay. + + ## Examples + + iex> update_gameplay(gameplay, %{field: new_value}) + {:ok, %Gameplay{}} + + iex> update_gameplay(gameplay, %{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def update_gameplay(%Gameplay{} = gameplay, attrs) do + gameplay + |> Gameplay.changeset(attrs) + |> Repo.update() + end + + @doc """ + Deletes a Gameplay. + + ## Examples + + iex> delete_gameplay(gameplay) + {:ok, %Gameplay{}} + + iex> delete_gameplay(gameplay) + {:error, %Ecto.Changeset{}} + + """ + def delete_gameplay(%Gameplay{} = gameplay) do + Repo.delete(gameplay) + end + + @doc """ + Returns an `%Ecto.Changeset{}` for tracking gameplay changes. + + ## Examples + + iex> change_gameplay(gameplay) + %Ecto.Changeset{source: %Gameplay{}} + + """ + def change_gameplay(%Gameplay{} = gameplay) do + Gameplay.changeset(gameplay, %{}) + end end diff --git a/lib/platform_web/controllers/gameplay_controller.ex b/lib/platform_web/controllers/gameplay_controller.ex new file mode 100644 index 0000000..67fcc2d --- /dev/null +++ b/lib/platform_web/controllers/gameplay_controller.ex @@ -0,0 +1,42 @@ +defmodule PlatformWeb.GameplayController do + use PlatformWeb, :controller + + alias Platform.Products + alias Platform.Products.Gameplay + + action_fallback PlatformWeb.FallbackController + + def index(conn, _params) do + gameplays = Products.list_gameplays() + render(conn, "index.json", gameplays: gameplays) + end + + def create(conn, %{"gameplay" => gameplay_params}) do + with {:ok, %Gameplay{} = gameplay} <- Products.create_gameplay(gameplay_params) do + conn + |> put_status(:created) + |> put_resp_header("location", gameplay_path(conn, :show, gameplay)) + |> render("show.json", gameplay: gameplay) + end + end + + def show(conn, %{"id" => id}) do + gameplay = Products.get_gameplay!(id) + render(conn, "show.json", gameplay: gameplay) + end + + def update(conn, %{"id" => id, "gameplay" => gameplay_params}) do + gameplay = Products.get_gameplay!(id) + + with {:ok, %Gameplay{} = gameplay} <- Products.update_gameplay(gameplay, gameplay_params) do + render(conn, "show.json", gameplay: gameplay) + end + end + + def delete(conn, %{"id" => id}) do + gameplay = Products.get_gameplay!(id) + with {:ok, %Gameplay{}} <- Products.delete_gameplay(gameplay) do + send_resp(conn, :no_content, "") + end + end +end diff --git a/lib/platform_web/router.ex b/lib/platform_web/router.ex index de76ace..efd77a6 100644 --- a/lib/platform_web/router.ex +++ b/lib/platform_web/router.ex @@ -29,6 +29,7 @@ defmodule PlatformWeb.Router do resources "/players", PlayerApiController, except: [:new, :edit] resources "/games", GameController, except: [:new, :edit] + resources "/gameplays", GameplayController, except: [:new, :edit] end defp put_user_token(conn, _) do diff --git a/lib/platform_web/views/gameplay_view.ex b/lib/platform_web/views/gameplay_view.ex new file mode 100644 index 0000000..9c4cdf7 --- /dev/null +++ b/lib/platform_web/views/gameplay_view.ex @@ -0,0 +1,17 @@ +defmodule PlatformWeb.GameplayView do + use PlatformWeb, :view + alias PlatformWeb.GameplayView + + def render("index.json", %{gameplays: gameplays}) do + %{data: render_many(gameplays, GameplayView, "gameplay.json")} + end + + def render("show.json", %{gameplay: gameplay}) do + %{data: render_one(gameplay, GameplayView, "gameplay.json")} + end + + def render("gameplay.json", %{gameplay: gameplay}) do + %{id: gameplay.id, + player_score: gameplay.player_score} + end +end diff --git a/priv/repo/migrations/20170826154100_create_games.exs b/priv/repo/migrations/20170826154100_create_games.exs index d657eed..2a5821d 100644 --- a/priv/repo/migrations/20170826154100_create_games.exs +++ b/priv/repo/migrations/20170826154100_create_games.exs @@ -10,13 +10,5 @@ defmodule Platform.Repo.Migrations.CreateGames do timestamps() end - - create table(:gameplays) do - add :game_id, references(:games, on_delete: :nothing), null: false - add :player_id, references(:players, on_delete: :nothing), null: false - add :player_score, :integer - - timestamps() - end end end diff --git a/priv/repo/migrations/20180115032842_create_gameplays.exs b/priv/repo/migrations/20180115032842_create_gameplays.exs new file mode 100644 index 0000000..939c73a --- /dev/null +++ b/priv/repo/migrations/20180115032842_create_gameplays.exs @@ -0,0 +1,16 @@ +defmodule Platform.Repo.Migrations.CreateGameplays do + use Ecto.Migration + + def change do + create table(:gameplays) do + add :player_score, :integer + add :game_id, references(:games, on_delete: :nothing) + add :player_id, references(:players, on_delete: :nothing) + + timestamps() + end + + create index(:gameplays, [:game_id]) + create index(:gameplays, [:player_id]) + end +end diff --git a/test/platform/products/products_test.exs b/test/platform/products/products_test.exs index 15e7b00..e8a3d32 100644 --- a/test/platform/products/products_test.exs +++ b/test/platform/products/products_test.exs @@ -68,4 +68,64 @@ defmodule Platform.ProductsTest do assert %Ecto.Changeset{} = Products.change_game(game) end end + + describe "gameplays" do + alias Platform.Products.Gameplay + + @valid_attrs %{player_score: 42} + @update_attrs %{player_score: 43} + @invalid_attrs %{player_score: nil} + + def gameplay_fixture(attrs \\ %{}) do + {:ok, gameplay} = + attrs + |> Enum.into(@valid_attrs) + |> Products.create_gameplay() + + gameplay + end + + test "list_gameplays/0 returns all gameplays" do + gameplay = gameplay_fixture() + assert Products.list_gameplays() == [gameplay] + end + + test "get_gameplay!/1 returns the gameplay with given id" do + gameplay = gameplay_fixture() + assert Products.get_gameplay!(gameplay.id) == gameplay + end + + test "create_gameplay/1 with valid data creates a gameplay" do + assert {:ok, %Gameplay{} = gameplay} = Products.create_gameplay(@valid_attrs) + assert gameplay.player_score == 42 + end + + test "create_gameplay/1 with invalid data returns error changeset" do + assert {:error, %Ecto.Changeset{}} = Products.create_gameplay(@invalid_attrs) + end + + test "update_gameplay/2 with valid data updates the gameplay" do + gameplay = gameplay_fixture() + assert {:ok, gameplay} = Products.update_gameplay(gameplay, @update_attrs) + assert %Gameplay{} = gameplay + assert gameplay.player_score == 43 + end + + test "update_gameplay/2 with invalid data returns error changeset" do + gameplay = gameplay_fixture() + assert {:error, %Ecto.Changeset{}} = Products.update_gameplay(gameplay, @invalid_attrs) + assert gameplay == Products.get_gameplay!(gameplay.id) + end + + test "delete_gameplay/1 deletes the gameplay" do + gameplay = gameplay_fixture() + assert {:ok, %Gameplay{}} = Products.delete_gameplay(gameplay) + assert_raise Ecto.NoResultsError, fn -> Products.get_gameplay!(gameplay.id) end + end + + test "change_gameplay/1 returns a gameplay changeset" do + gameplay = gameplay_fixture() + assert %Ecto.Changeset{} = Products.change_gameplay(gameplay) + end + end end diff --git a/test/platform_web/controllers/gameplay_controller_test.exs b/test/platform_web/controllers/gameplay_controller_test.exs new file mode 100644 index 0000000..e2419ce --- /dev/null +++ b/test/platform_web/controllers/gameplay_controller_test.exs @@ -0,0 +1,79 @@ +defmodule PlatformWeb.GameplayControllerTest do + use PlatformWeb.ConnCase + + alias Platform.Products + alias Platform.Products.Gameplay + + @create_attrs %{player_score: 42} + @update_attrs %{player_score: 43} + @invalid_attrs %{player_score: nil} + + def fixture(:gameplay) do + {:ok, gameplay} = Products.create_gameplay(@create_attrs) + gameplay + end + + setup %{conn: conn} do + {:ok, conn: put_req_header(conn, "accept", "application/json")} + end + + describe "index" do + test "lists all gameplays", %{conn: conn} do + conn = get conn, gameplay_path(conn, :index) + assert json_response(conn, 200)["data"] == [] + end + end + + describe "create gameplay" do + test "renders gameplay when data is valid", %{conn: conn} do + conn = post conn, gameplay_path(conn, :create), gameplay: @create_attrs + assert %{"id" => id} = json_response(conn, 201)["data"] + + conn = get conn, gameplay_path(conn, :show, id) + assert json_response(conn, 200)["data"] == %{ + "id" => id, + "player_score" => 42} + end + + test "renders errors when data is invalid", %{conn: conn} do + conn = post conn, gameplay_path(conn, :create), gameplay: @invalid_attrs + assert json_response(conn, 422)["errors"] != %{} + end + end + + describe "update gameplay" do + setup [:create_gameplay] + + test "renders gameplay when data is valid", %{conn: conn, gameplay: %Gameplay{id: id} = gameplay} do + conn = put conn, gameplay_path(conn, :update, gameplay), gameplay: @update_attrs + assert %{"id" => ^id} = json_response(conn, 200)["data"] + + conn = get conn, gameplay_path(conn, :show, id) + assert json_response(conn, 200)["data"] == %{ + "id" => id, + "player_score" => 43} + end + + test "renders errors when data is invalid", %{conn: conn, gameplay: gameplay} do + conn = put conn, gameplay_path(conn, :update, gameplay), gameplay: @invalid_attrs + assert json_response(conn, 422)["errors"] != %{} + end + end + + describe "delete gameplay" do + setup [:create_gameplay] + + test "deletes chosen gameplay", %{conn: conn, gameplay: gameplay} do + conn = delete conn, gameplay_path(conn, :delete, gameplay) + assert response(conn, 204) + assert_error_sent 404, fn -> + get conn, gameplay_path(conn, :show, gameplay) + end + end + end + + defp create_gameplay(_) do + gameplay = fixture(:gameplay) + {:ok, gameplay: gameplay} + end +end From 7eb2c33e52f4d1b785454c9c93d6b91c1cedd3ac Mon Sep 17 00:00:00 2001 From: Bijan Date: Sun, 14 Jan 2018 22:48:26 -0500 Subject: [PATCH 2/4] Update player's total score on gameplay creation --- lib/platform/products/products.ex | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/platform/products/products.ex b/lib/platform/products/products.ex index 3f9117e..0b42545 100644 --- a/lib/platform/products/products.ex +++ b/lib/platform/products/products.ex @@ -6,6 +6,7 @@ defmodule Platform.Products do import Ecto.Query, warn: false alias Platform.Repo + alias Platform.Accounts alias Platform.Products.Game alias Platform.Products.Gameplay @@ -151,6 +152,13 @@ defmodule Platform.Products do """ def create_gameplay(attrs \\ %{}) do + # Update player total score. + unless attrs[:player_id] == nil do + player = Accounts.get_player!(attrs[:player_id]) + Accounts.update_player(player, %{score: player.score + attrs[:player_score]}) + end + + # Create gameplay record. %Gameplay{} |> Gameplay.changeset(attrs) |> Repo.insert() From 0bf5045ae8e5c7ec4ae69ff94dcf8555bc59f7c9 Mon Sep 17 00:00:00 2001 From: Bijan Date: Sun, 14 Jan 2018 23:01:51 -0500 Subject: [PATCH 3/4] Render gameplay data for game page --- assets/elm/Platformer.elm | 58 ++++++----- assets/js/elm.js | 122 ++++++++++++------------ lib/platform_web/views/gameplay_view.ex | 2 + 3 files changed, 97 insertions(+), 85 deletions(-) diff --git a/assets/elm/Platformer.elm b/assets/elm/Platformer.elm index 9e68496..0661e81 100644 --- a/assets/elm/Platformer.elm +++ b/assets/elm/Platformer.elm @@ -140,7 +140,8 @@ init : Flags -> ( Model, Cmd Msg ) init flags = ( initialModel flags , Cmd.batch - [ fetchPlayersList + [ fetchGameplaysList + , fetchPlayersList , Cmd.map PhoenixMsg (initialSocketCommand flags) ] ) @@ -172,6 +173,27 @@ decodePlayer = (Decode.field "username" Decode.string) +fetchGameplaysList : Cmd Msg +fetchGameplaysList = + Http.get "/api/gameplays" decodeGameplaysList + |> Http.send FetchGameplaysList + + +decodeGameplaysList : Decode.Decoder (List Gameplay) +decodeGameplaysList = + decodeGameplay + |> Decode.list + |> Decode.at [ "data" ] + + +decodeGameplay : Decode.Decoder Gameplay +decodeGameplay = + Decode.map3 Gameplay + (Decode.field "game_id" Decode.int) + (Decode.field "player_id" Decode.int) + (Decode.field "player_score" Decode.int) + + anonymousPlayer : Player anonymousPlayer = { displayName = Just "Anonymous User" @@ -181,14 +203,6 @@ anonymousPlayer = } -scoreDecoder : Decode.Decoder Gameplay -scoreDecoder = - Decode.map3 Gameplay - (Decode.field "game_id" Decode.int) - (Decode.field "player_id" Decode.int) - (Decode.field "player_score" Decode.int) - - -- UPDATE @@ -196,6 +210,7 @@ scoreDecoder = type Msg = NoOp | CountdownTimer Time + | FetchGameplaysList (Result Http.Error (List Gameplay)) | FetchPlayersList (Result Http.Error (List Player)) | KeyDown KeyCode | PhoenixMsg (Phoenix.Socket.Msg Msg) @@ -219,6 +234,14 @@ update msg model = else ( model, Cmd.none ) + FetchGameplaysList result -> + case result of + Ok gameplays -> + ( { model | gameplays = gameplays }, Cmd.none ) + + Err message -> + ( { model | errors = toString message }, Cmd.none ) + FetchPlayersList result -> case result of Ok players -> @@ -279,7 +302,7 @@ update msg model = ) ReceiveScoreChanges raw -> - case Decode.decodeValue scoreDecoder raw of + case Decode.decodeValue decodeGameplay raw of Ok scoreChange -> ( { model | gameplays = scoreChange :: model.gameplays }, Cmd.none ) @@ -420,21 +443,6 @@ viewGameplayItem model gameplay = ] -playersListItem : Player -> Html msg -playersListItem player = - let - displayName = - if player.displayName == Nothing then - player.username - else - Maybe.withDefault "" player.displayName - in - li [ Html.Attributes.class "player-item list-group-item" ] - [ strong [] [ text displayName ] - , span [ Html.Attributes.class "badge" ] [ text (toString player.score) ] - ] - - viewGame : Model -> Svg Msg viewGame model = svg [ version "1.1", width "600", height "400" ] diff --git a/assets/js/elm.js b/assets/js/elm.js index 2ffe616..30de11b 100644 --- a/assets/js/elm.js +++ b/assets/js/elm.js @@ -16428,44 +16428,6 @@ var _user$project$Platformer$viewGame = function (model) { }, _user$project$Platformer$viewGameState(model)); }; -var _user$project$Platformer$playersListItem = function (player) { - var displayName = _elm_lang$core$Native_Utils.eq(player.displayName, _elm_lang$core$Maybe$Nothing) ? player.username : A2(_elm_lang$core$Maybe$withDefault, '', player.displayName); - return A2( - _elm_lang$html$Html$li, - { - ctor: '::', - _0: _elm_lang$html$Html_Attributes$class('player-item list-group-item'), - _1: {ctor: '[]'} - }, - { - ctor: '::', - _0: A2( - _elm_lang$html$Html$strong, - {ctor: '[]'}, - { - ctor: '::', - _0: _elm_lang$svg$Svg$text(displayName), - _1: {ctor: '[]'} - }), - _1: { - ctor: '::', - _0: A2( - _elm_lang$html$Html$span, - { - ctor: '::', - _0: _elm_lang$html$Html_Attributes$class('badge'), - _1: {ctor: '[]'} - }, - { - ctor: '::', - _0: _elm_lang$svg$Svg$text( - _elm_lang$core$Basics$toString(player.score)), - _1: {ctor: '[]'} - }), - _1: {ctor: '[]'} - } - }); -}; var _user$project$Platformer$characterFoundItem = function (model) { var approximateItemUpperBound = model.itemPositionX; var approximateItemLowerBound = model.itemPositionX - 35; @@ -16606,12 +16568,20 @@ var _user$project$Platformer$Gameplay = F3( function (a, b, c) { return {gameId: a, playerId: b, playerScore: c}; }); -var _user$project$Platformer$scoreDecoder = A4( +var _user$project$Platformer$decodeGameplay = A4( _elm_lang$core$Json_Decode$map3, _user$project$Platformer$Gameplay, A2(_elm_lang$core$Json_Decode$field, 'game_id', _elm_lang$core$Json_Decode$int), A2(_elm_lang$core$Json_Decode$field, 'player_id', _elm_lang$core$Json_Decode$int), A2(_elm_lang$core$Json_Decode$field, 'player_score', _elm_lang$core$Json_Decode$int)); +var _user$project$Platformer$decodeGameplaysList = A2( + _elm_lang$core$Json_Decode$at, + { + ctor: '::', + _0: 'data', + _1: {ctor: '[]'} + }, + _elm_lang$core$Json_Decode$list(_user$project$Platformer$decodeGameplay)); var _user$project$Platformer$Player = F4( function (a, b, c, d) { return {displayName: a, id: b, score: c, username: d}; @@ -16786,14 +16756,14 @@ var _user$project$Platformer$update = F2( {timeRemaining: model.timeRemaining - 1}), _1: _elm_lang$core$Platform_Cmd$none } : {ctor: '_Tuple2', _0: model, _1: _elm_lang$core$Platform_Cmd$none}; - case 'FetchPlayersList': + case 'FetchGameplaysList': var _p3 = _p2._0; if (_p3.ctor === 'Ok') { return { ctor: '_Tuple2', _0: _elm_lang$core$Native_Utils.update( model, - {playersList: _p3._0}), + {gameplays: _p3._0}), _1: _elm_lang$core$Platform_Cmd$none }; } else { @@ -16807,9 +16777,30 @@ var _user$project$Platformer$update = F2( _1: _elm_lang$core$Platform_Cmd$none }; } - case 'KeyDown': + case 'FetchPlayersList': var _p4 = _p2._0; - switch (_p4) { + if (_p4.ctor === 'Ok') { + return { + ctor: '_Tuple2', + _0: _elm_lang$core$Native_Utils.update( + model, + {playersList: _p4._0}), + _1: _elm_lang$core$Platform_Cmd$none + }; + } else { + return { + ctor: '_Tuple2', + _0: _elm_lang$core$Native_Utils.update( + model, + { + errors: _elm_lang$core$Basics$toString(_p4._0) + }), + _1: _elm_lang$core$Platform_Cmd$none + }; + } + case 'KeyDown': + var _p5 = _p2._0; + switch (_p5) { case 32: return (!_elm_lang$core$Native_Utils.eq(model.gameState, _user$project$Platformer$Playing)) ? { ctor: '_Tuple2', @@ -16838,9 +16829,9 @@ var _user$project$Platformer$update = F2( return {ctor: '_Tuple2', _0: model, _1: _elm_lang$core$Platform_Cmd$none}; } case 'PhoenixMsg': - var _p5 = A2(_fbonetti$elm_phoenix_socket$Phoenix_Socket$update, _p2._0, model.phxSocket); - var phxSocket = _p5._0; - var phxCmd = _p5._1; + var _p6 = A2(_fbonetti$elm_phoenix_socket$Phoenix_Socket$update, _p2._0, model.phxSocket); + var phxSocket = _p6._0; + var phxCmd = _p6._1; return { ctor: '_Tuple2', _0: _elm_lang$core$Native_Utils.update( @@ -16849,14 +16840,14 @@ var _user$project$Platformer$update = F2( _1: A2(_elm_lang$core$Platform_Cmd$map, _user$project$Platformer$PhoenixMsg, phxCmd) }; case 'ReceiveScoreChanges': - var _p6 = A2(_elm_lang$core$Json_Decode$decodeValue, _user$project$Platformer$scoreDecoder, _p2._0); - if (_p6.ctor === 'Ok') { + var _p7 = A2(_elm_lang$core$Json_Decode$decodeValue, _user$project$Platformer$decodeGameplay, _p2._0); + if (_p7.ctor === 'Ok') { return { ctor: '_Tuple2', _0: _elm_lang$core$Native_Utils.update( model, { - gameplays: {ctor: '::', _0: _p6._0, _1: model.gameplays} + gameplays: {ctor: '::', _0: _p7._0, _1: model.gameplays} }), _1: _elm_lang$core$Platform_Cmd$none }; @@ -16865,7 +16856,7 @@ var _user$project$Platformer$update = F2( ctor: '_Tuple2', _0: _elm_lang$core$Native_Utils.update( model, - {errors: _p6._0}), + {errors: _p7._0}), _1: _elm_lang$core$Platform_Cmd$none }; } @@ -16897,9 +16888,9 @@ var _user$project$Platformer$update = F2( _fbonetti$elm_phoenix_socket$Phoenix_Push$withPayload, payload, A2(_fbonetti$elm_phoenix_socket$Phoenix_Push$init, 'save_score', 'score:platformer')))); - var _p7 = A2(_fbonetti$elm_phoenix_socket$Phoenix_Socket$push, phxPush, model.phxSocket); - var phxSocket = _p7._0; - var phxCmd = _p7._1; + var _p8 = A2(_fbonetti$elm_phoenix_socket$Phoenix_Socket$push, phxPush, model.phxSocket); + var phxSocket = _p8._0; + var phxCmd = _p8._1; return { ctor: '_Tuple2', _0: _elm_lang$core$Native_Utils.update( @@ -16950,6 +16941,13 @@ var _user$project$Platformer$fetchPlayersList = A2( _elm_lang$http$Http$send, _user$project$Platformer$FetchPlayersList, A2(_elm_lang$http$Http$get, '/api/players', _user$project$Platformer$decodePlayersList)); +var _user$project$Platformer$FetchGameplaysList = function (a) { + return {ctor: 'FetchGameplaysList', _0: a}; +}; +var _user$project$Platformer$fetchGameplaysList = A2( + _elm_lang$http$Http$send, + _user$project$Platformer$FetchGameplaysList, + A2(_elm_lang$http$Http$get, '/api/gameplays', _user$project$Platformer$decodeGameplaysList)); var _user$project$Platformer$init = function (flags) { return { ctor: '_Tuple2', @@ -16957,14 +16955,18 @@ var _user$project$Platformer$init = function (flags) { _1: _elm_lang$core$Platform_Cmd$batch( { ctor: '::', - _0: _user$project$Platformer$fetchPlayersList, + _0: _user$project$Platformer$fetchGameplaysList, _1: { ctor: '::', - _0: A2( - _elm_lang$core$Platform_Cmd$map, - _user$project$Platformer$PhoenixMsg, - _user$project$Platformer$initialSocketCommand(flags)), - _1: {ctor: '[]'} + _0: _user$project$Platformer$fetchPlayersList, + _1: { + ctor: '::', + _0: A2( + _elm_lang$core$Platform_Cmd$map, + _user$project$Platformer$PhoenixMsg, + _user$project$Platformer$initialSocketCommand(flags)), + _1: {ctor: '[]'} + } } }) }; @@ -17010,7 +17012,7 @@ if (typeof _user$project$Main$main !== 'undefined') { } Elm['Platformer'] = Elm['Platformer'] || {}; if (typeof _user$project$Platformer$main !== 'undefined') { - _user$project$Platformer$main(Elm['Platformer'], 'Platformer', {"types":{"unions":{"Dict.LeafColor":{"args":[],"tags":{"LBBlack":[],"LBlack":[]}},"Platformer.Msg":{"args":[],"tags":{"SetNewItemPositionX":["Int"],"SaveScoreRequest":[],"CountdownTimer":["Time.Time"],"FetchPlayersList":["Result.Result Http.Error (List Platformer.Player)"],"SaveScore":["Json.Encode.Value"],"PhoenixMsg":["Phoenix.Socket.Msg Platformer.Msg"],"TimeUpdate":["Time.Time"],"KeyDown":["Keyboard.KeyCode"],"ReceiveScoreChanges":["Json.Encode.Value"],"NoOp":[],"SaveScoreError":["Json.Encode.Value"]}},"Json.Encode.Value":{"args":[],"tags":{"Value":[]}},"Dict.Dict":{"args":["k","v"],"tags":{"RBNode_elm_builtin":["Dict.NColor","k","v","Dict.Dict k v","Dict.Dict k v"],"RBEmpty_elm_builtin":["Dict.LeafColor"]}},"Maybe.Maybe":{"args":["a"],"tags":{"Just":["a"],"Nothing":[]}},"Dict.NColor":{"args":[],"tags":{"BBlack":[],"Red":[],"NBlack":[],"Black":[]}},"Http.Error":{"args":[],"tags":{"BadUrl":["String"],"NetworkError":[],"Timeout":[],"BadStatus":["Http.Response String"],"BadPayload":["String","Http.Response String"]}},"Result.Result":{"args":["error","value"],"tags":{"Ok":["value"],"Err":["error"]}},"Phoenix.Socket.Msg":{"args":["msg"],"tags":{"ChannelErrored":["String"],"ChannelClosed":["String"],"ExternalMsg":["msg"],"ChannelJoined":["String"],"Heartbeat":["Time.Time"],"NoOp":[],"ReceiveReply":["String","Int"]}}},"aliases":{"Http.Response":{"args":["body"],"type":"{ url : String , status : { code : Int, message : String } , headers : Dict.Dict String String , body : body }"},"Keyboard.KeyCode":{"args":[],"type":"Int"},"Platformer.Player":{"args":[],"type":"{ displayName : Maybe.Maybe String , id : Int , score : Int , username : String }"},"Time.Time":{"args":[],"type":"Float"}},"message":"Platformer.Msg"},"versions":{"elm":"0.18.0"}}); + _user$project$Platformer$main(Elm['Platformer'], 'Platformer', {"types":{"unions":{"Dict.LeafColor":{"args":[],"tags":{"LBBlack":[],"LBlack":[]}},"Platformer.Msg":{"args":[],"tags":{"FetchGameplaysList":["Result.Result Http.Error (List Platformer.Gameplay)"],"SetNewItemPositionX":["Int"],"SaveScoreRequest":[],"CountdownTimer":["Time.Time"],"FetchPlayersList":["Result.Result Http.Error (List Platformer.Player)"],"SaveScore":["Json.Encode.Value"],"PhoenixMsg":["Phoenix.Socket.Msg Platformer.Msg"],"TimeUpdate":["Time.Time"],"KeyDown":["Keyboard.KeyCode"],"ReceiveScoreChanges":["Json.Encode.Value"],"NoOp":[],"SaveScoreError":["Json.Encode.Value"]}},"Json.Encode.Value":{"args":[],"tags":{"Value":[]}},"Dict.Dict":{"args":["k","v"],"tags":{"RBNode_elm_builtin":["Dict.NColor","k","v","Dict.Dict k v","Dict.Dict k v"],"RBEmpty_elm_builtin":["Dict.LeafColor"]}},"Maybe.Maybe":{"args":["a"],"tags":{"Just":["a"],"Nothing":[]}},"Dict.NColor":{"args":[],"tags":{"BBlack":[],"Red":[],"NBlack":[],"Black":[]}},"Http.Error":{"args":[],"tags":{"BadUrl":["String"],"NetworkError":[],"Timeout":[],"BadStatus":["Http.Response String"],"BadPayload":["String","Http.Response String"]}},"Result.Result":{"args":["error","value"],"tags":{"Ok":["value"],"Err":["error"]}},"Phoenix.Socket.Msg":{"args":["msg"],"tags":{"ChannelErrored":["String"],"ChannelClosed":["String"],"ExternalMsg":["msg"],"ChannelJoined":["String"],"Heartbeat":["Time.Time"],"NoOp":[],"ReceiveReply":["String","Int"]}}},"aliases":{"Http.Response":{"args":["body"],"type":"{ url : String , status : { code : Int, message : String } , headers : Dict.Dict String String , body : body }"},"Keyboard.KeyCode":{"args":[],"type":"Int"},"Platformer.Gameplay":{"args":[],"type":"{ gameId : Int, playerId : Int, playerScore : Int }"},"Platformer.Player":{"args":[],"type":"{ displayName : Maybe.Maybe String , id : Int , score : Int , username : String }"},"Time.Time":{"args":[],"type":"Float"}},"message":"Platformer.Msg"},"versions":{"elm":"0.18.0"}}); } if (typeof define === "function" && define['amd']) diff --git a/lib/platform_web/views/gameplay_view.ex b/lib/platform_web/views/gameplay_view.ex index 9c4cdf7..429b509 100644 --- a/lib/platform_web/views/gameplay_view.ex +++ b/lib/platform_web/views/gameplay_view.ex @@ -12,6 +12,8 @@ defmodule PlatformWeb.GameplayView do def render("gameplay.json", %{gameplay: gameplay}) do %{id: gameplay.id, + game_id: gameplay.game_id, + player_id: gameplay.player_id, player_score: gameplay.player_score} end end From 4956f2455d9b4b82c366a69e43c9382762275faa Mon Sep 17 00:00:00 2001 From: Bijan Date: Sun, 14 Jan 2018 23:05:47 -0500 Subject: [PATCH 4/4] Resolve test issues for gameplays --- test/platform_web/controllers/gameplay_controller_test.exs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/platform_web/controllers/gameplay_controller_test.exs b/test/platform_web/controllers/gameplay_controller_test.exs index e2419ce..1ecf341 100644 --- a/test/platform_web/controllers/gameplay_controller_test.exs +++ b/test/platform_web/controllers/gameplay_controller_test.exs @@ -32,6 +32,8 @@ defmodule PlatformWeb.GameplayControllerTest do conn = get conn, gameplay_path(conn, :show, id) assert json_response(conn, 200)["data"] == %{ "id" => id, + "game_id" => nil, + "player_id" => nil, "player_score" => 42} end @@ -51,6 +53,8 @@ defmodule PlatformWeb.GameplayControllerTest do conn = get conn, gameplay_path(conn, :show, id) assert json_response(conn, 200)["data"] == %{ "id" => id, + "game_id" => nil, + "player_id" => nil, "player_score" => 43} end