Skip to content
Snippets Groups Projects
Commit 7ba452b4 authored by Bruce Cowan's avatar Bruce Cowan
Browse files

Replace GVariant with GPtrArray

Not the intended purpose for variants
parent 4fd81442
No related branches found
No related tags found
No related merge requests found
...@@ -95,58 +95,46 @@ rugby_score_store_class_init (RugbyScoreStoreClass *klass) ...@@ -95,58 +95,46 @@ rugby_score_store_class_init (RugbyScoreStoreClass *klass)
} }
static void static void
populate_store (RugbyScoreStore *store) populate_store_foreach (gpointer data,
gpointer user_data)
{ {
GtkListStore *lstore = GTK_LIST_STORE (store); RugbyPossibility *possiblity = (RugbyPossibility *) data;
GVariant *possibilities; GtkListStore *store = GTK_LIST_STORE (user_data);
GVariant *possibility;
GVariantIter iter; GtkTreeIter iter;
gsize total; gint tries = possiblity->tries;
gint utries = possiblity->utries;
gint kicks = possiblity->kicks;
g_autofree gchar *tooltip = g_strdup_printf ("%d tries, %d converted, %d kicks",
tries, utries, kicks);
gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter,
RUGBY_SCORE_STORE_TRIES, tries,
RUGBY_SCORE_STORE_UTRIES, utries,
RUGBY_SCORE_STORE_KICKS, kicks,
RUGBY_SCORE_STORE_TOOLTIP, tooltip,
-1);
}
static void
populate_store (RugbyScoreStore *self)
{
GtkListStore *store = GTK_LIST_STORE (self);
g_autoptr(GPtrArray) possibilities = NULL;
/* Clear the store */ /* Clear the store */
gtk_list_store_clear (lstore); gtk_list_store_clear (store);
possibilities = rugby_scoring_get_possibilities (store->score); possibilities = rugby_scoring_get_possibilities (self->score);
if (possibilities == NULL) if (possibilities->len == 0)
return; return;
total = g_variant_iter_init (&iter, possibilities); g_ptr_array_foreach (possibilities, populate_store_foreach, store);
while ((possibility = g_variant_iter_next_value (&iter)))
{
gint tries, utries, kicks;
GString *tooltip;
GtkTreeIter iter;
g_variant_get (possibility, "(iii)", &tries, &utries, &kicks);
g_variant_unref (possibility);
tooltip = g_string_new_len (NULL, 20);
if (utries > 0 || tries > 0)
{
g_string_append_printf (tooltip, "%d tries, %d converted",
tries + utries, tries);
if (kicks > 0)
g_string_append_printf (tooltip, ", %d kicks",
kicks);
}
else if (utries == 0 && tries == 0)
g_string_append_printf (tooltip, "%d kicks",
kicks);
/* Put result in list store */
gtk_list_store_append (lstore, &iter);
gtk_list_store_set (lstore, &iter,
RUGBY_SCORE_STORE_TRIES, tries,
RUGBY_SCORE_STORE_UTRIES, utries,
RUGBY_SCORE_STORE_KICKS, kicks,
RUGBY_SCORE_STORE_TOOLTIP, tooltip->str,
-1);
g_string_free (tooltip, TRUE);
}
g_signal_emit (store, signals[FINISHED], 0, (gint) total); g_signal_emit (self, signals[FINISHED], 0, (gint) possibilities->len);
} }
static void static void
......
#include "rugby-scoring.h" #include "rugby-scoring.h"
static inline void
possibility_free (gpointer possibility)
{
g_slice_free (RugbyPossibility, possibility);
}
/** /**
* rugby_scoring_get_possibilities: * rugby_scoring_get_possibilities:
* @score: the score of a team * @score: the score of a team
* *
* Gets the possible ways of scoring @score points, as a %GVariant of the * Gets the possible ways of scoring @score points.
* type "a(iii)", in the order "tries, utries, pens".
* *
* Returns: (transfer-none): the possibilities, or %NULL in the case there are * Returns: (transfer-full): the possibilities
* none
*/ */
GVariant * GPtrArray *
rugby_scoring_get_possibilities (gint score) rugby_scoring_get_possibilities (gint score)
{ {
GVariantBuilder builder; GPtrArray *array;
gint possibilities = 0;
gint max_tries, max_utries;
gint tries, utries;
g_return_val_if_fail (score >= 0, NULL);
g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY); g_return_val_if_fail (score >= 0, NULL);
max_tries = (gint) (score / TRY_POINTS); array = g_ptr_array_new_with_free_func (possibility_free);
max_utries = (gint) (score / UTRY_POINTS);
for (tries = 0; tries <= max_tries; tries++) gint possibilities = 0;
{ gint max_tries = score / TRY_POINTS;
for (utries = 0; utries <= max_utries; utries++) gint max_utries = score / UTRY_POINTS;
{
gint left;
/* Calculate left over score for penalties */ for (gint tries = 0; tries <= max_tries; tries++)
left = score - (tries * TRY_POINTS) - (utries * UTRY_POINTS); {
for (gint utries = 0; utries <= max_utries; utries++)
{
gint left = score - (tries * TRY_POINTS) - (utries * UTRY_POINTS);
if (left < 0) if (left < 0)
continue; continue;
/* If the score after pens is 0, it's a possibility */ if (left % KICK_POINTS == 0)
if (left % KICK_POINTS == 0) {
{ gint kicks = left / KICK_POINTS;
gint pens;
pens = left / KICK_POINTS; RugbyPossibility *possibility = g_slice_new (RugbyPossibility);
possibility->tries = tries;
possibility->utries = utries;
possibility->kicks = kicks;
/* Add result to variant */ g_ptr_array_add (array, possibility);
g_variant_builder_add (&builder, "(iii)", tries, utries, pens); possibilities++;
possibilities++; }
} }
} }
}
if (possibilities == 0) return array;
return NULL;
else
return g_variant_builder_end (&builder);
} }
/** /**
......
...@@ -11,14 +11,21 @@ G_BEGIN_DECLS ...@@ -11,14 +11,21 @@ G_BEGIN_DECLS
typedef enum typedef enum
{ {
RUGBY_SCORE_TYPE_TRY, RUGBY_SCORE_TYPE_TRY,
RUGBY_SCORE_TYPE_UTRY, RUGBY_SCORE_TYPE_UTRY,
RUGBY_SCORE_TYPE_KICK RUGBY_SCORE_TYPE_KICK
} RugbyScoreType; } RugbyScoreType;
GVariant * rugby_scoring_get_possibilities (gint score); typedef struct
gint rugby_scoring_get_max_tries (gint score); {
gint rugby_scoring_get_max_kicks (gint score); gint tries;
gint utries;
gint kicks;
} RugbyPossibility;
GPtrArray * rugby_scoring_get_possibilities (gint score);
gint rugby_scoring_get_max_tries (gint score);
gint rugby_scoring_get_max_kicks (gint score);
G_END_DECLS G_END_DECLS
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment