载入中...
搜索中...
未找到
Social.cpp
浏览该文件的文档.
1#include "social/Social.h"
2
3#include <simplesquirrel/simplesquirrel.hpp>
4
5namespace eve::social {
6namespace {
7
8std::string at(const std::vector<EntityId>& ids, int index) {
9 return index < 0 || static_cast<std::size_t>(index) >= ids.size() ? std::string{} : ids[index];
10}
11
12const SocialChangeEvent* eventAt(const SocialGraph& graph, int index) {
13 const auto& events = graph.events();
14 return index < 0 || static_cast<std::size_t>(index) >= events.size() ? nullptr : &events[index];
15}
16
17} // namespace
18
20
21std::string Social::idFromInt(int id) const { return SocialGraph::integerId(id); }
22
23bool Social::setOwner(const std::string& entity, const std::string& owner) { return graph_.setOwner(entity, owner); }
24bool Social::setOwnerInt(int entity, int owner) {
25 return graph_.setOwner(SocialGraph::integerId(entity), SocialGraph::integerId(owner));
26}
27std::string Social::ownerOf(const std::string& entity) const { return graph_.ownerOf(entity); }
28std::string Social::ownerOfInt(int entity) const { return graph_.ownerOf(SocialGraph::integerId(entity)); }
29int Social::ownedCount(const std::string& owner) const { return static_cast<int>(graph_.ownedBy(owner).size()); }
30std::string Social::ownedAt(const std::string& owner, int index) const { return at(graph_.ownedBy(owner), index); }
31
32bool Social::setController(const std::string& entity, const std::string& controller) {
33 return graph_.setController(entity, controller);
34}
35bool Social::setControllerInt(int entity, int controller) {
36 return graph_.setController(SocialGraph::integerId(entity), SocialGraph::integerId(controller));
37}
38std::string Social::controllerOf(const std::string& entity) const { return graph_.controllerOf(entity); }
39std::string Social::controllerOfInt(int entity) const { return graph_.controllerOf(SocialGraph::integerId(entity)); }
40int Social::controlledCount(const std::string& controller) const {
41 return static_cast<int>(graph_.controlledBy(controller).size());
42}
43std::string Social::controlledAt(const std::string& controller, int index) const {
44 return at(graph_.controlledBy(controller), index);
45}
46
47bool Social::assign(const std::string& source, const std::string& role, const std::string& target) {
48 return graph_.assign(source, role, target);
49}
50bool Social::assignInt(int source, const std::string& role, int target) {
52}
53bool Social::unassign(const std::string& source, const std::string& role, const std::string& target) {
54 return graph_.unassign(source, role, target);
55}
56bool Social::isAssigned(const std::string& source, const std::string& role, const std::string& target) const {
57 return graph_.isAssigned(source, role, target);
58}
59int Social::targetCount(const std::string& source, const std::string& role) const {
60 return static_cast<int>(graph_.targetsOf(source, role).size());
61}
62std::string Social::targetAt(const std::string& source, const std::string& role, int index) const {
63 return at(graph_.targetsOf(source, role), index);
64}
65int Social::assigneeCount(const std::string& target, const std::string& role) const {
66 return static_cast<int>(graph_.assigneesOf(target, role).size());
67}
68std::string Social::assigneeAt(const std::string& target, const std::string& role, int index) const {
69 return at(graph_.assigneesOf(target, role), index);
70}
71
72bool Social::setRelation(const std::string& source, const std::string& target, const std::string& type, float value) {
73 return graph_.setRelation(source, target, type, value);
74}
75bool Social::setRelationInt(int source, int target, const std::string& type, float value) {
77}
78float Social::addRelation(const std::string& source, const std::string& target, const std::string& type, float delta) {
79 return static_cast<float>(graph_.addRelation(source, target, type, delta));
80}
81bool Social::link(const std::string& source, const std::string& target, const std::string& type) {
82 return graph_.link(source, target, type);
83}
84bool Social::removeRelation(const std::string& source, const std::string& target, const std::string& type) {
85 return graph_.removeRelation(source, target, type);
86}
87bool Social::hasRelation(const std::string& source, const std::string& target, const std::string& type) const {
88 return graph_.hasRelation(source, target, type);
89}
90float Social::relation(const std::string& source, const std::string& target, const std::string& type,
91 float fallback) const {
92 return static_cast<float>(graph_.relation(source, target, type, fallback));
93}
94int Social::relationTargetCount(const std::string& source, const std::string& type, float minimum) const {
95 return static_cast<int>(graph_.relationTargets(source, type, minimum).size());
96}
97std::string Social::relationTargetAt(const std::string& source, const std::string& type, float minimum,
98 int index) const {
99 return at(graph_.relationTargets(source, type, minimum), index);
100}
101int Social::relationSourceCount(const std::string& target, const std::string& type, float minimum) const {
102 return static_cast<int>(graph_.relationSources(target, type, minimum).size());
103}
104std::string Social::relationSourceAt(const std::string& target, const std::string& type, float minimum,
105 int index) const {
106 return at(graph_.relationSources(target, type, minimum), index);
107}
108
109bool Social::removeEntity(const std::string& entity) { return graph_.removeEntity(entity); }
110bool Social::removeEntityInt(int entity) { return graph_.removeEntity(SocialGraph::integerId(entity)); }
111void Social::clear() { graph_.clear(); }
112
113int Social::eventCount() const { return static_cast<int>(graph_.events().size()); }
115 const auto* event = eventAt(graph_, index);
116 return event ? static_cast<int>(event->sequence) : 0;
117}
118std::string Social::eventAction(int index) const {
119 const auto* event = eventAt(graph_, index);
120 return event ? event->action : std::string{};
121}
122std::string Social::eventSource(int index) const {
123 const auto* event = eventAt(graph_, index);
124 return event ? event->source : std::string{};
125}
126std::string Social::eventTarget(int index) const {
127 const auto* event = eventAt(graph_, index);
128 return event ? event->target : std::string{};
129}
130std::string Social::eventType(int index) const {
131 const auto* event = eventAt(graph_, index);
132 return event ? event->type : std::string{};
133}
134float Social::eventOldValue(int index) const {
135 const auto* event = eventAt(graph_, index);
136 return event ? static_cast<float>(event->oldValue) : 0.0F;
137}
138float Social::eventNewValue(int index) const {
139 const auto* event = eventAt(graph_, index);
140 return event ? static_cast<float>(event->newValue) : 0.0F;
141}
143
144void Social::expose(ssq::Table& table) {
145 auto cls = table.addClass(name, Social::create, false);
146 expose(cls);
147}
148
149void Social::expose(ssq::Class& cls) {
150 cls.addFunc("getName", &Social::getName);
151 cls.addFunc("idFromInt", &Social::idFromInt);
152 cls.addFunc("setOwner", &Social::setOwner);
153 cls.addFunc("setOwnerInt", &Social::setOwnerInt);
154 cls.addFunc("ownerOf", &Social::ownerOf);
155 cls.addFunc("ownerOfInt", &Social::ownerOfInt);
156 cls.addFunc("ownedCount", &Social::ownedCount);
157 cls.addFunc("ownedAt", &Social::ownedAt);
158 cls.addFunc("setController", &Social::setController);
159 cls.addFunc("setControllerInt", &Social::setControllerInt);
160 cls.addFunc("controllerOf", &Social::controllerOf);
161 cls.addFunc("controllerOfInt", &Social::controllerOfInt);
162 cls.addFunc("controlledCount", &Social::controlledCount);
163 cls.addFunc("controlledAt", &Social::controlledAt);
164 cls.addFunc("assign", &Social::assign);
165 cls.addFunc("assignInt", &Social::assignInt);
166 cls.addFunc("unassign", &Social::unassign);
167 cls.addFunc("isAssigned", &Social::isAssigned);
168 cls.addFunc("targetCount", &Social::targetCount);
169 cls.addFunc("targetAt", &Social::targetAt);
170 cls.addFunc("assigneeCount", &Social::assigneeCount);
171 cls.addFunc("assigneeAt", &Social::assigneeAt);
172 cls.addFunc("setRelation", &Social::setRelation);
173 cls.addFunc("setRelationInt", &Social::setRelationInt);
174 cls.addFunc("addRelation", &Social::addRelation);
175 cls.addFunc("link", &Social::link);
176 cls.addFunc("removeRelation", &Social::removeRelation);
177 cls.addFunc("hasRelation", &Social::hasRelation);
178 cls.addFunc("relation", &Social::relation);
179 cls.addFunc("relationTargetCount", &Social::relationTargetCount);
180 cls.addFunc("relationTargetAt", &Social::relationTargetAt);
181 cls.addFunc("relationSourceCount", &Social::relationSourceCount);
182 cls.addFunc("relationSourceAt", &Social::relationSourceAt);
183 cls.addFunc("removeEntity", &Social::removeEntity);
184 cls.addFunc("removeEntityInt", &Social::removeEntityInt);
185 cls.addFunc("clear", &Social::clear);
186 cls.addFunc("eventCount", &Social::eventCount);
187 cls.addFunc("eventSequence", &Social::eventSequence);
188 cls.addFunc("eventAction", &Social::eventAction);
189 cls.addFunc("eventSource", &Social::eventSource);
190 cls.addFunc("eventTarget", &Social::eventTarget);
191 cls.addFunc("eventType", &Social::eventType);
192 cls.addFunc("eventOldValue", &Social::eventOldValue);
193 cls.addFunc("eventNewValue", &Social::eventNewValue);
194 cls.addFunc("clearEvents", &Social::clearEvents);
195}
196
197} // namespace eve::social
LogicalId target
double value
HSQOBJECT cls
Definition ECS.cpp:21
float minimum[3]
glm::uvec4 ids
std::string name
#define Module_IMPL(ModuleName, newExpr)
Definition Module.h:26
std::map< std::string, std::vector< std::string > > graph
Definition Package.cpp:59
Battle::Events events
uint32_t index
const UnitySourceAsset & source
std::size_t at
virtual std::string getName() const =0
Returns the name.
bool assign(const EntityId &source, const std::string &role, const EntityId &target)
Adds a role-based source-to-target assignment.
EntityId controllerOf(const EntityId &entity) const
Returns an entity's controller, or an empty string when absent.
bool link(const EntityId &source, const EntityId &target, const std::string &type)
Creates a directed typed relation with weight 1.
std::vector< EntityId > ownedBy(const EntityId &owner) const
Returns entities owned by owner in deterministic ID order.
bool setOwner(const EntityId &entity, const EntityId &owner)
Sets an entity's owner; an empty owner removes ownership.
static EntityId integerId(std::int64_t id)
Converts a stable integer ID to its canonical graph key.
double addRelation(const EntityId &source, const EntityId &target, const std::string &type, double delta)
Adds delta to a directed typed relation and returns its new weight.
void clearEvents()
Clears queued events without changing graph state.
Definition SocialGraph.h:90
std::vector< EntityId > controlledBy(const EntityId &controller) const
Returns entities controlled by controller in deterministic ID order.
bool setRelation(const EntityId &source, const EntityId &target, const std::string &type, double value)
Sets a directed typed relation weight, creating the edge if needed.
bool unassign(const EntityId &source, const std::string &role, const EntityId &target)
Removes a role-based source-to-target assignment.
bool setController(const EntityId &entity, const EntityId &controller)
Sets an entity's controller; an empty controller removes control.
std::vector< EntityId > targetsOf(const EntityId &source, const std::string &role) const
Returns targets assigned from source with role in deterministic order.
bool removeEntity(const EntityId &entity)
Removes an entity and every incoming/outgoing graph reference to it.
std::vector< EntityId > assigneesOf(const EntityId &target, const std::string &role) const
Returns sources assigned to target with role in deterministic order.
EntityId ownerOf(const EntityId &entity) const
Returns an entity's owner, or an empty string when absent.
bool removeRelation(const EntityId &source, const EntityId &target, const std::string &type)
Removes an exact directed typed relation.
void clear()
Clears all graph state and pending events.
bool isAssigned(const EntityId &source, const std::string &role, const EntityId &target) const
Tests whether an exact assignment exists.
double relation(const EntityId &source, const EntityId &target, const std::string &type, double fallback=0.0) const
Gets an exact relation weight, or fallback when absent.
std::vector< EntityId > relationTargets(const EntityId &source, const std::string &type, double minimum=0.0) const
Returns outgoing relation targets filtered by type and minimum weight.
bool hasRelation(const EntityId &source, const EntityId &target, const std::string &type) const
Tests whether an exact directed typed relation exists.
const std::vector< SocialChangeEvent > & events() const
Returns queued mutation events in sequence order.
Definition SocialGraph.h:88
std::vector< EntityId > relationSources(const EntityId &target, const std::string &type, double minimum=0.0) const
Returns incoming relation sources filtered by type and minimum weight.
Script-facing module for generic social graph composition.
Definition Social.h:11
bool removeEntityInt(int entity)
Removes an integer-ID entity and every graph reference to it.
Definition Social.cpp:110
std::string controllerOfInt(int entity) const
Gets controller of an integer ID as a canonical key.
Definition Social.cpp:39
std::string relationSourceAt(const std::string &target, const std::string &type, float minimum, int index) const
Gets incoming relation source by deterministic filtered index.
Definition Social.cpp:104
int ownedCount(const std::string &owner) const
Gets count of deterministically ordered entities owned by owner.
Definition Social.cpp:29
bool unassign(const std::string &source, const std::string &role, const std::string &target)
Removes an exact assignment.
Definition Social.cpp:53
int relationTargetCount(const std::string &source, const std::string &type, float minimum) const
Gets outgoing relation target count after threshold filtering.
Definition Social.cpp:94
std::string relationTargetAt(const std::string &source, const std::string &type, float minimum, int index) const
Gets outgoing relation target by deterministic filtered index.
Definition Social.cpp:97
int eventCount() const
Gets queued change event count.
Definition Social.cpp:113
float eventNewValue(int index) const
Gets event new numeric value by index.
Definition Social.cpp:138
std::string ownerOf(const std::string &entity) const
Gets owner of a string ID.
Definition Social.cpp:27
bool assign(const std::string &source, const std::string &role, const std::string &target)
Adds a role-based assignment between string IDs.
Definition Social.cpp:47
std::string targetAt(const std::string &source, const std::string &role, int index) const
Gets an assignment target by deterministic index.
Definition Social.cpp:62
void clearEvents()
Clears queued events only.
Definition Social.cpp:142
std::string controllerOf(const std::string &entity) const
Gets controller of a string ID.
Definition Social.cpp:38
bool setController(const std::string &entity, const std::string &controller)
Sets control between string IDs.
Definition Social.cpp:32
bool setRelation(const std::string &source, const std::string &target, const std::string &type, float value)
Sets a directed typed relation between string IDs.
Definition Social.cpp:72
bool setRelationInt(int source, int target, const std::string &type, float value)
Sets a directed typed relation between integer IDs.
Definition Social.cpp:75
float eventOldValue(int index) const
Gets event old numeric value by index.
Definition Social.cpp:134
int eventSequence(int index) const
Gets event sequence by index, or zero when out of range.
Definition Social.cpp:114
std::string controlledAt(const std::string &controller, int index) const
Gets a controlled entity by deterministic index.
Definition Social.cpp:43
bool removeEntity(const std::string &entity)
Removes a string-ID entity and every graph reference to it.
Definition Social.cpp:109
int targetCount(const std::string &source, const std::string &role) const
Gets target count for source and role.
Definition Social.cpp:59
bool isAssigned(const std::string &source, const std::string &role, const std::string &target) const
Tests an exact assignment.
Definition Social.cpp:56
bool setOwnerInt(int entity, int owner)
Sets ownership between integer IDs.
Definition Social.cpp:24
bool hasRelation(const std::string &source, const std::string &target, const std::string &type) const
Tests an exact relation.
Definition Social.cpp:87
std::string eventType(int index) const
Gets event type/role by index.
Definition Social.cpp:130
std::string eventSource(int index) const
Gets event source by index.
Definition Social.cpp:122
bool removeRelation(const std::string &source, const std::string &target, const std::string &type)
Removes an exact relation.
Definition Social.cpp:84
int assigneeCount(const std::string &target, const std::string &role) const
Gets assignee count for target and role.
Definition Social.cpp:65
int relationSourceCount(const std::string &target, const std::string &type, float minimum) const
Gets incoming relation source count after threshold filtering.
Definition Social.cpp:101
int controlledCount(const std::string &controller) const
Gets count of deterministically ordered controlled entities.
Definition Social.cpp:40
std::string assigneeAt(const std::string &target, const std::string &role, int index) const
Gets an assignee by deterministic index.
Definition Social.cpp:68
bool assignInt(int source, const std::string &role, int target)
Adds a role-based assignment between integer IDs.
Definition Social.cpp:50
std::string eventTarget(int index) const
Gets event target by index.
Definition Social.cpp:126
std::string ownedAt(const std::string &owner, int index) const
Gets an owned entity by deterministic index.
Definition Social.cpp:30
bool setControllerInt(int entity, int controller)
Sets control between integer IDs.
Definition Social.cpp:35
std::string idFromInt(int id) const
Returns the canonical graph key for an integer entity ID.
Definition Social.cpp:21
std::string eventAction(int index) const
Gets event action by index.
Definition Social.cpp:118
float relation(const std::string &source, const std::string &target, const std::string &type, float fallback) const
Gets relation value or fallback.
Definition Social.cpp:90
std::string ownerOfInt(int entity) const
Gets owner of an integer ID as a canonical key.
Definition Social.cpp:28
bool setOwner(const std::string &entity, const std::string &owner)
Sets ownership between string IDs.
Definition Social.cpp:23
float addRelation(const std::string &source, const std::string &target, const std::string &type, float delta)
Adds to a relation and returns its new value.
Definition Social.cpp:78
void clear()
Clears all graph state and events.
Definition Social.cpp:111
bool link(const std::string &source, const std::string &target, const std::string &type)
Creates a weight-one directed typed relation.
Definition Social.cpp:81