载入中...
搜索中...
未找到
ContainerAdapters.cpp
浏览该文件的文档.
18 return !stack.empty() && eve::container::MembershipId("inventory:" + std::to_string(stack.instanceId)) == id;
34 if (lhs.id != rhs.id || lhs.revision != rhs.revision || lhs.entries.size() != rhs.entries.size()) return false;
38 if (left.membership.object != right.membership.object || left.membership.slot != right.membership.slot ||
39 left.membership.generation != right.membership.generation || left.object.id != right.object.id ||
48class InventoryContainerAdapter::PreparedState final : public eve::container::IContainer::PreparedState {
50 PreparedState(InventoryContainerAdapter& owner, std::vector<ItemStack> staged, eve::Revision revision)
91InventoryContainerAdapter::InventoryContainerAdapter(eve::container::ContainerId id, EquipmentSet* equipment)
102eve::container::ContainerObject InventoryContainerAdapter::describe(const ItemStack& stack, std::string equipmentSlot) {
110 if (std::find(object.tags.begin(), object.tags.end(), tag) == object.tags.end()) object.tags.push_back(tag);
131 return const_cast<ItemStack*>(static_cast<const InventoryContainerAdapter*>(this)->stackAt(slot));
142 if (!descriptor_.id.isValid() || (kind_ == InventoryContainerKind::Bag ? bag_ == nullptr : equipment_ == nullptr))
144 eve::Diagnostic::error(eve::DiagnosticCode::InvalidArgument, "inventory container adapter is not bound"));
147 const int count = kind_ == InventoryContainerKind::Bag ? bag_->getSlotCount() : equipment_->getSlotCount();
152 if (!descriptor_.capacity.isUnlimited() && static_cast<std::size_t>(index) >= descriptor_.capacity.value())
164 if (!descriptor_.capacity.isUnlimited() && snapshot.entries.size() > descriptor_.capacity.value())
170eve::Result<void> InventoryContainerAdapter::validateObject(const InventoryContainerObject& object) const {
172 return eve::Result<void>::failure(eve::Diagnostic::error(eve::DiagnosticCode::InvalidArgument, "inventory transfer stack is incomplete"));
177 const eve::container::ContainerObject& object, std::optional<eve::container::SlotIndex> destination,
180 if (payload == nullptr) return eve::Result<void>::failure(eve::Diagnostic::error(eve::DiagnosticCode::StaleHandle, "inventory payload is stale"));
184 return eve::Result<void>::failure(eve::Diagnostic::error(eve::DiagnosticCode::StaleHandle, "inventory membership identity is stale"));
185 if (object.type != payload->stack.itemId || object.quantity != static_cast<std::uint32_t>(payload->stack.quantity))
186 return eve::Result<void>::failure(eve::Diagnostic::error(eve::DiagnosticCode::StaleHandle, "inventory object facts are stale"));
188 if (definition == nullptr) return eve::Result<void>::failure(eve::Diagnostic::error(eve::DiagnosticCode::NotFound, "inventory item definition was not found"));
190 return eve::Result<void>::failure(eve::Diagnostic::error(eve::DiagnosticCode::PreconditionViolation, "inventory stack exceeds item definition maxStack"));
193 if (bag_ == nullptr) return eve::Result<void>::failure(eve::Diagnostic::error(eve::DiagnosticCode::InvalidArgument, "bag adapter is unbound"));
196 descriptor_.capacity.isUnlimited() ? slotCount : std::min(slotCount, descriptor_.capacity.value());
198 return eve::Result<void>::failure(eve::Diagnostic::error(eve::DiagnosticCode::Conflict, "bag already contains inventory object"));
201 return eve::Result<void>::failure(eve::Diagnostic::error(eve::DiagnosticCode::InvalidArgument, "bag destination slot is out of range"));
204 return eve::Result<void>::failure(eve::Diagnostic::error(eve::DiagnosticCode::Conflict, "bag destination slot is occupied"));
207 std::any_of(bag_->slots().begin(), bag_->slots().begin() + static_cast<std::ptrdiff_t>(slotLimit),
210 return eve::Result<void>::failure(eve::Diagnostic::error(eve::DiagnosticCode::Conflict, "bag has no empty membership slot"));
215 return eve::Result<void>::failure(eve::Diagnostic::error(eve::DiagnosticCode::PreconditionViolation, reason.empty() ? "bag rejected inventory object" : reason));
220 if (equipment_ == nullptr) return eve::Result<void>::failure(eve::Diagnostic::error(eve::DiagnosticCode::InvalidArgument, "equipment is unbound"));
222 static_cast<std::size_t>(destination->value()) >= static_cast<std::size_t>(equipment_->getSlotCount()))
223 return eve::Result<void>::failure(eve::Diagnostic::error(eve::DiagnosticCode::PreconditionViolation, "equipment transfer requires a valid destination slot"));
225 if (target.empty()) return eve::Result<void>::failure(eve::Diagnostic::error(eve::DiagnosticCode::NotFound, "equipment slot was not found"));
227 if (current != nullptr && !current->empty() && (!ignoredObject || object.id != objectId(*current)))
228 return eve::Result<void>::failure(eve::Diagnostic::error(eve::DiagnosticCode::Conflict, "equipment destination slot is occupied"));
230 return eve::Result<void>::failure(eve::Diagnostic::error(eve::DiagnosticCode::PreconditionViolation, "inventory item does not fit equipment slot"));
233 const bool accepted = std::any_of(allowed->begin(), allowed->end(), [&](const std::string& tag) {
237 return eve::Result<void>::failure(eve::Diagnostic::error(eve::DiagnosticCode::PreconditionViolation, "equipment slot rejected inventory tags"));
242eve::Result<std::unique_ptr<eve::container::IContainer::PreparedState>> InventoryContainerAdapter::prepare(
243 const eve::container::ContainerSnapshot& expected, const eve::container::ContainerSnapshot& candidate) {
244 const bool bound = kind_ == InventoryContainerKind::Bag ? bag_ != nullptr : equipment_ != nullptr;
247 eve::Diagnostic::error(eve::DiagnosticCode::InvalidArgument, "inventory adapter is not bound"));
249 return eve::Result<std::unique_ptr<eve::container::IContainer::PreparedState>>::failure(eve::Diagnostic::error(
253 return eve::Result<std::unique_ptr<eve::container::IContainer::PreparedState>>::failure(currentResult.status());
256 eve::Diagnostic::error(eve::DiagnosticCode::StaleHandle, "inventory prepare expected snapshot is stale"));
259 return eve::Result<std::unique_ptr<eve::container::IContainer::PreparedState>>::failure(eve::Diagnostic::error(
260 eve::DiagnosticCode::InvariantViolation, "inventory candidate revision is not the next revision"));
261 const int slotCount = kind_ == InventoryContainerKind::Bag ? bag_->getSlotCount() : equipment_->getSlotCount();
262 if (!descriptor_.capacity.isUnlimited() && candidate.entries.size() > descriptor_.capacity.value())
264 eve::Diagnostic::error(eve::DiagnosticCode::Conflict, "inventory candidate exceeds container capacity"));
275 !ids.insert(entry.membership.object).second || !slots.insert(entry.membership.slot.value()).second)
279 const auto* payload = dynamic_cast<const InventoryContainerObject*>(entry.object.payload.get());
282 eve::Diagnostic::error(eve::DiagnosticCode::StaleHandle, "inventory candidate payload is stale"));
292 eve::Diagnostic::error(eve::DiagnosticCode::StaleHandle, "inventory candidate object facts are stale"));
298 return eve::Result<std::unique_ptr<eve::container::IContainer::PreparedState>>::success(std::move(prepared));
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={}, DiagnosticDetails details={}, std::string source={})
Construct an error diagnostic with the standard error severity.
Definition Diagnostic.h:125
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:175
Non-negative capacity measured in membership slots.
Definition Container.h:92
constexpr std::size_t value() const noexcept
Definition Container.h:99
constexpr bool isUnlimited() const noexcept
Definition Container.h:98
Strong identity of one runtime or persistent container.
Definition Container.h:44
bool isValid() const noexcept
Return whether this ID is usable by a transfer request.
Definition Container.h:58
Detached state prepared for an atomic container replacement.
Definition Container.h:296
Strong identity of an object which can have one container membership.
Definition Container.h:68
Explicit slot number; it never implicitly converts to an integer.
Definition Container.h:117
constexpr std::optional< StrongUint64 > incremented() const noexcept
Returns the next value, or empty instead of unsigned wraparound.
Definition StrongUint64.h:30
const std::vector< std::string > * allowedTags(const std::string &slotName) const
Allowed tags.
Definition Equipment.cpp:78
PreparedState(InventoryContainerAdapter &owner, std::vector< ItemStack > staged, eve::Revision revision)
Definition ContainerAdapters.cpp:50
void commit() noexcept override
Publish the staged state; the operation is non-throwing.
Definition ContainerAdapters.cpp:53
void rollback() noexcept override
Abandon an unpublished stage; the operation is non-throwing.
Definition ContainerAdapters.cpp:70
Adapts one Bag or EquipmentSet without changing their legacy APIs.
Definition ContainerAdapters.h:35
eve::Result< std::unique_ptr< eve::container::IContainer::PreparedState > > prepare(const eve::container::ContainerSnapshot &expected, const eve::container::ContainerSnapshot &candidate) override
Prepare an exact replacement from expected to candidate.
Definition ContainerAdapters.cpp:242
eve::Result< void > validateInsert(const eve::container::ContainerObject &object, std::optional< eve::container::SlotIndex > destination, std::optional< eve::container::MembershipId > ignoredObject=std::nullopt) const override
Validate insert.
Definition ContainerAdapters.cpp:176
InventoryContainerAdapter(eve::container::ContainerId id, Bag *bag, eve::container::Capacity capacity=eve::container::Capacity::unlimited())
Bind a borrowed bag.
Definition ContainerAdapters.cpp:81
eve::Revision revision() const noexcept
Return the adapter revision for optimistic transfer requests.
Definition ContainerAdapters.h:68
eve::Result< eve::container::ContainerSnapshot > snapshot() const override
Snapshot.
Definition ContainerAdapters.cpp:141
static bool canAdd(Bag *bag, const std::string &itemId, int quantity, std::string *reason=nullptr)
Can add.
Definition InventorySystem.cpp:350
Inventory adapters for generic Bag and EquipmentSet containers.
运行时容器:固定格数的背包 / 箱子 / 商店栏等。 行为由 InventorySystem 提供;本类暴露便于脚本绑定的薄封装方法。
Definition AvatarInstance.h:37
@ InvalidArgument
@ NotFound
@ PreconditionViolation
@ InvariantViolation
@ StaleHandle
@ Conflict
detail::StrongUint64< detail::GenerationTag > Generation
Registry/object replacement generation used to reject stale handles.
Definition Generation.h:13
Definition Container.h:602
Generic object facts used by filters and transfer diagnostics.
Definition Container.h:168
Lossless snapshot of one container at one revision.
Definition Container.h:190
std::vector< MembershipEntry > entries
Definition Container.h:193
Owning transfer payload for one inventory stack.
Definition ContainerAdapters.h:20