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

Improve lobby restrictions #303

Merged
merged 23 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion lib/teiserver/battle.ex
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ defmodule Teiserver.Battle do
@spec update_lobby(T.lobby(), nil | atom, any) :: T.lobby()
defdelegate update_lobby(lobby, data, reason), to: LobbyLib

@spec rename_lobby(T.lobby_id(), String.t(), T.userid()) :: :ok | nil
@spec rename_lobby(T.lobby_id(), String.t(), T.userid() | nil) :: :ok | nil
defdelegate rename_lobby(lobby_id, new_base_name, renamer_id), to: LobbyLib

# Requests
Expand Down
234 changes: 137 additions & 97 deletions lib/teiserver/coordinator/consul_commands.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Teiserver.Coordinator.ConsulCommands do
alias Teiserver.Config
alias Teiserver.Coordinator.{ConsulServer, RikerssMemes}
alias Teiserver.{Account, Battle, Lobby, Coordinator, CacheUser, Client, Telemetry}
alias Teiserver.Lobby.{ChatLib, LobbyLib}
alias Teiserver.Lobby.{ChatLib, LobbyLib, LobbyRestrictions}
alias Teiserver.Chat.WordLib
alias Teiserver.Data.Types, as: T
import Teiserver.Helper.NumberHelper, only: [int_parse: 1, round: 2]
Expand Down Expand Up @@ -102,41 +102,8 @@ defmodule Teiserver.Coordinator.ConsulCommands do
["Parties:" | party_list]
end

min_rate_play = state.minimum_rating_to_play
max_rate_play = state.maximum_rating_to_play

play_level_bounds =
cond do
min_rate_play > 0 and max_rate_play < 1000 ->
"Play rating boundaries set to min: #{min_rate_play}, max: #{max_rate_play}"

min_rate_play > 0 ->
"Play rating boundaries set to min: #{min_rate_play}"

max_rate_play < 1000 ->
"Play rating boundaries set to max: #{max_rate_play}"

true ->
nil
end

min_rank_play = state.minimum_rank_to_play
max_rank_play = state.maximum_rank_to_play

play_rank_bounds =
cond do
min_rank_play > 0 and max_rank_play < 1000 ->
"Play rank boundaries set to min: #{min_rank_play}, max: #{max_rank_play}"

min_rank_play > 0 ->
"Play rank boundaries set to min: #{min_rank_play}"

max_rank_play < 1000 ->
"Play rank boundaries set to max: #{max_rank_play}"

true ->
nil
end
play_level_bounds = LobbyRestrictions.get_rating_bounds_text(state)
play_rank_bounds = LobbyRestrictions.get_rank_bounds_text(state)

welcome_message =
if state.welcome_message do
Expand All @@ -161,7 +128,7 @@ defmodule Teiserver.Coordinator.ConsulCommands do

