43 lines
1.4 KiB
C#
43 lines
1.4 KiB
C#
// Server.World/Presence.cs
|
|
using StackExchange.Redis;
|
|
|
|
namespace Server.World;
|
|
|
|
public interface IPresenceStore
|
|
{
|
|
Task SetAsync(ulong charId, Pos p, CancellationToken ct);
|
|
Task<Pos?> GetAsync(ulong charId, CancellationToken ct);
|
|
}
|
|
|
|
public sealed class RedisPresenceStore : IPresenceStore
|
|
{
|
|
private readonly IDatabase _db;
|
|
public RedisPresenceStore(IDatabase db) => _db = db;
|
|
private static RedisKey PKey(ulong cid) => new($"p:{cid}:pos");
|
|
|
|
public async Task SetAsync(ulong charId, Pos p, CancellationToken ct)
|
|
{
|
|
var entries = new HashEntry[]
|
|
{
|
|
new("zone", (long)p.ZoneId),
|
|
new("x", p.X), new("y", p.Y), new("z", p.Z),
|
|
new("facingRad", p.FacingRad),
|
|
new("ts", DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()),
|
|
};
|
|
await _db.HashSetAsync(PKey(charId), entries);
|
|
}
|
|
|
|
public async Task<Pos?> GetAsync(ulong charId, CancellationToken ct)
|
|
{
|
|
var vals = await _db.HashGetAllAsync(PKey(charId));
|
|
if (vals.Length == 0) return null;
|
|
|
|
ulong zone = (ulong)(long)vals.First(v => v.Name == "zone").Value;
|
|
float x = (float)vals.First(v => v.Name == "x").Value;
|
|
float y = (float)vals.First(v => v.Name == "y").Value;
|
|
float z = (float)vals.First(v => v.Name == "z").Value;
|
|
float f = (float)vals.First(v => v.Name == "facingRad").Value;
|
|
return new Pos(zone, x, y, z, f);
|
|
}
|
|
}
|