载入中...
搜索中...
未找到
ActionInputBuffer.cpp
浏览该文件的文档.
2
3#include <algorithm>
4#include <utility>
5
6namespace eve::action::input {
7namespace {
8
9bool matchesAllow(const std::string& input, const std::vector<std::string>& allows) {
10 return std::find(allows.begin(), allows.end(), input) != allows.end();
11}
12
13} // namespace
14
15ActionInputBuffer::ActionInputBuffer(std::uint32_t capacity, std::uint32_t lifetimeTicks)
16 : capacity_(capacity), lifetimeTicks_(lifetimeTicks) {}
17
19 if (!input.subject.isValid()) return Result<void>::failure(
20 Diagnostic::error(DiagnosticCode::InvalidArgument, "buffered subject is nil", "subject"));
21 if (input.input.empty()) return Result<void>::failure(
22 Diagnostic::error(DiagnosticCode::InvalidArgument, "buffered input is empty", "input"));
23 if (capacity_ == 0 || lifetimeTicks_ == 0) return Result<void>::failure(
24 Diagnostic::error(DiagnosticCode::InvalidArgument, "input buffer capacity/lifetime invalid", "capacity"));
25 if (entries_.size() >= capacity_)
27 Diagnostic::error(DiagnosticCode::Failed, "action input buffer is full", "buffer"));
28 entries_.push_back(std::move(input));
30}
31
33 while (!entries_.empty()) {
34 const auto& front = entries_.front();
35 if (now.value() <= front.tick.value() + lifetimeTicks_) break;
36 entries_.pop_front();
37 }
39}
40
41Result<BufferedInput> ActionInputBuffer::peek(SubjectRef subject, const std::vector<std::string>& allows) const {
42 const BufferedInput* best = nullptr;
43 for (const auto& entry : entries_) {
44 if (entry.subject != subject) continue;
45 if (!matchesAllow(entry.input, allows)) continue;
46 if (!best || entry.priority > best->priority ||
47 (entry.priority == best->priority && entry.tick.value() < best->tick.value()))
48 best = &entry;
49 }
50 if (!best)
52 Diagnostic::error(DiagnosticCode::NotFound, "no buffered input matched", "input"));
54}
55
56Result<BufferedInput> ActionInputBuffer::consume(SubjectRef subject, const std::vector<std::string>& allows) {
57 auto peeked = peek(subject, allows);
58 if (!peeked) return peeked;
59 for (auto it = entries_.begin(); it != entries_.end(); ++it) {
60 if (it->subject == peeked.value().subject && it->input == peeked.value().input &&
61 it->tick == peeked.value().tick && it->priority == peeked.value().priority) {
62 BufferedInput value = *it;
63 entries_.erase(it);
65 }
66 }
68 Diagnostic::error(DiagnosticCode::NotFound, "buffered input disappeared", "input"));
69}
70
72 entries_.erase(std::remove_if(entries_.begin(), entries_.end(),
73 [&](const BufferedInput& entry) { return entry.subject == subject; }),
74 entries_.end());
75}
76
77} // namespace eve::action::input
Deterministic semantic input buffer for combat cancels.
double value
int subject
Definition AnimSmr.cpp:163
EvpackChunkInput input
Definition Evpack.cpp:170
std::uint32_t capacity
std::map< Cell, int > best
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={}, DiagnosticDetails details={}, std::string source={})
Construct an error diagnostic with the standard error severity.
Definition Diagnostic.h:125
Move-only operation result carrying either a value or Status.
Definition Result.h:155
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:164
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:175
static Status success(StatusCode code=StatusCode::Ok)
Construct a successful status with an explicit non-error outcome.
Definition Status.h:81
Strong, domain-neutral reference to a runtime subject.
Definition SubjectRef.h:26
Result< void > expire(SimulationTick now)
Drop entries older than lifetime relative to now.
void clear(SubjectRef subject)
Clear every buffered command for one subject.
Result< BufferedInput > peek(SubjectRef subject, const std::vector< std::string > &allows) const
Peek without consuming.
ActionInputBuffer(std::uint32_t capacity, std::uint32_t lifetimeTicks)
Construct with positive capacity and lifetime window in ticks.
Result< BufferedInput > consume(SubjectRef subject, const std::vector< std::string > &allows)
Consume the highest-priority buffered input that matches one of the allows.
Result< void > push(BufferedInput input)
Push one owning input at the supplied tick; full buffers reject.
constexpr std::uint64_t value() const noexcept
Returns the underlying value at an explicit protocol boundary.
One buffered semantic command tagged with the tick it was pressed.