diff --git a/xwords4/relay/cref.cpp b/xwords4/relay/cref.cpp index 60fac15ef..aff82b799 100644 --- a/xwords4/relay/cref.cpp +++ b/xwords4/relay/cref.cpp @@ -833,6 +833,8 @@ CookieRef::increasePlayerCounts( const CRefEvent* evt, bool reconn ) assert( m_nPlayersHere <= m_nPlayersSought ); addHost = true; + DBMgr::Get()->AddPlayers( ConnName(), nPlayersH ); + /* if ( m_nPlayersHere == m_nPlayersSought ) { /\* complete! *\/ */ /* newEvt = XWE_ALLHERE; */ /* } else { */ diff --git a/xwords4/relay/crefmgr.cpp b/xwords4/relay/crefmgr.cpp index 7dbe4a27f..a226746a1 100644 --- a/xwords4/relay/crefmgr.cpp +++ b/xwords4/relay/crefmgr.cpp @@ -64,7 +64,7 @@ CRefMgr::CRefMgr() pthread_mutex_init( &m_roomsFilledMutex, NULL ); pthread_mutex_init( &m_freeList_mutex, NULL ); pthread_rwlock_init( &m_cookieMapRWLock, NULL ); - m_db = new DBMgr(); + m_db = DBMgr::Get(); } CRefMgr::~CRefMgr() @@ -134,6 +134,14 @@ CRefMgr::FindOpenGameFor( const char* cookie, const char* connName, HostID hid, int socket, int nPlayersH, int nPlayersT, int gameSeed, int langCode, bool* alreadyHere ) { +#if 1 + CookieRef* found = NULL; + CookieID cid = m_db->FindOpen( cookie, langCode, nPlayersT, nPlayersH ); + if ( cid > 0 ) { + found = getCookieRef_impl( cid ); + } + return found; +#else logf( XW_LOGINFO, "%s(cookie=%s,connName=%s,hid=%d,seed=%x,socket=%d," "here=%d,total=%d)", __func__, cookie, connName, hid, gameSeed, socket, nPlayersH, nPlayersT ); @@ -200,6 +208,7 @@ CRefMgr::FindOpenGameFor( const char* cookie, const char* connName, logf( XW_LOGINFO, "%s=>%p", __func__, found ); return found; +#endif } /* FindOpenGameFor */ CookieID diff --git a/xwords4/relay/crefmgr.h b/xwords4/relay/crefmgr.h index 527bfd6ba..113a8d38f 100644 --- a/xwords4/relay/crefmgr.h +++ b/xwords4/relay/crefmgr.h @@ -78,10 +78,12 @@ class CRefMgr { it. */ + private: + CRefMgr(); + public: static CRefMgr* Get(); - CRefMgr(); ~CRefMgr(); void CloseAll(); diff --git a/xwords4/relay/dbmgr.cpp b/xwords4/relay/dbmgr.cpp index b76143b47..5e7e3fe8f 100644 --- a/xwords4/relay/dbmgr.cpp +++ b/xwords4/relay/dbmgr.cpp @@ -26,6 +26,17 @@ #define DB_NAME "xwgames" +static DBMgr* s_instance = NULL; + +/* static */ DBMgr* +DBMgr::Get() +{ + if ( s_instance == NULL ) { + s_instance = new DBMgr(); + } + return s_instance; +} /* Get */ + DBMgr::DBMgr() { logf( XW_LOGINFO, "%s called", __func__ ); @@ -37,15 +48,16 @@ DBMgr::DBMgr() /* Now figure out what the largest cid currently is. There must be a way to get postgres to do this for me.... */ - const char* query = "SELECT cid FROM games ORDER BY - cid LIMIT 1"; - PGresult* result = PQexec( m_pgconn, query ); - if ( 0 == PQntuples( result ) ) { - m_nextCID = 1; - } else { - char* value = PQgetvalue( result, 0, 0 ); - m_nextCID = 1 + atoi( value ); - } - logf( XW_LOGINFO, "%s: m_nextCID=%d", __func__, m_nextCID ); + /* const char* query = "SELECT cid FROM games ORDER BY - cid LIMIT 1"; */ + /* PGresult* result = PQexec( m_pgconn, query ); */ + /* if ( 0 == PQntuples( result ) ) { */ + /* m_nextCID = 1; */ + /* } else { */ + /* char* value = PQgetvalue( result, 0, 0 ); */ + /* m_nextCID = 1 + atoi( value ); */ + /* } */ + /* PQclear(result); */ + /* logf( XW_LOGINFO, "%s: m_nextCID=%d", __func__, m_nextCID ); */ } DBMgr::~DBMgr() @@ -67,10 +79,11 @@ DBMgr::AddNew( const char* cookie, const char* connName, CookieID cid, "(cid, cookie, connName, nTotal, nHere, lang) " "VALUES( %d, '%s', '%s', %d, %d, %d )"; char buf[256]; - snprintf( buf, sizeof(buf), fmt, m_nextCID++, cookie, connName, + snprintf( buf, sizeof(buf), fmt, cid/*m_nextCID++*/, cookie, connName, nPlayersT, nPlayersH, langCode ); logf( XW_LOGINFO, "passing %s", buf ); PGresult* result = PQexec( m_pgconn, buf ); + PQclear( result ); #else const char* command = "INSERT INTO games (cookie, connName, ntotal, nhere, lang) " "VALUES( $1, $2, $3, $4, $5 )"; @@ -95,19 +108,55 @@ DBMgr::AddNew( const char* cookie, const char* connName, CookieID cid, logf( XW_LOGINFO, "PQexecParams=>%d", result ); } +CookieID +DBMgr::FindOpen( const char* cookie, int lang, int nPlayersT, int nPlayersH ) +{ + CookieID cid = 0; + + const char* fmt = "SELECT cid from games where cookie = '%s' " + "AND lang = %d " + "AND nTotal = %d " + "AND %d <= nTotal-nHere " + "LIMIT 1"; + char query[256]; + snprintf( query, sizeof(query), fmt, + cookie, lang, nPlayersT, nPlayersH ); + logf( XW_LOGINFO, "query: %s", query ); + + PGresult* result = PQexec( m_pgconn, query ); + if ( 1 == PQntuples( result ) ) { + cid = atoi( PQgetvalue( result, 0, 0 ) ); + assert( cid > 0 ); + } + PQclear( result ); + logf( XW_LOGINFO, "%s=>%d", __func__, cid ); + return cid; +} + +void +DBMgr::AddPlayers( const char* connName, int nToAdd ) +{ + const char* fmt = "UPDATE games SET nHere = nHere+%d " + "WHERE connName = '%s'"; + char query[256]; + snprintf( query, sizeof(query), fmt, nToAdd, connName ); + logf( XW_LOGINFO, "%s: query: %s", __func__, query ); + + PGresult* result = PQexec( m_pgconn, query ); + PQclear( result ); +} /* Schema: CREATE TABLE games ( - cid integer PRIMARY KEY, + cid integer, cookie VARCHAR(32), - connName VARCHAR(64) UNIQUE, + connName VARCHAR(64) UNIQUE PRIMARY KEY, nTotal INTEGER, nHere INTEGER, lang INTEGER, ctime TIMESTAMP, mtime TIMESTAMP ); + */ - - diff --git a/xwords4/relay/dbmgr.h b/xwords4/relay/dbmgr.h index b1e74e4d0..6647f9e7a 100644 --- a/xwords4/relay/dbmgr.h +++ b/xwords4/relay/dbmgr.h @@ -26,15 +26,22 @@ class DBMgr { public: - DBMgr(); + static DBMgr* Get(); + ~DBMgr(); void AddNew( const char* cookie, const char* connName, CookieID cid, int langCode, int nPlayersT, int nPlayersH ); + CookieID FindOpen( const char* cookie, int lang, int nPlayersT, + int nPlayersH ); + + void AddPlayers( const char* connName, int nToAdd ); + private: + DBMgr(); PGconn* m_pgconn; - int m_nextCID; + //int m_nextCID; }; /* DBMgr */