aboutsummaryrefslogtreecommitdiffstats
path: root/scoretbl.c
blob: 9684c3df46f9d338a4f510f73d95265199fc2d0d (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
#include <inttypes.h>
#include <limits.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "highscore.h"

#define MAXLINE 4096		/* Max length of a text line */

struct bests bests;

/*
 * Insert a total; return a pointer to the player name string
 * if applicable.  Return place [1,MAX_BEST] or 0 if no ranking
 */
int highscore_add_total(uint64_t gameid, int64_t score,
			int endlvl, unsigned char **pptr)
{
  struct best_total *tp = bests.total;
  int i;

  for ( i = 0 ; i < MAX_BEST ; i++, tp++ ) {
    if ( score > tp->score ) {
      memmove(tp+1, tp, (MAX_BEST-1-i)*sizeof(*tp));
      tp->gameid = gameid;
      tp->score  = score;
      tp->endlvl = endlvl;
      if ( pptr ) *pptr = tp->player;
      return i+1;
    } else if ( gameid == tp->gameid && score == tp->score ) {
      return i+1;
    }
  }

  if ( pptr ) *pptr = NULL;
  return 0;
}

/*
 * Insert a level best score, return rank (or 0 if N/A)
 */
int highscore_add_level_score(uint64_t gameid, int lvl, int64_t score)
{
  struct best_level_score *tp;
  int i;

  if ( lvl < 0 || lvl >= MAX_LEVEL )
    return 0;

  tp = bests.level[lvl].score;

  for ( i = 0 ; i < MAX_PER_LEVEL ; i++, tp++ ) {
    if ( score > tp->score ) {
      memmove(tp+1, tp, (MAX_PER_LEVEL-1-i)*sizeof(*tp));
      tp->gameid = gameid;
      tp->score  = score;
      return i+1;
    } else if ( gameid == tp->gameid && score == tp->score ) {
      return i+1;
    }
  }

  return 0;
}

/*
 * Insert a level best time, return rank (or 0 if N/A)
 */
int highscore_add_level_time(uint64_t gameid, int lvl, int32_t time_ms)
{
  struct best_level_time *tp;
  int i;

  if ( lvl < 0 || lvl >= MAX_LEVEL )
    return 0;

  tp = bests.level[lvl].time_ms;

  for ( i = 0 ; i < MAX_PER_LEVEL ; i++, tp++ ) {
    if ( time_ms < tp->time_ms ) {
      memmove(tp+1, tp, (MAX_PER_LEVEL-1-i)*sizeof(*tp));
      tp->gameid  = gameid;
      tp->time_ms = time_ms;
      return i+1;
    } else if ( gameid == tp->gameid && time_ms == tp->time_ms ) {
      return i+1;
    }
  }

  return 0;
}

/*
 * Initialize the highscore data structures to "nothing"
 */
void highscore_init(void)
{
  int i, j;

  memset(&bests, 0, sizeof bests);
  for ( i = 0 ; i < MAX_LEVEL ; i++ )
    for ( j = 0 ; j < MAX_PER_LEVEL ; j++ )
      bests.level[i].time_ms[j].time_ms = NO_TIME;
}

/*
 * Unquote a string
 */
static unsigned char *unquote(const char *i)
{
  static unsigned char buf[MAXLINE], *o;
  char sb[3];
  sb[2] = 0;

  o = buf;
  while ( isxdigit(sb[0] = i[0]) &&
	  isxdigit(sb[1] = i[1]) ) {
    *o++ = (unsigned char)strtoul(sb, NULL, 16);
    i += 2;
  }
  *o++ = '\0';
  
  return buf;
}

/*
 * Quote a string
 */
static char *quote(const unsigned char *i)
{
  static char buf[MAXLINE], *o;
  unsigned char c;

  o = buf;
  while ( (c = *i++) ) {
    sprintf(o, "%02x", c);
    o += 2;
  }
  *o++ = '0';			/* End with 00 so even a null string is ok */
  *o++ = '0';
  *o++ = '\0';

  return buf;
}


/*
 * Parse an incoming high-score table.  It can come either from the network
 * or from a file.
 */
void highscore_parse(FILE *f)
{
  char line[MAXLINE], pname[MAXLINE];
  uint64_t gameid;
  long long score;
  long time_ms;
  int lvl;
  unsigned char *pptr;

  while ( fgets(line, MAXLINE, f) ) {
    if ( !strchr(line, '\n') )
      return;			/* Something bogus here... */

    if ( sscanf(line, "TS %llx %lld %d %[0-9a-f]",
		&gameid, &score, &lvl, pname) == 4 ) {
      highscore_add_total(gameid, score, lvl, &pptr);
      if ( pptr ) {
	strncpy(pptr, unquote(pname), PLAYER_LEN+1);
	pptr[PLAYER_LEN] = '\0';
      }
    } else if ( sscanf(line, "LS %llx %d %lld",
		       &gameid, &lvl, &score) == 3 ) {
      highscore_add_level_score(gameid, lvl-1, score);
    } else if ( sscanf(line, "LT %llx %d %ld",
		       &gameid, &lvl, &time_ms) == 3 ) {
      highscore_add_level_time(gameid, lvl-1, time_ms);
    }
  }
}

/*
 * Write out a high score file; return 0 on success, -1 on failure
 */
int highscore_write(FILE *f)
{
  int i, j;

  fprintf(f, "VC 1.0\n");	/* Version C 1.0 */

  for ( i = 0 ; i < MAX_BEST ; i++ ) {
    if ( bests.total[i].score ) {
      fprintf(f, "TS %llx %lld %d %s\n", bests.total[i].gameid,
	      bests.total[i].score, bests.total[i].endlvl,
	      quote(bests.total[i].player));
    }
  }

  for ( i = 0 ; i < MAX_LEVEL ; i++ ) {
    for ( j = 0 ; j < MAX_PER_LEVEL ; j++ ) {
      if ( bests.level[i].score[j].score )
	fprintf(f, "LS %llx %d %lld\n", bests.level[i].score[j].gameid,
		i+1, bests.level[i].score[j].score);
    }
    for ( j = 0 ; j < MAX_PER_LEVEL ; j++ ) {
      if ( bests.level[i].time_ms[j].time_ms != NO_TIME )
	fprintf(f, "LT %llx %d %d\n", bests.level[i].time_ms[j].gameid,
		i+1, bests.level[i].time_ms[j].time_ms);
    }
  }

  return ferror(f);
}