78 lines
2.9 KiB
C#
78 lines
2.9 KiB
C#
using LightlessSync.API.Data;
|
|
using LightlessSync.API.Dto.CharaData;
|
|
using LightlessSync.API.Dto.User;
|
|
using LightlessSync.Services.Mediator;
|
|
using LightlessSync.WebAPI;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace LightlessSync.Services
|
|
{
|
|
public class LocationShareService : DisposableMediatorSubscriberBase
|
|
{
|
|
private readonly DalamudUtilService _dalamudUtilService;
|
|
private readonly ApiController _apiController;
|
|
private Dictionary<string, LocationInfo> _locations = [];
|
|
|
|
public LocationShareService(ILogger<LocationShareService> logger, LightlessMediator mediator, DalamudUtilService dalamudUtilService, ApiController apiController) : base(logger, mediator)
|
|
{
|
|
_dalamudUtilService = dalamudUtilService;
|
|
_apiController = apiController;
|
|
|
|
|
|
Mediator.Subscribe<DisconnectedMessage>(this, (msg) => _locations.Clear());
|
|
Mediator.Subscribe<ConnectedMessage>(this, (msg) =>
|
|
{
|
|
_ = _apiController.UpdateLocation(new LocationDto(new UserData(_apiController.UID, apiController.DisplayName), _dalamudUtilService.GetMapData()));
|
|
_ = RequestAllLocation();
|
|
} );
|
|
Mediator.Subscribe<LocationMessage>(this, UpdateLocationList);
|
|
Mediator.Subscribe<ZoneSwitchEndMessage>(this,
|
|
msg => _ = _apiController.UpdateLocation(new LocationDto(new UserData(_apiController.UID, _apiController.DisplayName), _dalamudUtilService.GetMapData())));
|
|
}
|
|
|
|
private void UpdateLocationList(LocationMessage msg)
|
|
{
|
|
if (_locations.ContainsKey(msg.Uid) && msg.LocationInfo.ServerId is 0)
|
|
{
|
|
_locations.Remove(msg.Uid);
|
|
return;
|
|
}
|
|
|
|
if ( msg.LocationInfo.ServerId is not 0 && !_locations.TryAdd(msg.Uid, msg.LocationInfo))
|
|
{
|
|
_locations[msg.Uid] = msg.LocationInfo;
|
|
}
|
|
}
|
|
|
|
private async Task RequestAllLocation()
|
|
{
|
|
try
|
|
{
|
|
var data = await _apiController.RequestAllLocationInfo().ConfigureAwait(false);
|
|
_locations = data.ToDictionary(x => x.user.UID, x => x.location, StringComparer.Ordinal);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Logger.LogError(e,"RequestAllLocation error : ");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public string GetUserLocation(string uid)
|
|
{
|
|
try
|
|
{
|
|
if (_locations.TryGetValue(uid, out var location))
|
|
{
|
|
return _dalamudUtilService.LocationToString(location);
|
|
}
|
|
return String.Empty;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Logger.LogError(e,"GetUserLocation error : ");
|
|
throw;
|
|
}
|
|
}
|
|
}
|
|
} |