mirror of
https://github.com/WWhiteDreamProject/wwdpublic.git
synced 2026-04-17 21:48:58 +03:00
<!-- This is a semi-strict format, you can add/remove sections as needed but the order/format should be kept the same Remove these comments before submitting --> # Description the adding AI is now up to y'all because i'm not touching loadout code for name datasets, but it shouldn't be too bad from here --------- Signed-off-by: sleepyyapril <123355664+sleepyyapril@users.noreply.github.com> Signed-off-by: SolStar <44028047+ewokswagger@users.noreply.github.com> Signed-off-by: deltanedas <39013340+deltanedas@users.noreply.github.com> Co-authored-by: themias <89101928+themias@users.noreply.github.com> Co-authored-by: Verm <32827189+Vermidia@users.noreply.github.com> Co-authored-by: DrSmugleaf <10968691+DrSmugleaf@users.noreply.github.com> Co-authored-by: Sphiral <145869023+SphiraI@users.noreply.github.com> Co-authored-by: Ed <96445749+TheShuEd@users.noreply.github.com> Co-authored-by: Mr. 27 <45323883+Dutch-VanDerLinde@users.noreply.github.com> Co-authored-by: metalgearsloth <comedian_vs_clown@hotmail.com> Co-authored-by: Alzore <140123969+Blackern5000@users.noreply.github.com> Co-authored-by: ravage <142820619+ravage123321@users.noreply.github.com> Co-authored-by: slarticodefast <161409025+slarticodefast@users.noreply.github.com> Co-authored-by: Intoxicating-Innocence <188202277+Intoxicating-Innocence@users.noreply.github.com> Co-authored-by: Saphire <lattice@saphi.re> Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> Co-authored-by: Errant <35878406+Errant-4@users.noreply.github.com> Co-authored-by: Tayrtahn <tayrtahn@gmail.com> Co-authored-by: CaasGit <87243814+CaasGit@users.noreply.github.com> Co-authored-by: BramvanZijp <56019239+BramvanZijp@users.noreply.github.com> Co-authored-by: Boaz1111 <149967078+Boaz1111@users.noreply.github.com> Co-authored-by: NakataRin <45946146+NakataRin@users.noreply.github.com> Co-authored-by: Kara <lunarautomaton6@gmail.com> Co-authored-by: Plykiya <58439124+Plykiya@users.noreply.github.com> Co-authored-by: SlamBamActionman <slambamactionman@gmail.com> Co-authored-by: Doomsdrayk <robotdoughnut@comcast.net> Co-authored-by: Brandon Hu <103440971+Brandon-Huu@users.noreply.github.com> Co-authored-by: SlamBamActionman <83650252+SlamBamActionman@users.noreply.github.com> Co-authored-by: ElectroJr <leonsfriedrich@gmail.com> Co-authored-by: Pieter-Jan Briers <pieterjan.briers+git@gmail.com> Co-authored-by: DrSmugleaf <DrSmugleaf@users.noreply.github.com> Co-authored-by: Julian Giebel <juliangiebel@live.de> Co-authored-by: nikthechampiongr <32041239+nikthechampiongr@users.noreply.github.com> Co-authored-by: Repo <47093363+Titian3@users.noreply.github.com> Co-authored-by: Chief-Engineer <119664036+Chief-Engineer@users.noreply.github.com> Co-authored-by: icekot8 <93311212+icekot8@users.noreply.github.com> Co-authored-by: AJCM-git <60196617+AJCM-git@users.noreply.github.com> Co-authored-by: Leon Friedrich <60421075+ElectroJr@users.noreply.github.com> Co-authored-by: no <165581243+pissdemon@users.noreply.github.com> Co-authored-by: Tornado Tech <54727692+Tornado-Technology@users.noreply.github.com> Co-authored-by: osjarw <62134478+osjarw@users.noreply.github.com> Co-authored-by: Simon <63975668+Simyon264@users.noreply.github.com> Co-authored-by: TGRCDev <tgrc@tgrc.dev> Co-authored-by: Milon <milonpl.git@proton.me> Co-authored-by: deltanedas <39013340+deltanedas@users.noreply.github.com> Co-authored-by: ShadowCommander <10494922+ShadowCommander@users.noreply.github.com> Co-authored-by: Fildrance <fildrance@gmail.com> Co-authored-by: pa.pecherskij <pa.pecherskij@interfax.ru> Co-authored-by: chavonadelal <156101927+chavonadelal@users.noreply.github.com> Co-authored-by: SolStar <44028047+ewokswagger@users.noreply.github.com> Co-authored-by: K-Dynamic <20566341+K-Dynamic@users.noreply.github.com> Co-authored-by: lzk <124214523+lzk228@users.noreply.github.com> Co-authored-by: ArchRBX <5040911+ArchRBX@users.noreply.github.com> Co-authored-by: archrbx <punk.gear5260@fastmail.com> Co-authored-by: Radezolid <snappednexus@gmail.com> Co-authored-by: Nemanja <98561806+EmoGarbage404@users.noreply.github.com> Co-authored-by: EmoGarbage404 <retron404@gmail.com> Co-authored-by: MilenVolf <63782763+MilenVolf@users.noreply.github.com> Co-authored-by: Velcroboy <107660393+IamVelcroboy@users.noreply.github.com> Co-authored-by: Velcroboy <velcroboy333@hotmail.com> Co-authored-by: neuPanda <chriseparton@gmail.com> Co-authored-by: neuPanda <spainman0@yahoo.com> Co-authored-by: Dvir <39403717+dvir001@users.noreply.github.com> Co-authored-by: Whatstone <whatston3@gmail.com> Co-authored-by: VideoKompany <135313844+VlaDOS1408@users.noreply.github.com> (cherry picked from commit 93ed70acfeda357133a701f637d3faeec02749bb)
347 lines
13 KiB
C#
347 lines
13 KiB
C#
using System.Diagnostics.CodeAnalysis;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Content.Server.Database;
|
|
using Content.Shared.CCVar;
|
|
using Content.Shared.Preferences;
|
|
using Robust.Server.Player;
|
|
using Robust.Shared.Configuration;
|
|
using Robust.Shared.Network;
|
|
using Robust.Shared.Player;
|
|
using Robust.Shared.Prototypes;
|
|
using Robust.Shared.Utility;
|
|
|
|
|
|
namespace Content.Server.Preferences.Managers
|
|
{
|
|
/// <summary>
|
|
/// Sends <see cref="MsgPreferencesAndSettings"/> before the client joins the lobby.
|
|
/// Receives <see cref="MsgSelectCharacter"/> and <see cref="MsgUpdateCharacter"/> at any time.
|
|
/// </summary>
|
|
public sealed class ServerPreferencesManager : IServerPreferencesManager, IPostInjectInit
|
|
{
|
|
[Dependency] private readonly IServerNetManager _netManager = default!;
|
|
[Dependency] private readonly IConfigurationManager _cfg = default!;
|
|
[Dependency] private readonly IServerDbManager _db = default!;
|
|
[Dependency] private readonly IPlayerManager _playerManager = default!;
|
|
[Dependency] private readonly IDependencyCollection _dependencies = default!;
|
|
[Dependency] private readonly ILogManager _log = default!;
|
|
[Dependency] private readonly UserDbDataManager _userDb = default!;
|
|
[Dependency] private readonly IPrototypeManager _protos = default!;
|
|
|
|
// Cache player prefs on the server so we don't need as much async hell related to them.
|
|
private readonly Dictionary<NetUserId, PlayerPrefData> _cachedPlayerPrefs =
|
|
new();
|
|
|
|
private ISawmill _sawmill = default!;
|
|
|
|
private int MaxCharacterSlots => _cfg.GetCVar(CCVars.GameMaxCharacterSlots);
|
|
|
|
public void Init()
|
|
{
|
|
_netManager.RegisterNetMessage<MsgPreferencesAndSettings>();
|
|
_netManager.RegisterNetMessage<MsgSelectCharacter>(HandleSelectCharacterMessage);
|
|
_netManager.RegisterNetMessage<MsgUpdateCharacter>(HandleUpdateCharacterMessage);
|
|
_netManager.RegisterNetMessage<MsgDeleteCharacter>(HandleDeleteCharacterMessage);
|
|
_sawmill = _log.GetSawmill("prefs");
|
|
}
|
|
|
|
private async void HandleSelectCharacterMessage(MsgSelectCharacter message)
|
|
{
|
|
var index = message.SelectedCharacterIndex;
|
|
var userId = message.MsgChannel.UserId;
|
|
|
|
if (!_cachedPlayerPrefs.TryGetValue(userId, out var prefsData) || !prefsData.PrefsLoaded)
|
|
{
|
|
_sawmill.Error($"User {userId} tried to modify preferences before they loaded.");
|
|
return;
|
|
}
|
|
|
|
if (index < 0 || index >= MaxCharacterSlots)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var curPrefs = prefsData.Prefs!;
|
|
|
|
if (!curPrefs.Characters.ContainsKey(index))
|
|
{
|
|
// Non-existent slot.
|
|
return;
|
|
}
|
|
|
|
prefsData.Prefs = new PlayerPreferences(curPrefs.Characters, index, curPrefs.AdminOOCColor);
|
|
|
|
if (ShouldStorePrefs(message.MsgChannel.AuthType))
|
|
{
|
|
await _db.SaveSelectedCharacterIndexAsync(message.MsgChannel.UserId, message.SelectedCharacterIndex);
|
|
}
|
|
}
|
|
|
|
private async void HandleUpdateCharacterMessage(MsgUpdateCharacter message)
|
|
{
|
|
var userId = message.MsgChannel.UserId;
|
|
|
|
// ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
|
|
if (message.Profile == null)
|
|
_sawmill.Error($"User {userId} sent a {nameof(MsgUpdateCharacter)} with a null profile in slot {message.Slot}.");
|
|
else
|
|
await SetProfile(userId, message.Slot, message.Profile);
|
|
}
|
|
|
|
public async Task SetProfile(NetUserId userId, int slot, ICharacterProfile profile)
|
|
{
|
|
if (!_cachedPlayerPrefs.TryGetValue(userId, out var prefsData) || !prefsData.PrefsLoaded)
|
|
{
|
|
_sawmill.Error($"Tried to modify user {userId} preferences before they loaded.");
|
|
return;
|
|
}
|
|
|
|
if (slot < 0 || slot >= MaxCharacterSlots)
|
|
return;
|
|
|
|
var curPrefs = prefsData.Prefs!;
|
|
var session = _playerManager.GetSessionById(userId);
|
|
|
|
profile.EnsureValid(session, _dependencies);
|
|
|
|
var profiles = new Dictionary<int, ICharacterProfile>(curPrefs.Characters)
|
|
{
|
|
[slot] = profile
|
|
};
|
|
|
|
prefsData.Prefs = new PlayerPreferences(profiles, slot, curPrefs.AdminOOCColor);
|
|
|
|
if (ShouldStorePrefs(session.Channel.AuthType))
|
|
await _db.SaveCharacterSlotAsync(userId, profile, slot);
|
|
}
|
|
|
|
private async void HandleDeleteCharacterMessage(MsgDeleteCharacter message)
|
|
{
|
|
var slot = message.Slot;
|
|
var userId = message.MsgChannel.UserId;
|
|
|
|
if (!_cachedPlayerPrefs.TryGetValue(userId, out var prefsData) || !prefsData.PrefsLoaded)
|
|
{
|
|
Logger.WarningS("prefs", $"User {userId} tried to modify preferences before they loaded.");
|
|
return;
|
|
}
|
|
|
|
if (slot < 0 || slot >= MaxCharacterSlots)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var curPrefs = prefsData.Prefs!;
|
|
|
|
// If they try to delete the slot they have selected then we switch to another one.
|
|
// Of course, that's only if they HAVE another slot.
|
|
int? nextSlot = null;
|
|
if (curPrefs.SelectedCharacterIndex == slot)
|
|
{
|
|
// That ! on the end is because Rider doesn't like .NET 5.
|
|
var (ns, profile) = curPrefs.Characters.FirstOrDefault(p => p.Key != message.Slot);
|
|
if (profile == null)
|
|
{
|
|
// Only slot left, can't delete.
|
|
return;
|
|
}
|
|
|
|
nextSlot = ns;
|
|
}
|
|
|
|
var arr = new Dictionary<int, ICharacterProfile>(curPrefs.Characters);
|
|
arr.Remove(slot);
|
|
|
|
prefsData.Prefs = new PlayerPreferences(arr, nextSlot ?? curPrefs.SelectedCharacterIndex, curPrefs.AdminOOCColor);
|
|
|
|
if (!ShouldStorePrefs(message.MsgChannel.AuthType))
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (nextSlot != null)
|
|
{
|
|
await _db.DeleteSlotAndSetSelectedIndex(userId, slot, nextSlot.Value);
|
|
}
|
|
else
|
|
{
|
|
await _db.SaveCharacterSlotAsync(userId, null, slot);
|
|
}
|
|
}
|
|
|
|
// Should only be called via UserDbDataManager.
|
|
public async Task LoadData(ICommonSession session, CancellationToken cancel)
|
|
{
|
|
if (!ShouldStorePrefs(session.Channel.AuthType))
|
|
{
|
|
// Don't store data for guests.
|
|
var prefsData = new PlayerPrefData
|
|
{
|
|
PrefsLoaded = true,
|
|
Prefs = new PlayerPreferences(
|
|
new[] { new KeyValuePair<int, ICharacterProfile>(0, HumanoidCharacterProfile.Random()) },
|
|
0, Color.Transparent)
|
|
};
|
|
|
|
_cachedPlayerPrefs[session.UserId] = prefsData;
|
|
}
|
|
else
|
|
{
|
|
var prefsData = new PlayerPrefData();
|
|
var loadTask = LoadPrefs();
|
|
_cachedPlayerPrefs[session.UserId] = prefsData;
|
|
|
|
await loadTask;
|
|
|
|
async Task LoadPrefs()
|
|
{
|
|
var prefs = await GetOrCreatePreferencesAsync(session.UserId, cancel);
|
|
prefsData.Prefs = prefs;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void FinishLoad(ICommonSession session)
|
|
{
|
|
// This is a separate step from the actual database load.
|
|
// Sanitizing preferences requires play time info due to loadouts.
|
|
// And play time info is loaded concurrently from the DB with preferences.
|
|
var prefsData = _cachedPlayerPrefs[session.UserId];
|
|
DebugTools.Assert(prefsData.Prefs != null);
|
|
prefsData.Prefs = SanitizePreferences(session, prefsData.Prefs, _dependencies);
|
|
|
|
prefsData.PrefsLoaded = true;
|
|
|
|
var msg = new MsgPreferencesAndSettings();
|
|
msg.Preferences = prefsData.Prefs;
|
|
msg.Settings = new GameSettings
|
|
{
|
|
MaxCharacterSlots = MaxCharacterSlots
|
|
};
|
|
_netManager.ServerSendMessage(msg, session.Channel);
|
|
}
|
|
|
|
public void SanitizeData(ICommonSession session)
|
|
{
|
|
// This is a separate step from the actual database load.
|
|
// Sanitizing preferences requires play time info due to loadouts.
|
|
// And play time info is loaded concurrently from the DB with preferences.
|
|
var data = _cachedPlayerPrefs[session.UserId];
|
|
DebugTools.Assert(data.Prefs != null);
|
|
data.Prefs = SanitizePreferences(session, data.Prefs, _dependencies);
|
|
_sawmill.Debug("here");
|
|
}
|
|
|
|
public void OnClientDisconnected(ICommonSession session)
|
|
{
|
|
_cachedPlayerPrefs.Remove(session.UserId);
|
|
}
|
|
|
|
public bool HavePreferencesLoaded(ICommonSession session)
|
|
{
|
|
return _cachedPlayerPrefs.ContainsKey(session.UserId);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Tries to get the preferences from the cache
|
|
/// </summary>
|
|
/// <param name="userId">User Id to get preferences for</param>
|
|
/// <param name="playerPreferences">The user preferences if true, otherwise null</param>
|
|
/// <returns>If preferences are not null</returns>
|
|
public bool TryGetCachedPreferences(NetUserId userId,
|
|
[NotNullWhen(true)] out PlayerPreferences? playerPreferences)
|
|
{
|
|
if (_cachedPlayerPrefs.TryGetValue(userId, out var prefs))
|
|
{
|
|
playerPreferences = prefs.Prefs;
|
|
return prefs.Prefs != null;
|
|
}
|
|
|
|
playerPreferences = null;
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieves preferences for the given username from storage.
|
|
/// Creates and saves default preferences if they are not found, then returns them.
|
|
/// </summary>
|
|
public PlayerPreferences GetPreferences(NetUserId userId)
|
|
{
|
|
var prefs = _cachedPlayerPrefs[userId].Prefs;
|
|
if (prefs == null)
|
|
{
|
|
throw new InvalidOperationException("Preferences for this player have not loaded yet.");
|
|
}
|
|
|
|
return prefs;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieves preferences for the given username from storage or returns null.
|
|
/// Creates and saves default preferences if they are not found, then returns them.
|
|
/// </summary>
|
|
public PlayerPreferences? GetPreferencesOrNull(NetUserId? userId)
|
|
{
|
|
if (userId == null)
|
|
return null;
|
|
|
|
if (_cachedPlayerPrefs.TryGetValue(userId.Value, out var pref))
|
|
return pref.Prefs;
|
|
return null;
|
|
}
|
|
|
|
private async Task<PlayerPreferences> GetOrCreatePreferencesAsync(NetUserId userId, CancellationToken cancel)
|
|
{
|
|
var prefs = await _db.GetPlayerPreferencesAsync(userId, cancel);
|
|
if (prefs is null)
|
|
{
|
|
return await _db.InitPrefsAsync(userId, HumanoidCharacterProfile.Random(), cancel);
|
|
}
|
|
|
|
return prefs;
|
|
}
|
|
|
|
private PlayerPreferences SanitizePreferences(ICommonSession session, PlayerPreferences prefs,
|
|
IDependencyCollection collection)
|
|
{
|
|
// Clean up preferences in case of changes to the game,
|
|
// such as removed jobs still being selected.
|
|
return new PlayerPreferences(prefs.Characters.Select(p => new KeyValuePair<int, ICharacterProfile>(p.Key,
|
|
p.Value.Validated(session, collection))), prefs.SelectedCharacterIndex, prefs.AdminOOCColor);
|
|
}
|
|
|
|
public IEnumerable<KeyValuePair<NetUserId, ICharacterProfile>> GetSelectedProfilesForPlayers(
|
|
List<NetUserId> usernames)
|
|
{
|
|
return usernames
|
|
.Select(p => (_cachedPlayerPrefs[p].Prefs, p))
|
|
.Where(p => p.Prefs != null)
|
|
.Select(p =>
|
|
{
|
|
var idx = p.Prefs!.SelectedCharacterIndex;
|
|
return new KeyValuePair<NetUserId, ICharacterProfile>(p.p, p.Prefs!.GetProfile(idx));
|
|
});
|
|
}
|
|
|
|
internal static bool ShouldStorePrefs(LoginType loginType)
|
|
{
|
|
return loginType.HasStaticUserId();
|
|
}
|
|
|
|
private sealed class PlayerPrefData
|
|
{
|
|
public bool PrefsLoaded;
|
|
public PlayerPreferences? Prefs;
|
|
}
|
|
|
|
void IPostInjectInit.PostInject()
|
|
{
|
|
_userDb.AddOnLoadPlayer(LoadData);
|
|
_userDb.AddOnFinishLoad(FinishLoad);
|
|
_userDb.AddOnPlayerDisconnect(OnClientDisconnected);
|
|
}
|
|
}
|
|
}
|