Upgrade cellular client to v10 with readable play and assisted steering
This commit is contained in:
@@ -0,0 +1,281 @@
|
||||
#pragma once
|
||||
|
||||
#include "evo/network/session_protocol.hpp"
|
||||
#include <algorithm>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <deque>
|
||||
#include <limits>
|
||||
#include <optional>
|
||||
#include <utility>
|
||||
|
||||
namespace evo::client_app {
|
||||
// Presentation and outstanding requests only. No biological prediction or saved authority.
|
||||
struct MutationChoice {
|
||||
std::uint64_t life{}, event{}, offer_tick{};
|
||||
std::uint16_t operator_id{};
|
||||
friend bool operator==(const MutationChoice&, const MutationChoice&) = default;
|
||||
};
|
||||
enum class CellAction : std::uint8_t { engulf, bud, mutation };
|
||||
enum class CellActionState : std::uint8_t {
|
||||
queued,
|
||||
sent,
|
||||
admitted,
|
||||
unresolved,
|
||||
committed,
|
||||
no_effect,
|
||||
refused
|
||||
};
|
||||
struct CellActionRecord {
|
||||
CellAction kind{};
|
||||
CellActionState state{CellActionState::queued};
|
||||
std::uint64_t id{}, life{}, tick{};
|
||||
MutationChoice choice{};
|
||||
network::AdmitStatus refusal{network::AdmitStatus::admitted};
|
||||
};
|
||||
|
||||
class CellularPlay {
|
||||
public:
|
||||
static constexpr std::size_t history_limit = 8;
|
||||
void initialize(std::uint64_t first_id) {
|
||||
next_id_ = first_id == 0 ? 1 : first_id;
|
||||
birth_request_ = allocate_id();
|
||||
birth_pending_ = true;
|
||||
}
|
||||
[[nodiscard]] std::uint64_t allocate_id() {
|
||||
const auto id = next_id_;
|
||||
if (next_id_ != 0)
|
||||
next_id_ = next_id_ == std::numeric_limits<std::uint64_t>::max() ? 0 : next_id_ + 1;
|
||||
return id;
|
||||
}
|
||||
void connected() {
|
||||
connected_ = true;
|
||||
}
|
||||
void disconnected() {
|
||||
connected_ = false;
|
||||
fresh_ = false;
|
||||
offer_.reset();
|
||||
if (pending_) {
|
||||
if (pending_->state == CellActionState::queued) {
|
||||
pending_->state = CellActionState::refused;
|
||||
finish();
|
||||
} else {
|
||||
pending_->state = CellActionState::unresolved;
|
||||
}
|
||||
}
|
||||
}
|
||||
void observe(const network::CellularPerception& view) {
|
||||
if (view.tick < view_tick_)
|
||||
return;
|
||||
view_tick_ = view.tick;
|
||||
fresh_ = true;
|
||||
if (ended_ && view.life_identity == ended_->life_identity)
|
||||
return; // A retained pre-death view must never revive this identity.
|
||||
// Never replay an intention onto a later life. Keep its uncertain result visible.
|
||||
if (life_ != view.life_identity) {
|
||||
offer_.reset();
|
||||
engulf_feedback_.reset();
|
||||
bud_feedback_.reset();
|
||||
if (pending_) {
|
||||
pending_->state = pending_->state == CellActionState::queued
|
||||
? CellActionState::refused
|
||||
: CellActionState::unresolved;
|
||||
finish();
|
||||
}
|
||||
}
|
||||
life_ = view.life_identity;
|
||||
if (view.diagnostics && view.diagnostics->has_tick_effects) {
|
||||
const auto& d = *view.diagnostics;
|
||||
if (d.engulf != network::CellularEngulfResult::not_reported &&
|
||||
d.engulf != network::CellularEngulfResult::not_requested)
|
||||
engulf_feedback_ = std::pair{view.tick, d.engulf};
|
||||
if (d.bud != network::CellularBudOutcome::not_reported &&
|
||||
d.bud != network::CellularBudOutcome::not_requested)
|
||||
bud_feedback_ = std::pair{view.tick, d.bud};
|
||||
}
|
||||
if (life_ != 0) {
|
||||
birth_pending_ = false;
|
||||
if (ended_ && ended_->life_identity != life_)
|
||||
ended_.reset();
|
||||
}
|
||||
}
|
||||
void observe_offer(const std::optional<network::CellularMutationOffer>& offer) {
|
||||
offer_ = offer && offer->life_identity == life_ && fresh_ && !ended_ ? offer : std::nullopt;
|
||||
}
|
||||
void observe_end(const network::CellularLifeEnded& ended) {
|
||||
if (ended.life_identity == seen_end_ || (life_ != 0 && life_ != ended.life_identity) ||
|
||||
(ended_ && ended_->tick >= ended.tick) || ended.tick < birth_tick_)
|
||||
return;
|
||||
seen_end_ = ended.life_identity;
|
||||
ended_ = ended;
|
||||
life_ = 0;
|
||||
offer_.reset();
|
||||
if (pending_ && pending_->state == CellActionState::queued) {
|
||||
pending_->state = CellActionState::refused;
|
||||
finish();
|
||||
}
|
||||
// The discrete action may still have a final result arriving in this poll.
|
||||
}
|
||||
[[nodiscard]] bool can_act() const {
|
||||
return connected_ && fresh_ && life_ != 0 && !ended_ && !pending_ && !birth_pending_;
|
||||
}
|
||||
[[nodiscard]] bool queue(CellAction kind, std::uint64_t tick, MutationChoice choice = {}) {
|
||||
if (!can_act() || tick == 0)
|
||||
return false;
|
||||
if (kind == CellAction::mutation &&
|
||||
(!offer_ || choice.life != life_ || choice.event != offer_->event_index ||
|
||||
choice.offer_tick != offer_->offer_tick || tick <= choice.offer_tick ||
|
||||
!std::ranges::any_of(offer_->candidates, [&](const auto& c) {
|
||||
return c.operator_id == choice.operator_id;
|
||||
})))
|
||||
return false;
|
||||
const auto id = allocate_id();
|
||||
if (id == 0)
|
||||
return false;
|
||||
pending_ = CellActionRecord{kind, CellActionState::queued, id, life_, tick, choice};
|
||||
return true;
|
||||
}
|
||||
void sent() {
|
||||
if (pending_)
|
||||
pending_->state = CellActionState::sent;
|
||||
}
|
||||
void rechecking() {
|
||||
if (pending_ && pending_->state != CellActionState::queued)
|
||||
pending_->state = CellActionState::unresolved;
|
||||
}
|
||||
void receipt(const network::ActionReceipt& receipt) {
|
||||
if (!pending_ || pending_->id != receipt.action_id)
|
||||
return;
|
||||
if (receipt.status == network::AdmitStatus::admitted ||
|
||||
receipt.status == network::AdmitStatus::duplicate) {
|
||||
pending_->state = CellActionState::admitted;
|
||||
} else if (pending_->kind == CellAction::mutation &&
|
||||
receipt.status == network::AdmitStatus::not_ready) {
|
||||
// The host can report not_ready after an uncertain durable reservation.
|
||||
pending_->state = CellActionState::unresolved;
|
||||
} else if (pending_->state != CellActionState::unresolved &&
|
||||
pending_->state != CellActionState::admitted) {
|
||||
pending_->state = CellActionState::refused;
|
||||
pending_->refusal = receipt.status;
|
||||
finish();
|
||||
}
|
||||
// A refused retry cannot disprove a previously admitted or uncertain action.
|
||||
}
|
||||
void outcome(const network::ActionOutcome& outcome) {
|
||||
if (!pending_ || pending_->id != outcome.action_id) {
|
||||
// Final replies may arrive after the life ended or the user kept an unknown.
|
||||
for (auto& record : history_)
|
||||
if (record.id == outcome.action_id && record.state == CellActionState::unresolved) {
|
||||
if (outcome.result == network::ActionResult::committed)
|
||||
record.state = CellActionState::committed;
|
||||
else if (outcome.result == network::ActionResult::committed_no_effect)
|
||||
record.state = CellActionState::no_effect;
|
||||
else if (outcome.result == network::ActionResult::not_committed)
|
||||
record.state = CellActionState::refused;
|
||||
}
|
||||
return;
|
||||
}
|
||||
using enum network::ActionResult;
|
||||
switch (outcome.result) {
|
||||
case committed:
|
||||
pending_->state = CellActionState::committed;
|
||||
break;
|
||||
case committed_no_effect:
|
||||
pending_->state = CellActionState::no_effect;
|
||||
break;
|
||||
case not_committed:
|
||||
pending_->state = CellActionState::refused;
|
||||
break;
|
||||
case pending:
|
||||
pending_->state = CellActionState::admitted;
|
||||
return;
|
||||
case unknown:
|
||||
pending_->state = CellActionState::unresolved;
|
||||
return;
|
||||
}
|
||||
finish();
|
||||
}
|
||||
// User may leave an uncertain result in the bounded record, never as a success/refusal.
|
||||
void keep_unknown() {
|
||||
if (pending_ && pending_->state == CellActionState::unresolved)
|
||||
finish();
|
||||
}
|
||||
[[nodiscard]] bool can_restart() const {
|
||||
return connected_ && fresh_ && life_ == 0 && !birth_pending_;
|
||||
}
|
||||
[[nodiscard]] bool restart() {
|
||||
if (!can_restart())
|
||||
return false;
|
||||
const auto id = allocate_id();
|
||||
if (id == 0)
|
||||
return false;
|
||||
birth_request_ = id;
|
||||
birth_pending_ = true;
|
||||
return true;
|
||||
}
|
||||
void birth(const network::PrimitiveLifeOutcome& result) {
|
||||
if (result.request_id != birth_request_)
|
||||
return;
|
||||
birth_result_ = result.result;
|
||||
birth_pending_ = result.result == network::PrimitiveLifeResult::pending ||
|
||||
result.result == network::PrimitiveLifeResult::unknown ||
|
||||
result.result == network::PrimitiveLifeResult::born;
|
||||
if (result.result == network::PrimitiveLifeResult::born) {
|
||||
ended_.reset();
|
||||
birth_tick_ = view_tick_;
|
||||
}
|
||||
}
|
||||
[[nodiscard]] std::uint64_t birth_request() const {
|
||||
return birth_request_;
|
||||
}
|
||||
[[nodiscard]] bool birth_pending() const {
|
||||
return birth_pending_;
|
||||
}
|
||||
[[nodiscard]] network::PrimitiveLifeResult birth_result() const {
|
||||
return birth_result_;
|
||||
}
|
||||
[[nodiscard]] std::uint64_t life() const {
|
||||
return life_;
|
||||
}
|
||||
[[nodiscard]] bool fresh() const {
|
||||
return connected_ && fresh_;
|
||||
}
|
||||
[[nodiscard]] const auto& offer() const {
|
||||
return offer_;
|
||||
}
|
||||
[[nodiscard]] const auto& ended() const {
|
||||
return ended_;
|
||||
}
|
||||
[[nodiscard]] const auto& pending() const {
|
||||
return pending_;
|
||||
}
|
||||
[[nodiscard]] const auto& history() const {
|
||||
return history_;
|
||||
}
|
||||
[[nodiscard]] const auto& engulf_feedback() const {
|
||||
return engulf_feedback_;
|
||||
}
|
||||
[[nodiscard]] const auto& bud_feedback() const {
|
||||
return bud_feedback_;
|
||||
}
|
||||
|
||||
private:
|
||||
void finish() {
|
||||
if (!pending_)
|
||||
return;
|
||||
if (history_.size() == history_limit)
|
||||
history_.pop_front();
|
||||
history_.push_back(*pending_);
|
||||
pending_.reset();
|
||||
}
|
||||
std::uint64_t next_id_{1}, life_{}, view_tick_{}, birth_tick_{}, birth_request_{}, seen_end_{};
|
||||
bool connected_{}, fresh_{}, birth_pending_{};
|
||||
network::PrimitiveLifeResult birth_result_{network::PrimitiveLifeResult::pending};
|
||||
std::optional<network::CellularMutationOffer> offer_;
|
||||
std::optional<network::CellularLifeEnded> ended_;
|
||||
std::optional<CellActionRecord> pending_;
|
||||
std::deque<CellActionRecord> history_;
|
||||
std::optional<std::pair<std::uint64_t, network::CellularEngulfResult>> engulf_feedback_;
|
||||
std::optional<std::pair<std::uint64_t, network::CellularBudOutcome>> bud_feedback_;
|
||||
};
|
||||
} // namespace evo::client_app
|
||||
Reference in New Issue
Block a user