完善客户端功能并同步差距文档

This commit is contained in:
shenlei
2026-09-03 18:40:01 +09:00
parent f93a172296
commit 13ccb8d02d
135 changed files with 21881 additions and 1259 deletions
+191 -6
View File
@@ -9,8 +9,11 @@
# inventory_ui 在交易开着时右键道具 → 调 xu.offer(win, cell)。
extends Node
const ItemTooltip = preload("res://ui/item_tooltip.gd")
var client: Node
var proto: Node
var _tooltip_builder: RefCounted
var _root: Control
var _self_box: VBoxContainer
var _peer_box: VBoxContainer
@@ -21,10 +24,24 @@ var _accept_btn: Button
var _gold_input: LineEdit
var _next_display := 0
var _offered_cells := {}
var _self_grid: GridContainer
var _exchange_cells: Dictionary = {}
var item_mouse: Node
const EXCHANGE_SLOT_COUNT := 12
const INVENTORY_WINDOW := 1
const EXCHANGE_LIMIT_RANGE := 1000.0
const ITEM_ANTIFLAG_GIVE := 1 << 13
var _accept_pending := false
var _anchor := Vector2.ZERO
var _anchor_valid := false
func setup(m2client: Node, parent: Node, proto_node: Node = null) -> void:
client = m2client
proto = proto_node
_tooltip_builder = ItemTooltip.new()
_tooltip_builder.setup(AssetRoot.path(), "en", proto)
_build(parent)
if client.has_signal("exchange_changed"):
client.exchange_changed.connect(refresh)
@@ -33,10 +50,16 @@ func is_open() -> bool:
return _root != null and _root.visible
func close() -> void:
if item_mouse and item_mouse.has_method("unregister_owner"):
item_mouse.unregister_owner(self)
if item_mouse.has_method("is_attached") and item_mouse.is_attached():
item_mouse.cancel()
if _root:
_root.visible = false
_next_display = 0
_offered_cells.clear()
_accept_pending = false
_anchor_valid = false
if _gold_input:
_gold_input.text = ""
@@ -47,6 +70,8 @@ func offer(inv_window: int, inv_cell: int) -> void:
if _offered_cells.has(key):
_status.text = "该物品已经放入交易"
return
if not _can_give(_item_at(inv_window, inv_cell)):
return
var x: Dictionary = client.get_exchange()
var used := {}
for item in x.get("self_items", []):
@@ -73,6 +98,13 @@ func _name_of(vnum: int) -> String:
return n
return "#%d" % vnum
func _tooltip_for(item: Dictionary) -> String:
var vnum := int(item.get("vnum", 0))
if _tooltip_builder:
var pd: Dictionary = proto.item(vnum) if proto and proto.has_method("item") else {}
return _tooltip_builder.format(vnum, int(item.get("count", 1)), pd, item)
return "%s\n#%d ×%d" % [_name_of(vnum), vnum, int(item.get("count", 1))]
func refresh() -> void:
if client == null:
return
@@ -81,45 +113,183 @@ func refresh() -> void:
_root.visible = false
_next_display = 0
_offered_cells.clear()
_accept_pending = false
_anchor_valid = false
_gold_input.text = ""
_status.text = ""
return
_root.visible = true
_capture_anchor()
_render_self_grid(x.get("self_items", []))
_fill(_self_box, x.get("self_items", []))
_fill(_peer_box, x.get("peer_items", []))
_self_gold.text = "金币: %d" % int(x.get("self_gold", 0))
_peer_gold.text = "金币: %d" % int(x.get("peer_gold", 0))
var me: bool = x.get("self_accept", false)
var peer: bool = x.get("peer_accept", false)
var me := bool(x.get("self_accept", false))
var peer := bool(x.get("peer_accept", false))
# The server clears self_accept when either side changes the offer. Do not
# invent a local transaction state: mirror the authoritative bit exactly.
_accept_pending = me
_accept_btn.text = "已接受 ✓" if me else "接受"
_accept_btn.disabled = me
_accept_btn.modulate = Color(0.5, 1, 0.5) if me else Color(1, 1, 1)
_root.get_node("PeerAccept").text = "对方: 已接受" if peer else "对方: 未接受"
_status.text = ""
func _item_at(inv_window: int, inv_cell: int) -> Dictionary:
if client == null:
return {}
if client.has_method("get_item"):
var direct: Dictionary = client.get_item(inv_window, inv_cell)
if not direct.is_empty():
return direct
if inv_window == INVENTORY_WINDOW and client.has_method("get_inventory"):
for item in client.get_inventory():
if int(item.get("cell", -1)) == inv_cell:
return item
if inv_window == 2 and client.has_method("get_equipment"):
var equipment: Array = client.get_equipment()
if inv_cell >= 0 and inv_cell < equipment.size():
return equipment[inv_cell]
return {}
func _can_give(item_data: Dictionary) -> bool:
var vnum := int(item_data.get("vnum", 0))
var proto_data: Dictionary = proto.item(vnum) if proto and proto.has_method("item") else {}
var flags := int(item_data.get("anti_flags", 0)) | int(proto_data.get("anti_flags", 0))
if (flags & ITEM_ANTIFLAG_GIVE) != 0:
_status.text = "该物品不可交易"
return false
return true
func _render_self_grid(rows: Array) -> void:
if _self_grid == null:
return
for pos in _exchange_cells:
var cell: Button = _exchange_cells[pos]
cell.text = ""
cell.tooltip_text = ""
cell.set_meta("vnum", 0)
cell.set_meta("count", 0)
for row in rows:
var pos := int(row.get("slot", -1))
if not _exchange_cells.has(pos):
continue
var vnum := int(row.get("vnum", 0))
var count := int(row.get("count", 1))
var cell: Button = _exchange_cells[pos]
cell.text = "%s%s" % [_name_of(vnum).substr(0, 6), (" ×%d" % count) if count > 1 else ""]
cell.tooltip_text = _tooltip_for(row)
cell.set_meta("vnum", vnum)
cell.set_meta("count", count)
func _drop_to_slot(payload: Dictionary, display_pos: int) -> bool:
if payload.is_empty() or display_pos < 0 or display_pos >= EXCHANGE_SLOT_COUNT:
return false
if bool(payload.get("money", false)):
if int(client.get_exchange().get("self_gold", 0)) > 0:
_status.text = "金币已经放入,不能修改"
return false
var amount := int(payload.get("count", 0))
if amount <= 0 or not client.has_method("exchange_add_gold"):
return false
return client.exchange_add_gold(amount)
var source_window := int(payload.get("window", -1))
var source_cell := int(payload.get("cell", -1))
if source_window not in [INVENTORY_WINDOW, 2] or source_cell < 0:
return false
var item_data := payload.duplicate(true)
var source_data := _item_at(source_window, source_cell)
for key in source_data:
if not item_data.has(key):
item_data[key] = source_data[key]
if not _can_give(item_data):
return false
for row in client.get_exchange().get("self_items", []):
if int(row.get("slot", -1)) == display_pos:
return false
var key := "%d:%d" % [source_window, source_cell]
if _offered_cells.has(key):
return false
if not client.has_method("exchange_add_item") or not client.exchange_add_item(
source_window, source_cell, display_pos):
_status.text = "交易请求发送失败"
return false
_offered_cells[key] = true
return true
func _fill(box: VBoxContainer, rows: Array) -> void:
for c in box.get_children():
c.queue_free()
for r in rows:
var l := Label.new()
l.text = "%s ×%d" % [_name_of(int(r.get("vnum", 0))), int(r.get("count", 1))]
l.tooltip_text = _tooltip_for(r)
l.add_theme_font_size_override("font_size", 12)
box.add_child(l)
func _on_put_gold() -> void:
var current := int(client.get_exchange().get("self_gold", 0))
if current > 0:
_status.text = "金币已经放入,不能修改"
return
var raw := _gold_input.text.strip_edges()
if raw == "" or not raw.is_valid_int():
_status.text = "请输入有效金币数量"
return
var g := int(raw)
if g <= 0 or g > 2000000000:
_status.text = "金币数量必须在 12000000000 之间"
if g <= 0 or g > 9999999:
_status.text = "金币数量必须在 19999999 之间"
return
if client.exchange_add_gold(g):
if client.has_method("exchange_add_gold") and client.exchange_add_gold(g):
_gold_input.text = ""
_status.text = ""
else:
_status.text = "金币请求发送失败"
func _on_accept() -> void:
if _accept_pending or _accept_btn.disabled:
return
if client.has_method("exchange_accept") and client.exchange_accept():
_accept_pending = true
_accept_btn.disabled = true
else:
_status.text = "接受请求发送失败"
func _capture_anchor() -> void:
if _anchor_valid or client == null or not client.has_method("get_main_vid") \
or not client.has_method("get_entity"):
return
var entity: Dictionary = client.get_entity(int(client.get_main_vid()))
var p = entity.get("pos_cm", null)
if p is Vector3:
_anchor = Vector2(p.x, p.y)
_anchor_valid = true
elif entity.has("x") and entity.has("y"):
_anchor = Vector2(float(entity.get("x", 0)), float(entity.get("y", 0)))
_anchor_valid = true
func _current_position() -> Variant:
if client == null or not client.has_method("get_main_vid") or not client.has_method("get_entity"):
return null
var entity: Dictionary = client.get_entity(int(client.get_main_vid()))
var p = entity.get("pos_cm", null)
if p is Vector3:
return Vector2(p.x, p.y)
if entity.has("x") and entity.has("y"):
return Vector2(float(entity.get("x", 0)), float(entity.get("y", 0)))
return null
func _process(_dt: float) -> void:
if not is_open() or not _anchor_valid:
return
var current = _current_position()
if current is Vector2 and (absf(current.x - _anchor.x) > EXCHANGE_LIMIT_RANGE \
or absf(current.y - _anchor.y) > EXCHANGE_LIMIT_RANGE):
if client.has_method("exchange_cancel"):
client.exchange_cancel()
close()
func _build(parent: Node) -> void:
_root = Control.new()
_root.set_anchors_preset(Control.PRESET_CENTER)
@@ -148,6 +318,21 @@ func _build(parent: Node) -> void:
_self_box.position = Vector2(20, 56)
_self_box.custom_minimum_size = Vector2(200, 0)
_root.add_child(_self_box)
_self_grid = GridContainer.new()
_self_grid.columns = 6
_self_grid.position = Vector2(20, 138)
_self_grid.add_theme_constant_override("h_separation", 3)
_self_grid.add_theme_constant_override("v_separation", 3)
_root.add_child(_self_grid)
for pos in EXCHANGE_SLOT_COUNT:
var cell := Button.new()
cell.custom_minimum_size = Vector2(52, 32)
cell.add_theme_font_size_override("font_size", 8)
_self_grid.add_child(cell)
_exchange_cells[pos] = cell
if item_mouse and item_mouse.has_method("register_target"):
item_mouse.register_target(cell,
func(payload: Dictionary): return _drop_to_slot(payload, pos), self)
_self_gold = Label.new()
_self_gold.text = "金币: 0"
_self_gold.position = Vector2(20, 250)
@@ -191,7 +376,7 @@ func _build(parent: Node) -> void:
bottom.add_child(put)
_accept_btn = Button.new()
_accept_btn.text = "接受"
_accept_btn.pressed.connect(func() -> void: client.exchange_accept())
_accept_btn.pressed.connect(_on_accept)
bottom.add_child(_accept_btn)
var cancel := Button.new()
cancel.text = "取消"