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 _locations = []; public LocationShareService(ILogger logger, LightlessMediator mediator, DalamudUtilService dalamudUtilService, ApiController apiController) : base(logger, mediator) { _dalamudUtilService = dalamudUtilService; _apiController = apiController; Mediator.Subscribe(this, (msg) => _locations.Clear()); Mediator.Subscribe(this, (msg) => { _ = _apiController.UpdateLocation(new LocationDto(new UserData(_apiController.UID, apiController.DisplayName), _dalamudUtilService.GetMapData())); _ = RequestAllLocation(); } ); Mediator.Subscribe(this, UpdateLocationList); Mediator.Subscribe(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; } } } }