载入中...
搜索中...
未找到
EconomyLedger.cpp
浏览该文件的文档.
2
4
5#include <algorithm>
6
7namespace eve::economy {
8
9int EconomyLedger::credit(const std::string& type, int amount) {
10 if (amount <= 0) return 0;
11 const auto* def = ResourceTypeRegistry::find(type);
12 const int cap = def ? def->stockMax : 0;
13
14 int& current = current_[type];
15 int accepted = amount;
16 if (cap > 0 && current + amount > cap) accepted = std::max(0, cap - current);
17 current += accepted;
18 income_[type] += accepted;
19 if (accepted < amount) wasted_[type] += amount - accepted;
20 return accepted;
21}
22
23bool EconomyLedger::debit(const std::string& type, int amount) {
24 if (amount <= 0) return true;
25 auto it = current_.find(type);
26 const int current = it == current_.end() ? 0 : it->second;
27 if (current < amount) return false;
28 it->second = current - amount;
29 expense_[type] += amount;
30 return true;
31}
32
33bool EconomyLedger::canAfford(const std::string& type, int amount) const {
34 if (amount <= 0) return true;
35 auto it = current_.find(type);
36 return it != current_.end() && it->second >= amount;
37}
38
39int EconomyLedger::get(const std::string& type) const {
40 auto it = current_.find(type);
41 return it == current_.end() ? 0 : it->second;
42}
43
44int EconomyLedger::getCap(const std::string& type) const {
45 const auto* def = ResourceTypeRegistry::find(type);
46 return def ? def->stockMax : 0;
47}
48
49int EconomyLedger::getWasted(const std::string& type) const {
50 auto it = wasted_.find(type);
51 return it == wasted_.end() ? 0 : it->second;
52}
53
54int EconomyLedger::getIncome(const std::string& type) const {
55 auto it = income_.find(type);
56 return it == income_.end() ? 0 : it->second;
57}
58
59int EconomyLedger::getExpense(const std::string& type) const {
60 auto it = expense_.find(type);
61 return it == expense_.end() ? 0 : it->second;
62}
63
64void EconomyLedger::swap(EconomyLedger& other) noexcept {
65 using std::swap;
66 swap(current_, other.current_);
67 swap(wasted_, other.wasted_);
68 swap(income_, other.income_);
69 swap(expense_, other.expense_);
70}
71
73 return {{current_.begin(), current_.end()}, {wasted_.begin(), wasted_.end()},
74 {income_.begin(), income_.end()}, {expense_.begin(), expense_.end()}};
75}
76
77void EconomyLedger::restore(const Snapshot& snapshot) {
78 current_ = std::unordered_map<std::string, int>(snapshot.current.begin(), snapshot.current.end());
79 wasted_ = std::unordered_map<std::string, int>(snapshot.wasted.begin(), snapshot.wasted.end());
80 income_ = std::unordered_map<std::string, int>(snapshot.income.begin(), snapshot.income.end());
81 expense_ = std::unordered_map<std::string, int>(snapshot.expense.begin(), snapshot.expense.end());
82}
83
84} // namespace eve::economy
double current
单个玩家的资源账本:当前量、上限、收支与浪费统计。
bool canAfford(const std::string &type, int amount) const
是否可负担 amount。
Snapshot snapshot() const
Capture every ledger counter in deterministic key order.
void swap(EconomyLedger &other) noexcept
Swap two ledgers without exposing an intermediate partial state.
int credit(const std::string &type, int amount)
入账 amount。
int getIncome(const std::string &type) const
累计入账。
void restore(const Snapshot &snapshot)
Restore a previously captured complete ledger state.
int getExpense(const std::string &type) const
累计出账。
int get(const std::string &type) const
当前持有量。
bool debit(const std::string &type, int amount)
出账 amount;余额不足时返回 false 且不扣款。
int getCap(const std::string &type) const
类型上限(来自注册表;未注册返回 0=不限)。
int getWasted(const std::string &type) const
因满仓而浪费的累计量。
static const ResourceTypeDef * find(const std::string &id)
按 id 查询;未注册时返回 nullptr。
Owning deterministic copy of all balance and accounting counters.
std::map< std::string, int > wasted
std::map< std::string, int > income
std::map< std::string, int > current
std::map< std::string, int > expense