Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 1m56s
Foundation / checks (checks) (push) Failing after 3m58s
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 6m18s
Foundation / checks (postgres) (push) Canceled after 2m23s
Foundation / checks (network) (push) Canceled after 4m19s
249 lines
14 KiB
C++
249 lines
14 KiB
C++
#include "evo/server/cellular_session_batch.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <optional>
|
|
|
|
namespace evo::server {
|
|
namespace {
|
|
std::uint64_t little_endian(std::span<const std::uint8_t> bytes, std::size_t offset,
|
|
std::size_t count) {
|
|
std::uint64_t value{};
|
|
for (std::size_t i = 0; i < count; ++i)
|
|
value |= static_cast<std::uint64_t>(bytes[offset + i]) << (i * 8U);
|
|
return value;
|
|
}
|
|
} // namespace
|
|
|
|
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,
|
|
std::span<const persistence::CellularMutationReservation> mutations) {
|
|
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 ||
|
|
mutations.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};
|
|
if (input->has_intake_target) {
|
|
const auto view = state.perceive(life->identity.life);
|
|
const biology::CellularPatchAddress target{input->intake_x_um, input->intake_y_um};
|
|
if (!view ||
|
|
std::ranges::find(*view, target, &biology::CellularPatchPerception::address) ==
|
|
view->end())
|
|
return std::unexpected{CellularSessionBatchError::unperceived_target};
|
|
}
|
|
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;
|
|
});
|
|
|
|
if (!mutations.empty() && state.format_version() != cellular_lives_revision_format)
|
|
return std::unexpected{CellularSessionBatchError::mutation};
|
|
prepared.intents.mutations.reserve(mutations.size());
|
|
prepared.mutation_actions.reserve(mutations.size());
|
|
for (const auto& reservation : mutations) {
|
|
const auto bytes = std::span{reservation.input};
|
|
const auto target_tick = little_endian(bytes, 0, 8);
|
|
const auto life_id = little_endian(bytes, 8, 8);
|
|
const auto event_index = little_endian(bytes, 16, 8);
|
|
const auto offer_tick = little_endian(bytes, 24, 8);
|
|
const auto operator_id = static_cast<std::uint16_t>(little_endian(bytes, 32, 2));
|
|
if (reservation.account == persistence::Uuid{} || reservation.action_id == 0 ||
|
|
reservation.region != state.region() || reservation.generation == 0 ||
|
|
reservation.successor_identity == 0 || target_tick != tick ||
|
|
reservation.target_tick != tick || life_id == 0 || event_index == 0 ||
|
|
offer_tick == 0 || offer_tick >= tick || operator_id == 0)
|
|
return std::unexpected{CellularSessionBatchError::mutation};
|
|
const auto life = std::ranges::find(state.lives(), reservation.account,
|
|
&CellularOwnedLife::account);
|
|
if (life == state.lives().end() || life->identity.life != life_id ||
|
|
life->mutation.event_index != event_index || life->mutation.offer_tick != offer_tick ||
|
|
!life->mutation.offer ||
|
|
!std::ranges::any_of(life->mutation.offer->candidates, [&](const auto& candidate) {
|
|
return candidate.operator_id == operator_id;
|
|
}))
|
|
return std::unexpected{CellularSessionBatchError::mutation};
|
|
|
|
if (std::ranges::any_of(prepared.intents.mutations, [&](const auto& previous) {
|
|
return previous.life_id == life_id ||
|
|
previous.successor_id == reservation.successor_identity;
|
|
}) ||
|
|
std::ranges::any_of(prepared.mutation_actions, [&](const auto& previous) {
|
|
return previous.account == reservation.account &&
|
|
previous.action_id == reservation.action_id;
|
|
}))
|
|
return std::unexpected{CellularSessionBatchError::duplicate_mutation};
|
|
prepared.intents.mutations.push_back(
|
|
{life_id, event_index, offer_tick, reservation.successor_identity, operator_id});
|
|
prepared.mutation_actions.push_back(
|
|
{reservation.account, reservation.action_id, tick,
|
|
{life_id, event_index, offer_tick, operator_id}});
|
|
}
|
|
std::ranges::sort(prepared.intents.mutations, {}, &CellularMutationSelection::life_id);
|
|
std::ranges::sort(prepared.mutation_actions,
|
|
[](const auto& left, const auto& right) {
|
|
if (left.account != right.account)
|
|
return left.account < right.account;
|
|
return left.action_id < right.action_id;
|
|
});
|
|
return prepared;
|
|
}
|
|
|
|
std::expected<CellularSessionBatchBuilder, CellularSessionBatchError>
|
|
CellularSessionBatchBuilder::create(const CellularLives& committed_state,
|
|
const persistence::RegionHead& committed_head) {
|
|
if (committed_head.frame != committed_state.frame() ||
|
|
committed_head.end_tick != committed_state.tick() ||
|
|
std::ranges::all_of(committed_head.commit, [](std::uint8_t byte) { return byte == 0; }) ||
|
|
std::ranges::all_of(committed_head.digest, [](std::uint8_t byte) { return byte == 0; }))
|
|
return std::unexpected{CellularSessionBatchError::head};
|
|
auto working = CellularLives::restore(committed_state.snapshot(), committed_state.world(),
|
|
committed_state.region(), committed_state.frame(),
|
|
committed_state.tick());
|
|
if (!working)
|
|
return std::unexpected{CellularSessionBatchError::biology};
|
|
|
|
CellularSessionBatchBuilder builder{std::move(*working)};
|
|
builder.batch_.header = {committed_state.world(), committed_state.region(),
|
|
committed_state.frame(), committed_head.commit,
|
|
committed_head.digest, committed_state.tick(),
|
|
committed_state.tick()};
|
|
return builder;
|
|
}
|
|
|
|
std::expected<void, CellularSessionBatchError>
|
|
CellularSessionBatchBuilder::append(std::uint64_t tick,
|
|
std::span<const CellularLifeIntent> baseline,
|
|
std::span<const network::CarriedAction> actions,
|
|
std::span<const CellularTickIntents::Birth> births,
|
|
std::span<const persistence::CellularMutationReservation>
|
|
mutations) {
|
|
if (batch_.ticks.size() >= max_cellular_batch_ticks)
|
|
return std::unexpected{CellularSessionBatchError::batch_full};
|
|
if (actions_.size() + actions.size() > max_cellular_action_records)
|
|
return std::unexpected{CellularSessionBatchError::action_limit};
|
|
if (mutation_actions_.size() + mutations.size() > max_cellular_action_records)
|
|
return std::unexpected{CellularSessionBatchError::action_limit};
|
|
auto prepared = prepare_cellular_session_tick(*working_, tick, baseline, actions, mutations);
|
|
if (!prepared)
|
|
return std::unexpected{prepared.error()};
|
|
for (const auto& action : prepared->actions)
|
|
if (std::ranges::find_if(actions_, [&](const CellularActionRecord& previous) {
|
|
return previous.account == action.account && previous.action_id == action.action_id;
|
|
}) != actions_.end())
|
|
return std::unexpected{CellularSessionBatchError::duplicate_action};
|
|
for (const auto& action : prepared->mutation_actions)
|
|
if (std::ranges::find_if(mutation_actions_, [&](const auto& previous) {
|
|
return previous.account == action.account &&
|
|
previous.action_id == action.action_id;
|
|
}) != mutation_actions_.end())
|
|
return std::unexpected{CellularSessionBatchError::duplicate_mutation};
|
|
if (births.size() > biology::max_cellular_tick_lives)
|
|
return std::unexpected{CellularSessionBatchError::invalid_birth};
|
|
prepared->intents.births.assign(births.begin(), births.end());
|
|
std::ranges::sort(prepared->intents.births, [](const auto& left, const auto& right) {
|
|
if (left.account != right.account)
|
|
return left.account < right.account;
|
|
return left.request < right.request;
|
|
});
|
|
for (std::size_t i = 0; i < prepared->intents.births.size(); ++i) {
|
|
const auto& birth = prepared->intents.births[i];
|
|
if (std::ranges::all_of(birth.account, [](std::uint8_t value) { return value == 0; }) ||
|
|
birth.request == 0 || birth.generation == 0 || birth.identity == 0)
|
|
return std::unexpected{CellularSessionBatchError::invalid_birth};
|
|
if (i > 0 && prepared->intents.births[i - 1].account == birth.account)
|
|
return std::unexpected{CellularSessionBatchError::duplicate_birth};
|
|
for (const auto& previous_tick : batch_.ticks)
|
|
if (std::ranges::any_of(previous_tick.births, [&](const auto& previous) {
|
|
return previous.account == birth.account || previous.identity == birth.identity;
|
|
}))
|
|
return std::unexpected{CellularSessionBatchError::duplicate_birth};
|
|
}
|
|
|
|
auto next = CellularLives::restore(working_->snapshot(), working_->world(), working_->region(),
|
|
working_->frame(), working_->tick());
|
|
if (!next || !(*next)->advance_tick_with_mutations(prepared->intents.lives,
|
|
prepared->intents.mutations))
|
|
return std::unexpected{CellularSessionBatchError::biology};
|
|
|
|
for (const auto& birth : prepared->intents.births) {
|
|
const LifeRequest request{birth.account, birth.request};
|
|
const LifeAllocation allocation{
|
|
birth.identity, birth.identity, birth.identity, birth.identity, {tick}};
|
|
auto created = (*next)->birth_candidate(request, allocation);
|
|
if (!created)
|
|
return std::unexpected{CellularSessionBatchError::invalid_birth};
|
|
*next = std::move(*created);
|
|
}
|
|
|
|
auto next_batch = batch_;
|
|
next_batch.ticks.push_back(std::move(prepared->intents));
|
|
next_batch.header.end_tick = tick;
|
|
if (!encode_cellular_batch(next_batch))
|
|
return std::unexpected{CellularSessionBatchError::invalid_birth};
|
|
const auto next_mutation_actions = [&] {
|
|
auto records = mutation_actions_;
|
|
records.insert(records.end(), prepared->mutation_actions.begin(),
|
|
prepared->mutation_actions.end());
|
|
return records;
|
|
}();
|
|
if (!encode_cellular_mutation_action_block(next_mutation_actions,
|
|
next_batch.header.begin_tick,
|
|
next_batch.header.end_tick))
|
|
return std::unexpected{CellularSessionBatchError::mutation};
|
|
working_ = std::move(*next);
|
|
batch_ = std::move(next_batch);
|
|
actions_.insert(actions_.end(), prepared->actions.begin(), prepared->actions.end());
|
|
mutation_actions_ = next_mutation_actions;
|
|
return {};
|
|
}
|
|
} // namespace evo::server
|