Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GH-798] - Configurator Game Modes #855

Draft
wants to merge 25 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
2af4a79
Add game modes table
tvillegas98 Aug 12, 2024
b164d1b
Add relationship between versions and game modes
tvillegas98 Aug 12, 2024
178aa09
Add mvc for game modes
tvillegas98 Aug 12, 2024
34e1bab
Fix seeds. They were broken due to the game modes addition
tvillegas98 Aug 12, 2024
3aae697
Add tests for the game modes schema
tvillegas98 Aug 12, 2024
1b7cb68
Add queries for game mode logic
tvillegas98 Aug 12, 2024
6a13455
Add game modes routes
tvillegas98 Aug 12, 2024
6db329a
Add link to game modes in configurator's home
tvillegas98 Aug 12, 2024
d3ad3dc
Add endpoint and needed logic to retrieve the current config in a gam…
tvillegas98 Aug 12, 2024
c555f53
Add place holder for game mode name
tvillegas98 Aug 12, 2024
1441088
Arena gets the configuration by game mode and not by version
tvillegas98 Aug 12, 2024
4f51409
Remove no longer used endpoints for configuration
tvillegas98 Aug 12, 2024
9419d47
mix format
tvillegas98 Aug 12, 2024
88d5eb8
Fix credo
tvillegas98 Aug 12, 2024
b57e29d
Fix game mode fixture
tvillegas98 Aug 12, 2024
cee752f
Show game mode in version's show
tvillegas98 Aug 12, 2024
7d91aea
Fix all tests related to the game mode name's constraint
tvillegas98 Aug 13, 2024
86a3613
test commit
tvillegas98 Aug 13, 2024
cdbbc7d
Now get configs by game mode
tvillegas98 Aug 13, 2024
77c5b01
Refactor get_current_version_from_game_mode/1 function
tvillegas98 Aug 14, 2024
86450a9
Fix feature mark as current version when no former version exists
tvillegas98 Aug 16, 2024
a9981da
Merge branch 'main' into gh-798-game-modes
tvillegas98 Aug 16, 2024
f4b39ca
Add description field to game modes
tvillegas98 Aug 16, 2024
773c9be
Fix tests broken by the new description field
tvillegas98 Aug 16, 2024
7eb205c
Remove not needed migration
tvillegas98 Aug 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions apps/arena/lib/arena/configuration.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule Arena.Configuration do
Enum.find(config.characters, fn character -> character.name == name end)
end

def get_game_config() do
def get_game_config(game_mode) do
{:ok, config_json} =
Application.app_dir(:arena, "priv/config.json")
|> File.read()
Expand All @@ -16,15 +16,17 @@ defmodule Arena.Configuration do
client_config = get_client_config()

config
|> Map.merge(get_current_game_configuration())
|> Map.merge(get_current_game_configuration_by_game_mode(game_mode))
|> Map.put(:client_config, client_config)
end

defp get_current_game_configuration do
defp get_current_game_configuration_by_game_mode(game_mode) do
gateway_url = Application.get_env(:arena, :gateway_url)

{:ok, payload} =
Finch.build(:get, "#{gateway_url}/curse/configuration/current", [{"content-type", "application/json"}])
Finch.build(:get, "#{gateway_url}/curse/configuration/#{game_mode}/current", [
{"content-type", "application/json"}
])
|> Finch.request(Arena.Finch)

Jason.decode!(payload.body, [{:keys, :atoms}])
Expand Down
2 changes: 1 addition & 1 deletion apps/arena/lib/arena/game_updater.ex
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ defmodule Arena.GameUpdater do

def init(%{clients: clients, bot_clients: bot_clients, game_params: game_params}) do
game_id = self() |> :erlang.term_to_binary() |> Base58.encode()
game_config = Configuration.get_game_config()
game_config = Configuration.get_game_config("battle_royale")
game_config = Map.put(game_config, :game, Map.merge(game_config.game, game_params))

game_state = new_game(game_id, clients ++ bot_clients, game_config)
Expand Down
2 changes: 1 addition & 1 deletion apps/arena/lib/arena/matchmaking/game_launcher.ex
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ defmodule Arena.Matchmaking.GameLauncher do