status_msg =
[
"#{@splitter} Lobby status #{@splitter}",
@splitter, "Lobby status", @splitter,
"Status for battle ##{state.lobby_id}",
"Locks: #{locks}",
"Gatekeeper: #{state.gatekeeper}",
Expand Down Expand Up @@ -754,7 +721,109 @@ defmodule Teiserver.Coordinator.ConsulCommands do
def handle_command(%{command: "resetratinglevels", remaining: ""} = cmd, state) do
ConsulServer.say_command(cmd, state)
LobbyLib.cast_lobby(state.lobby_id, :refresh_name)
%{state | minimum_rating_to_play: 0, maximum_rating_to_play: 1000}
%{state | minimum_rating_to_play: 0, maximum_rating_to_play: LobbyRestrictions.rating_upper_bound()}
end

def handle_command(%{command: "resetchevlevels", remaining: ""} = cmd, state) do
ConsulServer.say_command(cmd, state)
LobbyLib.cast_lobby(state.lobby_id, :refresh_name)
%{state | minimum_rank_to_play: 0, maximum_rank_to_play: LobbyRestrictions.rank_upper_bound()}
end

#Reset min chev level for a lobby by using empty argument
def handle_command(%{command: "minchevlevel", remaining: ""} = cmd, state) do
ConsulServer.say_command(cmd, state)
LobbyLib.cast_lobby(state.lobby_id, :refresh_name)
%{state | minimum_rank_to_play: 0}
end

#Set min chev level for a lobby
def handle_command(
%{command: "minchevlevel", remaining: remaining, senderid: senderid} = cmd,
state
) do
{allow, allow_msg} = LobbyRestrictions.allowed_to_set_restrictions(state)
if allow == :ok do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left a comment below regarding the janky signature {:ok, nil} | {:error, reason}. You can (and should) directly pattern match in the cond like so:

case result do
  :ok -> ...
  {:error, reason} -> ...
end

case Integer.parse(remaining |> String.trim()) do
:error ->
Lobby.sayprivateex(
state.coordinator_id,
senderid,
[
"Unable to turn '#{remaining}' into an integer"
],
state.lobby_id
)

state

{chev_level, _} ->
ConsulServer.say_command(cmd, state)
LobbyLib.cast_lobby(state.lobby_id, :refresh_name)
level = chev_level - 1

Map.merge(state, %{
minimum_rank_to_play: level,
maximum_rank_to_play: LobbyRestrictions.rank_upper_bound()
})
end
else
Lobby.sayex(
state.coordinator_id,
allow_msg,
state.lobby_id
)

state
end
end

#Reset max chev level for a lobby by using empty argument
def handle_command(%{command: "maxchevlevel", remaining: ""} = cmd, state) do
ConsulServer.say_command(cmd, state)
LobbyLib.cast_lobby(state.lobby_id, :refresh_name)
%{state | maximum_rank_to_play: LobbyRestrictions.rank_upper_bound()}
end

#Setting max chev level we reset min chev level
def handle_command(
%{command: "maxchevlevel", remaining: remaining, senderid: senderid} = cmd,
state
) do
{allow, allow_msg} = LobbyRestrictions.allowed_to_set_restrictions(state)
if allow == :ok do
case Integer.parse(remaining |> String.trim()) do
:error ->
Lobby.sayprivateex(
state.coordinator_id,
senderid,
[
"Unable to turn '#{remaining}' into an integer"
],
state.lobby_id
)

state

{chev_level, _} ->
ConsulServer.say_command(cmd, state)
LobbyLib.cast_lobby(state.lobby_id, :refresh_name)
level = chev_level - 1

Map.merge(state, %{
maximum_rank_to_play: level,
minimum_rank_to_play: 0
})
end
else
Lobby.sayex(
state.coordinator_id,
allow_msg,
state.lobby_id
)

state
end
end

def handle_command(%{command: "minratinglevel", remaining: ""} = cmd, state) do
Expand All @@ -767,7 +836,8 @@ defmodule Teiserver.Coordinator.ConsulCommands do
%{command: "minratinglevel", remaining: remaining, senderid: senderid} = cmd,
state
) do
if allowed_to_set_rating_limit?(state) do
{allow, allow_msg} = LobbyRestrictions.allowed_to_set_restrictions(state)
if allow==:ok do
case Integer.parse(remaining |> String.trim()) do
:error ->
Lobby.sayprivateex(
Expand All @@ -785,15 +855,15 @@ defmodule Teiserver.Coordinator.ConsulCommands do
ConsulServer.say_command(cmd, state)
LobbyLib.cast_lobby(state.lobby_id, :refresh_name)

%{
state
| minimum_rating_to_play: level |> max(0) |> min(state.maximum_rating_to_play - 1)
}
Map.merge(state, %{
minimum_rating_to_play: level,
maximum_rating_to_play: LobbyRestrictions.rating_upper_bound()
})
end
else
Lobby.sayex(
state.coordinator_id,
"You cannot set a rating limit if all are welcome to the game",
allow_msg,
state.lobby_id
)

Expand All @@ -804,14 +874,15 @@ defmodule Teiserver.Coordinator.ConsulCommands do
def handle_command(%{command: "maxratinglevel", remaining: ""} = cmd, state) do
ConsulServer.say_command(cmd, state)
LobbyLib.cast_lobby(state.lobby_id, :refresh_name)
%{state | maximum_rating_to_play: 1000}
%{state | maximum_rating_to_play: LobbyRestrictions.rating_upper_bound()}
end

def handle_command(
%{command: "maxratinglevel", remaining: remaining, senderid: senderid} = cmd,
state
) do
if allowed_to_set_rating_limit?(state) do
{allow, allow_msg} = LobbyRestrictions.allowed_to_set_restrictions(state)
if allow == :ok do
case Integer.parse(remaining |> String.trim()) do
:error ->
Lobby.sayprivateex(
Expand All @@ -837,7 +908,7 @@ defmodule Teiserver.Coordinator.ConsulCommands do
else
Lobby.sayex(
state.coordinator_id,
"You cannot set a rating limit if all are welcome to the game",
allow_msg,
state.lobby_id
)

Expand Down Expand Up @@ -877,7 +948,8 @@ defmodule Teiserver.Coordinator.ConsulCommands do
state

{{min_level_o, _}, {max_level_o, _}} ->
if allowed_to_set_rating_limit?(state) do
{allow, allow_msg} = LobbyRestrictions.allowed_to_set_restrictions(state)
if allow == :ok do
min_level = min(min_level_o, max_level_o)
max_level = max(min_level_o, max_level_o)

Expand All @@ -892,7 +964,7 @@ defmodule Teiserver.Coordinator.ConsulCommands do
else
Lobby.sayex(
state.coordinator_id,
"You cannot set a rating limit if all are welcome to the game",
allow_msg,
state.lobby_id
)

Expand Down Expand Up @@ -976,7 +1048,14 @@ defmodule Teiserver.Coordinator.ConsulCommands do
{level, _} ->
ConsulServer.say_command(cmd, state)
LobbyLib.cast_lobby(state.lobby_id, :refresh_name)
%{state | maximum_rank_to_play: level |> min(1000) |> max(state.minimum_rank_to_play + 1)}

%{
state
| maximum_rank_to_play:
level
|> min(LobbyRestrictions.rating_upper_bound())
|> max(state.minimum_rank_to_play + 1)
}
end
end

Expand Down Expand Up @@ -1021,7 +1100,7 @@ defmodule Teiserver.Coordinator.ConsulCommands do
%{
state
| minimum_rank_to_play: max(min_level, 0),
maximum_rank_to_play: min(max_level, 1000)
maximum_rank_to_play: min(max_level, LobbyRestrictions.rank_upper_bound())
}
end

Expand Down Expand Up @@ -1418,12 +1497,7 @@ defmodule Teiserver.Coordinator.ConsulCommands do

lobby = Lobby.get_lobby(state.lobby_id)

not_all_welcome =
cond do
state.maximum_rating_to_play < 1000 -> true
state.minimum_rating_to_play > 0 -> true
true -> false
end
{check_name_result, check_name_msg} = LobbyRestrictions.check_lobby_name(stripped_name, state)

starts_with_lobby_policy =
new_name
Expand Down Expand Up @@ -1470,10 +1544,10 @@ defmodule Teiserver.Coordinator.ConsulCommands do

state

not_all_welcome && allwelcome_name?(stripped_name) ->
check_name_result != :ok ->
Lobby.sayex(
state.coordinator_id,
"You cannot declare a lobby to be welcome to all if there is a rating limit",
check_name_msg,
state.lobby_id
)

Expand All @@ -1498,21 +1572,10 @@ defmodule Teiserver.Coordinator.ConsulCommands do

ConsulServer.say_command(cmd, state)

downcase_name = new_name |> String.downcase()

skill_name =
[
String.contains?(downcase_name, "noob"),
String.contains?(downcase_name, "newbie")
]
|> Enum.any?()
if check_name_msg != nil do
#Send coordinator message which can be long; appears on right
CacheUser.send_direct_message(state.coordinator_id, senderid, check_name_msg)

if skill_name do
Lobby.sayex(
state.coordinator_id,
"Don't forget you can set skill requirements using the setratinglevels command, use $help setratinglevels for more information.",
state.lobby_id
)
end

state
Expand All @@ -1522,6 +1585,7 @@ defmodule Teiserver.Coordinator.ConsulCommands do

true ->
Battle.rename_lobby(state.lobby_id, new_name, nil)

state
end
end
Expand Down Expand Up @@ -2007,30 +2071,6 @@ defmodule Teiserver.Coordinator.ConsulCommands do
)
end

defp allowed_to_set_rating_limit?(state) do
name =
state.lobby_id
|> Battle.get_lobby()
|> Map.get(:name)

cond do
allwelcome_name?(name) -> false
true -> true
end
end

defp allwelcome_name?(name) do
name =
name
|> String.downcase()
|> String.replace(" ", "")

cond do
String.contains?(name, "allwelcome") -> true
true -> false
end
end

@spec get_lock(String.t()) :: atom | nil
defp get_lock(name) do
case name |> String.downcase() |> String.trim() do
Expand Down
Loading
Loading