PalacePal/Pal.Client/DependencyInjection/StatisticsService.cs

75 lines
2.2 KiB
C#
Raw Normal View History

2023-02-15 22:17:19 +00:00
using System;
using System.Threading.Tasks;
using Dalamud.Game.Gui;
using Grpc.Core;
using Microsoft.Extensions.Logging;
2023-02-15 22:17:19 +00:00
using Pal.Client.Configuration;
using Pal.Client.Extensions;
using Pal.Client.Net;
using Pal.Client.Properties;
using Pal.Client.Windows;
2023-03-30 20:01:43 +00:00
namespace Pal.Client.DependencyInjection;
internal sealed class StatisticsService
2023-02-15 22:17:19 +00:00
{
2023-03-30 20:01:43 +00:00
private readonly IPalacePalConfiguration _configuration;
private readonly ILogger<StatisticsService> _logger;
private readonly RemoteApi _remoteApi;
private readonly StatisticsWindow _statisticsWindow;
private readonly Chat _chat;
2023-02-15 22:17:19 +00:00
2023-03-30 20:01:43 +00:00
public StatisticsService(
IPalacePalConfiguration configuration,
ILogger<StatisticsService> logger,
RemoteApi remoteApi,
StatisticsWindow statisticsWindow,
Chat chat)
{
_configuration = configuration;
_logger = logger;
_remoteApi = remoteApi;
_statisticsWindow = statisticsWindow;
_chat = chat;
}
2023-02-15 22:17:19 +00:00
2023-03-30 20:01:43 +00:00
public void ShowGlobalStatistics()
{
Task.Run(async () => await FetchFloorStatistics());
}
2023-02-15 22:17:19 +00:00
2023-03-30 20:01:43 +00:00
private async Task FetchFloorStatistics()
{
try
2023-02-15 22:17:19 +00:00
{
2023-03-30 20:01:43 +00:00
if (!_configuration.HasRoleOnCurrentServer(RemoteApi.RemoteUrl, "statistics:view"))
2023-02-15 22:17:19 +00:00
{
2023-03-30 20:01:43 +00:00
_chat.Error(Localization.Command_pal_stats_CurrentFloor);
return;
2023-02-15 22:17:19 +00:00
}
2023-03-30 20:01:43 +00:00
var (success, floorStatistics) = await _remoteApi.FetchStatistics();
if (success)
2023-02-15 22:17:19 +00:00
{
2023-03-30 20:01:43 +00:00
_statisticsWindow.SetFloorData(floorStatistics);
_statisticsWindow.IsOpen = true;
2023-02-15 22:17:19 +00:00
}
2023-03-30 20:01:43 +00:00
else
2023-02-15 22:17:19 +00:00
{
2023-03-30 20:01:43 +00:00
_chat.Error(Localization.Command_pal_stats_UnableToFetchStatistics);
2023-02-15 22:17:19 +00:00
}
}
2023-03-30 20:01:43 +00:00
catch (RpcException e) when (e.StatusCode == StatusCode.PermissionDenied)
{
_logger.LogWarning(e, "Access denied while fetching floor statistics");
_chat.Error(Localization.Command_pal_stats_CurrentFloor);
}
catch (Exception e)
{
_logger.LogError(e, "Could not fetch floor statistics");
_chat.Error(string.Format(Localization.Error_CommandFailed,
$"{e.GetType()} - {e.Message}"));
}
2023-02-15 22:17:19 +00:00
}
}