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

Allow admin to restrict certain accolades to Ally or Enemy only #585

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
27 changes: 27 additions & 0 deletions lib/teiserver/account/libs/accolade_lib.ex
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,33 @@ defmodule Teiserver.Account.AccoladeLib do
end)
end

@spec get_giveable_accolade_types(boolean()) :: [map()]
def get_giveable_accolade_types(is_ally?) do
restriction =
if is_ally? do
"Ally"
else
"Enemy"
end

query =
"select id, name, icon, colour from teiserver_account_badge_types tabt
where (restriction in ($1) or restriction is null) and purpose = 'Accolade'
order by name;"

results = Ecto.Adapters.SQL.query!(Repo, query, [restriction])

results.rows
|> Enum.map(fn [id, name, icon, colour] ->
%{
id: id,
name: name,
icon: icon,
colour: colour
}
end)
end

@spec get_player_accolades(T.userid()) :: map()
def get_player_accolades(userid) do
Account.list_accolades(search: [recipient_id: userid, has_badge: true])
Expand Down
8 changes: 8 additions & 0 deletions lib/teiserver/account/libs/badge_type_lib.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ defmodule Teiserver.Account.BadgeTypeLib do
"Role"
]

@spec restriction_list() :: [String.t()]
def restriction_list(),
do: [
nil,
"Ally",
"Enemy"
]

@spec make_favourite(map()) :: map()
def make_favourite(badge_type) do
%{
Expand Down
4 changes: 2 additions & 2 deletions lib/teiserver/account/schemas/badge_type.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ defmodule Teiserver.Account.BadgeType do
field :icon, :string
field :colour, :string
field :description, :string

field :purpose, :string
field :restriction, :string

timestamps()
end
Expand All @@ -22,7 +22,7 @@ defmodule Teiserver.Account.BadgeType do
|> trim_strings(~w(name description)a)

struct
|> cast(params, ~w(name icon colour purpose description)a)
|> cast(params, ~w(name icon colour purpose description restriction)a)
|> validate_required(~w(name icon colour purpose)a)
end

Expand Down
14 changes: 12 additions & 2 deletions lib/teiserver_web/live/battles/match/show.ex
Original file line number Diff line number Diff line change
Expand Up @@ -465,11 +465,18 @@ defmodule TeiserverWeb.Battle.MatchLive.Show do

def handle_event(
"give-accolade",
%{"recipient_id" => recipient_id, "recipient_name" => recipient_name},
params,
socket
) do
recipient_id = params["recipient_id"]
recipient_name = params["recipient_name"]
current_user_team_id = params["current_user_team_id"]
recipient_team_id = params["recipient_team_id"]

is_ally? = current_user_team_id == recipient_team_id

badge_types =
Account.list_accolade_types()
AccoladeLib.get_giveable_accolade_types(is_ally?)

gift_limit = Config.get_site_config_cache("teiserver.Accolade gift limit")
gift_window = Config.get_site_config_cache("teiserver.Accolade gift window")
Expand Down Expand Up @@ -637,6 +644,9 @@ defmodule TeiserverWeb.Battle.MatchLive.Show do
<% end %>
</tbody>
</table>
<a phx-click="return-to-match" class="btn btn-sm btn-secondary">
Cancel
</a>
</div>
</div>
</div>
Expand Down
6 changes: 3 additions & 3 deletions lib/teiserver_web/live/battles/match/show.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,10 @@
phx-click="give-accolade"
phx-value-recipient_name={m.user.name}
phx-value-recipient_id={m.user.id}
phx-value-current_user_team_id={@current_user_team_id}
phx-value-recipient_team_id={m.team_id}
class={[
"btn btn-sm btn-success",
@current_user_team_id && m.team_id != @current_user_team_id &&
"invisible"
"btn btn-sm btn-success"
]}
>
<i class="fa-solid fa-award"></i>&nbsp;
Expand Down
5 changes: 5 additions & 0 deletions lib/teiserver_web/templates/admin/badge_type/form.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@
<label class="control-label" for="badge_type_purposes">Purposes</label>
<%= select(f, :purpose, purpose_list(), class: "form-control") %>
</div>

<div class="form-group">
<label class="control-label" for="badge_type_restrictions">Restriction</label>
<%= select(f, :restriction, restriction_list(), class: "form-control") %>
</div>
</div>

<div class="row mt-4">
Expand Down
3 changes: 3 additions & 0 deletions lib/teiserver_web/templates/admin/badge_type/index.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
<tr>
<th colspan="2">Name</th>
<th>Purposes</th>
<th>Restriction</th>

<th colspan="2">&nbsp;</th>
</tr>
</thead>
Expand All @@ -43,6 +45,7 @@
</td>
<td><%= badge_type.name %></td>
<td><%= badge_type.purpose %></td>
<td><%= badge_type.restriction %></td>

<td>
<a
Expand Down
3 changes: 3 additions & 0 deletions lib/teiserver_web/views/admin/badge_type_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ defmodule TeiserverWeb.Admin.BadgeTypeView do

@spec purpose_list() :: [String.t()]
defdelegate purpose_list(), to: Teiserver.Account.BadgeTypeLib

@spec restriction_list() :: [String.t()]
defdelegate restriction_list(), to: Teiserver.Account.BadgeTypeLib
end
36 changes: 36 additions & 0 deletions priv/repo/migrations/20250206221410_add_accolade_restriction.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
defmodule Teiserver.Repo.Migrations.AddAccoladeRestriction do
use Ecto.Migration

def change do
alter table("teiserver_account_badge_types") do
add :restriction, :string
end

# Restrict new column to certain values
constraint_query = """
ALTER TABLE teiserver_account_badge_types
ADD CONSTRAINT restriction_constraint check(restriction in ('Ally', 'Enemy') OR restriction is null);


"""

# If we rollback remove the constraint
rollback_query =
"ALTER TABLE teiserver_account_badge_types DROP CONSTRAINT restriction_constraint;"

execute(constraint_query, rollback_query)

# UPDATE Good Teammate accolade to be ally only
update_restrictions = """
UPDATE teiserver_account_badge_types
set restriction = 'Ally'
where lower(name) = 'good teammate';
"""

# If we rollback don't need to do anything extra
rollback_query =
""

execute(update_restrictions, rollback_query)
end
end
Loading