From 565b742971fa88daec8c0931414f57e42f2810d3 Mon Sep 17 00:00:00 2001 From: Eric House Date: Mon, 7 Jan 2013 08:00:47 -0800 Subject: [PATCH] snapshot -- connects to relay but doesn't play yet. --- xwords4/common/comtypes.h | 2 +- xwords4/linux/Makefile | 1 + xwords4/linux/gamesdb.c | 15 ++++++----- xwords4/linux/gamesdb.h | 2 +- xwords4/linux/gtkboard.c | 57 ++++++++++++++++++++------------------- xwords4/linux/gtkmain.c | 16 +++++++++-- xwords4/linux/gtkmain.h | 1 + xwords4/linux/linuxmain.c | 7 ++++- xwords4/linux/main.h | 7 +++++ 9 files changed, 70 insertions(+), 38 deletions(-) diff --git a/xwords4/common/comtypes.h b/xwords4/common/comtypes.h index 805694f1e..d841696f6 100644 --- a/xwords4/common/comtypes.h +++ b/xwords4/common/comtypes.h @@ -249,7 +249,7 @@ typedef struct _PlayerDicts { # define RELAY_ROOM_DEFAULT "Room 1" #endif #ifndef RELAY_PORT_DEFAULT -# define RELAY_PORT_DEFAULT 10999 +# define RELAY_PORT_DEFAULT 10997 #endif #ifdef MEM_DEBUG diff --git a/xwords4/linux/Makefile b/xwords4/linux/Makefile index ed114e839..38861248a 100644 --- a/xwords4/linux/Makefile +++ b/xwords4/linux/Makefile @@ -101,6 +101,7 @@ DEFINES += -DXWFEATURE_WALKDICT DEFINES += -DXWFEATURE_WALKDICT_FILTER DEFINES += -DXWFEATURE_DICTSANITY DEFINES += -DHASH_STREAM +DEFINES += -DRELAY_NAME_DEFAULT="\"localhost\"" #DEFINES += -DXWFEATURE_SCOREONEPASS ### Enable zero or one of these two ### #DEFINES += -DXWFEATURE_TRAYUNDO_ALL diff --git a/xwords4/linux/gamesdb.c b/xwords4/linux/gamesdb.c index dedbdc525..bd4b7a146 100644 --- a/xwords4/linux/gamesdb.c +++ b/xwords4/linux/gamesdb.c @@ -21,13 +21,11 @@ #include "gamesdb.h" #include "main.h" -#define DB_NAME "games.db" - sqlite3* -openGamesDB( void ) +openGamesDB( const char* dbName ) { sqlite3* pDb = NULL; - int result = sqlite3_open( DB_NAME, &pDb ); + int result = sqlite3_open( dbName, &pDb ); XP_ASSERT( SQLITE_OK == result ); const char* createStr = @@ -62,7 +60,8 @@ writeToDB( XWStreamCtxt* stream, void* closure ) char* query; sqlite3_stmt* stmt = NULL; - if ( -1 == selRow ) { /* new row; need to insert blob first */ + XP_Bool newGame = -1 == selRow; + if ( newGame ) { /* new row; need to insert blob first */ query = "INSERT INTO games (game) VALUES (?)"; } else { const char* fmt = "UPDATE games SET game=? where rowid=%lld"; @@ -77,7 +76,7 @@ writeToDB( XWStreamCtxt* stream, void* closure ) result = sqlite3_step( stmt ); XP_ASSERT( SQLITE_DONE == result ); - if ( -1 == selRow ) { /* new row; need to insert blob first */ + if ( newGame ) { /* new row; need to insert blob first */ selRow = sqlite3_last_insert_rowid( pDb ); XP_LOGF( "%s: new rowid: %lld", __func__, selRow ); cGlobals->selRow = selRow; @@ -95,6 +94,10 @@ writeToDB( XWStreamCtxt* stream, void* closure ) if ( !!stmt ) { sqlite3_finalize( stmt ); } + + if ( newGame ) { + (*cGlobals->firstSave)( cGlobals->firstSaveClosure ); + } } GSList* diff --git a/xwords4/linux/gamesdb.h b/xwords4/linux/gamesdb.h index bbda321f4..73f5b9a2d 100644 --- a/xwords4/linux/gamesdb.h +++ b/xwords4/linux/gamesdb.h @@ -27,7 +27,7 @@ #include "main.h" #include "comtypes.h" -sqlite3* openGamesDB( void ); +sqlite3* openGamesDB( const char* dbName ); void closeGamesDB( sqlite3* dbp ); void writeToDB( XWStreamCtxt* stream, void* closure ); diff --git a/xwords4/linux/gtkboard.c b/xwords4/linux/gtkboard.c index f10f99b3c..58f21c649 100644 --- a/xwords4/linux/gtkboard.c +++ b/xwords4/linux/gtkboard.c @@ -456,35 +456,34 @@ createOrLoadObjects( GtkAppGlobals* globals ) } if ( !opened ) { - CommsAddrRec addr; + CommsAddrRec addr = cGlobals->addr; - XP_MEMSET( &addr, 0, sizeof(addr) ); - addr.conType = params->conType; + /* XP_MEMSET( &addr, 0, sizeof(addr) ); */ + /* addr.conType = cGlobals->addr.conType; */ #ifdef XWFEATURE_RELAY - if ( addr.conType == COMMS_CONN_RELAY ) { - XP_ASSERT( !!params->connInfo.relay.relayName ); - globals->cGlobals.defaultServerName - = params->connInfo.relay.relayName; - } + /* if ( addr.conType == COMMS_CONN_RELAY ) { */ + /* XP_ASSERT( !!params->connInfo.relay.relayName ); */ + /* globals->cGlobals.defaultServerName */ + /* = params->connInfo.relay.relayName; */ + /* } */ #endif - CommonGlobals* cGlobals = &globals->cGlobals; game_makeNewGame( MEMPOOL &cGlobals->game, &cGlobals->gi, cGlobals->util, (DrawCtx*)globals->draw, &cGlobals->cp, &procs, params->gameSeed ); - addr.conType = params->conType; + // addr.conType = params->conType; if ( 0 ) { #ifdef XWFEATURE_RELAY } else if ( addr.conType == COMMS_CONN_RELAY ) { - addr.u.ip_relay.ipAddr = 0; - addr.u.ip_relay.port = params->connInfo.relay.defaultSendPort; - addr.u.ip_relay.seeksPublicRoom = params->connInfo.relay.seeksPublicRoom; - addr.u.ip_relay.advertiseRoom = params->connInfo.relay.advertiseRoom; - XP_STRNCPY( addr.u.ip_relay.hostName, params->connInfo.relay.relayName, - sizeof(addr.u.ip_relay.hostName) - 1 ); - XP_STRNCPY( addr.u.ip_relay.invite, params->connInfo.relay.invite, - sizeof(addr.u.ip_relay.invite) - 1 ); + /* addr.u.ip_relay.ipAddr = 0; */ + /* addr.u.ip_relay.port = params->connInfo.relay.defaultSendPort; */ + /* addr.u.ip_relay.seeksPublicRoom = params->connInfo.relay.seeksPublicRoom; */ + /* addr.u.ip_relay.advertiseRoom = params->connInfo.relay.advertiseRoom; */ + /* XP_STRNCPY( addr.u.ip_relay.hostName, params->connInfo.relay.relayName, */ + /* sizeof(addr.u.ip_relay.hostName) - 1 ); */ + /* XP_STRNCPY( addr.u.ip_relay.invite, params->connInfo.relay.invite, */ + /* sizeof(addr.u.ip_relay.invite) - 1 ); */ #endif #ifdef XWFEATURE_BLUETOOTH } else if ( addr.conType == COMMS_CONN_BT ) { @@ -2385,6 +2384,8 @@ initGlobals( GtkAppGlobals* globals, LaunchParams* params ) globals->cGlobals.socketChanged = gtk_socket_changed; globals->cGlobals.socketChangedClosure = globals; + globals->cGlobals.firstSave = newGameSaved; + globals->cGlobals.firstSaveClosure = globals; globals->cGlobals.addAcceptor = gtk_socket_acceptor; #endif @@ -2521,18 +2522,20 @@ freeGlobals( GtkAppGlobals* globals ) XP_Bool makeNewGame( GtkAppGlobals* globals ) { - CommsAddrRec addr; - if ( !!globals->cGlobals.game.comms ) { - comms_getAddr( globals->cGlobals.game.comms, &addr ); + CommonGlobals* cGlobals = &globals->cGlobals; + if ( !!cGlobals->game.comms ) { + comms_getAddr( cGlobals->game.comms, &cGlobals->addr ); } else { - comms_getInitialAddr( &addr, RELAY_NAME_DEFAULT, RELAY_PORT_DEFAULT ); + comms_getInitialAddr( &cGlobals->addr, RELAY_NAME_DEFAULT, + RELAY_PORT_DEFAULT ); } - CurGameInfo* gi = &globals->cGlobals.gi; - XP_Bool success = newGameDialog( globals, gi, &addr, XP_TRUE, XP_FALSE ); - if ( success && !!gi->dictName && !globals->cGlobals.dict ) { - globals->cGlobals.dict = - linux_dictionary_make( MEMPOOL globals->cGlobals.params, + CurGameInfo* gi = &cGlobals->gi; + XP_Bool success = newGameDialog( globals, gi, &cGlobals->addr, + XP_TRUE, XP_FALSE ); + if ( success && !!gi->dictName && !cGlobals->dict ) { + cGlobals->dict = + linux_dictionary_make( MEMPOOL cGlobals->params, gi->dictName, XP_TRUE ); } LOG_RETURNF( "%d", success ); diff --git a/xwords4/linux/gtkmain.c b/xwords4/linux/gtkmain.c index 2e5431dd3..e8c8411c9 100644 --- a/xwords4/linux/gtkmain.c +++ b/xwords4/linux/gtkmain.c @@ -183,7 +183,7 @@ addButton( gchar* label, GtkWidget* parent, GCallback proc, void* closure ) } static GtkWidget* -makeGamesWindow( GTKGamesGlobals* gg ) +makeGamesWindow( GTKGamesGlobals* gg ) { GtkWidget* window; @@ -195,6 +195,7 @@ makeGamesWindow( GTKGamesGlobals* gg ) gtk_container_add( GTK_CONTAINER(window), vbox ); gtk_widget_show( vbox ); GtkWidget* list = init_games_list( gg ); + gg->listWidget = list; gtk_container_add( GTK_CONTAINER(vbox), list ); gtk_widget_show( list ); @@ -238,6 +239,17 @@ windowDestroyed( GtkAppGlobals* globals ) (void)g_idle_add( freeGameGlobals, globals ); } +void +newGameSaved( void* closure ) +{ + GtkAppGlobals* globals = (GtkAppGlobals*)closure; + GTKGamesGlobals* gg = globals->gg; + CommonGlobals* cGlobals = &globals->cGlobals; + XP_UCHAR buf[128]; + getGameName( gg, &cGlobals->selRow, buf, sizeof(buf) ); + add_to_list( gg->listWidget, &cGlobals->selRow, buf ); +} + int gtkmain( LaunchParams* params ) { @@ -245,7 +257,7 @@ gtkmain( LaunchParams* params ) gg.selRow = -1; gg.params = params; XP_LOGF( "%s: I'M HERE!!! (calling makeGamesDB())", __func__ ); - gg.pDb = openGamesDB(); + gg.pDb = openGamesDB( params->dbName ); (void)makeGamesWindow( &gg ); gtk_main(); diff --git a/xwords4/linux/gtkmain.h b/xwords4/linux/gtkmain.h index 95753f0a3..015b7d307 100644 --- a/xwords4/linux/gtkmain.h +++ b/xwords4/linux/gtkmain.h @@ -25,5 +25,6 @@ int gtkmain( LaunchParams* params ); void windowDestroyed( GtkAppGlobals* globals ); +void newGameSaved( void* closure ); #endif diff --git a/xwords4/linux/linuxmain.c b/xwords4/linux/linuxmain.c index 9e9ee6224..ab2467f26 100644 --- a/xwords4/linux/linuxmain.c +++ b/xwords4/linux/linuxmain.c @@ -74,7 +74,7 @@ #include "memstream.h" #include "LocalizedStrIncludes.h" -#define DEFAULT_PORT 10999 +#define DEFAULT_PORT 10997 #define DEFAULT_LISTEN_PORT 4998 XP_Bool @@ -523,6 +523,7 @@ typedef enum { #endif ,CMD_GAMESEED ,CMD_GAMEFILE + ,CMD_DBFILE ,CMD_SAVEFAIL_PCT #ifdef USE_SQLITE ,CMD_GAMEDB_FILE @@ -620,6 +621,7 @@ static CmdInfoRec CmdInfoRecs[] = { #endif ,{ CMD_GAMESEED, true, "game-seed", "game seed (for relay play)" } ,{ CMD_GAMEFILE, true, "file", "file to save to/read from" } + ,{ CMD_DBFILE, true, "db", "sqlite3 db to store game data" } ,{ CMD_SAVEFAIL_PCT, true, "savefail-pct", "How often, at random, does save fail?" } #ifdef USE_SQLITE ,{ CMD_GAMEDB_FILE, true, "game-db-file", @@ -1736,6 +1738,9 @@ main( int argc, char** argv ) case CMD_GAMEFILE: mainParams.fileName = optarg; break; + case CMD_DBFILE: + mainParams.dbName = optarg; + break; case CMD_SAVEFAIL_PCT: mainParams.saveFailPct = atoi( optarg ); break; diff --git a/xwords4/linux/main.h b/xwords4/linux/main.h index a9870b57a..866b1e503 100644 --- a/xwords4/linux/main.h +++ b/xwords4/linux/main.h @@ -50,6 +50,7 @@ typedef struct LaunchParams { GSList* dictDirs; char* fileName; + char* dbName; XP_U16 saveFailPct; const XP_UCHAR* playerDictNames[MAX_NUM_PLAYERS]; #ifdef USE_SQLITE @@ -162,6 +163,8 @@ typedef struct _TimerInfo { #endif } TimerInfo; +typedef void (*FirstSaveFunc)(void* closure); + struct CommonGlobals { LaunchParams* params; CommonPrefs cp; @@ -169,6 +172,7 @@ struct CommonGlobals { XWGame game; CurGameInfo gi; + CommsAddrRec addr; DictionaryCtxt* dict; PlayerDicts dicts; XP_U16 lastNTilesToUse; @@ -179,6 +183,8 @@ struct CommonGlobals { SocketChangedFunc socketChanged; void* socketChangedClosure; + FirstSaveFunc firstSave; + void* firstSaveClosure; CommsRelayState state; @@ -216,6 +222,7 @@ typedef struct _GTKGamesGlobals { sqlite3_int64 selRow; LaunchParams* params; GSList* globalsList; + GtkWidget* listWidget; } GTKGamesGlobals; #endif