fixing offline updates

This commit is contained in:
hz
2026-05-21 18:34:56 -05:00
parent 5530a185a0
commit a2fd9a84e3
4 changed files with 101 additions and 40 deletions
@@ -646,9 +646,14 @@ public class LocationService
var destinationQ = worker.DestinationQ.Value;
var destinationR = worker.DestinationR.Value;
if (HexDistance(worker.Q, worker.R, destinationQ, destinationR) > 1)
var currentQ = worker.Q;
var currentR = worker.R;
var elapsedSteps = Math.Max(1, (int)Math.Floor((now - worker.MovementCompletesAtUtc!.Value).TotalMinutes) + 1);
var movementBlocked = false;
while (elapsedSteps > 0 && HexDistance(currentQ, currentR, destinationQ, destinationR) > 1)
{
var nextStep = await NextPeasantStepTowardAsync(username, worker.Q, worker.R, destinationQ, destinationR);
var nextStep = await NextPeasantStepTowardAsync(username, currentQ, currentR, destinationQ, destinationR);
if (nextStep is null)
{
await _units.UpdateOneAsync(
@@ -660,14 +665,25 @@ public class LocationService
.Unset(x => x.MovementStartedAtUtc)
.Unset(x => x.MovementCompletesAtUtc)
.Set(x => x.UpdatedAtUtc, now));
continue;
movementBlocked = true;
break;
}
currentQ = nextStep.Value.q;
currentR = nextStep.Value.r;
elapsedSteps--;
}
if (movementBlocked)
continue;
if (HexDistance(currentQ, currentR, destinationQ, destinationR) > 1)
{
await _units.UpdateOneAsync(
x => x.Id == worker.Id,
Builders<UnitInstance>.Update
.Set(x => x.Q, nextStep.Value.q)
.Set(x => x.R, nextStep.Value.r)
.Set(x => x.Q, currentQ)
.Set(x => x.R, currentR)
.Set(x => x.MovementCompletesAtUtc, now.AddMinutes(1))
.Set(x => x.UpdatedAtUtc, now));
continue;