the coolest fucking thing ive ever made
[carveJwlIkooP6JGAAIwe30JlM.git] / highscores.h
index 78db2ad3105961b74a2f6115ba15c8d7e1e0858c..f19f93a2fd2494ef4b6635a7d0a512d712e76365 100644 (file)
@@ -1,3 +1,7 @@
+/*
+ * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
+ */
+
 #ifndef HIGHSCORES_H
 #define HIGHSCORES_H
 
@@ -21,7 +25,7 @@ typedef struct highscore_playerinfo highscore_playerinfo;
 
 struct highscore_playerinfo
 {
-   char nickname[10];
+   char nickname[16];
    u64 playerid;
 
    union
@@ -88,6 +92,8 @@ static struct highscore_system
           aainfo_playerinfo;
 
    void *data, *playerinfo_data;
+
+   u32 pool_size, playerinfo_pool_size;
 }
 highscore_system;
 
@@ -128,7 +134,7 @@ static int highscore_cmp_playerinfo_playerid( void *a, void *b )
 static void *highscore_malloc( u32 count, u32 size )
 {
    size_t requested_mem = size * count;
-   void *data = malloc( requested_mem );
+   void *data = vg_alloc( requested_mem );
 
    requested_mem /= 1024;
    requested_mem /= 1024;
@@ -146,24 +152,90 @@ static void *highscore_malloc( u32 count, u32 size )
 
 static void highscores_free(void)
 {
-   free( highscore_system.data );
-   free( highscore_system.playerinfo_data );
+   vg_free( highscore_system.data );
+   vg_free( highscore_system.playerinfo_data );
 }
 
-static int highscores_init( u32 pool_size, u32 playerinfo_pool_size )
+static void highscores_create_db(void)
 {
    struct highscore_system *sys = &highscore_system;
 
-   sys->data = highscore_malloc( pool_size, sizeof(highscore_record) );
-   if( !sys->data ) return 0;
+   vg_info( "Initializing database nodes\n" );
+   memset( &sys->dbheader, 0, sizeof(highscore_database) );
+
+   sys->dbheader.pool_head = aatree_init_pool( &sys->aainfo, sys->pool_size );
+   sys->dbheader.entry_capacity = sys->pool_size;
 
-   sys->playerinfo_data = 
-      highscore_malloc( playerinfo_pool_size, sizeof(highscore_playerinfo));
-   if( !sys->playerinfo_data ) 
+   for( int i=0; i<vg_list_size(sys->dbheader.tracks); i++ )
    {
-      free( sys->data );
+      highscore_track_table *table = &sys->dbheader.tracks[i];
+      table->root_points = AATREE_PTR_NIL;
+      table->root_playerid = AATREE_PTR_NIL;
+      table->root_time = AATREE_PTR_NIL;
+      table->root_datetime = AATREE_PTR_NIL;
+   }
+
+   /* Initialize secondary db */
+   sys->dbheader.playerinfo_head = aatree_init_pool( 
+         &sys->aainfo_playerinfo,
+         sys->playerinfo_pool_size );
+   sys->dbheader.playerinfo_capacity = sys->playerinfo_pool_size;
+   sys->dbheader.playerinfo_root = AATREE_PTR_NIL;
+}
+
+static int highscores_read(void)
+{
+   struct highscore_system *sys = &highscore_system;
+
+   FILE *fp = fopen( ".aadb", "rb" );
+   if( fp )
+   {
+      vg_info( "Loading existing database\n" );
+      
+      u64 count = fread( &sys->dbheader, sizeof(highscore_database), 1, fp );
+
+      if( count != 1 )
+      {
+         vg_error( "Unexpected EOF reading database header\n" );
+         return 0;
+      }
+
+      count = fread( sys->data, sizeof(highscore_record), sys->pool_size, fp );
+      if( count != sys->pool_size )
+      {
+         vg_error( "Unexpected EOF reading database contents;"
+                   " %lu records of %u were read\n", count, sys->pool_size );
+         return 0;
+      }
+
+      count = fread( sys->playerinfo_data, sizeof(highscore_playerinfo),
+                     sys->playerinfo_pool_size, fp );
+
+      if( count != sys->playerinfo_pool_size )
+      {
+         vg_error( "Unexpected EOF reading playerinfo contents;"
+                   " %lu records of %u were read\n", count, 
+                   sys->playerinfo_pool_size );
+         return 0;
+      }
+
+      fclose( fp );
+      return 1;
+   }
+   else
+   {
+      vg_low( "No existing database found (.aadb)\n" );
       return 0;
    }
+}
+
+static void highscores_init( u32 pool_size, u32 playerinfo_pool_size )
+{
+   struct highscore_system *sys = &highscore_system;
+
+   sys->data = highscore_malloc( pool_size, sizeof(highscore_record) );
+   sys->playerinfo_data = highscore_malloc( playerinfo_pool_size, 
+                                            sizeof(highscore_playerinfo));
 
    /* This is ugly.. too bad! */
    sys->aainfo.base = highscore_system.data;
@@ -202,72 +274,8 @@ static int highscores_init( u32 pool_size, u32 playerinfo_pool_size )
    sys->aainfo_playerinfo.offset = offsetof(highscore_playerinfo,aapn);
    sys->aainfo_playerinfo.p_cmp = NULL;
 
-   FILE *fp = fopen( ".aadb", "rb" );
-   if( fp )
-   {
-      vg_info( "Loading existing database\n" );
-      
-      u64 count = fread( &sys->dbheader, sizeof(highscore_database), 1, fp );
-
-      if( count != 1 )
-      {
-         vg_error( "Unexpected EOF reading database header\n" );
-         
-         highscores_free();
-         return 0;
-      }
-
-      count = fread( sys->data, sizeof(highscore_record), pool_size, fp );
-      if( count != pool_size )
-      {
-         vg_error( "Unexpected EOF reading database contents;"
-                   " %lu records of %u were read\n", count, pool_size );
-
-         highscores_free();
-         return 0;
-      }
-
-      count = fread( sys->playerinfo_data, sizeof(highscore_playerinfo),
-                     playerinfo_pool_size, fp );
-      if( count != playerinfo_pool_size )
-      {
-         vg_error( "Unexpected EOF reading playerinfo contents;"
-                   " %lu records of %u were read\n", count, 
-                   playerinfo_pool_size );
-         
-         highscores_free();
-         return 0;
-      }
-
-      fclose( fp );
-   }
-   else
-   {
-      vg_low( "No existing database found (.aadb)\n" );
-      vg_info( "Initializing database nodes\n" );
-      memset( &sys->dbheader, 0, sizeof(highscore_database) );
-
-      sys->dbheader.pool_head = aatree_init_pool( &sys->aainfo, pool_size );
-      sys->dbheader.entry_capacity = pool_size;
-
-      for( int i=0; i<vg_list_size(sys->dbheader.tracks); i++ )
-      {
-         highscore_track_table *table = &sys->dbheader.tracks[i];
-         table->root_points = AATREE_PTR_NIL;
-         table->root_playerid = AATREE_PTR_NIL;
-         table->root_time = AATREE_PTR_NIL;
-         table->root_datetime = AATREE_PTR_NIL;
-      }
-
-      /* Initialize secondary db */
-      sys->dbheader.playerinfo_head = aatree_init_pool( 
-            &sys->aainfo_playerinfo,
-            playerinfo_pool_size );
-      sys->dbheader.playerinfo_capacity = playerinfo_pool_size;
-      sys->dbheader.playerinfo_root = AATREE_PTR_NIL;
-   }
-
-   return 1;
+   sys->playerinfo_pool_size = playerinfo_pool_size;
+   sys->pool_size = pool_size;
 }
 
 static int highscores_serialize_all(void)
@@ -314,7 +322,6 @@ static aatree_ptr highscores_push_record( highscore_record *record )
 {
    struct highscore_system *sys = &highscore_system;
 
-   /* TODO: Verify steam ID */
    vg_low( "Inserting record into database for track %hu\n",record->trackid );
 
    if( record->trackid >= vg_list_size(sys->dbheader.tracks) )
@@ -382,12 +389,12 @@ static aatree_ptr highscores_push_record( highscore_record *record )
    return index;
 }
 
-static aatree_ptr highscore_set_user_nickname( u64 steamid, char nick[10] )
+static aatree_ptr highscore_set_user_nickname( u64 steamid, char nick[16] )
 {
-   char name[11];
-   for( int i=0; i<10; i++ )
+   char name[17];
+   for( int i=0; i<16; i++ )
       name[i] = nick[i];
-   name[10] = '\0';
+   name[16] = '\0';
 
    vg_low( "Updating %lu's nickname -> %s\n", steamid, name );
 
@@ -426,7 +433,7 @@ static aatree_ptr highscore_set_user_nickname( u64 steamid, char nick[10] )
             record );
    }
 
