Bind cellular actions to tick traces
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (checks) (push) Failing after 40s
Foundation / checks (postgres) (push) Canceled after 0s
Foundation / checks (tsan) (push) Canceled after 0s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (client) (push) Canceled after 1m27s
Foundation / checks (ecs) (push) Canceled after 1m19s
Foundation / checks (network) (push) Canceled after 54s

This commit is contained in:
dingo
2026-10-10 23:34:52 -04:00
parent eeb663d696
commit 0b85aa45c2
6 changed files with 240 additions and 0 deletions
@@ -0,0 +1,70 @@
#include "evo/server/cellular_session_batch.hpp"
#include <algorithm>
#include <optional>
namespace evo::server {
std::expected<CellularSessionTick, CellularSessionBatchError>
prepare_cellular_session_tick(const CellularLives& state, std::uint64_t tick,
std::span<const CellularLifeIntent> baseline,
std::span<const network::CarriedAction> actions) {
if (tick == 0 || tick != state.tick() + 1)
return std::unexpected{CellularSessionBatchError::tick};
if (baseline.size() != state.lives().size())
return std::unexpected{CellularSessionBatchError::intents};
if (actions.size() > max_cellular_action_records)
return std::unexpected{CellularSessionBatchError::action};
CellularSessionTick prepared;
prepared.intents.lives.assign(baseline.begin(), baseline.end());
std::ranges::sort(prepared.intents.lives, {}, &CellularLifeIntent::life_id);
for (std::size_t i = 0; i < state.lives().size(); ++i)
if (prepared.intents.lives[i].life_id != state.lives()[i].identity.life ||
(i != 0 && prepared.intents.lives[i - 1].life_id ==
prepared.intents.lives[i].life_id))
return std::unexpected{CellularSessionBatchError::intents};
prepared.actions.reserve(actions.size());
for (const auto& action : actions) {
if (!action.account.valid() || action.action_id == 0 || action.tick != tick ||
action.kind != network::ActionKind::cellular || !action.cellular_input)
return std::unexpected{CellularSessionBatchError::action};
const auto input = network::decode_cellular_action_input(*action.cellular_input);
if (!input || input->tick != tick)
return std::unexpected{CellularSessionBatchError::action};
const auto life = std::ranges::find(state.lives(), action.account.bytes,
&CellularOwnedLife::account);
if (life == state.lives().end())
return std::unexpected{CellularSessionBatchError::life};
const auto owned_intent = std::ranges::lower_bound(
prepared.intents.lives, life->identity.life, {}, &CellularLifeIntent::life_id);
if (owned_intent == prepared.intents.lives.end() ||
owned_intent->life_id != life->identity.life)
return std::unexpected{CellularSessionBatchError::intents};
if (std::ranges::find_if(prepared.actions, [&](const CellularActionRecord& previous) {
return previous.account == action.account.bytes;
}) != prepared.actions.end())
return std::unexpected{CellularSessionBatchError::duplicate_action};
owned_intent->intent.movement = {input->move_x, input->move_y};
owned_intent->intent.intake_target =
input->has_intake_target
? std::optional<biology::CellularPatchAddress>{biology::CellularPatchAddress{
input->intake_x_um, input->intake_y_um}}
: std::nullopt;
CellularActionRecord record;
record.account = action.account.bytes;
record.action_id = action.action_id;
record.input = *action.cellular_input;
prepared.actions.push_back(record);
}
std::ranges::sort(prepared.actions, [](const CellularActionRecord& left,
const CellularActionRecord& right) {
if (left.account != right.account)
return left.account < right.account;
return left.action_id < right.action_id;
});
return prepared;
}
} // namespace evo::server