载入中...
搜索中...
未找到
ConditionKeys.h
浏览该文件的文档.
1#pragma once
2#include "common/Export.h"
3
12#include "common/Condition.h"
13
14#include <optional>
15#include <string>
16#include <string_view>
17
18namespace eve::condition {
19
21inline constexpr std::string_view kTagPrefix = "tag:";
23inline constexpr std::string_view kAttrPrefix = "attr:";
25inline constexpr std::string_view kResourcePrefix = "resource:";
27inline constexpr std::string_view kStatePrefix = "state:";
29inline constexpr std::string_view kAuthorityPrefix = "authority:";
31inline constexpr std::string_view kPolicyPrefix = "policy:";
33inline constexpr std::string_view kAuthorityAllowPolicy = "authority.allow";
34
39[[nodiscard]] inline std::string tagKey(std::string_view tag) {
40 std::string key(kTagPrefix);
41 key.append(tag);
42 return key;
43}
44
49[[nodiscard]] inline std::string attrKey(std::string_view name) {
50 std::string key(kAttrPrefix);
51 key.append(name);
52 return key;
53}
54
59[[nodiscard]] inline std::string resourceKey(std::string_view name) {
60 std::string key(kResourcePrefix);
61 key.append(name);
62 return key;
63}
64
69[[nodiscard]] inline std::string stateKey(std::string_view name) {
70 std::string key(kStatePrefix);
71 key.append(name);
72 return key;
73}
74
79[[nodiscard]] inline std::string authorityKey(std::string_view scope) {
80 std::string key(kAuthorityPrefix);
81 key.append(scope);
82 return key;
83}
84
89[[nodiscard]] inline std::optional<std::string_view> stripPrefix(std::string_view key, std::string_view prefix) {
90 if (key.size() <= prefix.size() || key.compare(0, prefix.size(), prefix) != 0) return std::nullopt;
91 return key.substr(prefix.size());
92}
93
98[[nodiscard]] inline Condition tagPresent(std::string tag) {
99 return Condition::compare(tagKey(std::move(tag)), CompareOperator::Equal, Value(true));
100}
101
106[[nodiscard]] inline Condition attributeExists(std::string name) {
108}
109
114[[nodiscard]] inline Condition resourceExists(std::string name) {
116}
117
123[[nodiscard]] inline Condition stateEquals(std::string name, Value expected) {
124 return Condition::compare(stateKey(std::move(name)), CompareOperator::Equal, std::move(expected));
125}
126
131[[nodiscard]] inline Condition authorityAllow(std::string scope) {
132 ScriptConditionDeclaration declaration;
133 declaration.name = std::string(kAuthorityAllowPolicy);
134 declaration.dependencies = {authorityKey(scope)};
136 return Condition::policyCall(std::string(kAuthorityAllowPolicy), Value::Object{{"scope", Value(scope)}},
137 std::move(declaration));
138}
139
145[[nodiscard]] EVENGINE_API_FOUNDATION std::optional<std::string> authorityAllowScope(const Value& arguments);
146
157template <class AuthorityQuery>
158[[nodiscard]] std::optional<ConditionResult> resolveAuthorityAllow(std::string_view name, const Value& arguments,
159 AuthorityQuery&& authorityQuery) {
160 if (name != kAuthorityAllowPolicy) return std::nullopt;
161 auto scope = authorityAllowScope(arguments);
162 if (!scope) {
164 Value::Object{{"policy", Value(std::string(kAuthorityAllowPolicy))},
165 {"error", Value("authority.allow requires string scope")}});
166 }
167 auto allowed = authorityQuery(std::string_view(*scope));
168 if (!allowed) {
170 Value::Object{{"scope", Value(*scope)}});
171 }
172 if (*allowed) {
173 return ConditionResult::success(Value(true), Value::Object{{"scope", Value(*scope)}});
174 }
176 Value::Object{{"scope", Value(*scope)}});
177}
178
179} // namespace eve::condition
Pure, explainable condition trees shared by gameplay domains.
SQObjectPtr key
Definition ECS.cpp:155
std::string name
Definition ECS.cpp:154
#define EVENGINE_API_FOUNDATION
每个链接组(link group)各自的导出宏。
Definition Export.h:106
JobScope scope
const std::string * tag
The canonical owning dynamic value used by data-facing protocols.
Definition Value.h:31
std::map< std::string, Value > Object
Definition Value.h:34
static ConditionResult success(Value evidence={}, Value details=Value::Object{})
Construct a passed result with optional evidence and details.
static ConditionResult failed(ConditionReasonCode reason, Value evidence={}, Value details=Value::Object{})
Construct a rejected result with a stable reason and explanation.
Immutable condition AST node.
Definition Condition.h:199
static Condition compare(std::string key, CompareOperator op, Value expected={})
Compare a context value against an expected scalar value.
static Condition policyCall(std::string name, Value arguments=Value::Object{}, std::optional< ScriptConditionDeclaration > scriptDeclaration=std::nullopt)
Call a named read-only policy and use its ConditionResult.
constexpr std::string_view kAuthorityPrefix
Prefix for authority scopes used as watch/dependency keys.
Condition stateEquals(std::string name, Value expected)
Compare that a state key equals an expected value.
std::string resourceKey(std::string_view name)
Build a resource:<name> Compare key.
Condition attributeExists(std::string name)
Compare that an attribute key exists (attr:<name> Exists).
constexpr std::string_view kResourcePrefix
Prefix for resource values (resource:<name>).
std::string authorityKey(std::string_view scope)
Build an authority:<scope> dependency/watch key.
constexpr std::string_view kPolicyPrefix
Prefix for policy watch keys (policy:<name>).
constexpr std::string_view kStatePrefix
Prefix for state values (state:<name>).
constexpr std::string_view kTagPrefix
Prefix for tag membership booleans (tag:<name>).
std::optional< ConditionResult > resolveAuthorityAllow(std::string_view name, const Value &arguments, AuthorityQuery &&authorityQuery)
Resolve the built-in authority.allow policy when name matches.
Condition tagPresent(std::string tag)
Compare that a tag key is present (tag:<name> == true).
std::string stateKey(std::string_view name)
Build a state:<name> Compare key.
@ BitExact
Same inputs produce the same bits without an external clock.
Condition resourceExists(std::string name)
Compare that a resource key exists (resource:<name> Exists).
std::optional< std::string_view > stripPrefix(std::string_view key, std::string_view prefix)
Strip a known domain prefix from a key.
constexpr std::string_view kAttrPrefix
Prefix for attribute values (attr:<name>).
std::string attrKey(std::string_view name)
Build an attr:<name> Compare key.
@ Exists
Passes when the keyed value is present; expected is ignored.
Condition authorityAllow(std::string scope)
PolicyCall for read-only authority (authority.allow).
std::optional< std::string > authorityAllowScope(const Value &arguments)
Extract the scope string from an authority.allow argument object.
constexpr std::string_view kAuthorityAllowPolicy
PolicyCall name for read-only authority checks.
std::string tagKey(std::string_view tag)
Build a tag:<name> Compare key.
Metadata a script policy must declare before it is used by a condition.
Definition Condition.h:65
std::string name
Stable script policy name.
Definition Condition.h:67
std::vector< std::string > dependencies
Logical state/query names read by the policy.
Definition Condition.h:69
DeterminismLevel determinism
Replay/network determinism contract.
Definition Condition.h:71