~ubuntu-branches/ubuntu/precise/gnome-games/precise-proposed

« back to all changes in this revision

Viewing changes to libgames-support/games-score.c

  • Committer: Package Import Robot
  • Author(s): Rodrigo Moya
  • Date: 2011-05-30 13:32:04 UTC
  • mfrom: (1.3.4)
  • mto: (163.1.3 precise)
  • mto: This revision was merged to the branch mainline in revision 143.
  • Revision ID: package-import@ubuntu.com-20110530133204-celaq1v1dsxc48q1
Tags: upstream-3.0.2
ImportĀ upstreamĀ versionĀ 3.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
 
22
22
#include "games-score.h"
23
23
 
24
 
GamesScore *
25
 
games_score_new (void)
26
 
{
27
 
  GamesScore *newscore;
 
24
G_DEFINE_TYPE (GamesScore, games_score, G_TYPE_OBJECT)
 
25
 
 
26
struct GamesScorePrivate {
 
27
  union {
 
28
    guint32 plain;
 
29
    gdouble time_double;                /* minutes.seconds */
 
30
  } value;
 
31
  time_t time;
 
32
  gchar *name;
 
33
};
 
34
 
 
35
/**
 
36
 * games_score_new:
 
37
 * 
 
38
 * Creates a new score object.
 
39
 * 
 
40
 * Return value: the new #GamesScore
 
41
 **/
 
42
GamesScore *
 
43
games_score_new ()
 
44
{
 
45
  return g_object_new (GAMES_TYPE_SCORE, NULL);
 
46
}
 
47
 
 
48
/**
 
49
 * games_score_new_plain:
 
50
 * @value: The value of the score.
 
51
 * 
 
52
 * Creates a new score object.
 
53
 * 
 
54
 * Return value: the new #GamesScore
 
55
 **/
 
56
GamesScore *
 
57
games_score_new_plain (guint32 value)
 
58
{
 
59
  GamesScore *score = g_object_new (GAMES_TYPE_SCORE, NULL);
 
60
  score->priv->value.plain = value;
 
61
  return score;
 
62
}
 
63
 
 
64
/**
 
65
 * games_score_new_time:
 
66
 * @value: The timer value of the score.
 
67
 * 
 
68
 * Creates a new score object.
 
69
 * 
 
70
 * Return value: the new #GamesScore
 
71
 **/
 
72
GamesScore *
 
73
games_score_new_time (gdouble value)
 
74
{
 
75
  GamesScore *score = g_object_new (GAMES_TYPE_SCORE, NULL);
 
76
  score->priv->value.time_double = value;
 
77
  return score;
 
78
}
 
79
 
 
80
const gchar *
 
81
games_score_get_name (GamesScore *score)
 
82
{
 
83
  return score->priv->name;
 
84
}
 
85
 
 
86
void
 
87
games_score_set_name (GamesScore *score, const gchar *name)
 
88
{
 
89
  g_free (score->priv->name);
 
90
  score->priv->name = g_strdup (name);
 
91
}
 
92
 
 
93
time_t
 
94
games_score_get_time (GamesScore *score)
 
95
{
 
96
  return score->priv->time;
 
97
}
 
98
 
 
99
void
 
100
games_score_set_time (GamesScore *score, time_t time)
 
101
{
 
102
  score->priv->time = time;  
 
103
}
 
104
 
 
105
guint32
 
106
games_score_get_value_as_plain (GamesScore *score)
 
107
{
 
108
  return score->priv->value.plain;
 
109
}
 
110
 
 
111
gdouble
 
112
games_score_get_value_as_time (GamesScore *score)
 
113
{
 
114
  return score->priv->value.time_double;
 
115
}
 
116
 
 
117
gint
 
118
games_score_compare (GamesScoreStyle style,
 
119
                     GamesScore * a,
 
120
                     GamesScore * b)
 
121
{
 
122
  switch (style) {
 
123
  case GAMES_SCORES_STYLE_PLAIN_DESCENDING:
 
124
    if (a->priv->value.plain > b->priv->value.plain)
 
125
      return +1;
 
126
    if (a->priv->value.plain < b->priv->value.plain)
 
127
      return -1;
 
128
    return 0;
 
129
  case GAMES_SCORES_STYLE_PLAIN_ASCENDING:
 
130
    if (a->priv->value.plain > b->priv->value.plain)
 
131
      return -1;
 
132
    if (a->priv->value.plain < b->priv->value.plain)
 
133
      return +1;
 
134
    return 0;
 
135
  case GAMES_SCORES_STYLE_TIME_DESCENDING:
 
136
    if (a->priv->value.time_double > b->priv->value.time_double)
 
137
      return +1;
 
138
    if (a->priv->value.time_double < b->priv->value.time_double)
 
139
      return -1;
 
140
    return 0;
 
141
  case GAMES_SCORES_STYLE_TIME_ASCENDING:
 
142
    if (a->priv->value.time_double > b->priv->value.time_double)
 
143
      return -1;
 
144
    if (a->priv->value.time_double < b->priv->value.time_double)
 
145
      return +1;
 
146
    return 0;
 
147
  default:
 
148
    g_warning
 
149
      ("Uknown score style in games_score_compare - treating as equal.");
 
150
    return 0;
 
151
  }
 
152
}
 
153
 
 
154
static void
 
155
games_score_finalize (GObject * object)
 
156
{
 
157
  GamesScore *score = GAMES_SCORE (object);
 
158
 
 
159
  g_free (score->priv->name);
 
160
 
 
161
  G_OBJECT_CLASS (games_score_parent_class)->finalize (object);
 
162
}
 
163
 
 
164
static void
 
165
games_score_class_init (GamesScoreClass * klass)
 
166
{
 
167
  GObjectClass *object_class = (GObjectClass *) klass;
 
168
 
 
169
  object_class->finalize = games_score_finalize;
 
170
 
 
171
  g_type_class_add_private (object_class, sizeof (GamesScorePrivate));
 
172
}
 
173
 
 
174
static void
 
175
games_score_init (GamesScore *score)
 
176
{
28
177
  const gchar* name;
29
178
 
30
 
  newscore = g_slice_new0 (GamesScore);
31
 
  newscore->time = time (NULL);
 
179
  score->priv = G_TYPE_INSTANCE_GET_PRIVATE (score, GAMES_TYPE_SCORE, GamesScorePrivate);
 
180
 
 
181
  score->priv->time = time (NULL);
32
182
  /* FIXME: We don't handle the "Unknown" case. */
33
183
  name = g_get_real_name ();
34
184
  if (name[0] == '\0' || g_utf8_validate (name, -1, NULL) != TRUE) {
37
187
      name = "";
38
188
    }
39
189
  }
40
 
  newscore->name = g_strdup (name);
41
 
 
42
 
  return newscore;
43
 
}
44
 
 
45
 
