Skip to content

Commit

Permalink
Add a button to reset lobby options to default.
Browse files Browse the repository at this point in the history
  • Loading branch information
pchote authored and PunkPun committed Nov 12, 2023
1 parent 9a3c398 commit 9d174cd
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 1 deletion.
44 changes: 44 additions & 0 deletions OpenRA.Mods.Common/ServerTraits/LobbyCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ public class LobbyCommands : ServerTrait, IInterpretCommand, INotifyServerStart,
{ "slot_bot", SlotBot },
{ "map", Map },
{ "option", Option },
{ "reset_options", ResetOptions },
{ "assignteams", AssignTeams },
{ "kick", Kick },
{ "vote_kick", VoteKick },
Expand Down Expand Up @@ -744,6 +745,49 @@ static bool Option(S server, Connection conn, Session.Client client, string s)
}
}

static bool ResetOptions(S server, Connection conn, Session.Client client, string s)
{
lock (server.LobbyInfo)
{
if (!client.IsAdmin)
{
server.SendLocalizedMessageTo(conn, NotAdmin);
return true;
}

var allOptions = server.Map.PlayerActorInfo.TraitInfos<ILobbyOptions>()
.Concat(server.Map.WorldActorInfo.TraitInfos<ILobbyOptions>())
.SelectMany(t => t.LobbyOptions(server.Map));

var options = new Dictionary<string, Session.LobbyOptionState>();
foreach (var o in allOptions)
{
if (o.DefaultValue != server.LobbyInfo.GlobalSettings.LobbyOptions[o.Id].Value)
server.SendLocalizedMessage(ValueChanged, Translation.Arguments(
"player", client.Name,
"name", o.Name,
"value", o.Label(o.DefaultValue)));

options[o.Id] = new Session.LobbyOptionState
{
IsLocked = o.IsLocked,
Value = o.DefaultValue,
PreferredValue = o.DefaultValue
};
}

server.LobbyInfo.GlobalSettings.LobbyOptions = options;
server.SyncLobbyGlobalSettings();

foreach (var c in server.LobbyInfo.Clients)
c.State = Session.ClientState.NotReady;

server.SyncLobbyClients();

return true;
}
}

static bool AssignTeams(S server, Connection conn, Session.Client client, string raw)
{
lock (server.LobbyInfo)
Expand Down
28 changes: 27 additions & 1 deletion OpenRA.Mods.Common/Widgets/Logic/Lobby/LobbyLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ enum PanelType { Players, Options, Music, Servers, Kick, ForceStart }
bool insufficientPlayerSpawns;
bool teamChat;
bool updateDiscordStatus = true;
bool resetOptionsButtonEnabled;
Dictionary<int, SpawnOccupant> spawnOccupants = new();

readonly string chatLineSound;
Expand Down Expand Up @@ -167,6 +168,7 @@ void OnConnect()
Game.LobbyInfoChanged += UpdatePlayerList;
Game.LobbyInfoChanged += UpdateDiscordStatus;
Game.LobbyInfoChanged += UpdateSpawnOccupants;
Game.LobbyInfoChanged += UpdateOptions;
Game.BeforeGameStart += OnGameStart;
Game.ConnectionStateChanged += ConnectionStateChanged;

Expand Down Expand Up @@ -263,7 +265,7 @@ void OnConnect()
var slotsButton = lobby.GetOrNull<DropDownButtonWidget>("SLOTS_DROPDOWNBUTTON");
if (slotsButton != null)
{
slotsButton.IsVisible = () => panel != PanelType.Servers;
slotsButton.IsVisible = () => panel != PanelType.Servers && panel != PanelType.Options;
slotsButton.IsDisabled = () => configurationDisabled() || panel != PanelType.Players ||
(orderManager.LobbyInfo.Slots.Values.All(s => !s.AllowBots) &&
!orderManager.LobbyInfo.Slots.Any(s => !s.Value.LockTeam && orderManager.LobbyInfo.ClientInSlot(s.Key) != null));
Expand Down Expand Up @@ -357,6 +359,14 @@ ScrollItemWidget SetupItem(DropDownOption option, ScrollItemWidget template)
};
}

var resetOptionsButton = lobby.GetOrNull<ButtonWidget>("RESET_OPTIONS_BUTTON");
if (resetOptionsButton != null)
{
resetOptionsButton.IsVisible = () => panel == PanelType.Options;
resetOptionsButton.IsDisabled = () => configurationDisabled() || !resetOptionsButtonEnabled;
resetOptionsButton.OnMouseDown = _ => orderManager.IssueOrder(Order.Command("reset_options"));
}

var optionsBin = Ui.LoadWidget("LOBBY_OPTIONS_BIN", lobby.Get("TOP_PANELS_ROOT"), new WidgetArgs()
{
{ "orderManager", orderManager },
Expand Down Expand Up @@ -910,6 +920,22 @@ void UpdateSpawnOccupants()
.ToDictionary(c => c.SpawnPoint, c => new SpawnOccupant(c));
}

void UpdateOptions()
{
if (map == null || map.WorldActorInfo == null)
return;

var serverOptions = orderManager.LobbyInfo.GlobalSettings.LobbyOptions;
var mapOptions = map.PlayerActorInfo.TraitInfos<ILobbyOptions>()
.Concat(map.WorldActorInfo.TraitInfos<ILobbyOptions>())
.SelectMany(t => t.LobbyOptions(map))
.Where(o => o.IsVisible)
.OrderBy(o => o.DisplayOrder)
.ToArray();

resetOptionsButtonEnabled = mapOptions.Any(o => o.DefaultValue != serverOptions[o.Id].Value);
}

void OnGameStart()
{
Ui.CloseWindow();
Expand Down
6 changes: 6 additions & 0 deletions mods/cnc/chrome/lobby.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ Container@SERVER_LOBBY:
Width: 211
Height: 25
Text: dropdownbutton-bg-slots
Button@RESET_OPTIONS_BUTTON:
X: 15
Y: 254
Width: 211
Height: 25
Text: button-bg-reset-options
Container@SKIRMISH_TABS:
X: 697 - WIDTH
Width: 465
Expand Down
1 change: 1 addition & 0 deletions mods/cnc/languages/chrome/en.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ dropdownbutton-lobby-servers-bin-filters = Filter Games
## lobby.yaml
dropdownbutton-bg-slots = Slot Admin
button-bg-reset-options = Reset Defaults
button-skirmish-tabs-players-tab = Players
button-skirmish-tabs-options-tab = Options
button-skirmish-tabs-music-tab = Music
Expand Down
7 changes: 7 additions & 0 deletions mods/common/chrome/lobby.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ Background@SERVER_LOBBY:
Height: 25
Font: Bold
Text: dropdownbutton-server-lobby-slots
Button@RESET_OPTIONS_BUTTON:
X: 20
Y: 291
Width: 185
Height: 25
Font: Bold
Text: button-server-lobby-reset-options
Container@SKIRMISH_TABS:
X: 695 - WIDTH
Width: 486
Expand Down
1 change: 1 addition & 0 deletions mods/common/languages/chrome/en.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ label-notice-container-playtest-available = A preview of the next OpenRA release
## lobby.yaml
dropdownbutton-server-lobby-slots = Slot Admin
button-server-lobby-reset-options = Reset Defaults
button-skirmish-tabs-players-tab = Players
button-skirmish-tabs-options-tab = Options
button-skirmish-tabs-music-tab = Music
Expand Down

0 comments on commit 9d174cd

Please sign in to comment.