Files
tbd-station-14/Content.Client/ClientPreferencesManager.cs
Acruid ca4fd649fe Massive Namespace Cleanup (#3120)
* Engine namespace changes.

* Automated remove redundant using statements.

* Simplified Graphics namespace.

* Apparently the container system stores full type names in the map file.😞 This updates those names.

* API Changes to LocalizationManager.LoadCulture.

* Update submodule to v0.3.2
2021-02-11 01:13:03 -08:00

100 lines
3.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using Content.Client.Interfaces;
using Content.Shared.Network.NetMessages;
using Content.Shared.Preferences;
using Robust.Shared.IoC;
using Robust.Shared.Network;
using Robust.Shared.Utility;
namespace Content.Client
{
/// <summary>
/// Receives <see cref="PlayerPreferences" /> and <see cref="GameSettings" /> from the server during the initial
/// connection.
/// Stores preferences on the server through <see cref="SelectCharacter" /> and <see cref="UpdateCharacter" />.
/// </summary>
public class ClientPreferencesManager : IClientPreferencesManager
{
[Dependency] private readonly IClientNetManager _netManager = default!;
public event Action OnServerDataLoaded;
public GameSettings Settings { get; private set; }
public PlayerPreferences Preferences { get; private set; }
public void Initialize()
{
_netManager.RegisterNetMessage<MsgPreferencesAndSettings>(nameof(MsgPreferencesAndSettings),
HandlePreferencesAndSettings);
_netManager.RegisterNetMessage<MsgUpdateCharacter>(nameof(MsgUpdateCharacter));
_netManager.RegisterNetMessage<MsgSelectCharacter>(nameof(MsgSelectCharacter));
_netManager.RegisterNetMessage<MsgDeleteCharacter>(nameof(MsgDeleteCharacter));
}
public void SelectCharacter(ICharacterProfile profile)
{
SelectCharacter(Preferences.IndexOfCharacter(profile));
}
public void SelectCharacter(int slot)
{
Preferences = new PlayerPreferences(Preferences.Characters, slot);
var msg = _netManager.CreateNetMessage<MsgSelectCharacter>();
msg.SelectedCharacterIndex = slot;
_netManager.ClientSendMessage(msg);
}
public void UpdateCharacter(ICharacterProfile profile, int slot)
{
var characters = new Dictionary<int, ICharacterProfile>(Preferences.Characters) {[slot] = profile};
Preferences = new PlayerPreferences(characters, Preferences.SelectedCharacterIndex);
var msg = _netManager.CreateNetMessage<MsgUpdateCharacter>();
msg.Profile = profile;
msg.Slot = slot;
_netManager.ClientSendMessage(msg);
}
public void CreateCharacter(ICharacterProfile profile)
{
var characters = new Dictionary<int, ICharacterProfile>(Preferences.Characters);
var lowest = Enumerable.Range(0, Settings.MaxCharacterSlots)
.Except(characters.Keys)
.FirstOrNull();
if (lowest == null)
{
throw new InvalidOperationException("Out of character slots!");
}
var l = lowest.Value;
characters.Add(l, profile);
Preferences = new PlayerPreferences(characters, Preferences.SelectedCharacterIndex);
UpdateCharacter(profile, l);
}
public void DeleteCharacter(ICharacterProfile profile)
{
DeleteCharacter(Preferences.IndexOfCharacter(profile));
}
public void DeleteCharacter(int slot)
{
var characters = Preferences.Characters.Where(p => p.Key != slot);
Preferences = new PlayerPreferences(characters, Preferences.SelectedCharacterIndex);
var msg = _netManager.CreateNetMessage<MsgDeleteCharacter>();
msg.Slot = slot;
_netManager.ClientSendMessage(msg);
}
private void HandlePreferencesAndSettings(MsgPreferencesAndSettings message)
{
Preferences = message.Preferences;
Settings = message.Settings;
OnServerDataLoaded?.Invoke();
}
}
}