defp get_bot_clients(missing_clients) do
characters =
Arena.Configuration.get_game_config()
Arena.Configuration.get_game_config("battle_royale")
|> Map.get(:characters)
|> Enum.filter(fn character -> character.active end)

Expand Down
2 changes: 1 addition & 1 deletion apps/arena/lib/arena/matchmaking/pair_mode.ex
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ defmodule Arena.Matchmaking.PairMode do

defp get_bot_clients(missing_clients) do
characters =
Arena.Configuration.get_game_config()
Arena.Configuration.get_game_config("pair_mode")
|> Map.get(:characters)
|> Enum.filter(fn character -> character.active end)

Expand Down
2 changes: 1 addition & 1 deletion apps/arena/lib/arena/matchmaking/quick_game_mode.ex
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ defmodule Arena.Matchmaking.QuickGameMode do

defp get_bot_clients(missing_clients) do
characters =
Arena.Configuration.get_game_config()
Arena.Configuration.get_game_config("battle_royale")
|> Map.get(:characters)
|> Enum.filter(fn character -> character.active end)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
defmodule ConfiguratorWeb.GameModeController do
use ConfiguratorWeb, :controller

alias GameBackend.Configuration
alias GameBackend.Configuration.GameMode

def index(conn, _params) do
game_modes = Configuration.list_game_modes()
render(conn, :index, game_modes: game_modes)
end

def new(conn, _params) do
changeset = Configuration.change_game_mode(%GameMode{})
render(conn, :new, changeset: changeset)
end

def create(conn, %{"game_mode" => game_mode_params}) do
case Configuration.create_game_mode(game_mode_params) do
{:ok, game_mode} ->
conn
|> put_flash(:info, "Game mode created successfully.")
|> redirect(to: ~p"/game_modes/#{game_mode}")

{:error, %Ecto.Changeset{} = changeset} ->
render(conn, :new, changeset: changeset)
end
end

def show(conn, %{"id" => id}) do
game_mode = Configuration.get_game_mode!(id)
render(conn, :show, game_mode: game_mode)
end

def edit(conn, %{"id" => id}) do
game_mode = Configuration.get_game_mode!(id)
changeset = Configuration.change_game_mode(game_mode)
render(conn, :edit, game_mode: game_mode, changeset: changeset)
end

def update(conn, %{"id" => id, "game_mode" => game_mode_params}) do
game_mode = Configuration.get_game_mode!(id)

case Configuration.update_game_mode(game_mode, game_mode_params) do
{:ok, game_mode} ->
conn
|> put_flash(:info, "Game mode updated successfully.")
|> redirect(to: ~p"/game_modes/#{game_mode}")

{:error, %Ecto.Changeset{} = changeset} ->
render(conn, :edit, game_mode: game_mode, changeset: changeset)
end
end

def delete(conn, %{"id" => id}) do
game_mode = Configuration.get_game_mode!(id)
{:ok, _game_mode} = Configuration.delete_game_mode(game_mode)

conn
|> put_flash(:info, "Game mode deleted successfully.")
|> redirect(to: ~p"/game_modes")
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
defmodule ConfiguratorWeb.GameModeHTML do
use ConfiguratorWeb, :html

embed_templates "game_mode_html/*"

@doc """
Renders a game_mode form.
"""
attr :changeset, Ecto.Changeset, required: true
attr :action, :string, required: true

def game_mode_form(assigns)
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<.header>
Edit Game mode <%= @game_mode.id %>
<:subtitle>Use this form to manage game_mode records in your database.</:subtitle>
</.header>

<.game_mode_form changeset={@changeset} action={~p"/game_modes/#{@game_mode}"} />

<.back navigate={~p"/game_modes"}>Back to game_modes</.back>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<.simple_form :let={f} for={@changeset} action={@action}>
<.error :if={@changeset.action}>
Oops, something went wrong! Please check the errors below.
</.error>
<.input field={f[:name]} type="text" label="Name" placeholder="some_game_mode" />
<:actions>
<.button>Save Game mode</.button>
</:actions>
</.simple_form>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<.header>
Listing Game modes
<:actions>
<.link href={~p"/game_modes/new"}>
<.button>New Game mode</.button>
</.link>
</:actions>
</.header>

