Files
ClubPenguinClient/MareSynchronos/Managers/ServerConfigurationManager.cs
rootdarkarchon b2276a1883 Client rework for API change and paradigm shift (#39)
* most of the groups refactoring on client

* register OnMethods for group stuff

* start implementing client (still pretty broken)

* finish implementing new api first iteration

* idk rework everything for pair shit (still WIP); goal is to remove PairedClients and GroupPairClients from ApiController

* move everything to PairManager, remove dictionaries from APiController

* remove admin stuff from client, cleanup

* adjust reconnection handling, add new settings, todo still to remove access from old stuff that's marked obsolete from config

* add back adding servers, fix intro ui

* fix obsolete calls

* adjust config namespace

* add UI for setting animation/sound permissions to syncshells

* add ConfigurationService to hot reload config on change from external

* move transient data cache to configuration

* add deleting service to ui

* fix saving of transient resources

* fix group pair user assignments

* halt scanner when penumbra inactive, add visible/online/offline split to individual pairs and tags

* add presence to syncshell ui

* move fullpause from config to server config

* fixes in code style

* more codestyle

* show info icon on player in shells, don't show icon when no changes from default state are made, add online notifs

* fixes to intro UI

---------

Co-authored-by: rootdarkarchon <root.darkarchon@outlook.com>
2023-01-29 15:13:53 +01:00

169 lines
5.5 KiB
C#

using MareSynchronos.MareConfiguration;
using MareSynchronos.Models;
using MareSynchronos.Utils;
using MareSynchronos.WebAPI;
namespace MareSynchronos.Managers;
public class ServerConfigurationManager
{
private readonly Dictionary<JwtCache, string> _tokenDictionary = new();
private readonly ConfigurationService _configService;
private readonly DalamudUtil _dalamudUtil;
public string CurrentApiUrl => string.IsNullOrEmpty(_configService.Current.CurrentServer) ? ApiController.MainServiceUri : _configService.Current.CurrentServer;
public ServerStorage? CurrentServer => (_configService.Current.ServerStorage.ContainsKey(CurrentApiUrl) ? _configService.Current.ServerStorage[CurrentApiUrl] : null);
public ServerConfigurationManager(ConfigurationService configService, DalamudUtil dalamudUtil)
{
_configService = configService;
_dalamudUtil = dalamudUtil;
}
public bool HasValidConfig()
{
return CurrentServer != null && (CurrentServer?.Authentications.Any() ?? false);
}
public string[] GetServerApiUrls()
{
return _configService.Current.ServerStorage.Keys.ToArray();
}
public string[] GetServerNames()
{
return _configService.Current.ServerStorage.Values.Select(v => v.ServerName).ToArray();
}
public ServerStorage GetServerByIndex(int idx)
{
try
{
return _configService.Current.ServerStorage.ElementAt(idx).Value;
}
catch
{
_configService.Current.CurrentServer = ApiController.MainServiceUri;
if (!_configService.Current.ServerStorage.ContainsKey(ApiController.MainServer))
{
_configService.Current.ServerStorage.Add(_configService.Current.CurrentServer, new ServerStorage() { ServerUri = ApiController.MainServiceUri, ServerName = ApiController.MainServer });
}
_configService.Save();
return CurrentServer!;
}
}
public int GetCurrentServerIndex()
{
return Array.IndexOf(_configService.Current.ServerStorage.Keys.ToArray(), CurrentApiUrl);
}
public void Save()
{
_configService.Save();
}
public void SelectServer(int idx)
{
_configService.Current.CurrentServer = GetServerByIndex(idx).ServerUri;
CurrentServer!.FullPause = false;
Save();
}
public string? GetSecretKey(int serverIdx = -1)
{
ServerStorage? currentServer;
currentServer = serverIdx == -1 ? CurrentServer : GetServerByIndex(serverIdx);
Save();
if (currentServer == null)
{
currentServer = new();
Save();
}
var charaName = _dalamudUtil.PlayerName;
var worldId = _dalamudUtil.WorldId;
if (!currentServer.Authentications.Any() && currentServer.SecretKeys.Any())
{
currentServer.Authentications.Add(new Authentication()
{
CharacterName = charaName,
WorldId = worldId,
SecretKeyIdx = currentServer.SecretKeys.Last().Key,
});
Save();
}
var auth = currentServer.Authentications.Find(f => string.Equals(f.CharacterName, charaName, StringComparison.Ordinal) && f.WorldId == worldId);
if (auth == null) return null;
if (currentServer.SecretKeys.TryGetValue(auth.SecretKeyIdx, out var secretKey))
{
return secretKey.Key;
}
return null;
}
public string? GetToken()
{
var charaName = _dalamudUtil.PlayerName;
var worldId = _dalamudUtil.WorldId;
var secretKey = GetSecretKey();
if (secretKey == null) return null;
if (_tokenDictionary.TryGetValue(new JwtCache(CurrentApiUrl, charaName, worldId, secretKey), out var token))
{
return token;
}
return null;
}
public void SaveToken(string token)
{
var charaName = _dalamudUtil.PlayerName;
var worldId = _dalamudUtil.WorldId;
var secretKey = GetSecretKey();
if (string.IsNullOrEmpty(secretKey)) throw new InvalidOperationException("No secret key set");
_tokenDictionary[new JwtCache(CurrentApiUrl, charaName, worldId, secretKey)] = token;
}
internal void AddCurrentCharacterToServer(int serverSelectionIndex = -1, bool addLastSecretKey = false)
{
if (serverSelectionIndex == -1) serverSelectionIndex = GetCurrentServerIndex();
var server = GetServerByIndex(serverSelectionIndex);
server.Authentications.Add(new Authentication()
{
CharacterName = _dalamudUtil.PlayerName,
WorldId = _dalamudUtil.WorldId,
SecretKeyIdx = addLastSecretKey ? server.SecretKeys.Last().Key : -1,
});
_configService.Save();
}
internal void AddEmptyCharacterToServer(int serverSelectionIndex)
{
var server = GetServerByIndex(serverSelectionIndex);
server.Authentications.Add(new Authentication());
_configService.Save();
}
internal void RemoveCharacterFromServer(int serverSelectionIndex, Authentication item)
{
var server = GetServerByIndex(serverSelectionIndex);
server.Authentications.Remove(item);
}
internal void AddServer(ServerStorage serverStorage)
{
_configService.Current.ServerStorage[serverStorage.ServerUri] = serverStorage;
_configService.Save();
}
internal void DeleteServer(ServerStorage selectedServer)
{
_configService.Current.ServerStorage.Remove(selectedServer.ServerUri);
}
}