载入中...
搜索中...
未找到
CardPlayFacade.cpp
浏览该文件的文档.
1#include "card/Card.h"
3namespace eve::card {
7 CardPlayComposition composition,
8 std::string transactionId) {
9 if (!ownsCard(cards_, card))
11 eve::DiagnosticCode::StaleHandle, "card does not belong to this Card facade", "card"));
12 const auto* definition = findDef(card.identity()->definitionId);
13 if (definition == nullptr)
15 eve::Diagnostic::error(eve::DiagnosticCode::NotFound, "card definition was not found", "definition"));
17 request.card = &card;
18 request.definition = definition;
19 request.playerAccount = &playerAccount;
20 request.composition = std::move(composition);
21 request.transactionId = std::move(transactionId);
22 return CardPlayPaymentAdapter::play(std::move(request));
23}
24
26 if (!ownsCard(cards_, card))
28 Diagnostic::error(DiagnosticCode::StaleHandle, "card is not owned by this facade", "card"));
29 const auto* definition = findDef(card.identity()->definitionId);
30 if (!definition)
32 Diagnostic::error(DiagnosticCode::NotFound, "card definition is absent", "card.definition"));
33 return CardPlayPaymentAdapter::manaCost(*definition);
34}
35
36namespace {
37class HandConsumption final : public transaction::ITransactionParticipant {
38public:
39 HandConsumption(Hand& hand, CardData& card) : hand_(ecs::handle_of(&hand)), card_(ecs::handle_of(&card)) {}
40 std::string_view name() const noexcept override { return "card-hand-consumption"; }
41 Result<void> prepare(const transaction::TransactionContext&) override {
42 if (prepared_ || committed_) return failure("hand stage is already in flight");
43 auto* hand = resolveHand();
44 auto* card = resolveCard();
45 if (!hand || !card) return failure("hand or card is stale");
46 const auto& values = hand->membership()->cards;
47 if (std::count(values.begin(), values.end(), card) != 1)
48 return failure("card membership is absent or duplicated");
49 before_ = values;
50 dragBefore_ = *hand->drag();
51 prepared_ = true;
52 return Result<void>::success();
53 }
54 Result<void> commit(const transaction::TransactionContext&) override {
55 if (!prepared_ || committed_) return failure("hand stage is not prepared");
56 auto* hand = resolveHand();
57 auto* card = resolveCard();
58 if (!hand || !card) return failure("hand or card is stale");
59 if (hand->membership()->cards != before_) return failure("hand changed before commit");
60 auto& values = hand->membership()->cards;
61 values.erase(std::find(values.begin(), values.end(), card));
62 if (hand->drag()->card == card) hand->drag()->card = nullptr;
63 if (hand->drag()->press == card) hand->drag()->press = nullptr;
64 committed_ = true;
65 return Result<void>::success();
66 }
67 Result<void> rollback(const transaction::TransactionContext&) override {
68 if (!prepared_ || committed_) return failure("hand stage is not prepared");
69 prepared_ = false;
70 return Result<void>::success();
71 }
72 Result<void> compensate(const transaction::TransactionContext&) override {
73 if (!committed_) return failure("hand stage is not committed");
74 auto* hand = resolveHand();
75 auto* card = resolveCard();
76 if (!hand || !card) return failure("hand or card is stale");
77 auto expected = before_;
78 expected.erase(std::find(expected.begin(), expected.end(), card));
79 if (hand->membership()->cards != expected) return failure("hand changed before compensation");
80 hand->membership()->cards.swap(before_);
81 *hand->drag() = dragBefore_;
82 committed_ = false;
83 prepared_ = false;
84 return Result<void>::success();
85 }
86
87private:
88 static Result<void> failure(std::string message) {
90 }
91 Hand* resolveHand() const {
92 return hand_.table == ecs::current() ? dynamic_cast<Hand*>(ecs::try_get(hand_)) : nullptr;
93 }
94 CardData* resolveCard() const {
95 return card_.table == ecs::current() ? dynamic_cast<CardData*>(ecs::try_get(card_)) : nullptr;
96 }
97 ecs::EntityHandle hand_, card_;
98 std::vector<CardData*> before_;
99 Hand::Drag dragBefore_;
100 bool prepared_ = false, committed_ = false;
101};
102} // namespace
104 auto cost = playCost(card);
106 Hand* found = nullptr;
107 for (const auto& handle : hands_) {
108 auto* hand = dynamic_cast<Hand*>(ecs::try_get(handle));
109 if (!hand) continue;
110 if (std::find(hand->membership()->cards.begin(), hand->membership()->cards.end(), &card) ==
111 hand->membership()->cards.end())
112 continue;
113 if (found)
115 Diagnostic::error(DiagnosticCode::Conflict, "card is in multiple hands", "card.hand"));
116 found = hand;
117 }
118 if (!found)
120 Diagnostic::error(DiagnosticCode::NotFound, "card is not in a hand", "card.hand"));
122 std::make_unique<HandConsumption>(*found, card));
123}
124} // namespace eve::card
eve::resource::CostSpec cost
std::string name
Definition ECS.cpp:154
std::string message
const GltfImportRequest & request
PrimitiveHandle handle
std::set< std::string > values
bool found
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
单张卡牌:ECS 实体,数据拆成 Identity / Stats / Visual / Layout / State。
Definition CardTypes.h:106
static eve::Result< std::optional< eve::resource::CostSpec > > manaCost(const CardDefinition &definition)
Convert a non-negative card mana value into a canonical cost.
Definition CardPlay.cpp:321
static eve::Result< eve::transaction::TransactionReceipt > play(CardPlayRequest request)
Execute condition, movement, card effect and Mana as one transaction.
Definition CardPlay.cpp:345
Result< std::optional< resource::CostSpec > > playCost(CardData &card) const
Resolve one owned card's canonical definition payment for composition.
eve::Result< eve::transaction::TransactionReceipt > play(CardData &card, eve::resource::IResourceAccount &playerAccount, CardPlayComposition composition={}, std::string transactionId={})
Play a card through the facade's condition, container and payment transaction.
Result< std::unique_ptr< transaction::ITransactionParticipant > > prepareHandConsumption(CardData &card) const
手牌:扇形布局 + 悬浮 + 拖拽 + 落区判定 + 渲染。
Definition CardTypes.h:292
Provider-neutral resource account contract.
Synchronous participant contract for one coordinator transaction.
bool ownsCard(const std::vector< ecs::EntityHandle > &handles, const CardData &card)
WidgetDesc card(std::vector< WidgetDesc > children, std::string id)
Bordered surface container with editor-friendly padding.
Definition Widget.cpp:594
Result< VehiclePhysicsStatus > commit(physics::World &world, physics::ForceBatch &batch, std::shared_ptr< VehicleWheelRuntime > &runtime, std::vector< VehicleEntity::SuspensionState::WheelState > candidate, const SimulationStep &step)
Optional cross-domain operations that belong to one card play.
Definition CardPlay.h:144
Complete caller-owned input for one atomic card play.
Definition CardPlay.h:172