载入中...
搜索中...
未找到
Roster.cpp
浏览该文件的文档.
15 return std::find_if(items.begin(), items.end(), [&](const auto& item) { return item.subject == subject; });
26 return Result<void>::failure(Diagnostic::error(code, std::move(message), std::move(path), {}, "roster"));
31 return Result<T>::failure(Diagnostic::error(code, std::move(message), std::move(path), {}, "roster"));
43void Roster::pushVacancy(std::string kind, std::string targetId, std::string role, std::string lostSubject,
45 vacancies_.push_back(Vacancy{std::move(kind), std::move(targetId), std::move(role), std::move(lostSubject), tick});
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");
59 for (auto& occupant : it->second.occupants) pushVacancy("post", it->first, occupant.role, occupant.subject, tick);
83Result<void> Roster::releasePost(std::string_view postId, std::string_view subject, std::uint64_t tick) {
96 if (it == posts_.end()) return failT<int>(DiagnosticCode::NotFound, "post was not found", "postId");
102 if (it == posts_.end()) return failT<int>(DiagnosticCode::NotFound, "post was not found", "postId");
108 if (it == posts_.end()) return failT<std::string>(DiagnosticCode::NotFound, "post was not found", "postId");
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);
116 if (it == posts_.end()) return failT<std::string>(DiagnosticCode::NotFound, "post was not found", "postId");
119 return failT<std::string>(DiagnosticCode::NotFound, "subject is not assigned to this post", "subject");
126 if (squads_.contains(squadId)) return fail(DiagnosticCode::AlreadyExists, "squad id already registered", "squadId");
133 if (it == squads_.end()) return fail(DiagnosticCode::NotFound, "squad was not found", "squadId");
139Result<void> Roster::addMember(std::string_view squadId, std::string subject, std::string role) {
141 return fail(DiagnosticCode::InvalidArgument, "squad id, subject and role are required", "addMember");
143 if (it == squads_.end()) return fail(DiagnosticCode::NotFound, "squad was not found", "squadId");
154Result<void> Roster::removeMember(std::string_view squadId, std::string_view subject, std::uint64_t tick) {
156 if (it == squads_.end()) return fail(DiagnosticCode::NotFound, "squad was not found", "squadId");
167 if (it == squads_.end()) return fail(DiagnosticCode::NotFound, "squad was not found", "squadId");
174 if (it == squads_.end()) return failT<std::string>(DiagnosticCode::NotFound, "squad was not found", "squadId");
180 if (it == squads_.end()) return failT<int>(DiagnosticCode::NotFound, "squad was not found", "squadId");
186 if (it == squads_.end()) return failT<std::string>(DiagnosticCode::NotFound, "squad was not found", "squadId");
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);
192Result<std::string> Roster::memberRole(std::string_view squadId, std::string_view subject) const {
194 if (it == squads_.end()) return failT<std::string>(DiagnosticCode::NotFound, "squad was not found", "squadId");
197 return failT<std::string>(DiagnosticCode::NotFound, "subject is not a squad member", "subject");
202 if (emptyId(subject)) return failT<int>(DiagnosticCode::InvalidArgument, "subject is required", "subject");
218 return Result<int>::success(released, Status::success(released == 0 ? StatusCode::NoOp : StatusCode::Applied));
231Result<void> Roster::setPolicy(std::string policyId, std::string preferredDef, int delayTicks, int budget,
237 return fail(DiagnosticCode::InvalidArgument, "policy may bind a post or a squad, not both", "policy");
249Result<void> Roster::setFallback(std::string_view policyId, std::string preferredDef, std::string fallbackDef) {
251 if (it == policies_.end()) return fail(DiagnosticCode::NotFound, "policy was not found", "policyId");
253 return fail(DiagnosticCode::InvalidArgument, "fallback needs distinct preferred and fallback defs", "fallback");
263 if (it == policies_.end()) return fail(DiagnosticCode::NotFound, "policy was not found", "policyId");
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;
275bool Roster::budgetAvailable(const Policy& policy) const { return policy.budget == 0 || policy.spent < policy.budget; }
317 return Result<int>::success(created, Status::success(created == 0 ? StatusCode::NoOp : StatusCode::Applied));
324 return failT<SpawnRequest>(DiagnosticCode::InvalidArgument, "spawn request index out of range", "index");
331 if (it == spawnRequests_.end()) return fail(DiagnosticCode::NotFound, "spawn request was not found", "requestId");
339 if (it == spawnRequests_.end()) return fail(DiagnosticCode::NotFound, "spawn request was not found", "requestId");
349 if (it == spawnRequests_.end()) return fail(DiagnosticCode::NotFound, "spawn request was not found", "requestId");
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));
Stable, structured diagnostics shared by engine modules.
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Definition Diagnostic.cpp:21
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
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
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
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
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
Result< int > postCapacity(std::string_view postId) const
Configured capacity for a post, or NotFound.
Definition Roster.cpp:100
Definition Roster.cpp:8
@ InvalidArgument
@ NotFound
@ AlreadyExists
@ Conflict
@ NoOp
@ Applied