-   for( int i=0; i<10; i++ )
+   for( int i=0; i<16; i++ )
       info->nickname[i] = nick[i];
 
    return AATREE_PTR_NIL;
@@ -565,7 +572,7 @@ static void highscores_board_generate( char *buf, u32 id, u32 count )
 
    highscore_strc ( buf+w*0, inf->name, w,w );
    highscore_clear( buf+w*1, '-', w );
-   highscore_strl ( buf+w*2, " #|  Player  | Time  | Pts", 27 );
+   highscore_strl ( buf+w*2, " #|     Player     | Time  ", 27 );
 
    for( int i=0; i<count; i++ )
    {
@@ -586,13 +593,13 @@ static void highscores_board_generate( char *buf, u32 id, u32 count )
       
       /* Player name */
       if( info_ptr == AATREE_PTR_NIL )
-         highscore_strl( line+3, "unknown", 10 );
+         highscore_strl( line+3, "unknown", 16 );
       else
       {
          highscore_playerinfo *inf = aatree_get_data( 
                &sys->aainfo_playerinfo_playerid, info_ptr );
 
-         highscore_strl( line+3, inf->nickname, 10 );
+         highscore_strl( line+3, inf->nickname, 16 );
       }
 
       u16 centiseconds = record->time,
@@ -606,14 +613,16 @@ static void highscores_board_generate( char *buf, u32 id, u32 count )
       if( minutes > 9 ) minutes = 9;
       
       /* Timer */
-      highscore_intr( line+14, minutes, 1, '0' );
-      line[15] = ':';
-      highscore_intr( line+16, seconds, 2, '0' );
-      line[18] = '.';
-      highscore_intr( line+19, centiseconds, 2, '0' );
+      highscore_intr( line+20, minutes, 1, '0' );
+      line[21] = ':';
+      highscore_intr( line+22, seconds, 2, '0' );
+      line[24] = '.';
+      highscore_intr( line+25, centiseconds, 2, '0' );
 
+#if 0
       /* Score */
       highscore_intl( line+22, record->points, 5 );
+#endif
       it = aatree_next( &sys->aainfo_time, it );
    }
 }