view weekly/all-time
[carveJwlIkooP6JGAAIwe30JlM.git] / network.c
index b0ae67786542032c53b3ee7872d7e5357bdb9654..5d00d16c3b0fb43862b54fd2841491f4c6625ac2 100644 (file)
--- a/network.c
+++ b/network.c
@@ -3,7 +3,9 @@
 #include "network_msg.h"
 #include "network_common.h"
 #include "player_remote.h"
+#include "world.h"
 #include "world_sfd.h"
+#include "world_routes.h"
 
 static void scores_update(void);
 
@@ -149,8 +151,10 @@ static void network_send_username(void){
 }
 
 static void network_send_request( netmsg_request *req, vg_msg *body,
-                                  void (*callback)( netmsg_request *res, 
-                                                    vg_msg *body )){
+                                  void (*callback)( 
+                                     netmsg_request *res, vg_msg *body, 
+                                     u64 userdata),
+                                  u64 userdata ){
    u32 len = 0;
    if( body ){
       len = body->cur.co;
@@ -171,6 +175,7 @@ static void network_send_request( netmsg_request *req, vg_msg *body,
             vg_pool_item( &network_client.request_pool, req->id );
          pn->callback = callback;
          pn->sendtime = vg.time_real;
+         pn->userdata = userdata;
       }
       else{
          vg_error( "Unable to send request. Pool is full.\n" );
@@ -186,43 +191,29 @@ static void network_send_request( netmsg_request *req, vg_msg *body,
          k_nSteamNetworkingSend_Reliable, NULL );
 }
 
-static void network_scoreboard_callback( netmsg_request *res, vg_msg *body ){
-   for( u32 i=0; i<13; i++ )
-      sfd_encode( i, "" );
+static void network_scoreboard_callback( netmsg_request *res, vg_msg *body,
+                                         u64 userdata ){
+   world_instance *world = world_current_instance();
 
-   if( res->status != k_request_status_ok ){
-      char buf[32];
-      vg_str s;
-      vg_strnull( &s, buf, 32 );
-      vg_strcat( &s, "Error: " );
-      vg_strcati32( &s, res->status );
-
-      sfd_encode( 4, buf );
-      return;
-   }
+   world_routes_recv_scoreboard( world, body, userdata, res->status );
+   if( userdata == world_sfd.active_route_board )
+      world_sfd_compile_active_scores();
+}
 
-   u32 l = 0;
-   if( vg_msg_seekframe( body, "rows" ) ){
-      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 );
-      }
-   }
-}
 
 /* mod_uid: world mod uid,
  * route_uid: run name (just a string)
- * week: 0 for all-time, n for week #
+ * week: 
+ *   0   ALL TIME
+ *   1   CURRENT WEEK
+ *   2   ALL TIME + CURRENT WEEK
+ *   .
+ *   10+ specific week index
  */
 static void network_request_scoreboard( const char *mod_uid, 
                                         const char *route_uid,
-                                        u32 week ){
+                                        u32 week, u64 userdata ){
    if( !network_client.remote )
       return;
 
@@ -235,10 +226,11 @@ static void network_request_scoreboard( const char *mod_uid,
    vg_msg_wkvstr( &data, "mod", mod_uid );
    vg_msg_wkvstr( &data, "route", route_uid );
    vg_msg_wkvu32( &data, "week", week );
-   network_send_request( req, &data, network_scoreboard_callback );
+   network_send_request( req, &data, network_scoreboard_callback, userdata );
 }
 
-static void network_publish_callback( netmsg_request *res, vg_msg *body ){
+static void network_publish_callback( netmsg_request *res, vg_msg *body,
+                                      u64 userdata ){
    if( res->status != k_request_status_ok ){
       vg_error( "Publish laptime, server error #%d\n", (i32)res->status );
    }
@@ -260,7 +252,7 @@ static void network_publish_laptime( const char *mod_uid,
    vg_msg_wkvstr( &data, "mod", mod_uid );
    vg_msg_wkvstr( &data, "route", route_uid );
    vg_msg_wkvi32( &data, "time", time_centiseconds );
-   network_send_request( req, &data, network_publish_callback );
+   network_send_request( req, &data, network_publish_callback, 0 );
 }
 
 static void network_request_rx_300_400( SteamNetworkingMessage_t *msg ){
@@ -288,7 +280,7 @@ static void network_request_rx_300_400( SteamNetworkingMessage_t *msg ){
       if( res->id ){
          struct network_request *pn = 
             vg_pool_item( &network_client.request_pool, res->id );
-         pn->callback( res, body );
+         pn->callback( res, body, pn->userdata );
          vg_pool_unwatch( &network_client.request_pool, res->id );
       }
    }
@@ -448,6 +440,7 @@ static void network_connect(void){
                   hSteamNetworkingSockets, &remoteAddr, 0, NULL );
 }
 
+#if 0
 static void on_inet_scoreboard( SteamNetworkingMessage_t *msg ){
    netmsg_scoreboard *sb = msg->m_pData;
 
@@ -481,6 +474,7 @@ static void on_inet_scoreboard( SteamNetworkingMessage_t *msg ){
 
    network_scores_updated = 1;
 }
+#endif
 
 static void poll_remote_connection(void){
    SteamNetworkingMessage_t *messages[32];