Persist construction menu favorites server-side (#35867)

* Persist construction menu favorites to player profile

* Use `ProtoId`s for construction favorites

* Validate construction favorites updates from the client

* Actually await the async database call
This commit is contained in:
YotaXP
2025-05-17 13:37:19 -04:00
committed by GitHub
parent 1141dcb868
commit e404e45ffc
16 changed files with 4424 additions and 25 deletions

View File

@@ -1,5 +1,6 @@
using System.Linq;
using System.Numerics;
using Content.Client.Lobby;
using Content.Client.Stylesheets;
using Content.Client.UserInterface.Systems.MenuBar.Widgets;
using Content.Shared.Construction.Prototypes;
@@ -28,6 +29,7 @@ namespace Content.Client.Construction.UI
[Dependency] private readonly IPlacementManager _placementManager = default!;
[Dependency] private readonly IUserInterfaceManager _uiManager = default!;
[Dependency] private readonly IPlayerManager _playerManager = default!;
[Dependency] private readonly IClientPreferencesManager _preferencesManager = default!;
private readonly SpriteSystem _spriteSystem;
private readonly IConstructionMenuView _constructionView;
@@ -116,7 +118,7 @@ namespace Content.Client.Construction.UI
_constructionView.RecipeFavorited += (_, _) => OnViewFavoriteRecipe();
PopulateCategories();
SetFavorites(_preferencesManager.Preferences?.ConstructionFavorites ?? []);
OnViewPopulateRecipes(_constructionView, (string.Empty, string.Empty));
}
@@ -493,10 +495,34 @@ namespace Content.Client.Construction.UI
_favoritedRecipes.Count > 0 ? (string.Empty, FavoriteCatName) : (string.Empty, string.Empty));
}
var newFavorites = new List<ProtoId<ConstructionPrototype>>(_favoritedRecipes.Count);
foreach (var recipe in _favoritedRecipes)
newFavorites.Add(recipe.ID);
_preferencesManager.UpdateConstructionFavorites(newFavorites);
PopulateInfo(_selected);
PopulateCategories(_selectedCategory);
}
public void SetFavorites(IReadOnlyList<ProtoId<ConstructionPrototype>> favorites)
{
_favoritedRecipes.Clear();
foreach (var id in favorites)
{
if (_prototypeManager.TryIndex(id, out ConstructionPrototype? recipe, logError: false))
_favoritedRecipes.Add(recipe);
}
if (_selectedCategory == FavoriteCatName)
{
OnViewPopulateRecipes(_constructionView,
_favoritedRecipes.Count > 0 ? (string.Empty, FavoriteCatName) : (string.Empty, string.Empty));
}
PopulateCategories(_selectedCategory);
}
private void SystemBindingChanged(ConstructionSystem? newSystem)
{
if (newSystem is null)

View File

@@ -1,8 +1,10 @@
using System.Linq;
using Content.Shared.Construction.Prototypes;
using Content.Shared.Preferences;
using Robust.Client;
using Robust.Client.Player;
using Robust.Shared.Network;
using Robust.Shared.Prototypes;
using Robust.Shared.Utility;
namespace Content.Client.Lobby
@@ -49,7 +51,7 @@ namespace Content.Client.Lobby
public void SelectCharacter(int slot)
{
Preferences = new PlayerPreferences(Preferences.Characters, slot, Preferences.AdminOOCColor);
Preferences = new PlayerPreferences(Preferences.Characters, slot, Preferences.AdminOOCColor, Preferences.ConstructionFavorites);
var msg = new MsgSelectCharacter
{
SelectedCharacterIndex = slot
@@ -62,7 +64,7 @@ namespace Content.Client.Lobby
var collection = IoCManager.Instance!;
profile.EnsureValid(_playerManager.LocalSession!, collection);
var characters = new Dictionary<int, ICharacterProfile>(Preferences.Characters) {[slot] = profile};
Preferences = new PlayerPreferences(characters, Preferences.SelectedCharacterIndex, Preferences.AdminOOCColor);
Preferences = new PlayerPreferences(characters, Preferences.SelectedCharacterIndex, Preferences.AdminOOCColor, Preferences.ConstructionFavorites);
var msg = new MsgUpdateCharacter
{
Profile = profile,
@@ -85,7 +87,7 @@ namespace Content.Client.Lobby
var l = lowest.Value;
characters.Add(l, profile);
Preferences = new PlayerPreferences(characters, Preferences.SelectedCharacterIndex, Preferences.AdminOOCColor);
Preferences = new PlayerPreferences(characters, Preferences.SelectedCharacterIndex, Preferences.AdminOOCColor, Preferences.ConstructionFavorites);
UpdateCharacter(profile, l);
}
@@ -98,7 +100,7 @@ namespace Content.Client.Lobby
public void DeleteCharacter(int slot)
{
var characters = Preferences.Characters.Where(p => p.Key != slot);
Preferences = new PlayerPreferences(characters, Preferences.SelectedCharacterIndex, Preferences.AdminOOCColor);
Preferences = new PlayerPreferences(characters, Preferences.SelectedCharacterIndex, Preferences.AdminOOCColor, Preferences.ConstructionFavorites);
var msg = new MsgDeleteCharacter
{
Slot = slot
@@ -106,6 +108,16 @@ namespace Content.Client.Lobby
_netManager.ClientSendMessage(msg);
}
public void UpdateConstructionFavorites(List<ProtoId<ConstructionPrototype>> favorites)
{
Preferences = new PlayerPreferences(Preferences.Characters, Preferences.SelectedCharacterIndex, Preferences.AdminOOCColor, favorites);
var msg = new MsgUpdateConstructionFavorites
{
Favorites = favorites
};
_netManager.ClientSendMessage(msg);
}
private void HandlePreferencesAndSettings(MsgPreferencesAndSettings message)
{
Preferences = message.Preferences;

View File

@@ -1,4 +1,6 @@
using Content.Shared.Construction.Prototypes;
using Content.Shared.Preferences;
using Robust.Shared.Prototypes;
namespace Content.Client.Lobby
{
@@ -17,5 +19,6 @@ namespace Content.Client.Lobby
void CreateCharacter(ICharacterProfile profile);
void DeleteCharacter(ICharacterProfile profile);
void DeleteCharacter(int slot);
void UpdateConstructionFavorites(List<ProtoId<ConstructionPrototype>> favorites);
}
}