Add deterministic transactional lifecycle command scheduler
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
#include "evo/simulation/scheduler.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <limits>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
namespace evo {
|
||||
std::expected<std::vector<CommandReceipt>, ScheduleError>
|
||||
advance(SimulationWorld& world, Tick target, std::span<const Command> commands) {
|
||||
if (world.current_tick().value == std::numeric_limits<std::uint64_t>::max()) {
|
||||
return std::unexpected{
|
||||
ScheduleError{ScheduleErrorCode::simulation_failure, 0, Error::tick_exhausted}};
|
||||
}
|
||||
if (target.value != world.current_tick().value + 1) {
|
||||
return std::unexpected{ScheduleError{ScheduleErrorCode::wrong_tick}};
|
||||
}
|
||||
if (commands.size() > max_commands_per_tick) {
|
||||
return std::unexpected{ScheduleError{ScheduleErrorCode::batch_limit}};
|
||||
}
|
||||
if (commands.empty()) {
|
||||
const auto result = world.tick();
|
||||
if (!result) {
|
||||
return std::unexpected{
|
||||
ScheduleError{ScheduleErrorCode::simulation_failure, 0, result.error()}};
|
||||
}
|
||||
return std::vector<CommandReceipt>{};
|
||||
}
|
||||
std::vector<Command> ordered{commands.begin(), commands.end()};
|
||||
std::ranges::sort(ordered, {}, &Command::sequence);
|
||||
std::uint64_t previous{};
|
||||
for (const auto& command : ordered) {
|
||||
if (command.sequence == 0) {
|
||||
return std::unexpected{ScheduleError{ScheduleErrorCode::invalid_sequence}};
|
||||
}
|
||||
if (command.sequence == previous) {
|
||||
return std::unexpected{
|
||||
ScheduleError{ScheduleErrorCode::duplicate_sequence, command.sequence}};
|
||||
}
|
||||
previous = command.sequence;
|
||||
}
|
||||
auto candidate = world;
|
||||
std::vector<CommandReceipt> receipts;
|
||||
receipts.reserve(ordered.size());
|
||||
for (const auto& command : ordered) {
|
||||
EntityId affected{};
|
||||
if (const auto* create = std::get_if<CreateEntity>(&command.operation)) {
|
||||
const auto result = candidate.create_entity(create->position);
|
||||
if (!result) {
|
||||
return std::unexpected{ScheduleError{ScheduleErrorCode::simulation_failure,
|
||||
command.sequence, result.error()}};
|
||||
}
|
||||
affected = *result;
|
||||
} else {
|
||||
affected = std::get<RemoveEntity>(command.operation).id;
|
||||
const auto result = candidate.remove_entity(affected);
|
||||
if (!result) {
|
||||
return std::unexpected{ScheduleError{ScheduleErrorCode::simulation_failure,
|
||||
command.sequence, result.error()}};
|
||||
}
|
||||
}
|
||||
receipts.push_back({command.sequence, affected});
|
||||
}
|
||||
const auto result = candidate.tick();
|
||||
if (!result) {
|
||||
return std::unexpected{
|
||||
ScheduleError{ScheduleErrorCode::simulation_failure, 0, result.error()}};
|
||||
}
|
||||
static_assert(std::is_nothrow_move_assignable_v<SimulationWorld>);
|
||||
static_assert(std::is_nothrow_move_constructible_v<std::vector<CommandReceipt>>);
|
||||
world = std::move(candidate);
|
||||
return receipts;
|
||||
}
|
||||
} // namespace evo
|
||||
Reference in New Issue
Block a user