snapshot -- connects to relay but doesn't play yet.

This commit is contained in:
Eric House 2013-01-07 08:00:47 -08:00
parent 469e656464
commit 565b742971
9 changed files with 70 additions and 38 deletions

View file

@ -249,7 +249,7 @@ typedef struct _PlayerDicts {
# define RELAY_ROOM_DEFAULT "Room 1" # define RELAY_ROOM_DEFAULT "Room 1"
#endif #endif
#ifndef RELAY_PORT_DEFAULT #ifndef RELAY_PORT_DEFAULT
# define RELAY_PORT_DEFAULT 10999 # define RELAY_PORT_DEFAULT 10997
#endif #endif
#ifdef MEM_DEBUG #ifdef MEM_DEBUG

View file

@ -101,6 +101,7 @@ DEFINES += -DXWFEATURE_WALKDICT
DEFINES += -DXWFEATURE_WALKDICT_FILTER DEFINES += -DXWFEATURE_WALKDICT_FILTER
DEFINES += -DXWFEATURE_DICTSANITY DEFINES += -DXWFEATURE_DICTSANITY
DEFINES += -DHASH_STREAM DEFINES += -DHASH_STREAM
DEFINES += -DRELAY_NAME_DEFAULT="\"localhost\""
#DEFINES += -DXWFEATURE_SCOREONEPASS #DEFINES += -DXWFEATURE_SCOREONEPASS
### Enable zero or one of these two ### ### Enable zero or one of these two ###
#DEFINES += -DXWFEATURE_TRAYUNDO_ALL #DEFINES += -DXWFEATURE_TRAYUNDO_ALL

View file

@ -21,13 +21,11 @@
#include "gamesdb.h" #include "gamesdb.h"
#include "main.h" #include "main.h"
#define DB_NAME "games.db"
sqlite3* sqlite3*
openGamesDB( void ) openGamesDB( const char* dbName )
{ {
sqlite3* pDb = NULL; sqlite3* pDb = NULL;
int result = sqlite3_open( DB_NAME, &pDb ); int result = sqlite3_open( dbName, &pDb );
XP_ASSERT( SQLITE_OK == result ); XP_ASSERT( SQLITE_OK == result );
const char* createStr = const char* createStr =
@ -62,7 +60,8 @@ writeToDB( XWStreamCtxt* stream, void* closure )
char* query; char* query;
sqlite3_stmt* stmt = NULL; 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 (?)"; query = "INSERT INTO games (game) VALUES (?)";
} else { } else {
const char* fmt = "UPDATE games SET game=? where rowid=%lld"; const char* fmt = "UPDATE games SET game=? where rowid=%lld";
@ -77,7 +76,7 @@ writeToDB( XWStreamCtxt* stream, void* closure )
result = sqlite3_step( stmt ); result = sqlite3_step( stmt );
XP_ASSERT( SQLITE_DONE == result ); 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 ); selRow = sqlite3_last_insert_rowid( pDb );
XP_LOGF( "%s: new rowid: %lld", __func__, selRow ); XP_LOGF( "%s: new rowid: %lld", __func__, selRow );
cGlobals->selRow = selRow; cGlobals->selRow = selRow;
@ -95,6 +94,10 @@ writeToDB( XWStreamCtxt* stream, void* closure )
if ( !!stmt ) { if ( !!stmt ) {
sqlite3_finalize( stmt ); sqlite3_finalize( stmt );
} }
if ( newGame ) {
(*cGlobals->firstSave)( cGlobals->firstSaveClosure );
}
} }
GSList* GSList*

View file

@ -27,7 +27,7 @@
#include "main.h" #include "main.h"
#include "comtypes.h" #include "comtypes.h"
sqlite3* openGamesDB( void ); sqlite3* openGamesDB( const char* dbName );
void closeGamesDB( sqlite3* dbp ); void closeGamesDB( sqlite3* dbp );
void writeToDB( XWStreamCtxt* stream, void* closure ); void writeToDB( XWStreamCtxt* stream, void* closure );

