载入中...
搜索中...
未找到
GameState.cpp
浏览该文件的文档.
1#include "rpg/GameState.h"
3
4#include "common/Value.h"
5
6#include <cmath>
7
8namespace {
9
11 const std::string &path) {
12 if (!value || !value->isObject())
14 eve::Diagnostic::error(eve::DiagnosticCode::ParseError, "game state field must be an object", path));
15 const auto& object = *value->getIf<eve::Value::Object>();
16 std::unordered_map<std::string, double> result;
17 result.reserve(object.size());
18 for (const auto& [key, item] : object) {
19 if (!item.isNumeric())
21 eve::DiagnosticCode::ParseError, "game state variable must be numeric", path + "." + key));
22 const double number = item.isInt64() ? static_cast<double>(item.asInt()) : item.asDouble();
23 if (!std::isfinite(number))
25 eve::DiagnosticCode::InvalidArgument, "game state variable must be finite", path + "." + key));
26 result.emplace(key, number);
27 }
29}
30
32 const std::string &path) {
33 if (!value || !value->isObject())
35 eve::Diagnostic::error(eve::DiagnosticCode::ParseError, "game state field must be an object", path));
36 const auto& object = *value->getIf<eve::Value::Object>();
37 std::unordered_map<std::string, std::string> result;
38 result.reserve(object.size());
39 for (const auto& [key, item] : object) {
40 if (!item.isString())
42 eve::DiagnosticCode::ParseError, "game state string fact must be a string", path + "." + key));
43 result.emplace(key, item.asString());
44 }
46}
47
48} // namespace
49
50namespace eve::rpg {
51
52void GameState::setSwitch(const std::string &name, bool on) { switches_[name] = on; }
53
54void GameState::switchOn(const std::string &name) { switches_[name] = true; }
55
56void GameState::switchOff(const std::string &name) { switches_[name] = false; }
57
58bool GameState::isSwitchOn(const std::string &name) const {
59 auto it = switches_.find(name);
60 return it != switches_.end() && it->second;
61}
62
63void GameState::setVariable(const std::string &name, double value) { variables_[name] = value; }
64
65double GameState::getVariable(const std::string &name) const {
66 auto it = variables_.find(name);
67 return it == variables_.end() ? 0.0 : it->second;
68}
69
70void GameState::addVariable(const std::string &name, double delta) {
71 variables_[name] = getVariable(name) + delta;
72}
73
74void GameState::setSelfVariable(const std::string &scope, const std::string &name, double value) {
75 selfVariables_[scope][name] = value;
76}
77
78double GameState::getSelfVariable(const std::string &scope, const std::string &name) const {
79 auto it = selfVariables_.find(scope);
80 if (it == selfVariables_.end()) return 0.0;
81 auto vit = it->second.find(name);
82 return vit == it->second.end() ? 0.0 : vit->second;
83}
84
85bool GameState::hasSelfVariable(const std::string &scope, const std::string &name) const {
86 auto it = selfVariables_.find(scope);
87 if (it == selfVariables_.end()) return false;
88 return it->second.find(name) != it->second.end();
89}
90
91void GameState::setSelfString(const std::string &scope, const std::string &name, std::string value) {
92 selfStrings_[scope][name] = std::move(value);
93}
94
95std::string GameState::getSelfString(const std::string &scope, const std::string &name) const {
96 auto it = selfStrings_.find(scope);
97 if (it == selfStrings_.end()) return {};
98 auto sit = it->second.find(name);
99 return sit == it->second.end() ? std::string{} : sit->second;
100}
101
102bool GameState::hasSelfString(const std::string &scope, const std::string &name) const {
103 auto it = selfStrings_.find(scope);
104 if (it == selfStrings_.end()) return false;
105 return it->second.find(name) != it->second.end();
106}
107
108void GameState::clearSelfString(const std::string &scope, const std::string &name) {
109 auto it = selfStrings_.find(scope);
110 if (it == selfStrings_.end()) return;
111 it->second.erase(name);
112 if (it->second.empty()) selfStrings_.erase(it);
113}
114
116 switches_.clear();
117 variables_.clear();
118 selfVariables_.clear();
119 selfStrings_.clear();
120}
121
123 eve::Value::Object switches;
124 for (const auto &[name, on] : switches_) switches.emplace(name, eve::Value(on));
126 for (const auto &[name, value] : variables_) variables.emplace(name, eve::Value(value));
127 eve::Value::Object scoped;
128 for (const auto &[scope, values] : selfVariables_) {
129 eve::Value::Object encoded;
130 for (const auto &[name, value] : values) encoded.emplace(name, eve::Value(value));
131 scoped.emplace(scope, eve::Value(std::move(encoded)));
132 }
133 eve::Value::Object scopedStrings;
134 for (const auto &[scope, values] : selfStrings_) {
135 eve::Value::Object encoded;
136 for (const auto &[name, value] : values) encoded.emplace(name, eve::Value(value));
137 scopedStrings.emplace(scope, eve::Value(std::move(encoded)));
138 }
140 root.emplace("schema", eve::Value("eve.rpg.game-state"));
141 root.emplace("selfStrings", eve::Value(std::move(scopedStrings)));
142 root.emplace("selfVariables", eve::Value(std::move(scoped)));
143 root.emplace("switches", eve::Value(std::move(switches)));
144 root.emplace("variables", eve::Value(std::move(variables)));
145 root.emplace("version", eve::Value(1));
146 return eve::Value(std::move(root)).toJson();
147}
148
150 auto parsed = eve::Value::fromJson(json);
151 if (!parsed.ok()) return eve::Result<void>::failure(parsed.status());
152 const eve::Value &root = parsed.value();
153 if (!root.isObject())
155 eve::Diagnostic::error(eve::DiagnosticCode::ParseError, "game state root must be an object", "$."));
156 const eve::Value *schema = root.find("schema");
157 if (!schema || !schema->isString() || schema->asString() != "eve.rpg.game-state")
159 eve::DiagnosticCode::InvalidArgument, "snapshot does not belong to RPG GameState", "$.schema"));
160 const eve::Value *version = root.find("version");
161 if (!version || !version->isInt64() || version->asInt() != 1)
163 eve::DiagnosticCode::UnknownVersion, "unsupported RPG GameState snapshot version", "$.version"));
164
165 const eve::Value *switches = root.find("switches");
166 if (!switches || !switches->isObject())
168 eve::DiagnosticCode::ParseError, "game state switches must be an object", "$.switches"));
169 const auto& switchObject = *switches->getIf<eve::Value::Object>();
170 std::unordered_map<std::string, bool> candidateSwitches;
171 candidateSwitches.reserve(switchObject.size());
172 for (const auto& [key, item] : switchObject) {
173 if (!item.isBool())
175 eve::DiagnosticCode::ParseError, "game state switch must be boolean", "$.switches." + key));
176 candidateSwitches.emplace(key, item.asBool());
177 }
178
179 auto candidateVariables = parseNumbers(root.find("variables"), "$.variables");
180 if (!candidateVariables.ok()) return eve::Result<void>::failure(candidateVariables.status());
181 const eve::Value *scoped = root.find("selfVariables");
182 if (!scoped || !scoped->isObject())
184 eve::DiagnosticCode::ParseError, "game state selfVariables must be an object", "$.selfVariables"));
185 std::unordered_map<std::string, std::unordered_map<std::string, double>> candidateScoped;
186 const auto& scopedObject = *scoped->getIf<eve::Value::Object>();
187 candidateScoped.reserve(scopedObject.size());
188 for (const auto& [scope, content] : scopedObject) {
189 auto values = parseNumbers(&content, "$.selfVariables." + scope);
190 if (!values.ok()) return eve::Result<void>::failure(values.status());
191 candidateScoped.emplace(scope, std::move(values).takeValue());
192 }
193
194 std::unordered_map<std::string, std::unordered_map<std::string, std::string>> candidateStrings;
195 if (const eve::Value *scopedStrings = root.find("selfStrings"); scopedStrings != nullptr) {
196 if (!scopedStrings->isObject())
198 eve::DiagnosticCode::ParseError, "game state selfStrings must be an object", "$.selfStrings"));
199 const auto& stringObject = *scopedStrings->getIf<eve::Value::Object>();
200 candidateStrings.reserve(stringObject.size());
201 for (const auto& [scope, content] : stringObject) {
202 auto values = parseStrings(&content, "$.selfStrings." + scope);
203 if (!values.ok()) return eve::Result<void>::failure(values.status());
204 candidateStrings.emplace(scope, std::move(values).takeValue());
205 }
206 }
207
208 switches_ = std::move(candidateSwitches);
209 variables_ = std::move(candidateVariables).takeValue();
210 selfVariables_ = std::move(candidateScoped);
211 selfStrings_ = std::move(candidateStrings);
213}
214
216 static GameState g;
217 return g;
218}
219
220} // namespace eve::rpg
int root
Definition AnimSmr.cpp:119
SQObjectPtr key
Definition ECS.cpp:155
std::string name
Definition ECS.cpp:154
全局游戏状态:开关(bool)、变量(数字)、独立变量(按作用域)。
tensor::Graph g
Definition GpuGraph.cpp:7
JobScope scope
Poco::JSON::Object::Ptr result
std::string path
Definition PlayHost.cpp:111
std::set< std::string > values
std::map< std::string, Type > variables
Object::Ptr value
double number
Share string attribute map operations through the Windows Foundation DLL.
Json object
float size
Definition TreeMesh.cpp:158
Owning, renderer-independent dynamic values.
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
The canonical owning dynamic value used by data-facing protocols.
Definition Value.h:31
Result< std::string > toJson() const
Serialize this value as deterministic compact JSON.
Definition Value.cpp:187
const std::string & asString() const
Return the string payload; the caller must have checked the kind.
Definition Value.cpp:207
static Result< Value > fromJson(std::string_view json)
Parse one strict JSON value into an owning Value.
Definition Value.cpp:177
std::map< std::string, Value > Object
Definition Value.h:34
bool isString() const noexcept
Return true when this value is a string.
Definition Value.h:106
const T * getIf() const noexcept
Return a typed pointer, or nullptr when the kind differs.
Definition Value.h:209
bool isObject() const noexcept
Return true when this value is an object.
Definition Value.h:110
Value * find(const std::string &key) noexcept
Return an object member, or nullptr when absent.
Definition Value.cpp:244
一份游戏状态(开关/变量/独立变量)。
Definition GameState.h:26
double getVariable(const std::string &name) const
Returns the variable.
Definition GameState.cpp:65
void clearSelfString(const std::string &scope, const std::string &name)
Remove one scoped string fact; no-op when absent.
void addVariable(const std::string &name, double delta)
Adds variable.
Definition GameState.cpp:70
static GameState & global()
进程级全局单例(脚本便利入口)。
eve::Result< void > restoreSnapshotJson(std::string_view json)
Validate and atomically restore a canonical RPG game-state JSON document.
void switchOn(const std::string &name)
Switch on.
Definition GameState.cpp:54
eve::Result< std::string > snapshotJson() const
Serialize this state as the canonical versioned RPG game-state JSON document.
double getSelfVariable(const std::string &scope, const std::string &name) const
Returns the self variable.
Definition GameState.cpp:78
void setSelfString(const std::string &scope, const std::string &name, std::string value)
Scoped string facts (for example a .dnut story cursor JSON blob).
Definition GameState.cpp:91
bool hasSelfVariable(const std::string &scope, const std::string &name) const
True when self variable.
Definition GameState.cpp:85
std::string getSelfString(const std::string &scope, const std::string &name) const
Return a scoped string fact, or an empty string when absent.
Definition GameState.cpp:95
void switchOff(const std::string &name)
Switch off.
Definition GameState.cpp:56
void setVariable(const std::string &name, double value)
设置/读取/增减变量。
Definition GameState.cpp:63
void clear()
清空全部状态。
void setSwitch(const std::string &name, bool on)
设置开关。
Definition GameState.cpp:52
bool hasSelfString(const std::string &scope, const std::string &name) const
Whether a scoped string fact is present.
void setSelfVariable(const std::string &scope, const std::string &name, double value)
独立变量:scope(如 "map:1:event:3")下按名字存数字。
Definition GameState.cpp:74
bool isSwitchOn(const std::string &name) const
开关是否打开。
Definition GameState.cpp:58
RPG 模块的基础实体:把属性 / 状态 / 技能三张表挂到一个 ECS 实体上。