<.table id="game_modes" rows={@game_modes} row_click={&JS.navigate(~p"/game_modes/#{&1}")}>
<:col :let={game_mode} label="Name"><%= game_mode.name %></:col>
<:action :let={game_mode}>
<div class="sr-only">
<.link navigate={~p"/game_modes/#{game_mode}"}>Show</.link>
</div>
<.link navigate={~p"/game_modes/#{game_mode}/edit"}>Edit</.link>
</:action>
<:action :let={game_mode}>
<.link href={~p"/game_modes/#{game_mode}"} method="delete" data-confirm="Are you sure?">
Delete
</.link>
</:action>
</.table>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<.header>
New Game mode
<:subtitle>Use this form to manage game_mode records in your database.</:subtitle>
</.header>

<.game_mode_form changeset={@changeset} action={~p"/game_modes"} />

<.back navigate={~p"/game_modes"}>Back to game_modes</.back>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<.header>
Game mode <%= @game_mode.id %>
<:subtitle>This is a game_mode record from your database.</:subtitle>
<:actions>
<.link href={~p"/game_modes/#{@game_mode}/edit"}>
<.button>Edit game_mode</.button>
</.link>
</:actions>
</.header>

<.list>
<:item title="Name"><%= @game_mode.name %></:item>
</.list>

<.back navigate={~p"/game_modes"}>Back to game_modes</.back>
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
</.header>

<.list>
<:item title="Game Modes"><.link href={~p"/game_modes"}>Link</.link></:item>
<:item title="Versions"><.link href={~p"/versions"}>Link</.link></:item>
<:item title="Character settings"><.link href={~p"/characters"}>Link</.link></:item>
<:item title="Game settings"><.link href={~p"/game_configurations"}>Link</.link></:item>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ defmodule ConfiguratorWeb.VersionController do

def new(conn, _params) do
changeset = Configuration.change_version(%Version{})
render(conn, :new, changeset: changeset)
game_modes = Configuration.list_game_modes()

render(conn, :new, changeset: changeset, game_modes: game_modes)
end

def create(conn, %{"version" => version_params}) do
Expand All @@ -22,19 +24,23 @@ defmodule ConfiguratorWeb.VersionController do
|> redirect(to: ~p"/versions/#{version}")

{:error, %Ecto.Changeset{} = changeset} ->
render(conn, :new, changeset: changeset)
game_modes = Configuration.list_game_modes()
render(conn, :new, changeset: changeset, game_modes: game_modes)
end
end

def show(conn, %{"id" => id}) do
version = Configuration.get_version!(id)
render(conn, :show, version: version)
game_mode = Configuration.get_game_mode!(version.game_mode_id)
render(conn, :show, version: version, game_mode: game_mode)
end

def edit(conn, %{"id" => id}) do
version = Configuration.get_version!(id)
changeset = Configuration.change_version(version)
render(conn, :edit, version: version, changeset: changeset)
game_modes = Configuration.list_game_modes()

render(conn, :edit, version: version, changeset: changeset, game_modes: game_modes)
end

def update(conn, %{"id" => id, "version" => version_params}) do
Expand All @@ -47,7 +53,8 @@ defmodule ConfiguratorWeb.VersionController do
|> redirect(to: ~p"/versions/#{version}")

