Skip to content

Commit

Permalink
VIP role improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
L-e-x-o-n committed Nov 7, 2024
1 parent 4219d22 commit c8a3a0e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 42 deletions.
2 changes: 1 addition & 1 deletion lib/teiserver/account/libs/role_lib.ex
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ defmodule Teiserver.Account.RoleLib do
},

# Privileged
%{name: "VIP", colour: "#AA8833", icon: "fa-solid fa-sparkles", contains: ["BAR+"]},
%{name: "VIP", colour: "#AA8833", icon: "fa-solid fa-sparkles", contains: ~w()},
%{name: "Streamer", colour: "#660066", icon: "fa-brands fa-twitch", contains: ~w()},
%{name: "Tournament", colour: "#0000AA", icon: "fa-solid fa-trophy", contains: ~w()},
%{
Expand Down
24 changes: 1 addition & 23 deletions lib/teiserver/coordinator/consul_server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ defmodule Teiserver.Coordinator.ConsulServer do

@always_allow ~w(status s y n follow joinq leaveq splitlobby afks roll password? tournament)
@boss_commands ~w(balancealgorithm gatekeeper welcome-message meme reset-approval rename minchevlevel maxchevlevel resetchevlevels resetratinglevels minratinglevel maxratinglevel setratinglevels)
@vip_boss_commands ~w(shuffle)
@host_commands ~w(specunready makeready settag speclock forceplay lobbyban lobbybanmult unban forcespec lock unlock makebalance set-config-teaser)
@admin_commands ~w(playerlimit broadcast)
@admin_commands ~w(playerlimit broadcast shuffle)

# @handled_by_lobby ~w(explain)
@splitter "########################################"
Expand Down Expand Up @@ -970,7 +969,6 @@ defmodule Teiserver.Coordinator.ConsulServer do

is_host = senderid == state.host_id
is_boss = Enum.member?(state.host_bosses, senderid)
is_vip = Enum.member?(user.roles, "VIP")
is_admin = Enum.member?(user.roles, "Admin")

cond do
Expand All @@ -997,26 +995,6 @@ defmodule Teiserver.Coordinator.ConsulServer do
Enum.member?(@boss_commands, cmd.command) and (is_host or is_boss) ->
true

Enum.member?(@vip_boss_commands, cmd.command) and (is_vip and is_boss) ->
true

Enum.member?(@vip_boss_commands, cmd.command) and not (is_vip and is_boss) ->
msg =
if is_vip do
"You also need to be boss to call '#{cmd.command}'"
else
"No listed command of '#{cmd.command}'"
end

ChatLib.sayprivateex(
state.coordinator_id,
cmd.senderid,
msg,
state.lobby_id
)

false

Enum.member?(@host_commands, cmd.command) and not is_host ->
ChatLib.sayprivateex(
state.coordinator_id,
Expand Down
47 changes: 29 additions & 18 deletions lib/teiserver/data/cache_user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -406,16 +406,7 @@ defmodule Teiserver.CacheUser do

@spec rename_user(T.userid(), String.t(), boolean) :: :success | {:error, String.t()}
def rename_user(userid, new_name, admin_action \\ false) do
rename_log =
Account.get_user_stat_data(userid)
|> Map.get("rename_log", [])

new_name = String.trim(new_name)

now = System.system_time(:second)
# since_most_recent_rename = now - (Enum.slice(rename_log, 0..0) ++ [0] |> hd)
since_rename_two = now - ((Enum.slice(rename_log, 1..1) ++ [0, 0, 0]) |> hd)
since_rename_three = now - ((Enum.slice(rename_log, 2..2) ++ [0, 0, 0]) |> hd)
max_username_length = Config.get_site_config_cache("teiserver.Username max length")

cond do
Expand All @@ -428,15 +419,8 @@ defmodule Teiserver.CacheUser do
admin_action == false and WordLib.acceptable_name?(new_name) == false ->
{:error, "Not an acceptable name, please see section 1.4 of the code of conduct"}

# Can't rename more than 2 times in 5 days
admin_action == false and since_rename_two < 60 * 60 * 24 * 5 ->
{:error,
"If you keep changing your name people won't know who you are; give it a bit of time (5 days)"}

# Can't rename more than 3 times in 30 days
admin_action == false and since_rename_three < 60 * 60 * 24 * 30 ->
{:error,
"If you keep changing your name people won't know who you are; give it a bit of time (30 days)"}
admin_action == false and not renamed_recently(userid) ->
{:error, "Rename limit reached (2 times in 5 days or 3 times in 30 days)"}

admin_action == false and is_restricted?(userid, ["All chat", "Renaming"]) ->
{:error, "Muted"}
Expand All @@ -460,6 +444,27 @@ defmodule Teiserver.CacheUser do
end
end

@spec do_rename_user(T.userid(), boolean) :: :ok
defp renamed_recently(user_id) do
rename_log =
Account.get_user_stat_data(user_id)
|> Map.get("rename_log", [])

now = System.system_time(:second)
since_rename_two = now - ((Enum.slice(rename_log, 1..1) ++ [0, 0, 0]) |> hd)
since_rename_three = now - ((Enum.slice(rename_log, 2..2) ++ [0, 0, 0]) |> hd)

cond do
# VIPs ignore time based rename restrictions
is_vip?(user_id) -> false
# Can't rename more than 2 times in 5 days
since_rename_two < 60 * 60 * 24 * 5 -> true
# Can't rename more than 3 times in 30 days
since_rename_three < 60 * 60 * 24 * 30 -> true
true -> false
end
end

@spec do_rename_user(T.userid(), String.t()) :: :ok
defp do_rename_user(userid, new_name) do
client = Account.get_client_by_id(userid)
Expand Down Expand Up @@ -1241,6 +1246,12 @@ defmodule Teiserver.CacheUser do
def is_admin?(%{roles: roles}), do: Enum.member?(roles, "Admin")
def is_admin?(_), do: false

@spec is_vip?(T.userid() | T.user()) :: boolean()
def is_vip?(nil), do: false
def is_vip?(userid) when is_integer(userid), do: is_vip?(get_user_by_id(userid))
def is_vip?(%{roles: roles}), do: Enum.member?(roles, "VIP")
def is_vip?(_), do: false

@spec rank_time(T.userid()) :: non_neg_integer()
def rank_time(userid) do
stats = Account.get_user_stat(userid) || %{data: %{}}
Expand Down

0 comments on commit c8a3a0e

Please sign in to comment.