载入中...
搜索中...
未找到
Roster.cpp
浏览该文件的文档.
1#include "roster/Roster.h"
2
3#include "common/Diagnostic.h"
4
5#include <algorithm>
6#include <utility>
7
8namespace eve::roster {
9namespace {
10
11bool emptyId(std::string_view value) { return value.empty(); }
12
13template <class Container>
14auto findOccupant(Container& items, std::string_view subject) {
15 return std::find_if(items.begin(), items.end(), [&](const auto& item) { return item.subject == subject; });
16}
17
18} // namespace
19
20Roster::Roster() = default;
21Roster::~Roster() = default;
22
24
25Result<void> Roster::fail(DiagnosticCode code, std::string message, std::string path) const {
26 return Result<void>::failure(Diagnostic::error(code, std::move(message), std::move(path), {}, "roster"));
27}
28
29template <class T>
30Result<T> Roster::failT(DiagnosticCode code, std::string message, std::string path) const {
31 return Result<T>::failure(Diagnostic::error(code, std::move(message), std::move(path), {}, "roster"));
32}
33
35 posts_.clear();
36 squads_.clear();
37 policies_.clear();
38 vacancies_.clear();
39 spawnRequests_.clear();
40 nextRequestSerial_ = 1;
41}
42
43void Roster::pushVacancy(std::string kind, std::string targetId, std::string role, std::string lostSubject,
44 std::uint64_t tick) {
45 vacancies_.push_back(Vacancy{std::move(kind), std::move(targetId), std::move(role), std::move(lostSubject), tick});
46}
47
48Result<void> Roster::registerPost(std::string postId, int capacity, std::string defaultRole) {
49 if (emptyId(postId) || capacity < 1 || emptyId(defaultRole))
50 return fail(DiagnosticCode::InvalidArgument, "post id, capacity>=1 and defaultRole are required", "post");
51 if (posts_.contains(postId)) return fail(DiagnosticCode::AlreadyExists, "post id already registered", "postId");
52 posts_.emplace(std::move(postId), Post{capacity, std::move(defaultRole), {}});
54}
55
56Result<void> Roster::removePost(std::string_view postId, std::uint64_t tick) {
57 auto it = posts_.find(std::string(postId));
58 if (it == posts_.end()) return fail(DiagnosticCode::NotFound, "post was not found", "postId");
59 for (auto& occupant : it->second.occupants) pushVacancy("post", it->first, occupant.role, occupant.subject, tick);
60 posts_.erase(it);
62}
63
64Result<void> Roster::assignPost(std::string_view postId, std::string subject, std::string role) {
65 if (emptyId(postId) || emptyId(subject))
66 return fail(DiagnosticCode::InvalidArgument, "post id and subject are required", "assignPost");
67 auto it = posts_.find(std::string(postId));
68 if (it == posts_.end()) return fail(DiagnosticCode::NotFound, "post was not found", "postId");
69 for (const auto& [id, post] : posts_) {
70 (void)id;
71 if (findOccupant(post.occupants, subject) != post.occupants.end())
72 return fail(DiagnosticCode::Conflict, "subject already occupies a post", "subject");
73 }
74 if (static_cast<int>(it->second.occupants.size()) >= it->second.capacity)
75 return fail(DiagnosticCode::Conflict, "post is at capacity", "postId");
76 if (role.empty()) role = it->second.defaultRole;
77 it->second.occupants.push_back(Occupant{std::move(subject), std::move(role)});
78 std::sort(it->second.occupants.begin(), it->second.occupants.end(),
79 [](const Occupant& a, const Occupant& b) { return a.subject < b.subject; });
81}
82
83Result<void> Roster::releasePost(std::string_view postId, std::string_view subject, std::uint64_t tick) {
84 auto it = posts_.find(std::string(postId));
85 if (it == posts_.end()) return fail(DiagnosticCode::NotFound, "post was not found", "postId");
86 auto occupant = findOccupant(it->second.occupants, subject);
87 if (occupant == it->second.occupants.end())
88 return fail(DiagnosticCode::NotFound, "subject is not assigned to this post", "subject");
89 pushVacancy("post", it->first, occupant->role, occupant->subject, tick);
90 it->second.occupants.erase(occupant);
92}
93
94Result<int> Roster::postOccupancy(std::string_view postId) const {
95 auto it = posts_.find(std::string(postId));
96 if (it == posts_.end()) return failT<int>(DiagnosticCode::NotFound, "post was not found", "postId");
97 return Result<int>::success(static_cast<int>(it->second.occupants.size()));
98}
99
100Result<int> Roster::postCapacity(std::string_view postId) const {
101 auto it = posts_.find(std::string(postId));
102 if (it == posts_.end()) return failT<int>(DiagnosticCode::NotFound, "post was not found", "postId");
103 return Result<int>::success(it->second.capacity);
104}
105
106Result<std::string> Roster::postAssigneeAt(std::string_view postId, int index) const {
107 auto it = posts_.find(std::string(postId));
108 if (it == posts_.end()) return failT<std::string>(DiagnosticCode::NotFound, "post was not found", "postId");
109 if (index < 0 || static_cast<std::size_t>(index) >= it->second.occupants.size())
110 return failT<std::string>(DiagnosticCode::InvalidArgument, "assignee index out of range", "index");
111 return Result<std::string>::success(it->second.occupants[static_cast<std::size_t>(index)].subject);
112}
113
114Result<std::string> Roster::postRole(std::string_view postId, std::string_view subject) const {
115 auto it = posts_.find(std::string(postId));
116 if (it == posts_.end()) return failT<std::string>(DiagnosticCode::NotFound, "post was not found", "postId");
117 auto occupant = findOccupant(it->second.occupants, subject);
118 if (occupant == it->second.occupants.end())
119 return failT<std::string>(DiagnosticCode::NotFound, "subject is not assigned to this post", "subject");
120 return Result<std::string>::success(occupant->role);
121}
122
123Result<void> Roster::createSquad(std::string squadId, int capacity, std::string intent) {
124 if (emptyId(squadId) || capacity < 1)
125 return fail(DiagnosticCode::InvalidArgument, "squad id and capacity>=1 are required", "squad");
126 if (squads_.contains(squadId)) return fail(DiagnosticCode::AlreadyExists, "squad id already registered", "squadId");
127 squads_.emplace(std::move(squadId), Squad{capacity, std::move(intent), {}});
129}
130
131Result<void> Roster::destroySquad(std::string_view squadId, std::uint64_t tick) {
132 auto it = squads_.find(std::string(squadId));
133 if (it == squads_.end()) return fail(DiagnosticCode::NotFound, "squad was not found", "squadId");
134 for (auto& member : it->second.members) pushVacancy("squad", it->first, member.role, member.subject, tick);
135 squads_.erase(it);
137}
138
139Result<void> Roster::addMember(std::string_view squadId, std::string subject, std::string role) {
140 if (emptyId(squadId) || emptyId(subject) || emptyId(role))
141 return fail(DiagnosticCode::InvalidArgument, "squad id, subject and role are required", "addMember");
142 auto it = squads_.find(std::string(squadId));
143 if (it == squads_.end()) return fail(DiagnosticCode::NotFound, "squad was not found", "squadId");
144 if (findOccupant(it->second.members, subject) != it->second.members.end())
145 return fail(DiagnosticCode::Conflict, "subject is already a squad member", "subject");
146 if (static_cast<int>(it->second.members.size()) >= it->second.capacity)
147 return fail(DiagnosticCode::Conflict, "squad is at capacity", "squadId");
148 it->second.members.push_back(Occupant{std::move(subject), std::move(role)});
149 std::sort(it->second.members.begin(), it->second.members.end(),
150 [](const Occupant& a, const Occupant& b) { return a.subject < b.subject; });
152}
153
154Result<void> Roster::removeMember(std::string_view squadId, std::string_view subject, std::uint64_t tick) {
155 auto it = squads_.find(std::string(squadId));
156 if (it == squads_.end()) return fail(DiagnosticCode::NotFound, "squad was not found", "squadId");
157 auto member = findOccupant(it->second.members, subject);
158 if (member == it->second.members.end())
159 return fail(DiagnosticCode::NotFound, "subject is not a squad member", "subject");
160 pushVacancy("squad", it->first, member->role, member->subject, tick);
161 it->second.members.erase(member);
163}
164
165Result<void> Roster::setIntent(std::string_view squadId, std::string intent) {
166 auto it = squads_.find(std::string(squadId));
167 if (it == squads_.end()) return fail(DiagnosticCode::NotFound, "squad was not found", "squadId");
168 it->second.intent = std::move(intent);
170}
171
172Result<std::string> Roster::intent(std::string_view squadId) const {
173 auto it = squads_.find(std::string(squadId));
174 if (it == squads_.end()) return failT<std::string>(DiagnosticCode::NotFound, "squad was not found", "squadId");
175 return Result<std::string>::success(it->second.intent);
176}
177
178Result<int> Roster::memberCount(std::string_view squadId) const {
179 auto it = squads_.find(std::string(squadId));
180 if (it == squads_.end()) return failT<int>(DiagnosticCode::NotFound, "squad was not found", "squadId");
181 return Result<int>::success(static_cast<int>(it->second.members.size()));
182}
183
184Result<std::string> Roster::memberAt(std::string_view squadId, int index) const {
185 auto it = squads_.find(std::string(squadId));
186 if (it == squads_.end()) return failT<std::string>(DiagnosticCode::NotFound, "squad was not found", "squadId");
187 if (index < 0 || static_cast<std::size_t>(index) >= it->second.members.size())
188 return failT<std::string>(DiagnosticCode::InvalidArgument, "member index out of range", "index");
189 return Result<std::string>::success(it->second.members[static_cast<std::size_t>(index)].subject);
190}
191
192Result<std::string> Roster::memberRole(std::string_view squadId, std::string_view subject) const {
193 auto it = squads_.find(std::string(squadId));
194 if (it == squads_.end()) return failT<std::string>(DiagnosticCode::NotFound, "squad was not found", "squadId");
195 auto member = findOccupant(it->second.members, subject);
196 if (member == it->second.members.end())
197 return failT<std::string>(DiagnosticCode::NotFound, "subject is not a squad member", "subject");
199}
200
201Result<int> Roster::releaseSubject(std::string_view subject, std::uint64_t tick) {
202 if (emptyId(subject)) return failT<int>(DiagnosticCode::InvalidArgument, "subject is required", "subject");
203 int released = 0;
204 for (auto& [postId, post] : posts_) {
205 auto occupant = findOccupant(post.occupants, subject);
206 if (occupant == post.occupants.end()) continue;
207 pushVacancy("post", postId, occupant->role, occupant->subject, tick);
208 post.occupants.erase(occupant);
209 ++released;
210 }
211 for (auto& [squadId, squad] : squads_) {
212 auto member = findOccupant(squad.members, subject);
213 if (member == squad.members.end()) continue;
214 pushVacancy("squad", squadId, member->role, member->subject, tick);
215 squad.members.erase(member);
216 ++released;
217 }
219}
220
221int Roster::vacancyCount() const { return static_cast<int>(vacancies_.size()); }
222
224 if (index < 0 || static_cast<std::size_t>(index) >= vacancies_.size())
225 return failT<Vacancy>(DiagnosticCode::InvalidArgument, "vacancy index out of range", "index");
226 return Result<Vacancy>::success(vacancies_[static_cast<std::size_t>(index)]);
227}
228
229void Roster::clearVacancies() { vacancies_.clear(); }
230
231Result<void> Roster::setPolicy(std::string policyId, std::string preferredDef, int delayTicks, int budget,
232 std::string postId, std::string squadId, std::string role) {
233 if (emptyId(policyId) || emptyId(preferredDef) || delayTicks < 0 || budget < 0)
235 "policy id, preferredDef, non-negative delayTicks and budget are required", "policy");
236 if (!postId.empty() && !squadId.empty())
237 return fail(DiagnosticCode::InvalidArgument, "policy may bind a post or a squad, not both", "policy");
238 auto& policy = policies_[policyId];
239 policy.preferredDef = std::move(preferredDef);
240 policy.delayTicks = delayTicks;
241 policy.budget = budget;
242 policy.postId = std::move(postId);
243 policy.squadId = std::move(squadId);
244 policy.role = std::move(role);
245 // Keep spent/fallbacks across upsert so live budgets do not reset silently.
247}
248
249Result<void> Roster::setFallback(std::string_view policyId, std::string preferredDef, std::string fallbackDef) {
250 auto it = policies_.find(std::string(policyId));
251 if (it == policies_.end()) return fail(DiagnosticCode::NotFound, "policy was not found", "policyId");
252 if (emptyId(preferredDef) || emptyId(fallbackDef) || preferredDef == fallbackDef)
253 return fail(DiagnosticCode::InvalidArgument, "fallback needs distinct preferred and fallback defs", "fallback");
254 // Reject one-step cycles; longer cycles are detected when failing a request.
255 if (it->second.fallbacks[fallbackDef] == preferredDef)
256 return fail(DiagnosticCode::Conflict, "fallback chain contains a cycle", "fallback");
257 it->second.fallbacks[std::move(preferredDef)] = std::move(fallbackDef);
259}
260
262 auto it = policies_.find(std::string(policyId));
263 if (it == policies_.end()) return fail(DiagnosticCode::NotFound, "policy was not found", "policyId");
264 policies_.erase(it);
266}
267
268bool Roster::policyMatches(const Policy& policy, const Vacancy& vacancy) const {
269 if (!policy.postId.empty() && (vacancy.kind != "post" || vacancy.targetId != policy.postId)) return false;
270 if (!policy.squadId.empty() && (vacancy.kind != "squad" || vacancy.targetId != policy.squadId)) return false;
271 if (!policy.role.empty() && vacancy.role != policy.role) return false;
272 return true;
273}
274
275bool Roster::budgetAvailable(const Policy& policy) const { return policy.budget == 0 || policy.spent < policy.budget; }
276
277std::string Roster::nextRequestId() { return "spawn:" + std::to_string(nextRequestSerial_++); }
278
280 int created = 0;
281 std::vector<Vacancy> remaining;
282 remaining.reserve(vacancies_.size());
283 for (auto& vacancy : vacancies_) {
284 if (vacancy.tick > tick) {
285 remaining.push_back(std::move(vacancy));
286 continue;
287 }
288 bool matched = false;
289 for (auto& [policyId, policy] : policies_) {
290 if (!policyMatches(policy, vacancy)) continue;
291 if (static_cast<std::uint64_t>(policy.delayTicks) > tick - vacancy.tick) {
292 remaining.push_back(std::move(vacancy));
293 matched = true;
294 break;
295 }
296 if (!budgetAvailable(policy)) {
297 // Budget exhausted: drop this vacancy for this policy family.
298 matched = true;
299 break;
300 }
302 request.requestId = nextRequestId();
303 request.policyId = policyId;
304 request.preferredDef = policy.preferredDef;
305 request.role = vacancy.role;
306 if (vacancy.kind == "post") request.postId = vacancy.targetId;
307 if (vacancy.kind == "squad") request.squadId = vacancy.targetId;
308 request.readyAtTick = tick;
309 spawnRequests_.push_back(std::move(request));
310 ++created;
311 matched = true;
312 break;
313 }
314 if (!matched) remaining.push_back(std::move(vacancy));
315 }
316 vacancies_ = std::move(remaining);
318}
319
320int Roster::spawnRequestCount() const { return static_cast<int>(spawnRequests_.size()); }
321
323 if (index < 0 || static_cast<std::size_t>(index) >= spawnRequests_.size())
324 return failT<SpawnRequest>(DiagnosticCode::InvalidArgument, "spawn request index out of range", "index");
325 return Result<SpawnRequest>::success(spawnRequests_[static_cast<std::size_t>(index)]);
326}
327
328Result<void> Roster::eraseSpawnRequest(std::string_view requestId) {
329 auto it = std::find_if(spawnRequests_.begin(), spawnRequests_.end(),
330 [&](const SpawnRequest& request) { return request.requestId == requestId; });
331 if (it == spawnRequests_.end()) return fail(DiagnosticCode::NotFound, "spawn request was not found", "requestId");
332 spawnRequests_.erase(it);
334}
335
336Result<void> Roster::ackSpawnRequest(std::string_view requestId) {
337 auto it = std::find_if(spawnRequests_.begin(), spawnRequests_.end(),
338 [&](const SpawnRequest& request) { return request.requestId == requestId; });
339 if (it == spawnRequests_.end()) return fail(DiagnosticCode::NotFound, "spawn request was not found", "requestId");
340 auto policy = policies_.find(it->policyId);
341 if (policy != policies_.end() && policy->second.budget > 0) ++policy->second.spent;
342 spawnRequests_.erase(it);
344}
345
346Result<void> Roster::failSpawnRequest(std::string_view requestId, std::uint64_t tick) {
347 auto it = std::find_if(spawnRequests_.begin(), spawnRequests_.end(),
348 [&](const SpawnRequest& request) { return request.requestId == requestId; });
349 if (it == spawnRequests_.end()) return fail(DiagnosticCode::NotFound, "spawn request was not found", "requestId");
350 SpawnRequest failed = *it;
351 spawnRequests_.erase(it);
352 auto policy = policies_.find(failed.policyId);
353 if (policy == policies_.end()) return Result<void>::success(Status::success(StatusCode::NoOp));
354 auto fallback = policy->second.fallbacks.find(failed.preferredDef);
355 if (fallback == policy->second.fallbacks.end()) return Result<void>::success(Status::success(StatusCode::NoOp));
356 if (!budgetAvailable(policy->second)) return Result<void>::success(Status::success(StatusCode::NoOp));
357 SpawnRequest retry = failed;
358 retry.requestId = nextRequestId();
359 retry.preferredDef = fallback->second;
360 retry.readyAtTick = tick;
361 spawnRequests_.push_back(std::move(retry));
363}
364
365} // namespace eve::roster
int subject
Definition AnimSmr.cpp:163
Stable, structured diagnostics shared by engine modules.
std::string message
DiagnosticCode code
const GltfImportRequest & request
std::uint32_t capacity
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
#define Module_IMPL(ModuleName, newExpr)
Definition Module.h:27
std::string path
Definition PlayHost.cpp:111
std::uint32_t targetId
Object::Ptr value
int created
std::string kind
SimulationTick tick
std::string policyId
float(ui::Theme::* member)[4]
uint32_t index
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
static Status success(StatusCode code=StatusCode::Ok)
Construct a successful status with an explicit non-error outcome.
Definition Status.cpp:19
Authoritative squad membership, post occupancy, and reinforce spawn requests.
Definition Roster.h:46
Result< std::string > postRole(std::string_view postId, std::string_view subject) const
Role for an assignee on a post, or NotFound.
Definition Roster.cpp:114
Result< void > setPolicy(std::string policyId, std::string preferredDef, int delayTicks, int budget, std::string postId={}, std::string squadId={}, std::string role={})
Upserts a reinforce policy that turns matching vacancies into spawn requests.
Definition Roster.cpp:231
Result< void > createSquad(std::string squadId, int capacity, std::string intent={})
Creates a squad with a membership cap and optional initial intent.
Definition Roster.cpp:123
Result< std::string > postAssigneeAt(std::string_view postId, int index) const
Assignee at a stable index (sorted by subject id), or empty / failure.
Definition Roster.cpp:106
int vacancyCount() const
Queued vacancy count (stable insertion order).
Definition Roster.cpp:221
Result< void > addMember(std::string_view squadId, std::string subject, std::string role="member")
Adds a member with a role; rejects duplicates and capacity overflow.
Definition Roster.cpp:139
void clearVacancies()
Clears the vacancy queue without changing occupancy.
Definition Roster.cpp:229
Result< std::string > intent(std::string_view squadId) const
Reads the squad intent, or NotFound.
Definition Roster.cpp:172
Result< void > destroySquad(std::string_view squadId, std::uint64_t tick)
Destroys a squad and emits vacancies for every remaining member.
Definition Roster.cpp:131
Result< Vacancy > vacancyAt(int index) const
Vacancy at index, or InvalidArgument when out of range.
Definition Roster.cpp:223
Result< SpawnRequest > spawnRequestAt(int index) const
Spawn request at index, or InvalidArgument.
Definition Roster.cpp:322
Result< void > setFallback(std::string_view policyId, std::string preferredDef, std::string fallbackDef)
Adds or replaces a preferred→fallback definition edge for a policy.
Definition Roster.cpp:249
Result< int > memberCount(std::string_view squadId) const
Current member count, or NotFound.
Definition Roster.cpp:178
Result< int > step(std::uint64_t tick)
Advances reinforce matching for vacancies ready at or before tick.
Definition Roster.cpp:279
Result< int > releaseSubject(std::string_view subject, std::uint64_t tick)
Removes a subject from every post and squad, emitting one vacancy per seat.
Definition Roster.cpp:201
Result< void > releasePost(std::string_view postId, std::string_view subject, std::uint64_t tick)
Releases one subject's seat on a post and records a vacancy.
Definition Roster.cpp:83
Result< void > clearPolicy(std::string_view policyId)
Removes a policy; pending requests for it remain until acked/failed.
Definition Roster.cpp:261
void clear()
Clears every post, squad, policy, vacancy and spawn request.
Definition Roster.cpp:34
Result< void > failSpawnRequest(std::string_view requestId, std::uint64_t tick)
Fails a spawn request; enqueues a fallback definition when configured, else drops it.
Definition Roster.cpp:346
Result< std::string > memberAt(std::string_view squadId, int index) const
Member subject at a stable index, or failure.
Definition Roster.cpp:184
Result< void > removeMember(std::string_view squadId, std::string_view subject, std::uint64_t tick)
Removes a member and records a squad vacancy.
Definition Roster.cpp:154
Roster()
Constructs an empty roster world.
Result< void > assignPost(std::string_view postId, std::string subject, std::string role={})
Occupies one post seat with a subject.
Definition Roster.cpp:64
Result< void > setIntent(std::string_view squadId, std::string intent)
Replaces the squad's shared intent string.
Definition Roster.cpp:165
Result< int > postOccupancy(std::string_view postId) const
Current occupants for a post, or NotFound.
Definition Roster.cpp:94
Result< void > ackSpawnRequest(std::string_view requestId)
Marks a spawn request fulfilled and consumes one budget unit when limited.
Definition Roster.cpp:336
Result< void > registerPost(std::string postId, int capacity, std::string defaultRole="guard")
Registers a named post with a fixed seat capacity.
Definition Roster.cpp:48
Result< std::string > memberRole(std::string_view squadId, std::string_view subject) const
Role for a squad member, or NotFound.
Definition Roster.cpp:192
Result< void > removePost(std::string_view postId, std::uint64_t tick)
Removes a post and emits vacancies for every remaining occupant.
Definition Roster.cpp:56
int spawnRequestCount() const
Pending spawn request count.
Definition Roster.cpp:320
~Roster() override
Destroys roster storage.
Result< int > postCapacity(std::string_view postId) const
Configured capacity for a post, or NotFound.
Definition Roster.cpp:100
DiagnosticCode
Stable machine-readable diagnostic codes.
Definition Diagnostic.h:47
One pending spawn the game must fulfill (or fail).
Definition Roster.h:25
std::uint64_t readyAtTick
Definition Roster.h:32
std::string requestId
Definition Roster.h:26
std::string preferredDef
Definition / archetype id to spawn
Definition Roster.h:28
One open post/squad seat after a subject left.
Definition Roster.h:16
std::uint64_t tick
Simulation tick when recorded
Definition Roster.h:21
std::string role
Required role for refill
Definition Roster.h:19
std::string targetId
Post or squad logical id
Definition Roster.h:18
std::string kind
"post" or "squad"
Definition Roster.h:17