dont always play waves
[carveJwlIkooP6JGAAIwe30JlM.git] / gameserver_db.h
index d3d3429669c24ef3233b0c32c8c4808fad48c4c7..fe39931af843ae30b3715b6192367193dedc489a 100644 (file)
@@ -5,13 +5,12 @@
 #include "vg/vg_mem_queue.h"
 #include "network_common.h"
 #include "dep/sqlite3/sqlite3.h"
-#include "highscores.h"
 #include <pthread.h>
 #include <unistd.h>
 
 #define DB_COURSE_UID_MAX 32
 #define DB_TABLE_UID_MAX (ADDON_UID_MAX+DB_COURSE_UID_MAX+32)
-#define DB_CRASH_ON_SQLITE_ERROR
+//#define DB_CRASH_ON_SQLITE_ERROR
 #define DB_LOG_SQL_STATEMENTS
 #define DB_REQUEST_BUFFER_SIZE (1024*2)
 
@@ -92,8 +91,8 @@ static int db_verify_charset( const char *str, int mincount ){
 /*
  * Find table name from mod UID and course UID, plus the week number
  */
-static int db_get_highscore_table_name( char mod_uid[ADDON_UID_MAX], 
-                                        char run_uid[DB_COURSE_UID_MAX],
+static int db_get_highscore_table_name( const char *mod_uid,
+                                        const char *run_uid,
                                         u32 week,
                                         char table_name[DB_TABLE_UID_MAX] ){
    if( !db_verify_charset( mod_uid, 13 ) ||
@@ -126,9 +125,8 @@ static i32 db_readusertime( char table[DB_TABLE_UID_MAX], u64 steamid ){
    if( !vg_strgood(&q) ) return 0;
 
    sqlite3_stmt *stmt = db_stmt( q.buffer );
-   sqlite3_bind_int64( stmt, 1, *((i64 *)&steamid) );
-
    if( stmt ){
+      sqlite3_bind_int64( stmt, 1, *((i64 *)&steamid) );
       int fc = sqlite3_step( stmt );
 
       i32 result = 0;
@@ -156,7 +154,7 @@ static int db_writeusertime( char table[DB_TABLE_UID_MAX], u64 steamid,
    vg_strnull( &q, buf, 512 );
    vg_strcat( &q, "CREATE TABLE IF NOT EXISTS \n \"" );
    vg_strcat( &q, table );
-   vg_strcat( &q, "\"\n (steamid BIGINT PRIMARY KEY, time INT);" );
+   vg_strcat( &q, "\"\n (steamid BIGINT UNIQUE, time INT);" );
    if( !vg_strgood(&q) ) return 0;
 
    vg_str str;
@@ -231,12 +229,40 @@ static int db_updateuser( u64 steamid, const char *username, int admin ){
    else return 0;
 }
 
+/*
+ * Get user info 
+ */
+static int db_getuserinfo( u64 steamid, char *out_username, u32 username_max, 
+                           i32 *out_type ){
+   sqlite3_stmt *stmt = db_stmt( "SELECT * FROM users WHERE steamid = ?;" );
+   if( !stmt ) return 0;
+
+   sqlite3_bind_int64( stmt, 1, *((i64 *)&steamid) );
+   int fc = sqlite3_step( stmt );
+
+   if( fc != SQLITE_ROW ){
+      log_sqlite3( fc );
+      sqlite3_finalize( stmt );
+      return 0;
+   }
+
+   if( out_username ){
+      const char *name = (const char *)sqlite3_column_text( stmt, 1 );
+      vg_strncpy( name, out_username, username_max, k_strncpy_allow_cutoff );
+   }
+   
+   if( out_type )
+      *out_type = sqlite3_column_int( stmt, 2 );
+   
+   sqlite3_finalize( stmt );
+   return 1;
+}
+
 static void _db_thread_end(void){
    pthread_mutex_lock( &database.mux );
    database.kill = 1;
    pthread_mutex_unlock( &database.mux );
    sqlite3_close( database.db );
-   pthread_mutex_destroy( &database.mux );
 }
 
 static void *db_loop(void *_){
@@ -250,7 +276,7 @@ static void *db_loop(void *_){
 
    sqlite3_stmt *stmt = db_stmt(
          "CREATE TABLE IF NOT EXISTS \n"
-         " users(steamid BIGINT PRIMARY KEY, name VARCHAR(128), type INT);" );
+         " users(steamid BIGINT UNIQUE, name VARCHAR(128), type INT);" );
 
    if( stmt ){
       int fc = sqlite3_step( stmt );
@@ -337,17 +363,22 @@ static int db_killed(void){
    return result;
 }
 
-static void db_free(void){
+static void db_kill(void){
    pthread_mutex_lock( &database.mux );
    database.kill = 1;
    pthread_mutex_unlock( &database.mux );
+   pthread_join( database.thread, NULL );
+}
+
+static void db_free(void){
+   pthread_mutex_destroy( &database.mux );
 }
 
 static db_request *db_alloc_request( u32 size ){
-   u32 total = sizeof(db_request) + vg_align8(size);
+   u32 total = sizeof(db_request) + size;
 
    pthread_mutex_lock( &database.mux );
-   vg_queue_frame *frame = vg_queue_alloc( &database.queue, size );
+   vg_queue_frame *frame = vg_queue_alloc( &database.queue, total );
 
    if( frame ){
       db_request *req = (db_request *)frame->data;