minor changes
[carveJwlIkooP6JGAAIwe30JlM.git] / world_sfd.c
index a92a4b090719c2fd457e33f4a9a4eef00fb55f66..478f431ee7aa01956d27746184d39d1ef506454c 100644 (file)
@@ -6,6 +6,8 @@
 #include "shaders/scene_vertex_blend.h"
 #include "network.h"
 #include "entity.h"
+#include "network_common.h"
+#include "world_routes.h"
 
 static f32 sfd_encode_glyph( char c ){
    int value = 0;
@@ -40,8 +42,7 @@ static f32 sfd_encode_glyph( char c ){
    return (float)value;
 }
 
-static void sfd_encode( u32 row, const char *str )
-{
+static void sfd_encode( u32 row, const char *str ){
    int end=0;
    u32 row_h = world_sfd.h -1 -row;
 
@@ -60,6 +61,77 @@ static void sfd_encode( u32 row, const char *str )
    }
 }
 
+static void world_sfd_compile_scores( struct leaderboard_cache *board ){
+   for( u32 i=0; i<13; i++ )
+      sfd_encode( i, "" );
+
+   if( !board ){
+      sfd_encode( 4, "Error out of range" );
+      return;
+   }
+
+   if( !network_client.remote ){
+      sfd_encode( 4, "Offline" );
+      return;
+   }
+
+   if( board->status == k_request_status_not_found ){
+      sfd_encode( 4, "No records" );
+      return;
+   }
+
+   if( board->status != k_request_status_ok ){
+      char buf[32];
+      vg_str s;
+      vg_strnull( &s, buf, 32 );
+      vg_strcat( &s, "Error: " );
+      vg_strcati32( &s, board->status );
+      sfd_encode( 4, buf );
+      return;
+   }
+
+   vg_msg body;
+   vg_msg_init( &body, board->data, board->data_len );
+
+   const char *alias = "rows";
+
+   if( world_sfd.view_weekly ){
+      alias = "rows_weekly";
+      sfd_encode( 0, "Weekly" );
+   }
+   else {
+      sfd_encode( 0, "All-Time" );
+   }
+
+   u32 l = 1;
+   if( vg_msg_seekframe( &body, alias ) ){
+      while( vg_msg_seekframe( &body, NULL ) ){
+         const char *username = vg_msg_getkvstr( &body, "username" );
+
+         if( username )
+            sfd_encode( l ++, username );
+         else
+            sfd_encode( l ++, "UNKNOWN USER" );
+
+         vg_msg_skip_frame( &body );
+      }
+   }
+   else {
+      sfd_encode( 4, "No records" );
+   }
+}
+
+static void world_sfd_compile_active_scores(void){
+   world_instance *world = world_current_instance();
+   
+   struct leaderboard_cache *board = NULL;
+
+   if( world_sfd.active_route_board < mdl_arrcount( &world->ent_route ) )
+      board = &world->leaderboard_cache[ world_sfd.active_route_board ];
+         
+   world_sfd_compile_scores( board );
+}
+
 static void world_sfd_update( world_instance *world, v3f pos ){
    if( mdl_arrcount( &world->ent_route ) ){
       u32 closest = 0;
@@ -75,32 +147,40 @@ static void world_sfd_update( world_instance *world, v3f pos ){
          }
       }
 
-      if( (world_sfd.active_route_board != closest) || network_scores_updated ){
-         network_scores_updated = 0;
-         world_sfd.active_route_board = closest;
+      struct leaderboard_cache *board = &world->leaderboard_cache[ closest ];
 
-         ent_route *route = mdl_arritm( &world->ent_route, closest );
-         u32 id = route->anon.official_track_id;
+      /* request new board if cache expires */
+      if( network_client.remote ){
+         f64 delta = vg.time_real - board->cache_time;
+         if( (delta > 45.0) || (board->cache_time == 0.0) ){
+            board->cache_time = vg.time_real;
+            ent_route *route = mdl_arritm( &world->ent_route, closest );
+            addon_reg *world_reg = 
+               world_static.instance_addons[ world - world_static.instances ];
 
-         if( id != 0xffffffff ){
-            struct netmsg_board *local_board = 
-               &scoreboard_client_data.boards[id];
+            char mod_uid[ ADDON_UID_MAX ];
+            addon_alias_uid( &world_reg->alias, mod_uid );
 
-            for( int i=0; i<13; i++ ){
-               sfd_encode( i, &local_board->data[27*i] );
-            }
-         }else{
-            sfd_encode( 0, mdl_pstr( &world->meta, route->pstr_name ) );
-            sfd_encode( 1, "No data" );
+            network_request_scoreboard( 
+                  mod_uid, 
+                  mdl_pstr( &world->meta, route->pstr_name ),
+                  NETWORK_LEADERBOARD_ALLTIME_AND_CURRENT_WEEK, closest );
          }
       }
+
+      /* compile board text if we changed. */
+      if( world_sfd.active_route_board != closest ){
+         world_sfd_compile_active_scores();
+      }
+
+      world_sfd.active_route_board = closest;
    }
 
    for( int i=0; i<world_sfd.w*world_sfd.h; i++ ){
       float *target = &world_sfd.buffer[i*2+0],
             *cur =    &world_sfd.buffer[i*2+1];
       
-      float const rate = vg.time_delta * 15.2313131414f;
+      float const rate = vg.time_delta * 25.2313131414f;
       float d1 = *target-*cur;
       
       if( fabsf(d1) > rate ){