summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@gmail.com>2025-06-05 15:00:34 +0200
committerFelix Morgner <felix.morgner@gmail.com>2025-06-05 15:00:34 +0200
commit8b871a613647f64658bf6c098510cc3a35ba1b48 (patch)
treeff87802a949ae9bbdb5f10ca7da1e4e317d62a3b
parent938328d3c87d5b7f2d56921cc5893f1c2d98302d (diff)
downloadturns-8b871a613647f64658bf6c098510cc3a35ba1b48.tar.xz
turns-8b871a613647f64658bf6c098510cc3a35ba1b48.zip
lib: add round-progress property to TurnOrder
-rw-r--r--lib/include/turns-turn-order.h10
-rw-r--r--lib/include/turnsmm/turn-order.hpp2
-rw-r--r--lib/src/turns-turn-order.c27
-rw-r--r--lib/src/turnsmm/turn-order.cpp10
-rw-r--r--lib/tests/turns-turn-order.cpp5
5 files changed, 54 insertions, 0 deletions
diff --git a/lib/include/turns-turn-order.h b/lib/include/turns-turn-order.h
index 5e34405..0f42369 100644
--- a/lib/include/turns-turn-order.h
+++ b/lib/include/turns-turn-order.h
@@ -10,6 +10,7 @@
#include <glib-object.h>
#include <glib.h>
+#include <glibconfig.h>
G_BEGIN_DECLS
@@ -103,6 +104,15 @@ G_GNUC_WARN_UNUSED_RESULT
gboolean turns_turn_order_get_running(TurnsTurnOrder const * self);
/**
+ * turns_turn_order_get_round_progress: (get-property round-progress):
+ * @self: a turn order.
+ *
+ * Gets the progress of the current round.
+ */
+G_GNUC_WARN_UNUSED_RESULT
+gfloat turns_turn_order_get_round_progress(TurnsTurnOrder const * self);
+
+/**
* turns_turn_order_get_sort_mode: (get-property sort-mode):
* @self: a turn order.
*
diff --git a/lib/include/turnsmm/turn-order.hpp b/lib/include/turnsmm/turn-order.hpp
index ebe09f7..759eedb 100644
--- a/lib/include/turnsmm/turn-order.hpp
+++ b/lib/include/turnsmm/turn-order.hpp
@@ -55,6 +55,7 @@ namespace Turns
[[nodiscard]] auto get_empty() const noexcept -> bool;
[[nodiscard]] auto get_participant_count() const noexcept -> std::size_t;
[[nodiscard]] auto get_running() const noexcept -> bool;
+ [[nodiscard]] auto get_round_progress() const noexcept -> float;
[[nodiscard]] auto get_sort_mode() const noexcept -> SortMode;
auto set_sort_mode(SortMode value) noexcept -> void;
@@ -62,6 +63,7 @@ namespace Turns
[[nodiscard]] auto property_empty() const noexcept -> Glib::PropertyProxy_ReadOnly<bool>;
[[nodiscard]] auto property_participant_count() const noexcept -> Glib::PropertyProxy_ReadOnly<std::size_t>;
[[nodiscard]] auto property_running() const noexcept -> Glib::PropertyProxy_ReadOnly<bool>;
+ [[nodiscard]] auto property_round_progress() const noexcept -> Glib::PropertyProxy_ReadOnly<float>;
[[nodiscard]] auto property_sort_mode() noexcept -> Glib::PropertyProxy<SortMode>;
[[nodiscard]] auto property_sort_mode() const noexcept -> Glib::PropertyProxy_ReadOnly<SortMode>;
diff --git a/lib/src/turns-turn-order.c b/lib/src/turns-turn-order.c
index 1861888..b192d74 100644
--- a/lib/src/turns-turn-order.c
+++ b/lib/src/turns-turn-order.c
@@ -18,6 +18,7 @@ enum
PROP_EMPTY = 1,
PROP_PARTICIPANT_COUNT,
PROP_RUNNING,
+ PROP_ROUND_PROGRESS,
PROP_SORT_MODE,
N_PROPERTIES,
};
@@ -26,6 +27,7 @@ struct _TurnsTurnOrder
{
GObject parent_instance;
+ gsize active;
GSList * participants;
gboolean running;
TurnsTurnOrderSortMode sort_mode;
@@ -120,6 +122,9 @@ static void turns_turn_order_get_property(GObject * self, guint id, GValue * val
case PROP_RUNNING:
g_value_set_boolean(value, turns_turn_order_get_running(instance));
return;
+ case PROP_ROUND_PROGRESS:
+ g_value_set_float(value, turns_turn_order_get_round_progress(instance));
+ return;
case PROP_SORT_MODE:
g_value_set_enum(value, turns_turn_order_get_sort_mode(instance));
return;
@@ -144,6 +149,7 @@ static void turns_turn_order_set_property(GObject * self, guint id, GValue const
static void turns_turn_order_init(TurnsTurnOrder * self)
{
+ self->active = 0;
self->participants = nullptr;
self->running = false;
self->sort_mode = TURNS_TURN_ORDER_SORT_MODE_DESCENDING;
@@ -186,6 +192,14 @@ static void turns_turn_order_class_init(TurnsTurnOrderClass * klass)
false,
G_PARAM_STATIC_STRINGS | G_PARAM_READABLE | G_PARAM_EXPLICIT_NOTIFY);
+ properties[PROP_ROUND_PROGRESS] = g_param_spec_float("round-progress",
+ "Round Progress",
+ "The current progress of the round.",
+ 0.0f,
+ 1.0f,
+ 0.0f,
+ G_PARAM_STATIC_STRINGS | G_PARAM_READABLE | G_PARAM_EXPLICIT_NOTIFY);
+
properties[PROP_SORT_MODE] = g_param_spec_enum("sort-mode",
"Sort Mode",
"The sort mode applied to the turn order",
@@ -304,6 +318,19 @@ gboolean turns_turn_order_get_running(TurnsTurnOrder const * self)
return self->running;
}
+gfloat turns_turn_order_get_round_progress(TurnsTurnOrder const * self)
+{
+ g_return_val_if_fail(TURNS_IS_TURN_ORDER((TurnsTurnOrder *)self), 0.0f);
+
+ auto participant_count = turns_turn_order_get_participant_count(self);
+ if (participant_count == 0)
+ {
+ return 0.0f;
+ }
+
+ return ((gfloat)(self->active + 1)) / ((gfloat)participant_count);
+}
+
TurnsTurnOrderSortMode turns_turn_order_get_sort_mode(TurnsTurnOrder const * self)
{
g_return_val_if_fail(TURNS_IS_TURN_ORDER((TurnsTurnOrder *)self), TURNS_TURN_ORDER_SORT_MODE_ASCENDING);
diff --git a/lib/src/turnsmm/turn-order.cpp b/lib/src/turnsmm/turn-order.cpp
index ae9bd43..58f4527 100644
--- a/lib/src/turnsmm/turn-order.cpp
+++ b/lib/src/turnsmm/turn-order.cpp
@@ -118,6 +118,11 @@ namespace Turns
return turns_turn_order_get_running(unwrap(this));
}
+ auto TurnOrder::get_round_progress() const noexcept -> float
+ {
+ return turns_turn_order_get_round_progress(unwrap(this));
+ }
+
auto TurnOrder::get_sort_mode() const noexcept -> SortMode
{
return static_cast<SortMode>(turns_turn_order_get_sort_mode(unwrap(this)));
@@ -143,6 +148,11 @@ namespace Turns
return {this, "running"};
}
+ auto TurnOrder::property_round_progress() const noexcept -> Glib::PropertyProxy_ReadOnly<float>
+ {
+ return {this, "round-progress"};
+ }
+
auto TurnOrder::property_sort_mode() noexcept -> Glib::PropertyProxy<SortMode>
{
return {this, "sort-mode"};
diff --git a/lib/tests/turns-turn-order.cpp b/lib/tests/turns-turn-order.cpp
index a4576f7..4ff1a83 100644
--- a/lib/tests/turns-turn-order.cpp
+++ b/lib/tests/turns-turn-order.cpp
@@ -75,6 +75,11 @@ SCENARIO("Creating a turn order", "[lib][object][lifetime]")
REQUIRE(g_list_model_get_object(G_LIST_MODEL(instance), 0) == nullptr);
}
+ THEN("its round progress is 0")
+ {
+ REQUIRE(turns_turn_order_get_round_progress(instance) == 0);
+ }
+
THEN("it's empty")
{
REQUIRE(turns_turn_order_get_empty(instance));