载入中...
搜索中...
未找到
EconomySystem.cpp
浏览该文件的文档.
3
4#include "common/Assert.h"
5
6#include <utility>
7
8namespace eve::economy {
9
10namespace {
11std::uint64_t& accountEpoch() {
12 static std::uint64_t epoch = 1;
13 return epoch;
14}
15std::unordered_map<int, std::weak_ptr<resource::IResourceAccount>>& accountCache() {
16 static std::unordered_map<int, std::weak_ptr<resource::IResourceAccount>> cache;
17 return cache;
18}
19class PlayerAccount final : public resource::IResourceAccount {
20public:
21 explicit PlayerAccount(EconomyLedger& ledger) : adapter_(ledger), epoch_(accountEpoch()) {}
22 Result<resource::Affordability> canAfford(const resource::CostSpec& cost) const override {
24 return adapter_.canAfford(cost);
25 }
26 Result<resource::Reservation> reserve(const resource::CostSpec& cost) override {
27 if (stale()) return Result<resource::Reservation>::failure(error());
28 return adapter_.reserve(cost);
29 }
30 Result<resource::Receipt> debit(const resource::CostSpec& cost) override {
31 if (stale()) return Result<resource::Receipt>::failure(error());
32 return adapter_.debit(cost);
33 }
34 Result<resource::Receipt> credit(const resource::CostSpec& cost) override {
35 if (stale()) return Result<resource::Receipt>::failure(error());
36 return adapter_.credit(cost);
37 }
38 Result<resource::Receipt> commit(const resource::Reservation& reservation) override {
39 if (stale()) return Result<resource::Receipt>::failure(error());
40 return adapter_.commit(reservation);
41 }
42 Result<void> rollback(const resource::Reservation& reservation) override {
43 if (stale()) return Result<void>::failure(error());
44 return adapter_.rollback(reservation);
45 }
46
47private:
48 bool stale() const { return epoch_ != accountEpoch(); }
49 static Diagnostic error() {
50 return Diagnostic::error(DiagnosticCode::StaleHandle, "player ledger was reset", "account");
51 }
52 EconomyLedgerResourceAccount adapter_;
53 std::uint64_t epoch_;
54};
55} // namespace
56std::shared_ptr<resource::IResourceAccount> EconomySystem::resourceAccount(int player) {
57 auto& slot = accountCache()[player];
58 if (auto existing = slot.lock()) return existing;
59 auto account = std::make_shared<PlayerAccount>(ledger(player));
60 slot = account;
61 return account;
62}
63
64std::unordered_map<int, EconomyLedger>& EconomySystem::ledgers() {
65 static std::unordered_map<int, EconomyLedger> s_ledgers;
66 return s_ledgers;
67}
68
69std::vector<EconomyEvent>& EconomySystem::events() {
70 static std::vector<EconomyEvent> s_events;
71 return s_events;
72}
73
74std::unordered_map<std::string, EconomyHook>& EconomySystem::hooks() {
75 static std::unordered_map<std::string, EconomyHook> s_hooks;
76 return s_hooks;
77}
78
79EconomyLedger& EconomySystem::ledger(int player) { return ledgers()[player]; }
80
82 ++accountEpoch();
83 accountCache().clear();
84 ledgers().clear();
85 events().clear();
86 hooks().clear();
87}
88
89int EconomySystem::credit(int player, const std::string& type, int amount) {
90 const int accepted = ledger(player).credit(type, amount);
91 emit(EconomyEvent{player, type, "credit", accepted});
92 const int wasted = amount - accepted;
93 if (wasted > 0) emit(EconomyEvent{player, type, "waste", wasted});
94 return accepted;
95}
96
97bool EconomySystem::debit(int player, const std::string& type, int amount) {
98 if (!ledger(player).debit(type, amount)) return false;
99 emit(EconomyEvent{player, type, "debit", amount});
100 return true;
101}
102
103int EconomySystem::get(int player, const std::string& type) { return ledger(player).get(type); }
104
105int EconomySystem::getCap(int player, const std::string& type) { return ledger(player).getCap(type); }
106
107int EconomySystem::getWasted(int player, const std::string& type) { return ledger(player).getWasted(type); }
108
109int EconomySystem::getIncome(int player, const std::string& type) { return ledger(player).getIncome(type); }
110
111int EconomySystem::getExpense(int player, const std::string& type) { return ledger(player).getExpense(type); }
112
113EconomyLedger::Snapshot EconomySystem::snapshot(int player) { return ledger(player).snapshot(); }
114
116 EV_PARAM_CHECK(!name.empty(), "hook name must not be empty");
117 hooks()[name] = std::move(fn);
118}
119
120void EconomySystem::unregisterHook(const std::string& name) { hooks().erase(name); }
121
122void EconomySystem::clearEvents() { events().clear(); }
123
124int EconomySystem::eventCount() { return static_cast<int>(events().size()); }
125
127 const bool valid = index >= 0 && size_t(index) < events().size();
128 EV_PARAM_CHECK(valid, "event index out of range");
129 return events()[static_cast<size_t>(index)];
130}
131
132void EconomySystem::emit(const EconomyEvent& ev) {
133 events().push_back(ev);
134 for (auto& [name, fn] : hooks()) {
135 (void)name;
136 fn(ev);
137 }
138}
139
140} // namespace eve::economy
EVEngine assertion entry point, backed by zeroerr.
#define EV_PARAM_CHECK(cond,...)
Validate a function parameter / public API precondition.
Definition Assert.h:30
eve::resource::CostSpec cost
eve::resource::IResourceAccount * account
std::string name
Definition ECS.cpp:154
Resource-account adapter backed by an EconomyLedger.
bool valid
uint64_t epoch
Definition OnnxGpgpu.cpp:28
std::string error
Definition Package.cpp:174
std::string slot
uint32_t index
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
Snapshot snapshot() const
Capture every ledger counter in deterministic key order.
int credit(const std::string &type, int amount)
入账 amount。
int getIncome(const std::string &type) const
累计入账。
int getExpense(const std::string &type) const
累计出账。
int get(const std::string &type) const
当前持有量。
int getCap(const std::string &type) const
类型上限(来自注册表;未注册返回 0=不限)。
int getWasted(const std::string &type) const
因满仓而浪费的累计量。
static int credit(int player, const std::string &type, int amount)
入账并广播事件。
static const EconomyEvent & eventAt(int index)
Event at.
static void clear()
重置所有玩家账本、事件与钩子(测试用)。
static void registerHook(const std::string &name, EconomyHook fn)
注册事件钩子(同名替换)。
static int getWasted(int player, const std::string &type)
因满仓浪费的累计量。
static int getExpense(int player, const std::string &type)
累计出账量。
static bool debit(int player, const std::string &type, int amount)
出账并广播事件;余额不足返回 false。
static EconomyLedger::Snapshot snapshot(int player)
取一个玩家账本的完整只读快照(含未注册类型的余额与流水)。
static int getIncome(int player, const std::string &type)
累计入账量。
static std::shared_ptr< resource::IResourceAccount > resourceAccount(int player)
Share a cached protocol adapter over the canonical player ledger. @ownership The adapter is shared; E...
static void clearEvents()
事件队列(按发生顺序)。
static void unregisterHook(const std::string &name)
注销事件钩子。
static int eventCount()
Event count.
static int getCap(int player, const std::string &type)
玩家该类型持有上限(0=不限)。
static int get(int player, const std::string &type)
玩家当前持有量。
std::function< void(const EconomyEvent &)> EconomyHook
eve::Diagnostic Diagnostic
Result< VehiclePhysicsStatus > commit(physics::World &world, physics::ForceBatch &batch, std::shared_ptr< VehicleWheelRuntime > &runtime, std::vector< VehicleEntity::SuspensionState::WheelState > candidate, const SimulationStep &step)
SettlementPipeline::Stage fn
一次经济事件(credit / debit / waste)。
Owning deterministic copy of all balance and accounting counters.