From bc65377cfd3cea0ee07193032d832093257c9ee6 Mon Sep 17 00:00:00 2001 From: Eric House Date: Tue, 28 Sep 2010 18:06:50 -0700 Subject: [PATCH] track device seeds in db. --- xwords4/relay/cref.cpp | 2 +- xwords4/relay/dbmgr.cpp | 61 ++++++++++++---------------------------- xwords4/relay/dbmgr.h | 2 +- xwords4/relay/xwrelay.sh | 13 +++++++++ 4 files changed, 33 insertions(+), 45 deletions(-) diff --git a/xwords4/relay/cref.cpp b/xwords4/relay/cref.cpp index c5d29cd8d..d0287465b 100644 --- a/xwords4/relay/cref.cpp +++ b/xwords4/relay/cref.cpp @@ -791,7 +791,7 @@ CookieRef::increasePlayerCounts( CRefEvent* evt, bool reconn ) if ( !reconn ) { m_nPlayersHere += nPlayersH; assert( m_nPlayersHere <= m_nPlayersSought ); - evt->u.con.srcID = DBMgr::Get()->AddDevice( ConnName(), nPlayersH ); + evt->u.con.srcID = DBMgr::Get()->AddDevice( ConnName(), nPlayersH, seed ); } HostID hostid = evt->u.con.srcID; diff --git a/xwords4/relay/dbmgr.cpp b/xwords4/relay/dbmgr.cpp index 4a54a1e21..913481843 100644 --- a/xwords4/relay/dbmgr.cpp +++ b/xwords4/relay/dbmgr.cpp @@ -162,7 +162,7 @@ DBMgr::FindOpen( const char* cookie, int lang, int nPlayersT, int nPlayersH, } /* FindOpen */ HostID -DBMgr::AddDevice( const char* connName, int nToAdd ) +DBMgr::AddDevice( const char* connName, int nToAdd, unsigned short seed ) { HostID newID = HOST_ID_NONE; int arr[4]; @@ -177,10 +177,10 @@ DBMgr::AddDevice( const char* connName, int nToAdd ) } assert( newID <= 4 ); - const char* fmt = "UPDATE " GAMES_TABLE " SET nPerDevice[%d] = %d " - "WHERE connName = '%s'"; + const char* fmt = "UPDATE " GAMES_TABLE " SET nPerDevice[%d] = %d," + " seeds[%d] = %d WHERE connName = '%s'"; char query[256]; - snprintf( query, sizeof(query), fmt, newID, nToAdd, connName ); + snprintf( query, sizeof(query), fmt, newID, nToAdd, newID, seed, connName ); logf( XW_LOGINFO, "%s: query: %s", __func__, query ); execSql_locked( query ); @@ -191,10 +191,10 @@ DBMgr::AddDevice( const char* connName, int nToAdd ) void DBMgr::RmDevice( const char* connName, HostID hid ) { - const char* fmt = "UPDATE " GAMES_TABLE " SET nPerDevice[%d] = 0 " - "WHERE connName = '%s'"; + const char* fmt = "UPDATE " GAMES_TABLE " SET nPerDevice[%d] = 0, " + "seeds[%d] = 0 WHERE connName = '%s'"; char query[256]; - snprintf( query, sizeof(query), fmt, hid, connName ); + snprintf( query, sizeof(query), fmt, hid, hid, connName ); logf( XW_LOGINFO, "%s: query: %s", __func__, query ); execSql( query ); @@ -233,11 +233,11 @@ DBMgr::ClearCIDs( void ) void DBMgr::PublicRooms( int lang, int nPlayers, int* nNames, string& names ) { - int ii; - int nTuples; - const char* fmt = "SELECT room, nTotal-" ARRAYSUM " FROM " GAMES_TABLE - " WHERE pub = TRUE AND lang = %d AND ntotal =% d"; + " WHERE pub = TRUE" + " AND lang = %d" + " AND nTotal>" ARRAYSUM + " AND nTotal = %d"; char query[256]; snprintf( query, sizeof(query), fmt, lang, nPlayers ); @@ -246,20 +246,15 @@ DBMgr::PublicRooms( int lang, int nPlayers, int* nNames, string& names ) MutexLock ml( &m_dbMutex ); PGresult* result = PQexec( m_pgconn, query ); - nTuples = PQntuples( result ); - int goodCount = 0; - for ( ii = 0; ii < nTuples; ++ii ) { - int nOpen = atoi( PQgetvalue( result, ii, 1 ) ); - if ( nOpen > 0 ) { - names.append( PQgetvalue( result, ii, 0 ) ); - names.append( "/" ); - names.append( PQgetvalue( result, ii, 1 ) ); - names.append( "\n" ); - ++goodCount; - } + int nTuples = PQntuples( result ); + for ( int ii = 0; ii < nTuples; ++ii ) { + names.append( PQgetvalue( result, ii, 0 ) ); + names.append( "/" ); + names.append( PQgetvalue( result, ii, 1 ) ); + names.append( "\n" ); } PQclear( result ); - *nNames = goodCount; + *nNames = nTuples; } int @@ -428,23 +423,3 @@ DBMgr::RemoveStoredMessage( int msgID ) execSql( query ); } - -/* - Schema: - CREATE TABLE games ( - cid integer, - room VARCHAR(32), - connName VARCHAR(64) UNIQUE PRIMARY KEY, - nTotal INTEGER, - nPerDevice INTEGER[], - lang INTEGER, - ctime TIMESTAMP, - mtime TIMESTAMP -); - - May also want - seeds INTEGER ARRAY, - ipAddresses INTEGER ARRAY, - - - */ diff --git a/xwords4/relay/dbmgr.h b/xwords4/relay/dbmgr.h index 4b74b3afc..c84ec76b5 100644 --- a/xwords4/relay/dbmgr.h +++ b/xwords4/relay/dbmgr.h @@ -46,7 +46,7 @@ class DBMgr { int nPlayersH, bool wantsPublic, char* connNameBuf, int bufLen, int* nPlayersHP ); - HostID AddDevice( const char* const connName, int nToAdd ); + HostID AddDevice( const char* const connName, int nToAdd, unsigned short seed ); void RmDevice( const char* const connName, HostID id ); void AddCID( const char* connName, CookieID cid ); void ClearCID( const char* connName ); diff --git a/xwords4/relay/xwrelay.sh b/xwords4/relay/xwrelay.sh index 19aadf10d..497ffd003 100755 --- a/xwords4/relay/xwrelay.sh +++ b/xwords4/relay/xwrelay.sh @@ -18,6 +18,18 @@ usage() { make_db() { createdb xwgames + cat | psql xwgames --file - <