Tracking character movement
This commit is contained in:
@@ -21,14 +21,44 @@ public class CharacterStore
|
||||
|
||||
public Task CreateAsync(Character character) => _col.InsertOneAsync(character);
|
||||
|
||||
public Task<List<Character>> GetForOwnerAsync(string ownerUserId) =>
|
||||
_col.Find(c => c.OwnerUserId == ownerUserId).ToListAsync();
|
||||
|
||||
public async Task<bool> DeleteForOwnerAsync(string id, string ownerUserId, bool allowAnyOwner)
|
||||
{
|
||||
var filter = Builders<Character>.Filter.Eq(c => c.Id, id);
|
||||
if (!allowAnyOwner)
|
||||
{
|
||||
public Task<List<Character>> GetForOwnerAsync(string ownerUserId) =>
|
||||
_col.Find(c => c.OwnerUserId == ownerUserId).ToListAsync();
|
||||
|
||||
public async Task<Character?> GetForOwnerByIdAsync(string id, string ownerUserId, bool allowAnyOwner)
|
||||
{
|
||||
var filter = Builders<Character>.Filter.Eq(c => c.Id, id);
|
||||
if (!allowAnyOwner)
|
||||
{
|
||||
filter = Builders<Character>.Filter.And(
|
||||
filter,
|
||||
Builders<Character>.Filter.Eq(c => c.OwnerUserId, ownerUserId)
|
||||
);
|
||||
}
|
||||
|
||||
return await _col.Find(filter).FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
public async Task<bool> UpdateCoordAsync(string id, string ownerUserId, bool allowAnyOwner, Coord coord)
|
||||
{
|
||||
var filter = Builders<Character>.Filter.Eq(c => c.Id, id);
|
||||
if (!allowAnyOwner)
|
||||
{
|
||||
filter = Builders<Character>.Filter.And(
|
||||
filter,
|
||||
Builders<Character>.Filter.Eq(c => c.OwnerUserId, ownerUserId)
|
||||
);
|
||||
}
|
||||
|
||||
var update = Builders<Character>.Update.Set(c => c.Coord, coord);
|
||||
var result = await _col.UpdateOneAsync(filter, update);
|
||||
return result.ModifiedCount > 0;
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteForOwnerAsync(string id, string ownerUserId, bool allowAnyOwner)
|
||||
{
|
||||
var filter = Builders<Character>.Filter.Eq(c => c.Id, id);
|
||||
if (!allowAnyOwner)
|
||||
{
|
||||
filter = Builders<Character>.Filter.And(
|
||||
filter,
|
||||
Builders<Character>.Filter.Eq(c => c.OwnerUserId, ownerUserId)
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
using CharacterApi.Models;
|
||||
using System.Net;
|
||||
using System.Net.Http.Json;
|
||||
|
||||
namespace CharacterApi.Services;
|
||||
|
||||
public class LocationsClient
|
||||
{
|
||||
private readonly HttpClient _http;
|
||||
private readonly string _internalKey;
|
||||
|
||||
public LocationsClient(HttpClient http, IConfiguration cfg)
|
||||
{
|
||||
_http = http;
|
||||
_internalKey = cfg["LocationsApi:InternalKey"] ?? string.Empty;
|
||||
}
|
||||
|
||||
public async Task<(bool Ok, HttpStatusCode Status, string? Body)> UpdatePresenceAsync(
|
||||
string characterId,
|
||||
Coord coord,
|
||||
CancellationToken ct = default)
|
||||
{
|
||||
using var request = new HttpRequestMessage(HttpMethod.Post, "api/locations/presence")
|
||||
{
|
||||
Content = JsonContent.Create(new { characterId, coord })
|
||||
};
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(_internalKey))
|
||||
request.Headers.TryAddWithoutValidation("X-Internal-Key", _internalKey);
|
||||
|
||||
using var response = await _http.SendAsync(request, ct);
|
||||
var body = await response.Content.ReadAsStringAsync(ct);
|
||||
return (response.IsSuccessStatusCode, response.StatusCode, body);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user