2025-08-18 03:00:08 -05:00
|
|
|
|
using LANCommander.SDK.Models;
|
2024-01-02 02:34:58 -06:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
2024-10-04 23:37:49 -05:00
|
|
|
|
namespace LANCommander.SDK.Services
|
2024-01-02 02:34:58 -06:00
|
|
|
|
{
|
|
|
|
|
|
public class ProfileService
|
|
|
|
|
|
{
|
2025-08-18 03:02:49 -05:00
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
private readonly Client _client;
|
2024-01-02 02:34:58 -06:00
|
|
|
|
|
2025-08-18 03:02:49 -05:00
|
|
|
|
private User _user;
|
2024-08-07 01:14:13 -05:00
|
|
|
|
|
2024-01-02 02:34:58 -06:00
|
|
|
|
public ProfileService(Client client)
|
|
|
|
|
|
{
|
2025-08-18 03:02:49 -05:00
|
|
|
|
_client = client;
|
2024-01-02 02:34:58 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public ProfileService(Client client, ILogger logger)
|
|
|
|
|
|
{
|
2025-08-18 03:02:49 -05:00
|
|
|
|
_client = client;
|
|
|
|
|
|
_logger = logger;
|
2024-01-02 02:34:58 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-08-07 01:14:13 -05:00
|
|
|
|
public User Get(bool forceLoad = false)
|
2024-01-02 02:34:58 -06:00
|
|
|
|
{
|
2025-08-18 03:02:49 -05:00
|
|
|
|
_logger?.LogTrace("Requesting player's profile...");
|
2024-01-02 02:34:58 -06:00
|
|
|
|
|
2025-08-18 03:02:49 -05:00
|
|
|
|
if (_user == null || forceLoad)
|
|
|
|
|
|
_user = _client.GetRequest<User>("/api/Profile");
|
2024-08-07 01:14:13 -05:00
|
|
|
|
|
2025-08-18 03:02:49 -05:00
|
|
|
|
return _user;
|
2024-01-02 02:34:58 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-08-07 01:14:13 -05:00
|
|
|
|
public async Task<User> GetAsync(bool forceLoad = false)
|
2024-06-03 19:08:50 -05:00
|
|
|
|
{
|
2025-08-18 03:02:49 -05:00
|
|
|
|
_logger?.LogTrace("Requesting player's profile...");
|
2024-06-03 19:08:50 -05:00
|
|
|
|
|
2025-08-18 03:02:49 -05:00
|
|
|
|
if (_user == null || forceLoad)
|
|
|
|
|
|
_user = await _client.GetRequestAsync<User>("/api/Profile");
|
2024-08-07 01:14:13 -05:00
|
|
|
|
|
2025-08-18 03:02:49 -05:00
|
|
|
|
return _user;
|
2024-08-07 01:14:13 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<string> GetAliasAsync()
|
|
|
|
|
|
{
|
2024-10-05 17:09:17 -05:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-08-18 03:02:49 -05:00
|
|
|
|
_user = await GetAsync();
|
2024-10-05 17:09:17 -05:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2025-08-18 03:02:49 -05:00
|
|
|
|
_logger?.LogError(ex, "Could not get user alias from server");
|
2024-10-05 17:09:17 -05:00
|
|
|
|
}
|
2024-08-07 01:14:13 -05:00
|
|
|
|
|
2025-08-18 03:02:49 -05:00
|
|
|
|
return String.IsNullOrWhiteSpace(_user.Alias) ? _user.UserName : _user.Alias;
|
2024-06-03 19:08:50 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-06-05 23:37:16 -05:00
|
|
|
|
public async Task<string> ChangeAliasAsync(string alias)
|
|
|
|
|
|
{
|
2025-08-18 03:02:49 -05:00
|
|
|
|
_logger?.LogTrace("Requesting to change player alias...");
|
2024-06-05 23:37:16 -05:00
|
|
|
|
|
2025-08-18 03:02:49 -05:00
|
|
|
|
if (_user == null)
|
|
|
|
|
|
_user = new User();
|
2024-10-05 17:09:17 -05:00
|
|
|
|
|
2025-08-18 03:02:49 -05:00
|
|
|
|
_user.Alias = alias;
|
2024-10-05 17:09:17 -05:00
|
|
|
|
|
2025-08-18 03:02:49 -05:00
|
|
|
|
var response = await _client.PutRequestAsync<string>("/api/Profile/ChangeAlias", new
|
2024-06-05 23:37:16 -05:00
|
|
|
|
{
|
|
|
|
|
|
Alias = alias
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
return response;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-29 23:11:52 -06:00
|
|
|
|
public async Task<byte[]> GetAvatarAsync()
|
|
|
|
|
|
{
|
2025-08-18 03:02:49 -05:00
|
|
|
|
_logger?.LogTrace("Requesting avatar contents...");
|
2025-02-28 18:13:34 -06:00
|
|
|
|
|
|
|
|
|
|
using (var ms = new MemoryStream())
|
|
|
|
|
|
{
|
2025-08-18 03:02:49 -05:00
|
|
|
|
var stream = _client.StreamRequest("/api/Profile/Avatar");
|
2025-02-28 18:13:34 -06:00
|
|
|
|
|
|
|
|
|
|
await stream.CopyToAsync(ms);
|
|
|
|
|
|
|
|
|
|
|
|
return ms.ToArray();
|
|
|
|
|
|
}
|
2025-01-29 23:11:52 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-06-03 19:08:50 -05:00
|
|
|
|
public async Task<string> DownloadAvatar()
|
|
|
|
|
|
{
|
2025-08-18 03:02:49 -05:00
|
|
|
|
_logger?.LogTrace("Retrieving player's avatar...");
|
2024-06-03 19:08:50 -05:00
|
|
|
|
|
|
|
|
|
|
var tempFile = Path.GetTempFileName();
|
|
|
|
|
|
|
2025-08-18 03:02:49 -05:00
|
|
|
|
return await _client.DownloadRequestAsync("/api/Profile/Avatar", tempFile);
|
2024-06-03 19:08:50 -05:00
|
|
|
|
}
|
2024-08-21 20:19:04 -05:00
|
|
|
|
|
|
|
|
|
|
public async Task<string> GetCustomField(string name)
|
|
|
|
|
|
{
|
2025-08-18 03:02:49 -05:00
|
|
|
|
_logger?.LogTrace("Getting player custom field with name {CustomFieldName}...", name);
|
2024-08-21 20:19:04 -05:00
|
|
|
|
|
2025-08-18 03:02:49 -05:00
|
|
|
|
return await _client.GetRequestAsync<string>($"/api/Profile/CustomField/{name}");
|
2024-08-21 20:19:04 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<string> UpdateCustomField(string name, string value)
|
|
|
|
|
|
{
|
2025-08-18 03:02:49 -05:00
|
|
|
|
_logger?.LogTrace("Updating player custom fields: {CustomFieldName} = {CustomFieldValue}", name, value);
|
2024-08-21 20:19:04 -05:00
|
|
|
|
|
2025-08-18 03:02:49 -05:00
|
|
|
|
return await _client.PutRequestAsync<string>($"/api/Profile/CustomField/{name}", value);
|
2024-08-21 20:19:04 -05:00
|
|
|
|
}
|
2024-01-02 02:34:58 -06:00
|
|
|
|
}
|
|
|
|
|
|
}
|