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

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