summaryrefslogtreecommitdiff
path: root/lib/src/turns-turn-order.c
blob: 186188839c135a27dc99f8d3d010de45a404b1dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
 * SPDX-FileCopyrightText: 2025 Felix Morgner <felix.morgner@gmail.com>
 * SPDX-License-Identifier: LGPL-2.1-only
 */

#include "turns-turn-order.h"

#include "turns-participant.h"

#include <gio/gio.h>
#include <glib-object.h>
#include <glib.h>

#include <stdint.h>

enum
{
  PROP_EMPTY = 1,
  PROP_PARTICIPANT_COUNT,
  PROP_RUNNING,
  PROP_SORT_MODE,
  N_PROPERTIES,
};

struct _TurnsTurnOrder
{
  GObject parent_instance;

  GSList * participants;
  gboolean running;
  TurnsTurnOrderSortMode sort_mode;
};

static GParamSpec * properties[N_PROPERTIES] = {
    nullptr,
};

static void turns_turn_order_list_model_init(GListModelInterface * iface);

G_DEFINE_FINAL_TYPE_WITH_CODE(TurnsTurnOrder,
                              turns_turn_order,
                              G_TYPE_OBJECT,
                              G_IMPLEMENT_INTERFACE(G_TYPE_LIST_MODEL, turns_turn_order_list_model_init))

static gint compare_participant(void const * lhs, void const * rhs, void * sort_mode)
{
  auto left_priority = turns_participant_get_priority(lhs);
  auto right_priority = turns_participant_get_priority(rhs);

  auto result = 0;

  if (left_priority < right_priority)
  {
    result = -1;
  }
  else if (left_priority > right_priority)
  {
    result = 1;
  }

  if ((TurnsTurnOrderSortMode)(long long)sort_mode == TURNS_TURN_ORDER_SORT_MODE_DESCENDING)
  {
    return result * -1;
  }

  return result;
}

static void sort_participants(TurnsTurnOrder * self)
{
  auto const sort_mode = turns_turn_order_get_sort_mode(self);

  self->participants = g_slist_sort_with_data(self->participants, &compare_participant, (void *)sort_mode);

  auto const participant_count = turns_turn_order_get_participant_count(self);

  g_list_model_items_changed(G_LIST_MODEL(self), 0, participant_count, participant_count);
}

static void clear_participants(TurnsTurnOrder * self)
{
  g_return_if_fail(TURNS_IS_TURN_ORDER(self));

  g_slist_free_full(self->participants, g_object_unref);
  self->participants = nullptr;

  g_object_notify_by_pspec(G_OBJECT(self), properties[PROP_EMPTY]);
}

static void set_running(TurnsTurnOrder * self, gboolean value)
{
  g_return_if_fail(TURNS_IS_TURN_ORDER(self));

  if (self->running == value)
  {
    return;
  }

  self->running = value;
  g_object_notify_by_pspec(G_OBJECT(self), properties[PROP_RUNNING]);
}

static void handle_participant_property_changed(TurnsParticipant const *, GParamSpec *, TurnsTurnOrder * self)
{
  sort_participants(self);
}

static void turns_turn_order_get_property(GObject * self, guint id, GValue * value, GParamSpec * specification)
{
  auto instance = TURNS_TURN_ORDER(self);

  switch (id)
  {
  case PROP_EMPTY:
    g_value_set_boolean(value, turns_turn_order_get_empty(instance));
    return;
  case PROP_PARTICIPANT_COUNT:
    g_value_set_uint64(value, turns_turn_order_get_participant_count(instance));
    return;
  case PROP_RUNNING:
    g_value_set_boolean(value, turns_turn_order_get_running(instance));
    return;
  case PROP_SORT_MODE:
    g_value_set_enum(value, turns_turn_order_get_sort_mode(instance));
    return;
  default:
    G_OBJECT_WARN_INVALID_PROPERTY_ID(self, id, specification);
  }
}

static void turns_turn_order_set_property(GObject * self, guint id, GValue const * value, GParamSpec * specification)
{
  auto instance = TURNS_TURN_ORDER(self);

  switch (id)
  {
  case PROP_SORT_MODE:
    turns_turn_order_set_sort_mode(instance, g_value_get_enum(value));
    return;
  default:
    G_OBJECT_WARN_INVALID_PROPERTY_ID(self, id, specification);
  }
}

static void turns_turn_order_init(TurnsTurnOrder * self)
{
  self->participants = nullptr;
  self->running = false;
  self->sort_mode = TURNS_TURN_ORDER_SORT_MODE_DESCENDING;
}

static void turns_turn_order_finalize(GObject * self)
{
  auto instance = TURNS_TURN_ORDER(self);

  clear_participants(instance);

  G_OBJECT_CLASS(turns_turn_order_parent_class)->finalize(self);
}

