summaryrefslogtreecommitdiff
path: root/core/src
diff options
context:
space:
mode:
Diffstat (limited to 'core/src')
-rw-r--r--core/src/participant.cpp11
-rw-r--r--core/src/turn_order.cpp40
2 files changed, 46 insertions, 5 deletions
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 <glibmm/refptr.h>
#include <nlohmann/json.hpp>
@@ -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<std::string>(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 <glibmm/refptr.h>
+#include <nlohmann/json.hpp>
+
#include <algorithm>
+#include <ranges>
#include <typeinfo>
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<nlohmann::json>{}) |
+ std::views::transform([](auto const & j) { return participant::create(j); }) | std::ranges::to<std::vector>();
+
+ 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<turn_order>
{
return Glib::make_refptr_for_instance(new turn_order{});
}
+ auto turn_order::create(nlohmann::json const & from) -> Glib::RefPtr<turn_order>
+ {
+ return Glib::make_refptr_for_instance(new turn_order{from});
+ }
+
/** Queries */
auto turn_order::is_empty() const -> Glib::PropertyProxy_ReadOnly<is_empty_type>
@@ -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<std::vector>();
+ ;
+ return serialized;
+ }
+
/** ListModel implementation */
auto turn_order::get_item_type_vfunc() -> GType