gather #9
@ -1,6 +1,7 @@
|
||||
extends Node3D
|
||||
|
||||
const CHARACTER_API_URL := "https://pchar.ranaze.com/api/Characters"
|
||||
const LOCATION_API_URL := "https://ploc.ranaze.com/api/Locations"
|
||||
|
||||
@export var tile_size := 16.0
|
||||
@export var block_height := 1.0
|
||||
@ -31,6 +32,7 @@ var _coord_sync_in_flight := false
|
||||
var _queued_coord_sync: Variant = null
|
||||
var _locations_refresh_in_flight := false
|
||||
var _queued_locations_refresh := false
|
||||
var _gather_in_flight := false
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
@ -69,6 +71,11 @@ func _process(_delta: float) -> void:
|
||||
_queue_locations_refresh()
|
||||
|
||||
|
||||
func _unhandled_input(event: InputEvent) -> void:
|
||||
if event.is_action_pressed("interact"):
|
||||
_try_gather_current_tile()
|
||||
|
||||
|
||||
func _get_stream_position() -> Vector3:
|
||||
if _tracked_node:
|
||||
return _tracked_node.global_position
|
||||
@ -104,7 +111,7 @@ func _rebuild_tiles(center: Vector2i) -> void:
|
||||
wanted_keys[coord] = true
|
||||
if _tile_nodes.has(coord):
|
||||
continue
|
||||
_spawn_tile(coord, String(_known_locations[coord]))
|
||||
_spawn_tile(coord, _get_location_name(coord))
|
||||
|
||||
for key in _tile_nodes.keys():
|
||||
if wanted_keys.has(key):
|
||||
@ -196,7 +203,11 @@ func _create_tile_label(location_name: String) -> Label3D:
|
||||
func _ensure_selected_location_exists(coord: Vector2i) -> void:
|
||||
if _known_locations.has(coord):
|
||||
return
|
||||
_known_locations[coord] = _selected_location_name(coord)
|
||||
_known_locations[coord] = {
|
||||
"id": "",
|
||||
"name": _selected_location_name(coord),
|
||||
"resources": []
|
||||
}
|
||||
|
||||
|
||||
func _selected_location_name(coord: Vector2i) -> String:
|
||||
@ -263,7 +274,11 @@ func _load_existing_locations() -> void:
|
||||
var location_name := String(location.get("name", "")).strip_edges()
|
||||
if location_name.is_empty():
|
||||
location_name = "Location %d,%d" % [coord.x, coord.y]
|
||||
_known_locations[coord] = location_name
|
||||
_known_locations[coord] = {
|
||||
"id": String(location.get("id", "")).strip_edges(),
|
||||
"name": location_name,
|
||||
"resources": _parse_location_resources(location.get("resources", []))
|
||||
}
|
||||
loaded_count += 1
|
||||
|
||||
print("LocationLevel loaded %d visible locations for character %s." % [loaded_count, _character_id])
|
||||
@ -330,3 +345,123 @@ func _sync_character_coord_async(coord: Vector2i) -> void:
|
||||
if _queued_coord_sync != null and _queued_coord_sync is Vector2i and _queued_coord_sync != _persisted_coord:
|
||||
var queued_coord: Vector2i = _queued_coord_sync
|
||||
_sync_character_coord(queued_coord)
|
||||
|
||||
|
||||
func _try_gather_current_tile() -> void:
|
||||
if _gather_in_flight:
|
||||
return
|
||||
var location_data := _get_location_data(_center_coord)
|
||||
if location_data.is_empty():
|
||||
push_warning("No location data available for current tile.")
|
||||
return
|
||||
var location_id := String(location_data.get("id", "")).strip_edges()
|
||||
if location_id.is_empty():
|
||||
push_warning("Current tile has no location id; cannot gather.")
|
||||
return
|
||||
var resources: Array = location_data.get("resources", [])
|
||||
if resources.is_empty():
|
||||
push_warning("No gatherable resources remain at this location.")
|
||||
return
|
||||
var resource: Dictionary = resources[0]
|
||||
var resource_key := String(resource.get("itemKey", "")).strip_edges()
|
||||
if resource_key.is_empty():
|
||||
push_warning("Current location resource is missing an itemKey.")
|
||||
return
|
||||
_gather_current_tile(location_id, resource_key)
|
||||
|
||||
|
||||
func _gather_current_tile(location_id: String, resource_key: String) -> void:
|
||||
_gather_in_flight = true
|
||||
_gather_current_tile_async(location_id, resource_key)
|
||||
|
||||
|
||||
func _gather_current_tile_async(location_id: String, resource_key: String) -> void:
|
||||
var request := HTTPRequest.new()
|
||||
add_child(request)
|
||||
|
||||
var headers := PackedStringArray()
|
||||
if not AuthState.access_token.is_empty():
|
||||
headers.append("Authorization: Bearer %s" % AuthState.access_token)
|
||||
headers.append("Content-Type: application/json")
|
||||
|
||||
var body := JSON.stringify({
|
||||
"characterId": _character_id,
|
||||
"resourceKey": resource_key
|
||||
})
|
||||
|
||||
var err := request.request("%s/%s/gather" % [LOCATION_API_URL, location_id], headers, HTTPClient.METHOD_POST, body)
|
||||
if err != OK:
|
||||
push_warning("Failed to request gather action: %s" % err)
|
||||
request.queue_free()
|
||||
_gather_in_flight = false
|
||||
return
|
||||
|
||||
var result: Array = await request.request_completed
|
||||
request.queue_free()
|
||||
|
||||
var result_code: int = result[0]
|
||||
var response_code: int = result[1]
|
||||
var response_body: String = result[3].get_string_from_utf8()
|
||||
if result_code != HTTPRequest.RESULT_SUCCESS or response_code < 200 or response_code >= 300:
|
||||
push_warning("Failed to gather resource (%s/%s): %s" % [result_code, response_code, response_body])
|
||||
_gather_in_flight = false
|
||||
return
|
||||
|
||||
var parsed: Variant = JSON.parse_string(response_body)
|
||||
if typeof(parsed) != TYPE_DICTIONARY:
|
||||
push_warning("Gather response was not a dictionary.")
|
||||
_gather_in_flight = false
|
||||
return
|
||||
|
||||
var gather_response := parsed as Dictionary
|
||||
var remaining_quantity := int(gather_response.get("remainingQuantity", 0))
|
||||
var quantity_granted := int(gather_response.get("quantityGranted", 0))
|
||||
_update_location_resource(_center_coord, resource_key, remaining_quantity)
|
||||
print("Gathered %s x%s at %s." % [resource_key, quantity_granted, _center_coord])
|
||||
_gather_in_flight = false
|
||||
|
||||
|
||||
func _get_location_data(coord: Vector2i) -> Dictionary:
|
||||
var value: Variant = _known_locations.get(coord, {})
|
||||
if typeof(value) == TYPE_DICTIONARY:
|
||||
return value as Dictionary
|
||||
return {}
|
||||
|
||||
|
||||
func _get_location_name(coord: Vector2i) -> String:
|
||||
var location_data := _get_location_data(coord)
|
||||
return String(location_data.get("name", "Location %d,%d" % [coord.x, coord.y]))
|
||||
|
||||
|
||||
func _parse_location_resources(resources_value: Variant) -> Array:
|
||||
var results: Array = []
|
||||
if typeof(resources_value) != TYPE_ARRAY:
|
||||
return results
|
||||
for entry in resources_value:
|
||||
if typeof(entry) != TYPE_DICTIONARY:
|
||||
continue
|
||||
var resource := entry as Dictionary
|
||||
results.append({
|
||||
"itemKey": String(resource.get("itemKey", "")).strip_edges(),
|
||||
"remainingQuantity": int(resource.get("remainingQuantity", 0)),
|
||||
"gatherQuantity": int(resource.get("gatherQuantity", 1))
|
||||
})
|
||||
return results
|
||||
|
||||
|
||||
func _update_location_resource(coord: Vector2i, resource_key: String, remaining_quantity: int) -> void:
|
||||
var location_data := _get_location_data(coord)
|
||||
if location_data.is_empty():
|
||||
return
|
||||
var resources: Array = location_data.get("resources", [])
|
||||
var updated_resources: Array = []
|
||||
for entry in resources:
|
||||
if typeof(entry) != TYPE_DICTIONARY:
|
||||
continue
|
||||
var resource := (entry as Dictionary).duplicate()
|
||||
if String(resource.get("itemKey", "")).strip_edges() == resource_key:
|
||||
resource["remainingQuantity"] = remaining_quantity
|
||||
if int(resource.get("remainingQuantity", 0)) > 0:
|
||||
updated_resources.append(resource)
|
||||
location_data["resources"] = updated_resources
|
||||
_known_locations[coord] = location_data
|
||||
|
||||
@ -46,7 +46,14 @@ Outbound JSON documents
|
||||
"coord": {
|
||||
"x": "number",
|
||||
"y": "number"
|
||||
}
|
||||
},
|
||||
"resources": [
|
||||
{
|
||||
"itemKey": "wood",
|
||||
"remainingQuantity": 100,
|
||||
"gatherQuantity": 3
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
```
|
||||
|
||||
@ -15,4 +15,7 @@ public class VisibleLocation
|
||||
|
||||
[BsonElement("coord")]
|
||||
public LocationCoord Coord { get; set; } = new();
|
||||
|
||||
[BsonElement("resources")]
|
||||
public List<VisibleLocationResource> Resources { get; set; } = [];
|
||||
}
|
||||
|
||||
@ -140,10 +140,34 @@ public class CharacterStore
|
||||
{
|
||||
X = coord.GetValue("x", 0).ToInt32(),
|
||||
Y = coord.GetValue("y", 0).ToInt32()
|
||||
}
|
||||
},
|
||||
Resources = MapVisibleLocationResources(document)
|
||||
};
|
||||
}
|
||||
|
||||
private static List<VisibleLocationResource> MapVisibleLocationResources(BsonDocument document)
|
||||
{
|
||||
if (!document.TryGetValue("resources", out var resourcesValue) || resourcesValue.BsonType != BsonType.Array)
|
||||
return [];
|
||||
|
||||
var results = new List<VisibleLocationResource>();
|
||||
foreach (var value in resourcesValue.AsBsonArray)
|
||||
{
|
||||
if (value.BsonType != BsonType.Document)
|
||||
continue;
|
||||
|
||||
var resource = value.AsBsonDocument;
|
||||
results.Add(new VisibleLocationResource
|
||||
{
|
||||
ItemKey = resource.GetValue("itemKey", "").AsString,
|
||||
RemainingQuantity = resource.GetValue("remainingQuantity", 0).ToInt32(),
|
||||
GatherQuantity = resource.GetValue("gatherQuantity", 1).ToInt32()
|
||||
});
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
private void EnsureLocationCoordIndexes()
|
||||
{
|
||||
var indexes = _locations.Indexes.List().ToList();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user