diff options
| author | Felix Morgner <felix.morgner@gmail.com> | 2025-05-16 20:47:07 +0200 |
|---|---|---|
| committer | Felix Morgner <felix.morgner@gmail.com> | 2025-05-16 20:47:07 +0200 |
| commit | d0b647c3156b07c70e9831aa86ef76ee40413d0c (patch) | |
| tree | 0908404f80ea28db00962c8eeb26b042dad98cb9 | |
| parent | 9e1280599c322df6e5f3e8bac2a56915bf8531f2 (diff) | |
| download | turns-d0b647c3156b07c70e9831aa86ef76ee40413d0c.tar.xz turns-d0b647c3156b07c70e9831aa86ef76ee40413d0c.zip | |
lib: react to priority changes
| -rw-r--r-- | lib/src/turns-turn-order.cpp | 11 | ||||
| -rw-r--r-- | lib/tests/turns-turn-order.cpp | 64 | ||||
| -rw-r--r-- | lib/tests/turnsmm/turn-order.cpp | 2 |
3 files changed, 74 insertions, 3 deletions
diff --git a/lib/src/turns-turn-order.cpp b/lib/src/turns-turn-order.cpp index 46ea0e6..b7bd03e 100644 --- a/lib/src/turns-turn-order.cpp +++ b/lib/src/turns-turn-order.cpp @@ -46,11 +46,11 @@ namespace if (left_priority < right_priority) { - return -1; + return 1; } else if (left_priority > right_priority) { - return 1; + return -1; } else { @@ -80,6 +80,11 @@ namespace } } + auto handle_participant_property_changed(TurnsParticipant const *, GParamSpec *, TurnsTurnOrder * self) + { + self->participants = g_slist_sort(self->participants, std::bit_cast<GCompareFunc>(&compare_participant)); + } + } // namespace G_BEGIN_DECLS @@ -140,6 +145,8 @@ void turns_turn_order_add(TurnsTurnOrder * self, TurnsParticipant * participant) { g_return_if_fail(participant != nullptr); self->participants = g_slist_insert_sorted(self->participants, g_object_ref(participant), std::bit_cast<GCompareFunc>(&compare_participant)); + + g_signal_connect(participant, "notify::priority", std::bit_cast<GCallback>(&handle_participant_property_changed), self); } gsize turns_turn_order_get_participant_count(TurnsTurnOrder const * self) diff --git a/lib/tests/turns-turn-order.cpp b/lib/tests/turns-turn-order.cpp index 05e3b07..9f4d5d6 100644 --- a/lib/tests/turns-turn-order.cpp +++ b/lib/tests/turns-turn-order.cpp @@ -85,4 +85,68 @@ SCENARIO("Modifying a turn order", "[lib][object][data]") } } } +} + +SCENARIO("Sorting a turn order") +{ + GIVEN("A turn order with two participants - A/10/H and B/20/N") + { + g_autoptr(TurnsTurnOrder) instance = turns_turn_order_new(); + g_autoptr(TurnsParticipant) participant_a = turns_participant_new_with("A", 10, TURNS_DISPOSITION_HOSTILE); + g_autoptr(TurnsParticipant) participant_b = turns_participant_new_with("B", 20, TURNS_DISPOSITION_NEUTRAL); + + turns_turn_order_add(instance, participant_a); + turns_turn_order_add(instance, participant_b); + + THEN("B is the first and A the second element") + { + g_autoptr(TurnsParticipant) first = TURNS_PARTICIPANT(g_list_model_get_object(G_LIST_MODEL(instance), 0)); + g_autoptr(TurnsParticipant) second = TURNS_PARTICIPANT(g_list_model_get_object(G_LIST_MODEL(instance), 1)); + + REQUIRE(first == participant_b); + REQUIRE(second == participant_a); + } + + WHEN("the priority of A is changed to 30") + { + turns_participant_set_priority(participant_a, 30); + + THEN("A is the first and B the second element") + { + g_autoptr(TurnsParticipant) first = TURNS_PARTICIPANT(g_list_model_get_object(G_LIST_MODEL(instance), 0)); + g_autoptr(TurnsParticipant) second = TURNS_PARTICIPANT(g_list_model_get_object(G_LIST_MODEL(instance), 1)); + + REQUIRE(first == participant_a); + REQUIRE(second == participant_b); + } + } + + WHEN("the priority of A is changed to 0") + { + turns_participant_set_priority(participant_a, 0); + + THEN("B is the first and A the second element") + { + g_autoptr(TurnsParticipant) first = TURNS_PARTICIPANT(g_list_model_get_object(G_LIST_MODEL(instance), 0)); + g_autoptr(TurnsParticipant) second = TURNS_PARTICIPANT(g_list_model_get_object(G_LIST_MODEL(instance), 1)); + + REQUIRE(first == participant_b); + REQUIRE(second == participant_a); + } + } + + WHEN("the priority of B is changed to 0") + { + turns_participant_set_priority(participant_b, 0); + + THEN("A is the first and B the second element") + { + g_autoptr(TurnsParticipant) first = TURNS_PARTICIPANT(g_list_model_get_object(G_LIST_MODEL(instance), 0)); + g_autoptr(TurnsParticipant) second = TURNS_PARTICIPANT(g_list_model_get_object(G_LIST_MODEL(instance), 1)); + + REQUIRE(first == participant_a); + REQUIRE(second == participant_b); + } + } + } }
\ No newline at end of file diff --git a/lib/tests/turnsmm/turn-order.cpp b/lib/tests/turnsmm/turn-order.cpp index 089e6eb..21c75d3 100644 --- a/lib/tests/turnsmm/turn-order.cpp +++ b/lib/tests/turnsmm/turn-order.cpp @@ -80,4 +80,4 @@ SCENARIO("Modifying a turn order", "[lib][object][data]") } } } -}
\ No newline at end of file +} |