GamesScore *
46
 
games_score_dup (GamesScore * orig)
47
 
{
48
 
  GamesScore *new;
49
 
 
50
 
  new = g_slice_new (GamesScore);
51
 
  *new = *orig;
52
 
  new->name = g_strdup (orig->name);
53
 
 
54
 
  return new;
55
 
}
56
 
 
57
 
void
58
 
games_score_destroy (GamesScore * score)
59
 
{
60
 
  g_free (score->name);
61
 
  g_slice_free (GamesScore, score);
62
 
}
63
 
 
64
 
gint
65
 
games_score_compare_values (GamesScoreStyle style, GamesScoreValue a,
66
 
                            GamesScoreValue b)
67
 
{
68
 
  switch (style) {
69
 
  case GAMES_SCORES_STYLE_PLAIN_DESCENDING:
70
 
    if (a.plain > b.plain)
71
 
      return +1;
72
 
    if (a.plain < b.plain)
73
 
      return -1;
74
 
    return 0;
75
 
  case GAMES_SCORES_STYLE_PLAIN_ASCENDING:
76
 
    if (a.plain > b.plain)
77
 
      return -1;
78
 
    if (a.plain < b.plain)
79
 
      return +1;
80
 
    return 0;
81
 
  case GAMES_SCORES_STYLE_TIME_DESCENDING:
82
 
    if (a.time_double > b.time_double)
83
 
      return +1;
84
 
    if (a.time_double < b.time_double)
85
 
      return -1;
86
 
    return 0;
87
 
  case GAMES_SCORES_STYLE_TIME_ASCENDING:
88
 
    if (a.time_double > b.time_double)
89
 
      return -1;
90
 
    if (a.time_double < b.time_double)
91
 
      return +1;
92
 
    return 0;
93
 
  default:
94
 
    g_warning
95
 
      ("Uknown score style in games_score_compare - treating as equal.");
96
 
    return 0;
97
 
  }
98
 
}
99
 
 
100
 
gint
101
 
games_score_compare (GamesScoreStyle style, GamesScore * a, GamesScore * b)
102
 
{
103
 
  return games_score_compare_values (style, a->value, b->value);
 
190
  score->priv->name = g_strdup (name);
104
191
}