static void turns_turn_order_class_init(TurnsTurnOrderClass * klass)
{
  auto object_class = G_OBJECT_CLASS(klass);

  object_class->finalize = turns_turn_order_finalize;
  object_class->get_property = turns_turn_order_get_property;
  object_class->set_property = turns_turn_order_set_property;

  properties[PROP_EMPTY] = g_param_spec_boolean("empty",
                                                "Empty",
                                                "Whether the turn order is empty.",
                                                TRUE,
                                                G_PARAM_STATIC_STRINGS | G_PARAM_READABLE | G_PARAM_EXPLICIT_NOTIFY);

  properties[PROP_PARTICIPANT_COUNT] = g_param_spec_uint64("participant-count",
                                                           "Participant count",
                                                           "The number of participants in the turn order.",
                                                           0,
                                                           UINT64_MAX,
                                                           0,
                                                           G_PARAM_STATIC_STRINGS | G_PARAM_READABLE | G_PARAM_EXPLICIT_NOTIFY);

  properties[PROP_RUNNING] = g_param_spec_boolean("running",
                                                  "Running",
                                                  "Whether or not the turn order is running (e.g. has been started)",
                                                  false,
                                                  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",
                                                 TURNS_TYPE_TURN_ORDER_SORT_MODE,
                                                 TURNS_TURN_ORDER_SORT_MODE_DESCENDING,
                                                 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);

  g_object_class_install_properties(object_class, N_PROPERTIES, properties);
}

static gpointer turns_turn_order_list_model_get_item(GListModel * self, guint position)
{
  g_return_val_if_fail(position < turns_turn_order_get_participant_count(TURNS_TURN_ORDER(self)), nullptr);
  return g_object_ref(g_slist_nth_data(TURNS_TURN_ORDER(self)->participants, position));
}

static guint turns_turn_order_list_model_get_n_items(GListModel * self)
{
  return turns_turn_order_get_participant_count(TURNS_TURN_ORDER(self));
}

static GType turns_turn_order_list_model_get_item_type(GListModel * self)
{
  (void)self;
  return TURNS_TYPE_PARTICIPANT;
}

static void turns_turn_order_list_model_init(GListModelInterface * iface)
{
  iface->get_item = turns_turn_order_list_model_get_item;
  iface->get_item_type = turns_turn_order_list_model_get_item_type;
  iface->get_n_items = turns_turn_order_list_model_get_n_items;
}

TurnsTurnOrder * turns_turn_order_new()
{
  return TURNS_TURN_ORDER(g_object_new(TURNS_TYPE_TURN_ORDER, nullptr));
}

void turns_turn_order_add(TurnsTurnOrder * self, TurnsParticipant * participant)
{
  g_return_if_fail(participant != nullptr);

  g_signal_connect(participant, "notify::priority", (GCallback)&handle_participant_property_changed, self);

  auto sort_mode = turns_turn_order_get_sort_mode(self);

  auto old_head = self->participants;
  self->participants = g_slist_insert_sorted_with_data(self->participants, g_object_ref(participant), compare_participant, (void *)sort_mode);

  auto position = g_slist_index(self->participants, participant);

  if (old_head == nullptr && !turns_turn_order_get_empty(self))
  {
    g_object_notify_by_pspec(G_OBJECT(self), properties[PROP_EMPTY]);
  }

  g_list_model_items_changed(G_LIST_MODEL(self), position, 0, 1);
}

void turns_turn_order_clear(TurnsTurnOrder * self)
{
  g_return_if_fail(TURNS_IS_TURN_ORDER(self));

  auto old_size = turns_turn_order_get_participant_count(self);
  clear_participants(self);

  g_object_freeze_notify(G_OBJECT(self));

  set_running(self, false);

  g_object_thaw_notify(G_OBJECT(self));

  g_list_model_items_changed(G_LIST_MODEL(self), 0, old_size, 0);
}

void turns_turn_order_remove_at(TurnsTurnOrder * self, guint position)
{
  g_return_if_fail(TURNS_IS_TURN_ORDER(self));

  auto element = g_slist_nth(self->participants, position);

  if (!element)
  {
    return;
  }

  g_object_unref(element->data);
  auto old_head = self->participants;
  self->participants = g_slist_delete_link(self->participants, element);

  if (old_head != nullptr && turns_turn_order_get_empty(self))
  {
    g_object_notify_by_pspec(G_OBJECT(self), properties[PROP_EMPTY]);
    set_running(self, false);
  }

  g_list_model_items_changed(G_LIST_MODEL(self), position, 1, 0);
}

gboolean turns_turn_order_get_empty(TurnsTurnOrder const * self)
{
  g_return_val_if_fail(TURNS_IS_TURN_ORDER((TurnsTurnOrder *)self), true);
  return self->participants == nullptr;
}

gsize turns_turn_order_get_participant_count(TurnsTurnOrder const * self