View file

@ -456,35 +456,34 @@ createOrLoadObjects( GtkAppGlobals* globals )
} }
if ( !opened ) { if ( !opened ) {
CommsAddrRec addr; CommsAddrRec addr = cGlobals->addr;
XP_MEMSET( &addr, 0, sizeof(addr) ); /* XP_MEMSET( &addr, 0, sizeof(addr) ); */
addr.conType = params->conType; /* addr.conType = cGlobals->addr.conType; */
#ifdef XWFEATURE_RELAY #ifdef XWFEATURE_RELAY
if ( addr.conType == COMMS_CONN_RELAY ) { /* if ( addr.conType == COMMS_CONN_RELAY ) { */
XP_ASSERT( !!params->connInfo.relay.relayName ); /* XP_ASSERT( !!params->connInfo.relay.relayName ); */
globals->cGlobals.defaultServerName /* globals->cGlobals.defaultServerName */
= params->connInfo.relay.relayName; /* = params->connInfo.relay.relayName; */
} /* } */
#endif #endif
CommonGlobals* cGlobals = &globals->cGlobals;
game_makeNewGame( MEMPOOL &cGlobals->game, &cGlobals->gi, game_makeNewGame( MEMPOOL &cGlobals->game, &cGlobals->gi,
cGlobals->util, (DrawCtx*)globals->draw, cGlobals->util, (DrawCtx*)globals->draw,
&cGlobals->cp, &procs, params->gameSeed ); &cGlobals->cp, &procs, params->gameSeed );
addr.conType = params->conType; // addr.conType = params->conType;
if ( 0 ) { if ( 0 ) {
#ifdef XWFEATURE_RELAY #ifdef XWFEATURE_RELAY
} else if ( addr.conType == COMMS_CONN_RELAY ) { } else if ( addr.conType == COMMS_CONN_RELAY ) {
addr.u.ip_relay.ipAddr = 0; /* addr.u.ip_relay.ipAddr = 0; */
addr.u.ip_relay.port = params->connInfo.relay.defaultSendPort; /* addr.u.ip_relay.port = params->connInfo.relay.defaultSendPort; */
addr.u.ip_relay.seeksPublicRoom = params->connInfo.relay.seeksPublicRoom; /* addr.u.ip_relay.seeksPublicRoom = params->connInfo.relay.seeksPublicRoom; */
addr.u.ip_relay.advertiseRoom = params->connInfo.relay.advertiseRoom; /* addr.u.ip_relay.advertiseRoom = params->connInfo.relay.advertiseRoom; */
XP_STRNCPY( addr.u.ip_relay.hostName, params->connInfo.relay.relayName, /* XP_STRNCPY( addr.u.ip_relay.hostName, params->connInfo.relay.relayName, */
sizeof(addr.u.ip_relay.hostName) - 1 ); /* sizeof(addr.u.ip_relay.hostName) - 1 ); */
XP_STRNCPY( addr.u.ip_relay.invite, params->connInfo.relay.invite, /* XP_STRNCPY( addr.u.ip_relay.invite, params->connInfo.relay.invite, */
sizeof(addr.u.ip_relay.invite) - 1 ); /* sizeof(addr.u.ip_relay.invite) - 1 ); */
#endif #endif
#ifdef XWFEATURE_BLUETOOTH #ifdef XWFEATURE_BLUETOOTH
} else if ( addr.conType == COMMS_CONN_BT ) { } else if ( addr.conType == COMMS_CONN_BT ) {
@ -2385,6 +2384,8 @@ initGlobals( GtkAppGlobals* globals, LaunchParams* params )
globals->cGlobals.socketChanged = gtk_socket_changed; globals->cGlobals.socketChanged = gtk_socket_changed;
globals->cGlobals.socketChangedClosure = globals; globals->cGlobals.socketChangedClosure = globals;
globals->cGlobals.firstSave = newGameSaved;
globals->cGlobals.firstSaveClosure = globals;
globals->cGlobals.addAcceptor = gtk_socket_acceptor; globals->cGlobals.addAcceptor = gtk_socket_acceptor;
#endif #endif
@ -2521,18 +2522,20 @@ freeGlobals( GtkAppGlobals* globals )
XP_Bool XP_Bool
makeNewGame( GtkAppGlobals* globals ) makeNewGame( GtkAppGlobals* globals )
{ {
CommsAddrRec addr; CommonGlobals* cGlobals = &globals->cGlobals;
if ( !!globals->cGlobals.game.comms ) { if ( !!cGlobals->game.comms ) {
comms_getAddr( globals->cGlobals.game.comms, &addr ); comms_getAddr( cGlobals->game.comms, &cGlobals->addr );
} else { } 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; CurGameInfo* gi = &cGlobals->gi;
XP_Bool success = newGameDialog( globals, gi, &addr, XP_TRUE, XP_FALSE ); XP_Bool success = newGameDialog( globals, gi, &cGlobals->addr,
if ( success && !!gi->dictName && !globals->cGlobals.dict ) { XP_TRUE, XP_FALSE );
globals->cGlobals.dict = if ( success && !!gi->dictName && !cGlobals->dict ) {
linux_dictionary_make( MEMPOOL globals->cGlobals.params, cGlobals->dict =
linux_dictionary_make( MEMPOOL cGlobals->params,
gi->dictName, XP_TRUE ); gi->dictName, XP_TRUE );
} }
LOG_RETURNF( "%d", success ); LOG_RETURNF( "%d", success );

View file

@ -183,7 +183,7 @@ addButton( gchar* label, GtkWidget* parent, GCallback proc, void* closure )
} }
static GtkWidget* static GtkWidget*
makeGamesWindow( GTKGamesGlobals* gg ) makeGamesWindow( GTKGamesGlobals* gg )
{ {
GtkWidget* window; GtkWidget* window;
@ -195,6 +195,7 @@ makeGamesWindow( GTKGamesGlobals* gg )
gtk_container_add( GTK_CONTAINER(window), vbox ); gtk_container_add( GTK_CONTAINER(window), vbox );
gtk_widget_show( vbox ); gtk_widget_show( vbox );
GtkWidget* list = init_games_list( gg ); GtkWidget* list = init_games_list( gg );
gg->listWidget = list;
gtk_container_add( GTK_CONTAINER(vbox), list ); gtk_container_add( GTK_CONTAINER(vbox), list );
gtk_widget_show( list ); gtk_widget_show( list );
@ -238,6 +239,17 @@ windowDestroyed( GtkAppGlobals* globals )
(void)g_idle_add( freeGameGlobals, 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 int
gtkmain( LaunchParams* params ) gtkmain( LaunchParams* params )
{ {
@ -245,7 +257,7 @@ gtkmain( LaunchParams* params )
gg.selRow = -1; gg.selRow = -1;
gg.params = params; gg.params = params;
XP_LOGF( "%s: I'M HERE!!! (calling makeGamesDB())", __func__ ); XP_LOGF( "%s: I'M HERE!!! (calling makeGamesDB())", __func__ );
gg.pDb = openGamesDB(); gg.pDb = openGamesDB( params->dbName );
(void)makeGamesWindow( &gg ); (void)makeGamesWindow( &gg );
gtk_main(); gtk_main();

View file

@ -25,5 +25,6 @@
int gtkmain( LaunchParams* params ); int gtkmain( LaunchParams* params );
void windowDestroyed( GtkAppGlobals* globals ); void windowDestroyed( GtkAppGlobals* globals );
void newGameSaved( void* closure );
#endif #endif

View file

@ -74,7 +74,7 @@
#include "memstream.h" #include "memstream.h"
#include "LocalizedStrIncludes.h" #include "LocalizedStrIncludes.h"
#define DEFAULT_PORT 10999 #define DEFAULT_PORT 10997
#define DEFAULT_LISTEN_PORT 4998 #define DEFAULT_LISTEN_PORT 4998
XP_Bool XP_Bool
@ -523,6 +523,7 @@ typedef enum {
#endif #endif
,CMD_GAMESEED ,CMD_GAMESEED
,CMD_GAMEFILE ,CMD_GAMEFILE
,CMD_DBFILE
,CMD_SAVEFAIL_PCT ,CMD_SAVEFAIL_PCT
#ifdef USE_SQLITE #ifdef USE_SQLITE
,CMD_GAMEDB_FILE ,CMD_GAMEDB_FILE
@ -620,6 +621,7 @@ static CmdInfoRec CmdInfoRecs[] = {
#endif #endif
,{ CMD_GAMESEED, true, "game-seed", "game seed (for relay play)" } ,{ CMD_GAMESEED, true, "game-seed", "game seed (for relay play)" }
,{ CMD_GAMEFILE, true, "file", "file to save to/read from" } ,{ 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?" } ,{ CMD_SAVEFAIL_PCT, true, "savefail-pct", "How often, at random, does save fail?" }
#ifdef USE_SQLITE #ifdef USE_SQLITE
,{ CMD_GAMEDB_FILE, true, "game-db-file", ,{ CMD_GAMEDB_FILE, true, "game-db-file",
@ -1736,6 +1738,9 @@ main( int argc, char** argv )
case CMD_GAMEFILE: case CMD_GAMEFILE:
mainParams.fileName = optarg; mainParams.fileName = optarg;
break; break;
case CMD_DBFILE:
mainParams.dbName = optarg;
break;
case CMD_SAVEFAIL_PCT: case CMD_SAVEFAIL_PCT:
mainParams.saveFailPct = atoi( optarg ); mainParams.saveFailPct = atoi( optarg );
break; break;

View file

@ -50,6 +50,7 @@ typedef struct LaunchParams {
GSList* dictDirs; GSList* dictDirs;
char* fileName; char* fileName;
char* dbName;
XP_U16 saveFailPct; XP_U16 saveFailPct;
const XP_UCHAR* playerDictNames[MAX_NUM_PLAYERS]; const XP_UCHAR* playerDictNames[MAX_NUM_PLAYERS];
#ifdef USE_SQLITE #ifdef USE_SQLITE
@ -162,6 +163,8 @@ typedef struct _TimerInfo {
#endif #endif
} TimerInfo; } TimerInfo;
typedef void (*FirstSaveFunc)(void* closure);
struct CommonGlobals { struct CommonGlobals {
LaunchParams* params; LaunchParams* params;
CommonPrefs cp; CommonPrefs cp;
@ -169,6 +172,7 @@ struct CommonGlobals {
XWGame game; XWGame game;
CurGameInfo gi; CurGameInfo gi;
CommsAddrRec addr;
DictionaryCtxt* dict; DictionaryCtxt* dict;
PlayerDicts dicts; PlayerDicts dicts;
XP_U16 lastNTilesToUse; XP_U16 lastNTilesToUse;
@ -179,6 +183,8 @@ struct CommonGlobals {
SocketChangedFunc socketChanged; SocketChangedFunc socketChanged;
void* socketChangedClosure; void* socketChangedClosure;
FirstSaveFunc firstSave;
void* firstSaveClosure;
CommsRelayState state; CommsRelayState state;
@ -216,6 +222,7 @@ typedef struct _GTKGamesGlobals {
sqlite3_int64 selRow; sqlite3_int64 selRow;
LaunchParams* params; LaunchParams* params;
GSList* globalsList; GSList* globalsList;
GtkWidget* listWidget;
} GTKGamesGlobals; } GTKGamesGlobals;
#endif #endif