diff options
Diffstat (limited to 'lib/src/turns-participant.c')
| -rw-r--r-- | lib/src/turns-participant.c | 264 |
1 files changed, 264 insertions, 0 deletions
diff --git a/lib/src/turns-participant.c b/lib/src/turns-participant.c new file mode 100644 index 0000000..18785db --- /dev/null +++ b/lib/src/turns-participant.c @@ -0,0 +1,264 @@ +/* + * SPDX-FileCopyrightText: 2025 Felix Morgner <felix.morgner@gmail.com> + * SPDX-License-Identifier: LGPL-2.1-only + */ + +#include "turns-participant.h" + +#include <glib-object.h> +#include <glib.h> +#include <math.h> + +struct _TurnsParticipant +{ + GObject parent_instance; + + gboolean active; + gboolean defeated; + TurnsParticipantDisposition disposition; + gchar * id; + gchar * name; + gfloat priority; +}; + +G_DEFINE_TYPE(TurnsParticipant, turns_participant, G_TYPE_OBJECT) + +enum PROPERTIES +{ + PROP_ACTIVE = 1, + PROP_DEFEATED, + PROP_DISPOSITION, + PROP_NAME, + PROP_PRIORITY, + N_PROPERTIES, +}; + +static GParamSpec * properties[N_PROPERTIES] = { + nullptr, +}; + +static void turns_participant_finalize(GObject * self) +{ + auto participant = TURNS_PARTICIPANT(self); + + g_free(participant->id); + g_free(participant->name); + + G_OBJECT_CLASS(turns_participant_parent_class)->finalize(self); +} + +static void turns_participant_get_property(GObject * self, guint id, GValue * value, GParamSpec * specification) +{ + auto participant = TURNS_PARTICIPANT(self); + + switch ((enum PROPERTIES)id) + { + case PROP_ACTIVE: + g_value_set_boolean(value, turns_participant_get_active(participant)); + return; + case PROP_DEFEATED: + g_value_set_boolean(value, turns_participant_get_defeated(participant)); + return; + case PROP_NAME: + g_value_set_string(value, turns_participant_get_name(participant)); + return; + case PROP_PRIORITY: + g_value_set_float(value, turns_participant_get_priority(participant)); + return; + case PROP_DISPOSITION: + g_value_set_enum(value, turns_participant_get_disposition(participant)); + return; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(self, id, specification); + }; +} + +static void turns_participant_set_property(GObject * self, guint id, GValue const * value, GParamSpec * specification) +{ + auto participant = TURNS_PARTICIPANT(self); + + switch ((enum PROPERTIES)id) + { + case PROP_ACTIVE: + turns_participant_set_active(participant, g_value_get_boolean(value)); + return; + case PROP_DEFEATED: + turns_participant_set_defeated(participant, g_value_get_boolean(value)); + return; + case PROP_NAME: + turns_participant_set_name(participant, g_value_get_string(value)); + return; + case PROP_PRIORITY: + turns_participant_set_priority(participant, g_value_get_float(value)); + return; + case PROP_DISPOSITION: + turns_participant_set_disposition(participant, (TurnsParticipantDisposition)g_value_get_enum(value)); + return; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(self, id, specification); + } +} + +static void turns_participant_class_init(TurnsParticipantClass * klass) +{ + GObjectClass * object_class = G_OBJECT_CLASS(klass); + + (void)object_class; + + object_class->get_property = turns_participant_get_property; + object_class->set_property = turns_participant_set_property; + object_class->finalize = turns_participant_finalize; + + properties[PROP_ACTIVE] = g_param_spec_boolean("active", + "Active", + "Whether or not the participant is currently active (taking their turn)", + false, + G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY); + + properties[PROP_DEFEATED] = g_param_spec_boolean("defeated", + "Defeated", + "Whether or not the participant has been defeated", + false, + G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY); + + properties[PROP_NAME] = g_param_spec_string("name", + "Name", + "The Name of the participant", + "", + G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY); + + properties[PROP_PRIORITY] = g_param_spec_float("priority", + "Priority", + "The turn priority of the participant", + -INFINITY, + +INFINITY, + 0.0f, + G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY); + + properties[PROP_DISPOSITION] = g_param_spec_enum("disposition", + "Disposition", + "Disposition of the participant toward the players", + turns_participant_disposition_get_type(), + TURNS_PARTICIPANT_DISPOSITION_NEUTRAL, + G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY); + + g_object_class_install_properties(object_class, N_PROPERTIES, properties); +} + +static void turns_participant_init(TurnsParticipant * self) +{ + self->id = g_uuid_string_random(); +} + +TurnsParticipant * turns_participant_new() +{ + return TURNS_PARTICIPANT(g_object_new(TURNS_TYPE_PARTICIPANT, nullptr)); +} + +TurnsParticipant * turns_participant_new_with(gchar const * name, gfloat priority, TurnsParticipantDisposition disposition) +{ + g_return_val_if_fail(name != nullptr, nullptr); + + return TURNS_PARTICIPANT(g_object_new(TURNS_TYPE_PARTICIPANT, "name", name, "priority", priority, "disposition", disposition, nullptr)); +} + +gboolean turns_participant_get_active(TurnsParticipant const * self) +{ + g_return_val_if_fail(TURNS_IS_PARTICIPANT((TurnsParticipant *)self), false); + return self->active; +} + +gboolean turns_participant_get_defeated(TurnsParticipant const * self) +{ + g_return_val_if_fail(TURNS_IS_PARTICIPANT((TurnsParticipant *)self), false); + return self->defeated; +} + +TurnsParticipantDisposition turns_participant_get_disposition(TurnsParticipant const * self) +{ + g_return_val_if_fail(TURNS_IS_PARTICIPANT((TurnsParticipant *)self), TURNS_PARTICIPANT_DISPOSITION_NEUTRAL); + return self->disposition; +} + +gchar const * turns_participant_get_id(TurnsParticipant const * self) +{ + g_return_val_if_fail(TURNS_IS_PARTICIPANT((TurnsParticipant *)self), nullptr); + return self->id; +} + +gchar const * turns_participant_get_name(TurnsParticipant const * self) +{ + g_return_val_if_fail(TURNS_IS_PARTICIPANT((TurnsParticipant *)self), nullptr); + return self->name; +} + +gfloat turns_participant_get_priority(TurnsParticipant const * self) +{ + g_return_val_if_fail(TURNS_IS_PARTICIPANT((TurnsParticipant *)self), 0.0f); + return self->priority; +} + +void turns_participant_set_active(TurnsParticipant * self, gboolean value) +{ + g_return_if_fail(TURNS_IS_PARTICIPANT(self)); + + if (value == self->active) + { + return; + } + + self->active = value; + g_object_notify_by_pspec(G_OBJECT(self), properties[PROP_ACTIVE]); +} + +void turns_participant_set_defeated(TurnsParticipant * self, gboolean value) +{ + g_return_if_fail(TURNS_IS_PARTICIPANT(self)); + + if (value == self->defeated) + { + return; + } + + self->defeated = value; + g_object_notify_by_pspec(G_OBJECT(self), properties[PROP_DEFEATED]); +} + +void turns_participant_set_disposition(TurnsParticipant * self, TurnsParticipantDisposition value) +{ + g_return_if_fail(TURNS_IS_PARTICIPANT(self)); + + if (value == self->disposition) + { + return; + } + + self->disposition = value; + g_object_notify_by_pspec(G_OBJECT(self), properties[PROP_DISPOSITION]); +} + +void turns_participant_set_name(TurnsParticipant * self, gchar const * value) +{ + g_return_if_fail(TURNS_IS_PARTICIPANT(self)); + g_return_if_fail(value != nullptr); + + if (!g_set_str(&self->name, value)) + { + return; + } + + g_object_notify_by_pspec(G_OBJECT(self), properties[PROP_NAME]); +} + +void turns_participant_set_priority(TurnsParticipant * self, gfloat value) +{ + g_return_if_fail(TURNS_IS_PARTICIPANT(self)); + + if (value == self->priority) + { + return; + } + + self->priority = value; + g_object_notify_by_pspec(G_OBJECT(self), properties[PROP_PRIORITY]); +} |
