4 #include "highscores.h"
6 static int highscore_cmp_points( void *a
, void *b
)
8 highscore_record
*pa
= a
, *pb
= b
;
9 return (int)pa
->points
- (int)pb
->points
;
12 static int highscore_cmp_datetime( void *a
, void *b
)
14 highscore_record
*pa
= a
, *pb
= b
;
16 if( pa
->datetime
== pb
->datetime
) return 0;
17 return pa
->datetime
< pb
->datetime
? 1: -1;
20 static int highscore_cmp_time( void *a
, void *b
)
22 highscore_record
*pa
= a
, *pb
= b
;
23 return (int)pb
->time
- (int)pa
->time
;
26 static int highscore_cmp_playerid( void *a
, void *b
)
28 highscore_record
*pa
= a
, *pb
= b
;
29 if( pa
->playerid
== pb
->playerid
) return 0;
30 return pa
->playerid
< pb
->playerid
? -1: 1;
33 static int highscore_cmp_playerinfo_playerid( void *a
, void *b
)
35 highscore_playerinfo
*pa
= a
, *pb
= b
;
36 if( pa
->playerid
== pb
->playerid
) return 0;
37 return pa
->playerid
< pb
->playerid
? -1: 1;
40 static void highscores_create_db(void)
42 struct highscore_system
*sys
= &highscore_system
;
44 vg_info( "Initializing database nodes\n" );
45 memset( &sys
->dbheader
, 0, sizeof(highscore_database
) );
47 sys
->dbheader
.pool_head
= aatree_init_pool( &sys
->aainfo
, sys
->pool_size
);
48 sys
->dbheader
.entry_capacity
= sys
->pool_size
;
50 for( int i
=0; i
<vg_list_size(sys
->dbheader
.tracks
); i
++ )
52 highscore_track_table
*table
= &sys
->dbheader
.tracks
[i
];
53 table
->root_points
= AATREE_PTR_NIL
;
54 table
->root_playerid
= AATREE_PTR_NIL
;
55 table
->root_time
= AATREE_PTR_NIL
;
56 table
->root_datetime
= AATREE_PTR_NIL
;
59 /* Initialize secondary db */
60 sys
->dbheader
.playerinfo_head
= aatree_init_pool(
61 &sys
->aainfo_playerinfo
,
62 sys
->playerinfo_pool_size
);
63 sys
->dbheader
.playerinfo_capacity
= sys
->playerinfo_pool_size
;
64 sys
->dbheader
.playerinfo_root
= AATREE_PTR_NIL
;
67 static int highscores_read(void)
69 struct highscore_system
*sys
= &highscore_system
;
71 FILE *fp
= fopen( ".aadb", "rb" );
73 vg_info( "Loading existing database\n" );
75 u64 count
= fread( &sys
->dbheader
, sizeof(highscore_database
), 1, fp
);
79 vg_error( "Unexpected EOF reading database header\n" );
83 count
= fread( sys
->data
, sizeof(highscore_record
),
86 if( count
!= sys
->pool_size
)
88 vg_error( "Unexpected EOF reading database contents;"
89 " %lu records of %u were read\n", count
, sys
->pool_size
);
93 count
= fread( sys
->playerinfo_data
, sizeof(highscore_playerinfo
),
94 sys
->playerinfo_pool_size
, fp
);
96 if( count
!= sys
->playerinfo_pool_size
)
98 vg_error( "Unexpected EOF reading playerinfo contents;"
99 " %lu records of %u were read\n", count
,
100 sys
->playerinfo_pool_size
);
109 vg_low( "No existing database found (.aadb)\n" );
114 static void highscores_init( u32 pool_size
, u32 playerinfo_pool_size
)
116 struct highscore_system
*sys
= &highscore_system
;
118 sys
->data
= vg_linear_alloc( vg_mem
.rtmemory
,
119 pool_size
*sizeof(highscore_record
) );
121 sys
->playerinfo_data
=
122 vg_linear_alloc( vg_mem
.rtmemory
,
123 playerinfo_pool_size
* sizeof(highscore_playerinfo
) );
125 memset( sys
->data
, 0, pool_size
*sizeof(highscore_record
) );
126 memset( sys
->playerinfo_data
, 0,
127 playerinfo_pool_size
*sizeof(highscore_playerinfo
) );
130 /* This is ugly.. too bad! */
131 sys
->aainfo
.base
= highscore_system
.data
;
132 sys
->aainfo
.stride
= sizeof(highscore_record
);
133 sys
->aainfo
.offset
= offsetof(highscore_record
,pool
);
134 sys
->aainfo
.p_cmp
= NULL
;
136 sys
->aainfo_datetime
.base
= highscore_system
.data
;
137 sys
->aainfo_datetime
.stride
= sizeof(highscore_record
);
138 sys
->aainfo_datetime
.offset
= offsetof(highscore_record
,aa
.datetime
);
139 sys
->aainfo_datetime
.p_cmp
= highscore_cmp_datetime
;
141 sys
->aainfo_points
.base
= highscore_system
.data
;
142 sys
->aainfo_points
.stride
= sizeof(highscore_record
);
143 sys
->aainfo_points
.offset
= offsetof(highscore_record
,aa
.points
);
144 sys
->aainfo_points
.p_cmp
= highscore_cmp_points
;
146 sys
->aainfo_time
.base
= highscore_system
.data
;
147 sys
->aainfo_time
.stride
= sizeof(highscore_record
);
148 sys
->aainfo_time
.offset
= offsetof(highscore_record
,aa
.time
);
149 sys
->aainfo_time
.p_cmp
= highscore_cmp_time
;
151 sys
->aainfo_playerid
.base
= highscore_system
.data
;
152 sys
->aainfo_playerid
.stride
= sizeof(highscore_record
);
153 sys
->aainfo_playerid
.offset
= offsetof(highscore_record
,aa
.playerid
);
154 sys
->aainfo_playerid
.p_cmp
= highscore_cmp_playerid
;
156 sys
->aainfo_playerinfo_playerid
.base
= highscore_system
.playerinfo_data
;
157 sys
->aainfo_playerinfo_playerid
.stride
= sizeof(highscore_playerinfo
);
158 sys
->aainfo_playerinfo_playerid
.offset
=
159 offsetof(highscore_playerinfo
,aa_playerid
);
160 sys
->aainfo_playerinfo_playerid
.p_cmp
= highscore_cmp_playerinfo_playerid
;
162 sys
->aainfo_playerinfo
.base
= highscore_system
.playerinfo_data
;
163 sys
->aainfo_playerinfo
.stride
= sizeof(highscore_playerinfo
);
164 sys
->aainfo_playerinfo
.offset
= offsetof(highscore_playerinfo
,aapn
);
165 sys
->aainfo_playerinfo
.p_cmp
= NULL
;
167 sys
->playerinfo_pool_size
= playerinfo_pool_size
;
168 sys
->pool_size
= pool_size
;
171 static int highscores_serialize_all(void)
173 struct highscore_system
*sys
= &highscore_system
;
174 vg_info( "Serializing database\n" );
176 FILE *fp
= fopen( ".aadb", "wb" );
180 vg_error( "Could not open .aadb\n" );
184 fwrite( &sys
->dbheader
, sizeof(highscore_database
), 1, fp
);
185 fwrite( sys
->data
, sizeof(highscore_record
),
186 sys
->dbheader
.entry_capacity
, fp
);
187 fwrite( sys
->playerinfo_data
, sizeof(highscore_playerinfo
),
188 sys
->dbheader
.playerinfo_capacity
, fp
);
194 static highscore_record
*highscore_find_user_record( u64 playerid
,
197 struct highscore_system
*sys
= &highscore_system
;
199 highscore_track_table
*table
= &sys
->dbheader
.tracks
[trackid
];
200 highscore_record temp
;
201 temp
.playerid
= playerid
;
204 aatree_find( &sys
->aainfo_playerid
, table
->root_playerid
, &temp
);
206 if( find
== AATREE_PTR_NIL
)
209 return aatree_get_data( &sys
->aainfo_playerid
, find
);
212 static aatree_ptr
highscores_push_record( highscore_record
*record
)
214 struct highscore_system
*sys
= &highscore_system
;
216 vg_low( "Inserting record into database for track %hu\n",record
->trackid
);
218 if( record
->trackid
>= vg_list_size(sys
->dbheader
.tracks
) ){
219 vg_error( "TrackID out of range (%hu>=%d)\n", record
->trackid
,
220 vg_list_size(sys
->dbheader
.tracks
) );
222 return AATREE_PTR_NIL
;
225 /* Search for existing record on this track */
226 highscore_track_table
*table
= &sys
->dbheader
.tracks
[record
->trackid
];
227 aatree_ptr existing
= aatree_find( &sys
->aainfo_playerid
,
228 table
->root_playerid
,
231 if( existing
!= AATREE_PTR_NIL
){
232 highscore_record
*crecord
= aatree_get_data( &sys
->aainfo_playerid
,
235 if( crecord
->time
< record
->time
||
236 (crecord
->time
== record
->time
&& crecord
->points
> record
->points
))
238 vg_low( "Not overwriting better score\n" );
242 vg_low( "Freeing existing record for player %lu\n", record
->playerid
);
243 table
->root_playerid
= aatree_del( &sys
->aainfo_playerid
, existing
);
244 table
->root_datetime
= aatree_del( &sys
->aainfo_datetime
, existing
);
245 table
->root_points
= aatree_del( &sys
->aainfo_points
, existing
);
246 table
->root_time
= aatree_del( &sys
->aainfo_time
, existing
);
248 aatree_pool_free( &sys
->aainfo
, existing
, &sys
->dbheader
.pool_head
);
252 aatree_pool_alloc( &sys
->aainfo
, &sys
->dbheader
.pool_head
);
254 if( index
== AATREE_PTR_NIL
)
256 vg_error( "Database records are over capacity!\n" );
260 highscore_record
*dst
= aatree_get_data( &sys
->aainfo
, index
);
261 memset( dst
, 0, sizeof(highscore_record
) );
263 dst
->trackid
= record
->trackid
;
264 dst
->datetime
= record
->datetime
;
265 dst
->playerid
= record
->playerid
;
266 dst
->points
= record
->points
;
267 dst
->time
= record
->time
;
270 aatree_insert( &sys
->aainfo_time
, table
->root_time
, index
);
271 table
->root_datetime
=
272 aatree_insert( &sys
->aainfo_datetime
, table
->root_datetime
, index
);
273 table
->root_playerid
=
274 aatree_insert( &sys
->aainfo_playerid
, table
->root_playerid
, index
);
276 aatree_insert( &sys
->aainfo_points
, table
->root_points
, index
);
281 static aatree_ptr
highscore_set_user_nickname( u64 steamid
, char nick
[16] )
284 for( int i
=0; i
<16; i
++ )
288 vg_low( "Updating %lu's nickname -> %s\n", steamid
, name
);
290 struct highscore_system
*sys
= &highscore_system
;
292 highscore_playerinfo temp
;
293 temp
.playerid
= steamid
;
295 aatree_ptr record
= aatree_find( &sys
->aainfo_playerinfo_playerid
,
296 sys
->dbheader
.playerinfo_root
,
298 highscore_playerinfo
*info
;
300 if( record
!= AATREE_PTR_NIL
)
302 info
= aatree_get_data( &sys
->aainfo_playerinfo
, record
);
306 record
= aatree_pool_alloc( &sys
->aainfo_playerinfo
,
307 &sys
->dbheader
.playerinfo_head
);
309 if( record
== AATREE_PTR_NIL
)
311 vg_error( "Player info database is over capacity!\n" );
312 return AATREE_PTR_NIL
;
315 info
= aatree_get_data( &sys
->aainfo_playerinfo
, record
);
316 memset( info
, 0, sizeof(highscore_playerinfo
) );
318 info
->playerid
= steamid
;
319 sys
->dbheader
.playerinfo_root
= aatree_insert(
320 &sys
->aainfo_playerinfo_playerid
,
321 sys
->dbheader
.playerinfo_root
, record
);
324 for( int i
=0; i
<16; i
++ )
325 info
->nickname
[i
] = nick
[i
];
327 return AATREE_PTR_NIL
;
330 /* Get the length of a string, bounded by '\0' or len, whichever is first */
331 static int highscore_strlen( const char *str
, int len
)
334 for( str_length
=0; str_length
<len
; str_length
++ )
335 if( !str
[str_length
] )
341 /* Print the string(max length:len) centered into buf (has width:width) */
342 static void highscore_strc( char *buf
, const char *str
, int len
, int width
)
344 int str_length
= highscore_strlen( str
, len
),
345 offs
= (width
-str_length
)/2;
347 for( int i
=0; i
<str_length
; i
++ )
358 /* Print the string(max length:len) left aligned into buf */
359 static void highscore_strl( char *buf
, const char *str
, int len
)
361 for( int i
=0; i
<len
; i
++ )
370 /* Print the string (max length:len) right aligned into buf (has width:width) */
371 static void highscore_strr( char *buf
, const char *str
, int len
, int width
)
373 int str_length
= highscore_strlen( str
, len
);
375 for( int i
=0; i
<len
; i
++ )
380 buf
[width
-str_length
+i
] = str
[i
];
384 /* Print integer (padded with: alt), right aligned into buf(width: len)
385 * returns number of digits (not including alt), that were written to buf */
386 static int highscore_intr( char *buf
, int value
, int len
, char alt
)
393 buf
[ len
-1 - (i
++) ] = '0' + (value
% 10);
398 buf
[ len
-1 - i
] = alt
;
403 /* Print integer into buffer with max length len
404 * retuns the number of digits written to buf */
405 static int highscore_intl( char *buf
, int value
, int len
){
413 temp
[ i
++ ] = '0' + (value
% 10);
420 for( int j
=0; j
<i
; j
++ )
421 buf
[j
] = temp
[ i
-1-j
];
431 /* Clear buffer with length using clr character */
432 static void highscore_clear( char *buf
, char clr
, int length
)
434 for( int i
=0; i
<length
; i
++ )
440 --------------------------
441 #| Player | Time | Pts
442 1|aaaabbbbcc 5:23.32 30000
444 3|aaabbbcccl 2:30.45 20000
454 /* Generate a highscores board in text form, the width is always 27. Buffer
455 * must be (count+3)*27 in size. */
456 static void highscores_board_generate( char *buf
, u32 id
, u32 count
)
459 highscore_clear( buf
, ' ', (count
+3)*w
);
461 struct track_info
*inf
= &track_infos
[id
];
462 struct highscore_system
*sys
= &highscore_system
;
464 highscore_track_table
*table
= &sys
->dbheader
.tracks
[ id
];
465 aatree_ptr it
= aatree_kth( &sys
->aainfo_time
, table
->root_time
, 0 );
467 highscore_strc ( buf
+w
*0, inf
->name
, w
,w
);
468 highscore_clear( buf
+w
*1, '-', w
);
469 highscore_strl ( buf
+w
*2, " #| Player | Time ", 27 );
471 for( int i
=0; i
<count
; i
++ )
473 char *line
= buf
+w
*(3+i
);
474 highscore_intr( line
, i
+1, 2, ' ' );
477 if( it
== AATREE_PTR_NIL
)
480 highscore_record
*record
= aatree_get_data( &sys
->aainfo_time
, it
);
481 highscore_playerinfo temp
;
482 temp
.playerid
= record
->playerid
;
484 aatree_ptr info_ptr
= aatree_find( &sys
->aainfo_playerinfo_playerid
,
485 sys
->dbheader
.playerinfo_root
,
489 if( info_ptr
== AATREE_PTR_NIL
)
490 highscore_strl( line
+3, "unknown", 16 );
493 highscore_playerinfo
*inf
= aatree_get_data(
494 &sys
->aainfo_playerinfo_playerid
, info_ptr
);
496 highscore_strl( line
+3, inf
->nickname
, 16 );
498 /* yep, this is fucking awesome! */
499 if( inf
->playerid
== 0x8297744501001001 ||
500 inf
->playerid
== 0x1ec4620101001001 ||
501 inf
->playerid
== 0x0110000145749782 ||
502 inf
->playerid
== 0x011000010162c41e )
505 /* FIXME: Clear line, or yknow, do it properly */
509 u16 centiseconds
= record
->time
,
510 seconds
= centiseconds
/ 100,
511 minutes
= seconds
/ 60;
517 if( minutes
> 9 ) minutes
= 9;
520 highscore_intr( line
+20, minutes
, 1, '0' );
522 highscore_intr( line
+22, seconds
, 2, '0' );
524 highscore_intr( line
+25, centiseconds
, 2, '0' );
528 highscore_intl( line
+22, record
->points
, 5 );
530 it
= aatree_next( &sys
->aainfo_time
, it
);
534 /* Print string out to file using newlines. Count is number of records
535 * ( this requires a buffer of (count+3)*27 size */
536 static void highscores_board_printf( FILE *fp
, const char *buf
, u32 count
)
540 for( int i
=0; i
<count
+3; i
++ )
541 fprintf( fp
, "%.27s\n", &buf
[i
*w
] );
544 #endif /* HIGHSCORES_C */