{:error, %Ecto.Changeset{} = changeset} ->
render(conn, :edit, version: version, changeset: changeset)
game_modes = Configuration.list_game_modes()
render(conn, :edit, version: version, changeset: changeset, game_modes: game_modes)
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ defmodule ConfiguratorWeb.VersionHTML do
"""
attr :changeset, Ecto.Changeset, required: true
attr :action, :string, required: true
attr :game_modes, :list

def version_form(assigns)
end
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
<:subtitle>Use this form to manage version records in your database.</:subtitle>
</.header>

<.version_form changeset={@changeset} action={~p"/versions/#{@version}"} />
<.version_form changeset={@changeset} action={~p"/versions/#{@version}"} game_modes={@game_modes} />

<.back navigate={~p"/versions"}>Back to versions</.back>
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
<:subtitle>Use this form to manage version records in your database.</:subtitle>
</.header>

<.version_form changeset={@changeset} action={~p"/versions"} />
<.version_form changeset={@changeset} action={~p"/versions"} game_modes={@game_modes} />

<.back navigate={~p"/versions"}>Back to versions</.back>
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<.list>
<:item title="Name"><%= @version.name %></:item>
<:item title="Game mode"><%= @game_mode.name %></:item>
</.list>

<.back navigate={~p"/versions"}>Back to versions</.back>
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
Oops, something went wrong! Please check the errors below.
</.error>
<.input field={f[:name]} type="text" label="Name" />
<.input field={f[:game_mode_id]} type="select" options={Enum.map(@game_modes, &{&1.name, &1.id})} label="Game Mode" />

<:actions>
<.button>Save Version</.button>
</:actions>
Expand Down
1 change: 1 addition & 0 deletions apps/configurator/lib/configurator_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ defmodule ConfiguratorWeb.Router do
put "/map_configurations/:id/update_pools", MapConfigurationController, :update_pools
resources "/consumable_items", ConsumableItemController
resources "/arena_servers", ArenaServerController
resources "/game_modes", GameModeController

scope "/versions" do
resources "/", VersionController
Expand Down
52 changes: 51 additions & 1 deletion apps/configurator/test/configurator/configuration_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ defmodule Configurator.ConfigurationTest do
end

test "create_version/1 with valid data creates a version" do
valid_attrs = %{name: "some name"}
game_mode = game_mode_fixture()
valid_attrs = %{name: "some name", game_mode_id: game_mode.id}

assert {:ok, %Version{} = version} = Configuration.create_version(valid_attrs)
assert version.name == "some name"
Expand Down Expand Up @@ -173,4 +174,53 @@ defmodule Configurator.ConfigurationTest do
assert %Ecto.Changeset{} = Configuration.change_version(version)
end
end

describe "game_modes" do
alias GameBackend.Configuration.GameMode

import Configurator.ConfigurationFixtures

@invalid_attrs %{name: nil}
test "get_game_mode!/1 returns the game_mode with given id" do
game_mode = game_mode_fixture()
assert Configuration.get_game_mode!(game_mode.id) == game_mode
end

test "create_game_mode/1 with valid data creates a game_mode" do
valid_attrs = %{name: "some name"}

assert {:ok, %GameMode{} = game_mode} = Configuration.create_game_mode(valid_attrs)
assert game_mode.name == "some name"
end

test "create_game_mode/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = Configuration.create_game_mode(@invalid_attrs)
end

test "update_game_mode/2 with valid data updates the game_mode" do
game_mode = game_mode_fixture()
game_modes = Configuration.list_game_modes()
update_attrs = %{name: "some updated name" <> "#{Enum.count(game_modes)}"}

assert {:ok, %GameMode{} = game_mode} = Configuration.update_game_mode(game_mode, update_attrs)
assert game_mode.name == "some updated name" <> "#{Enum.count(game_modes)}"
end

test "update_game_mode/2 with invalid data returns error changeset" do
game_mode = game_mode_fixture()
assert {:error, %Ecto.Changeset{}} = Configuration.update_game_mode(game_mode, @invalid_attrs)
assert game_mode == Configuration.get_game_mode!(game_mode.id)
end

test "delete_game_mode/1 deletes the game_mode" do
game_mode = game_mode_fixture()
assert {:ok, %GameMode{}} = Configuration.delete_game_mode(game_mode)
assert_raise Ecto.NoResultsError, fn -> Configuration.get_game_mode!(game_mode.id) end
end

test "change_game_mode/1 returns a game_mode changeset" do
game_mode = game_mode_fixture()
assert %Ecto.Changeset{} = Configuration.change_game_mode(game_mode)
end
end
end
Loading
Loading