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
@@ -100,6 +100,7 @@ var pending_move_r := 9999
var refresh_prompt_active := false var refresh_prompt_active := false
var refresh_request_active := false var refresh_request_active := false
var units_reload_queued := false var units_reload_queued := false
var visible_reload_queued := false
func _ready() -> void: func _ready() -> void:
@@ -1069,6 +1070,8 @@ func _on_home_request_completed(result: int, response_code: int, _headers: Packe
grid_view.set_home_location(q, r) grid_view.set_home_location(q, r)
coordinate_label.text = "Home territory assigned at q:%d r:%d | %s" % [q, r, terrain] coordinate_label.text = "Home territory assigned at q:%d r:%d | %s" % [q, r, terrain]
_load_visible_locations() _load_visible_locations()
_refresh_player_state()
resources_timer.start()
func _load_visible_locations() -> void: func _load_visible_locations() -> void:
@@ -1077,6 +1080,10 @@ func _load_visible_locations() -> void:
coordinate_label.text = "No auth token available. Visibility was not loaded." coordinate_label.text = "No auth token available. Visibility was not loaded."
return return
if visible_request.get_http_client_status() != HTTPClient.STATUS_DISCONNECTED:
visible_reload_queued = true
return
var error: int = visible_request.request( var error: int = visible_request.request(
LOCATIONS_BASE_URL + "/api/locations/visible", LOCATIONS_BASE_URL + "/api/locations/visible",
PackedStringArray([ PackedStringArray([
@@ -1091,16 +1098,19 @@ func _load_visible_locations() -> void:
func _on_visible_request_completed(result: int, response_code: int, _headers: PackedStringArray, body: PackedByteArray) -> void: func _on_visible_request_completed(result: int, response_code: int, _headers: PackedStringArray, body: PackedByteArray) -> void:
if result != HTTPRequest.RESULT_SUCCESS: if result != HTTPRequest.RESULT_SUCCESS:
coordinate_label.text = "Visible territory request failed." coordinate_label.text = "Visible territory request failed."
_run_queued_visible_reload()
return return
var text: String = body.get_string_from_utf8() var text: String = body.get_string_from_utf8()
if response_code < 200 or response_code >= 300: if response_code < 200 or response_code >= 300:
coordinate_label.text = "Visible territory failed: %s" % text coordinate_label.text = "Visible territory failed: %s" % text
_run_queued_visible_reload()
return return
var parsed = JSON.parse_string(text) var parsed = JSON.parse_string(text)
if typeof(parsed) != TYPE_ARRAY: if typeof(parsed) != TYPE_ARRAY:
coordinate_label.text = "Locations service returned invalid visibility data." coordinate_label.text = "Locations service returned invalid visibility data."
_run_queued_visible_reload()
return return
var username: String = str(session.get("username", "")) var username: String = str(session.get("username", ""))
@@ -1108,8 +1118,14 @@ func _on_visible_request_completed(result: int, response_code: int, _headers: Pa
grid_view.set_visible_locations(visible_locations, username) grid_view.set_visible_locations(visible_locations, username)
_sync_active_building_timer(visible_locations, username) _sync_active_building_timer(visible_locations, username)
coordinate_label.text = "Visible territory loaded. Sight extends 3 hexes beyond controlled land." coordinate_label.text = "Visible territory loaded. Sight extends 3 hexes beyond controlled land."
_refresh_player_state() _run_queued_visible_reload()
resources_timer.start()
func _run_queued_visible_reload() -> void:
if not visible_reload_queued:
return
visible_reload_queued = false
call_deferred("_load_visible_locations")
func _refresh_player_state() -> void: func _refresh_player_state() -> void:
@@ -646,9 +646,14 @@ public class LocationService
var destinationQ = worker.DestinationQ.Value; var destinationQ = worker.DestinationQ.Value;
var destinationR = worker.DestinationR.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) if (nextStep is null)
{ {
await _units.UpdateOneAsync( await _units.UpdateOneAsync(
@@ -660,14 +665,25 @@ public class LocationService
.Unset(x => x.MovementStartedAtUtc) .Unset(x => x.MovementStartedAtUtc)
.Unset(x => x.MovementCompletesAtUtc) .Unset(x => x.MovementCompletesAtUtc)
.Set(x => x.UpdatedAtUtc, now)); .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( await _units.UpdateOneAsync(
x => x.Id == worker.Id, x => x.Id == worker.Id,
Builders<UnitInstance>.Update Builders<UnitInstance>.Update
.Set(x => x.Q, nextStep.Value.q) .Set(x => x.Q, currentQ)
.Set(x => x.R, nextStep.Value.r) .Set(x => x.R, currentR)
.Set(x => x.MovementCompletesAtUtc, now.AddMinutes(1)) .Set(x => x.MovementCompletesAtUtc, now.AddMinutes(1))
.Set(x => x.UpdatedAtUtc, now)); .Set(x => x.UpdatedAtUtc, now));
continue; continue;
@@ -540,9 +540,14 @@ public class ResourceService
var destinationQ = worker.DestinationQ.Value; var destinationQ = worker.DestinationQ.Value;
var destinationR = worker.DestinationR.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) if (nextStep is null)
{ {
await _units.UpdateOneAsync( await _units.UpdateOneAsync(
@@ -554,14 +559,25 @@ public class ResourceService
.Unset(x => x.MovementStartedAtUtc) .Unset(x => x.MovementStartedAtUtc)
.Unset(x => x.MovementCompletesAtUtc) .Unset(x => x.MovementCompletesAtUtc)
.Set(x => x.UpdatedAtUtc, now)); .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( await _units.UpdateOneAsync(
x => x.Id == worker.Id, x => x.Id == worker.Id,
Builders<UnitInstance>.Update Builders<UnitInstance>.Update
.Set(x => x.Q, nextStep.Value.q) .Set(x => x.Q, currentQ)
.Set(x => x.R, nextStep.Value.r) .Set(x => x.R, currentR)
.Set(x => x.MovementCompletesAtUtc, now.AddMinutes(1)) .Set(x => x.MovementCompletesAtUtc, now.AddMinutes(1))
.Set(x => x.UpdatedAtUtc, now)); .Set(x => x.UpdatedAtUtc, now));
continue; continue;
@@ -325,9 +325,16 @@ public class UnitTrainingService
var destinationQ = unit.DestinationQ.Value; var destinationQ = unit.DestinationQ.Value;
var destinationR = unit.DestinationR.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 (unit.UnitType == PeasantUnitType)
{
var nextStep = await NextPeasantStepTowardAsync(username, currentQ, currentR, destinationQ, destinationR);
if (nextStep is null) if (nextStep is null)
{ {
await _units.UpdateOneAsync( await _units.UpdateOneAsync(
@@ -342,27 +349,33 @@ public class UnitTrainingService
.Unset(x => x.MovementStartedAtUtc) .Unset(x => x.MovementStartedAtUtc)
.Unset(x => x.MovementCompletesAtUtc) .Unset(x => x.MovementCompletesAtUtc)
.Set(x => x.UpdatedAtUtc, now)); .Set(x => x.UpdatedAtUtc, now));
continue; movementBlocked = true;
break;
} }
await _units.UpdateOneAsync( currentQ = nextStep.Value.q;
x => x.Id == unit.Id, currentR = nextStep.Value.r;
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;
} }
else
if (unit.UnitType != PeasantUnitType && HexDistance(unit.Q, unit.R, destinationQ, destinationR) > 1) {
var nextStep = NextStepToward(currentQ, currentR, destinationQ, destinationR);
currentQ = nextStep.q;
currentR = nextStep.r;
}
elapsedSteps--;
}
if (movementBlocked)
continue;
if (HexDistance(currentQ, currentR, destinationQ, destinationR) > 1)
{ {
var nextStep = NextStepToward(unit.Q, unit.R, destinationQ, destinationR);
await _units.UpdateOneAsync( await _units.UpdateOneAsync(
x => x.Id == unit.Id, x => x.Id == unit.Id,
Builders<UnitInstance>.Update Builders<UnitInstance>.Update
.Set(x => x.Q, nextStep.q) .Set(x => x.Q, currentQ)
.Set(x => x.R, nextStep.r) .Set(x => x.R, currentR)
.Set(x => x.MovementCompletesAtUtc, now.AddMinutes(1)) .Set(x => x.MovementCompletesAtUtc, now.AddMinutes(1))
.Set(x => x.UpdatedAtUtc, now)); .Set(x => x.UpdatedAtUtc, now));
continue; continue;