From d0b5c8c14d4a41b39470821d43dd719dbb5246aa Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Thu, 25 Jul 2024 16:38:30 +0200 Subject: core/turn_order: implement basic serialization --- core/src/participant.cpp | 11 ++++++----- core/src/turn_order.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 5 deletions(-) (limited to 'core/src') diff --git a/core/src/participant.cpp b/core/src/participant.cpp index 0c32172..6b69754 100644 --- a/core/src/participant.cpp +++ b/core/src/participant.cpp @@ -1,5 +1,7 @@ #include "turns/core/participant.hpp" +#include "turns/core/json_ext.hpp" + #include #include @@ -50,11 +52,10 @@ namespace turns::core auto participant::serialize() -> nlohmann::json { return nlohmann::json{ - {"active", m_is_active.get_value() }, - {"disposition", m_disposition.get_value() }, - {"name", static_cast(m_name.get_value())}, - {"priority", m_priority.get_value() }, + {"active", m_is_active }, + {"disposition", m_disposition}, + {"name", m_name }, + {"priority", m_priority }, }; } - } // namespace turns::core \ No newline at end of file diff --git a/core/src/turn_order.cpp b/core/src/turn_order.cpp index 835baab..6442d37 100644 --- a/core/src/turn_order.cpp +++ b/core/src/turn_order.cpp @@ -1,10 +1,14 @@ #include "turns/core/turn_order.hpp" +#include "turns/core/json_ext.hpp" #include "turns/core/participant.hpp" #include +#include + #include +#include #include namespace turns::core @@ -29,11 +33,34 @@ namespace turns::core { } + turn_order::turn_order(nlohmann::json const & from) + : turn_order{} + { + m_round_number = from.value("round", invalid_round_number); + m_data = from.value("participants", std::vector{}) | + std::views::transform([](auto const & j) { return participant::create(j); }) | std::ranges::to(); + + auto active = std::ranges::find_if(m_data, [](auto participant) { return participant->is_active(); }); + if (active != std::ranges::end(m_data)) + { + m_active = std::ranges::distance(std::ranges::begin(m_data), active); + } + + m_is_empty = m_data.empty(); + m_has_next = !m_is_empty; + m_has_previous = m_round_number > 0 || m_active > 0; + } + auto turn_order::create() -> Glib::RefPtr { return Glib::make_refptr_for_instance(new turn_order{}); } + auto turn_order::create(nlohmann::json const & from) -> Glib::RefPtr + { + return Glib::make_refptr_for_instance(new turn_order{from}); + } + /** Queries */ auto turn_order::is_empty() const -> Glib::PropertyProxy_ReadOnly @@ -176,6 +203,19 @@ namespace turns::core m_progress = 0; } + /** Serialization */ + auto turn_order::serialize() -> nlohmann::json + { + auto serialized = nlohmann::json{}; + if (m_round_number != invalid_round_number) + { + serialized["round"] = m_round_number; + } + serialized["participants"] = m_data | std::views::transform([](auto const & p) { return p->serialize(); }) | std::ranges::to(); + ; + return serialized; + } + /** ListModel implementation */ auto turn_order::get_item_type_vfunc() -> GType -- cgit v1.2.3