载入中...
搜索中...
未找到
EconomySystem.cpp
浏览该文件的文档.
2
3#include "common/Assert.h"
4
5#include <utility>
6
7namespace eve::economy {
8
9std::unordered_map<int, EconomyLedger>& EconomySystem::ledgers() {
10 static std::unordered_map<int, EconomyLedger> s_ledgers;
11 return s_ledgers;
12}
13
14std::vector<EconomyEvent>& EconomySystem::events() {
15 static std::vector<EconomyEvent> s_events;
16 return s_events;
17}
18
19std::unordered_map<std::string, EconomyHook>& EconomySystem::hooks() {
20 static std::unordered_map<std::string, EconomyHook> s_hooks;
21 return s_hooks;
22}
23
24EconomyLedger& EconomySystem::ledger(int player) { return ledgers()[player]; }
25
27 ledgers().clear();
28 events().clear();
29 hooks().clear();
30}
31
32int EconomySystem::credit(int player, const std::string& type, int amount) {
33 const int accepted = ledger(player).credit(type, amount);
34 emit(EconomyEvent{player, type, "credit", accepted});
35 const int wasted = amount - accepted;
36 if (wasted > 0) emit(EconomyEvent{player, type, "waste", wasted});
37 return accepted;
38}
39
40bool EconomySystem::debit(int player, const std::string& type, int amount) {
41 if (!ledger(player).debit(type, amount)) return false;
42 emit(EconomyEvent{player, type, "debit", amount});
43 return true;
44}
45
46int EconomySystem::get(int player, const std::string& type) { return ledger(player).get(type); }
47
48int EconomySystem::getCap(int player, const std::string& type) { return ledger(player).getCap(type); }
49
50int EconomySystem::getWasted(int player, const std::string& type) { return ledger(player).getWasted(type); }
51
52int EconomySystem::getIncome(int player, const std::string& type) { return ledger(player).getIncome(type); }
53
54int EconomySystem::getExpense(int player, const std::string& type) { return ledger(player).getExpense(type); }
55
56EconomyLedger::Snapshot EconomySystem::snapshot(int player) { return ledger(player).snapshot(); }
57
59 EV_PARAM_CHECK(!name.empty(), "hook name must not be empty");
60 hooks()[name] = std::move(fn);
61}
62
63void EconomySystem::unregisterHook(const std::string& name) { hooks().erase(name); }
64
65void EconomySystem::clearEvents() { events().clear(); }
66
67int EconomySystem::eventCount() { return static_cast<int>(events().size()); }
68
70 const bool valid = index >= 0 && size_t(index) < events().size();
71 EV_PARAM_CHECK(valid, "event index out of range");
72 return events()[static_cast<size_t>(index)];
73}
74
75void EconomySystem::emit(const EconomyEvent& ev) {
76 events().push_back(ev);
77 for (auto& [name, fn] : hooks()) {
78 (void)name;
79 fn(ev);
80 }
81}
82
83} // 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
std::string name
bool valid
uint32_t index
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 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
SettlementPipeline::Stage fn
一次经济事件(credit / debit / waste)。
Owning deterministic copy of all balance and accounting counters.