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;
@@ -540,9 +540,14 @@ public class ResourceService
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(
@@ -554,14 +559,25 @@ public class ResourceService
.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;
@@ -325,44 +325,57 @@ public class UnitTrainingService
var destinationQ = unit.DestinationQ.Value;
var destinationR = unit.DestinationR.Value;
if (unit.UnitType == PeasantUnitType && HexDistance(unit.Q, unit.R, destinationQ, destinationR) > 1)
var currentQ = unit.Q;
var currentR = unit.R;
var elapsedSteps = Math.Max(1, (int)Math.Floor((now - unit.MovementCompletesAtUtc!.Value).TotalMinutes) + 1);
var movementBlocked = false;
while (elapsedSteps > 0 && HexDistance(currentQ, currentR, destinationQ, destinationR) > 1)
{
var nextStep = await NextPeasantStepTowardAsync(username, unit.Q, unit.R, destinationQ, destinationR);
if (nextStep is null)
if (unit.UnitType == PeasantUnitType)
{
await _units.UpdateOneAsync(
x => x.Id == unit.Id,
Builders<UnitInstance>.Update
.Set(x => x.Status, "Idle")
.Set(x => x.MovementPerTick, movementPerTick)
.Set(x => x.MovementRemaining, movementPerTick)
.Unset(x => x.DestinationQ)
.Unset(x => x.DestinationR)
.Set(x => x.ClaimOnArrival, false)
.Unset(x => x.MovementStartedAtUtc)
.Unset(x => x.MovementCompletesAtUtc)
.Set(x => x.UpdatedAtUtc, now));
continue;
var nextStep = await NextPeasantStepTowardAsync(username, currentQ, currentR, destinationQ, destinationR);
if (nextStep is null)
{
await _units.UpdateOneAsync(
x => x.Id == unit.Id,
Builders<UnitInstance>.Update
.Set(x => x.Status, "Idle")
.Set(x => x.MovementPerTick, movementPerTick)
.Set(x => x.MovementRemaining, movementPerTick)
.Unset(x => x.DestinationQ)
.Unset(x => x.DestinationR)
.Set(x => x.ClaimOnArrival, false)
.Unset(x => x.MovementStartedAtUtc)
.Unset(x => x.MovementCompletesAtUtc)
.Set(x => x.UpdatedAtUtc, now));
movementBlocked = true;
break;
}
currentQ = nextStep.Value.q;
currentR = nextStep.Value.r;
}
else
{
var nextStep = NextStepToward(currentQ, currentR, destinationQ, destinationR);
currentQ = nextStep.q;
currentR = nextStep.r;
}
await _units.UpdateOneAsync(
x => x.Id == unit.Id,
Builders<UnitInstance>.Update
.Set(x => x.Q, nextStep.Value.q)
.Set(x => x.R, nextStep.Value.r)
.Set(x => x.MovementCompletesAtUtc, now.AddMinutes(1))
.Set(x => x.UpdatedAtUtc, now));
continue;
elapsedSteps--;
}
if (unit.UnitType != PeasantUnitType && HexDistance(unit.Q, unit.R, destinationQ, destinationR) > 1)
if (movementBlocked)
continue;
if (HexDistance(currentQ, currentR, destinationQ, destinationR) > 1)
{
var nextStep = NextStepToward(unit.Q, unit.R, destinationQ, destinationR);
await _units.UpdateOneAsync(
x => x.Id == unit.Id,
Builders<UnitInstance>.Update
.Set(x => x.Q, nextStep.q)
.Set(x => x.R, nextStep.r)
.Set(x => x.Q, currentQ)
.Set(x => x.R, currentR)
.Set(x => x.MovementCompletesAtUtc, now.AddMinutes(1))
.Set(x => x.UpdatedAtUtc, now));
continue;