PalacePal/Pal.Client/Net/RemoteApi.PalaceService.cs

87 lines
3.6 KiB
C#
Raw Normal View History

2023-03-26 13:47:18 +00:00
using System;
2022-12-21 19:23:48 +00:00
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Threading;
using System.Threading.Tasks;
2023-02-22 19:29:58 +00:00
using Pal.Client.Database;
2023-02-18 20:12:36 +00:00
using Pal.Client.Floors;
2023-03-26 13:47:18 +00:00
using Palace;
2022-12-21 19:23:48 +00:00
2023-03-30 20:01:43 +00:00
namespace Pal.Client.Net;
internal partial class RemoteApi
2022-12-21 19:23:48 +00:00
{
2023-03-30 20:01:43 +00:00
public async Task<(bool, List<PersistentLocation>)> DownloadRemoteMarkers(ushort territoryId, CancellationToken cancellationToken = default)
2022-12-21 19:23:48 +00:00
{
2023-03-30 20:01:43 +00:00
if (!await Connect(cancellationToken))
return (false, new());
2022-12-21 19:23:48 +00:00
2023-03-30 20:01:43 +00:00
var palaceClient = new PalaceService.PalaceServiceClient(_channel);
var downloadReply = await palaceClient.DownloadFloorsAsync(new DownloadFloorsRequest { TerritoryType = territoryId }, headers: AuthorizedHeaders(), cancellationToken: cancellationToken);
return (downloadReply.Success, downloadReply.Objects.Select(CreateLocationFromNetworkObject).ToList());
}
2022-12-21 19:23:48 +00:00
2023-03-30 20:01:43 +00:00
public async Task<(bool, List<PersistentLocation>)> UploadLocations(ushort territoryType, IReadOnlyList<PersistentLocation> locations, CancellationToken cancellationToken = default)
{
if (locations.Count == 0)
return (true, new());
2022-12-21 19:23:48 +00:00
2023-03-30 20:01:43 +00:00
if (!await Connect(cancellationToken))
return (false, new());
2022-12-21 19:23:48 +00:00
2023-03-30 20:01:43 +00:00
var palaceClient = new PalaceService.PalaceServiceClient(_channel);
var uploadRequest = new UploadFloorsRequest
2022-12-21 19:23:48 +00:00
{
2023-03-30 20:01:43 +00:00
TerritoryType = territoryType,
};
uploadRequest.Objects.AddRange(locations.Select(m => new PalaceObject
{
Type = m.Type.ToObjectType(),
X = m.Position.X,
Y = m.Position.Y,
Z = m.Position.Z
}));
var uploadReply = await palaceClient.UploadFloorsAsync(uploadRequest, headers: AuthorizedHeaders(), cancellationToken: cancellationToken);
return (uploadReply.Success, uploadReply.Objects.Select(CreateLocationFromNetworkObject).ToList());
}
2022-12-21 19:23:48 +00:00
2023-03-30 20:01:43 +00:00
public async Task<bool> MarkAsSeen(ushort territoryType, IReadOnlyList<PersistentLocation> locations, CancellationToken cancellationToken = default)
{
if (locations.Count == 0)
return true;
2022-12-21 19:23:48 +00:00
2023-03-30 20:01:43 +00:00
if (!await Connect(cancellationToken))
return false;
2022-12-21 19:23:48 +00:00
2023-03-30 20:01:43 +00:00
var palaceClient = new PalaceService.PalaceServiceClient(_channel);
var seenRequest = new MarkObjectsSeenRequest { TerritoryType = territoryType };
foreach (var marker in locations)
seenRequest.NetworkIds.Add(marker.NetworkId.ToString());
2022-12-21 19:23:48 +00:00
2023-03-30 20:01:43 +00:00
var seenReply = await palaceClient.MarkObjectsSeenAsync(seenRequest, headers: AuthorizedHeaders(), deadline: DateTime.UtcNow.AddSeconds(10), cancellationToken: cancellationToken);
return seenReply.Success;
}
2022-12-21 19:23:48 +00:00
2023-03-30 20:01:43 +00:00
private PersistentLocation CreateLocationFromNetworkObject(PalaceObject obj)
{
return new PersistentLocation
2022-12-21 19:23:48 +00:00
{
2023-03-30 20:01:43 +00:00
Type = obj.Type.ToMemoryType(),
Position = new Vector3(obj.X, obj.Y, obj.Z),
NetworkId = Guid.Parse(obj.NetworkId),
Source = ClientLocation.ESource.Download,
};
}
public async Task<(bool, List<FloorStatistics>)> FetchStatistics(CancellationToken cancellationToken = default)
{
if (!await Connect(cancellationToken))
return new(false, new List<FloorStatistics>());
2022-12-21 19:23:48 +00:00
2023-03-30 20:01:43 +00:00
var palaceClient = new PalaceService.PalaceServiceClient(_channel);
var statisticsReply = await palaceClient.FetchStatisticsAsync(new StatisticsRequest(), headers: AuthorizedHeaders(), deadline: DateTime.UtcNow.AddSeconds(30), cancellationToken: cancellationToken);
return (statisticsReply.Success, statisticsReply.FloorStatistics.ToList());
2022-12-21 19:23:48 +00:00
}
}