using LANCommander.SDK.Models; using Microsoft.Extensions.Logging; using System; using System.IO; using System.Threading.Tasks; using LANCommander.SDK.Extensions; using LANCommander.SDK.Factories; namespace LANCommander.SDK.Services { public class ProfileClient( ApiRequestFactory apiRequestFactory, ILogger logger, IConnectionClient connectionClient) { private User _user; public async Task GetAsync(bool forceLoad = false) { logger?.LogTrace("Requesting player's profile..."); if (_user == null || forceLoad) { _user = await apiRequestFactory .Create() .UseAuthenticationToken() .UseVersioning() .UseRoute("/api/Profile") .GetAsync(); } return _user; } public async Task GetAliasAsync() { try { _user = await GetAsync(); } catch (Exception ex) { logger?.LogError(ex, "Could not get user alias from server"); } return String.IsNullOrWhiteSpace(_user.Alias) ? _user.UserName : _user.Alias; } public async Task ChangeAliasAsync(string alias) { logger?.LogTrace("Requesting to change player alias..."); if (_user == null) _user = new User(); _user.Alias = alias; return await apiRequestFactory .Create() .UseAuthenticationToken() .UseVersioning() .UseRoute("/api/Profile/ChangeAlias") .AddBody(new { Alias = alias }) .PutAsync(); } public async Task GetAvatarAsync() { logger?.LogTrace("Requesting avatar contents..."); using (var ms = new MemoryStream()) { var stream = await apiRequestFactory .Create() .UseAuthenticationToken() .UseVersioning() .UseRoute("/api/Profile/Avatar") .StreamAsync(); await stream.CopyToAsync(ms); return ms.ToArray(); } } public async Task DownloadAvatar() { logger?.LogTrace("Retrieving player's avatar..."); var tempFile = Path.GetTempFileName(); var result = await apiRequestFactory .Create() .UseAuthenticationToken() .UseVersioning() .UseRoute("/api/Profile/Avatar") .DownloadAsync(tempFile); return result.FullName; } public Uri GetAvatarUri(string userName) => connectionClient.GetServerAddress().Join("api", "Profile", userName, "Avatar"); public async Task GetCustomFieldAsync(string name) { logger?.LogTrace("Getting player custom field with name {CustomFieldName}...", name); return await apiRequestFactory .Create() .UseAuthenticationToken() .UseVersioning() .UseRoute($"/api/Profile/CustomField/{name}") .GetAsync(); } public async Task UpdateCustomFieldAsync(string name, string value) { logger?.LogTrace("Updating player custom fields: {CustomFieldName} = {CustomFieldValue}", name, value); return await apiRequestFactory .Create() .UseAuthenticationToken() .UseVersioning() .UseRoute($"/api/Profile/CustomField/{name}") .AddBody(value) .PutAsync(); } } }