summaryrefslogtreecommitdiff
path: root/domain/src/turn_order.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'domain/src/turn_order.cpp')
-rw-r--r--domain/src/turn_order.cpp142
1 files changed, 133 insertions, 9 deletions
diff --git a/domain/src/turn_order.cpp b/domain/src/turn_order.cpp
index 990e9c1..595b55d 100644
--- a/domain/src/turn_order.cpp
+++ b/domain/src/turn_order.cpp
@@ -3,6 +3,7 @@
#include "turns/domain/participant.hpp"
#include <compare>
+#include <limits>
#include <typeinfo>
#include <glibmm/binding.h>
@@ -39,50 +40,121 @@ namespace turns::domain
turn_order::turn_order()
: Glib::ObjectBase{typeid(turn_order)}
, m_model{Gio::ListStore<participant>::create()}
+ , m_active_participant(*this, "active_participant", std::numeric_limits<active_participant_type>::max())
, m_empty{*this, "empty", true}
+ , m_has_next{*this, "has-next", false}
+ , m_has_previous{*this, "has-previous", false}
+ , m_running{*this, "running", false}
+ , m_round{*this, "round", 0}
{
Glib::Binding::bind_property(m_model->property_n_items(), m_empty.get_proxy(), Glib::Binding::Flags::DEFAULT, [](auto n) {
return n == 0;
});
}
+ /** Modifiers */
+
auto turn_order::add(Glib::ustring const & name, float priority, disposition disposition) -> void
{
auto participant = participant::create(name, priority, disposition);
if (auto [found, index] = m_model->find(participant, equal_comparator); !found)
{
- m_model->insert_sorted(participant, comparator);
+ auto position = m_model->insert_sorted(participant, comparator);
participant->property_priority().signal_changed().connect([this] { m_model->sort(comparator); });
+
+ if (m_active_participant != std::numeric_limits<active_participant_type>::max() && position <= m_active_participant)
+ {
+ m_active_participant = m_active_participant + 1;
+ }
}
}
- auto turn_order::get(unsigned int index) -> Glib::RefPtr<participant>
+ auto turn_order::clear() -> void
{
- return m_model->get_item(index);
+ m_model->remove_all();
+ m_active_participant = std::numeric_limits<active_participant_type>::max();
+ m_has_next = false;
+ m_has_previous = false;
+ m_running = false;
}
- auto turn_order::list_model() -> Glib::RefPtr<Gio::ListModel>
+ auto turn_order::next() -> void
{
- return m_model;
+ m_active_participant = (m_active_participant + 1) % size();
+ if (!m_active_participant)
+ {
+ m_round = round() + 1;
+ }
+ m_has_previous = m_active_participant || m_round;
}
- auto turn_order::size() -> unsigned int
+ auto turn_order::previous() -> void
{
- return m_model->get_n_items();
+ if (!m_has_previous)
+ {
+ return;
+ }
+
+ if (m_active_participant)
+ {
+ m_active_participant = m_active_participant - 1;
+ }
+ else if (m_round)
+ {
+ m_round = round() - 1;
+ m_active_participant = size() - 1;
+ }
+
+ m_has_previous = m_active_participant || m_round;
}
auto turn_order::remove(unsigned int index) -> void
{
m_model->remove(index);
+ if (empty())
+ {
+ m_active_participant = std::numeric_limits<active_participant_type>::max();
+ m_has_next = false;
+ m_has_previous = false;
+ m_running = false;
+ }
+ else if (m_active_participant >= size() - 1)
+ {
+ m_active_participant = size() - 1;
+ }
+ else if (index <= m_active_participant)
+ {
+ m_active_participant = m_active_participant - 1;
+ }
}
- auto turn_order::clear() -> void
+ auto turn_order::reset() -> void
{
- m_model->remove_all();
+ m_running = false;
+ m_active_participant = 0;
}
auto turn_order::start() -> void
{
+ if (m_active_participant == std::numeric_limits<active_participant_type>::max())
+ {
+ m_active_participant = 0;
+ }
+ m_running = true;
+ m_has_next = true;
+ }
+
+ auto turn_order::stop() -> void
+ {
+ m_running = false;
+ m_has_next = false;
+ }
+
+ /** Querries */
+
+ auto turn_order::active_participant() const noexcept -> active_participant_type
+ {
+ return m_active_participant;
}
auto turn_order::empty() const noexcept -> bool
@@ -90,9 +162,61 @@ namespace turns::domain
return m_empty;
}
+ auto turn_order::get(unsigned int index) const noexcept -> Glib::RefPtr<participant>
+ {
+ return m_model->get_item(index);
+ }
+
+ auto turn_order::list_model() -> Glib::RefPtr<Gio::ListModel>
+ {
+ return m_model;
+ }
+
+ auto turn_order::round() const noexcept -> round_type
+ {
+ return m_round;
+ }
+
+ auto turn_order::running() const noexcept -> running_type
+ {
+ return m_running;
+ }
+
+ auto turn_order::size() const noexcept -> unsigned int
+ {
+ return m_model->get_n_items();
+ }
+
+ /** Properties */
+
+ auto turn_order::property_active_participant() const -> Glib::PropertyProxy_ReadOnly<active_participant_type>
+ {
+ return m_active_participant.get_proxy();
+ }
+
auto turn_order::property_empty() const -> Glib::PropertyProxy_ReadOnly<bool>
{
return m_empty.get_proxy();
}
+ auto turn_order::property_has_next() const -> Glib::PropertyProxy_ReadOnly<has_next_type>
+ {
+ return m_has_next.get_proxy();
+ }
+
+ auto turn_order::property_has_previous() const -> Glib::PropertyProxy_ReadOnly<has_previous_type>
+ {
+ return m_has_previous.get_proxy();
+ }
+
+ auto turn_order::property_running() const -> Glib::PropertyProxy_ReadOnly<running_type>
+ {
+ return m_running.get_proxy();
+ }
+
+ auto turn_order::property_round() const -> Glib::PropertyProxy_ReadOnly<round_type>
+ {
+ return m_round.get_proxy();
+ }
+
} // namespace turns::domain \ No newline at end of file