do finding of open games using database rather than cref list, which

requires updating the number of players when new devices join.  Drop
requirement that cid be unique.
This commit is contained in:
Andy2 2010-09-12 04:49:03 -07:00
parent 663551fcb5
commit ded6e22180
5 changed files with 87 additions and 18 deletions

View file

@ -833,6 +833,8 @@ CookieRef::increasePlayerCounts( const CRefEvent* evt, bool reconn )
assert( m_nPlayersHere <= m_nPlayersSought ); assert( m_nPlayersHere <= m_nPlayersSought );
addHost = true; addHost = true;
DBMgr::Get()->AddPlayers( ConnName(), nPlayersH );
/* if ( m_nPlayersHere == m_nPlayersSought ) { /\* complete! *\/ */ /* if ( m_nPlayersHere == m_nPlayersSought ) { /\* complete! *\/ */
/* newEvt = XWE_ALLHERE; */ /* newEvt = XWE_ALLHERE; */
/* } else { */ /* } else { */

View file

@ -64,7 +64,7 @@ CRefMgr::CRefMgr()
pthread_mutex_init( &m_roomsFilledMutex, NULL ); pthread_mutex_init( &m_roomsFilledMutex, NULL );
pthread_mutex_init( &m_freeList_mutex, NULL ); pthread_mutex_init( &m_freeList_mutex, NULL );
pthread_rwlock_init( &m_cookieMapRWLock, NULL ); pthread_rwlock_init( &m_cookieMapRWLock, NULL );
m_db = new DBMgr(); m_db = DBMgr::Get();
} }
CRefMgr::~CRefMgr() CRefMgr::~CRefMgr()
@ -134,6 +134,14 @@ CRefMgr::FindOpenGameFor( const char* cookie, const char* connName,
HostID hid, int socket, int nPlayersH, int nPlayersT, HostID hid, int socket, int nPlayersH, int nPlayersT,
int gameSeed, int langCode, bool* alreadyHere ) 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," logf( XW_LOGINFO, "%s(cookie=%s,connName=%s,hid=%d,seed=%x,socket=%d,"
"here=%d,total=%d)", __func__, cookie, connName, hid, gameSeed, "here=%d,total=%d)", __func__, cookie, connName, hid, gameSeed,
socket, nPlayersH, nPlayersT ); socket, nPlayersH, nPlayersT );
@ -200,6 +208,7 @@ CRefMgr::FindOpenGameFor( const char* cookie, const char* connName,
logf( XW_LOGINFO, "%s=>%p", __func__, found ); logf( XW_LOGINFO, "%s=>%p", __func__, found );
return found; return found;
#endif
} /* FindOpenGameFor */ } /* FindOpenGameFor */
CookieID CookieID

View file

@ -78,10 +78,12 @@ class CRefMgr {
it. it.
*/ */
private:
CRefMgr();
public: public:
static CRefMgr* Get(); static CRefMgr* Get();
CRefMgr();
~CRefMgr(); ~CRefMgr();
void CloseAll(); void CloseAll();

View file

@ -26,6 +26,17 @@
#define DB_NAME "xwgames" #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() DBMgr::DBMgr()
{ {
logf( XW_LOGINFO, "%s called", __func__ ); 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 /* Now figure out what the largest cid currently is. There must be a way
to get postgres to do this for me.... */ to get postgres to do this for me.... */
const char* query = "SELECT cid FROM games ORDER BY - cid LIMIT 1"; /* const char* query = "SELECT cid FROM games ORDER BY - cid LIMIT 1"; */
PGresult* result = PQexec( m_pgconn, query ); /* PGresult* result = PQexec( m_pgconn, query ); */
if ( 0 == PQntuples( result ) ) { /* if ( 0 == PQntuples( result ) ) { */
m_nextCID = 1; /* m_nextCID = 1; */
} else { /* } else { */
char* value = PQgetvalue( result, 0, 0 ); /* char* value = PQgetvalue( result, 0, 0 ); */
m_nextCID = 1 + atoi( value ); /* m_nextCID = 1 + atoi( value ); */
} /* } */
logf( XW_LOGINFO, "%s: m_nextCID=%d", __func__, m_nextCID ); /* PQclear(result); */
/* logf( XW_LOGINFO, "%s: m_nextCID=%d", __func__, m_nextCID ); */
} }
DBMgr::~DBMgr() DBMgr::~DBMgr()
@ -67,10 +79,11 @@ DBMgr::AddNew( const char* cookie, const char* connName, CookieID cid,
"(cid, cookie, connName, nTotal, nHere, lang) " "(cid, cookie, connName, nTotal, nHere, lang) "
"VALUES( %d, '%s', '%s', %d, %d, %d )"; "VALUES( %d, '%s', '%s', %d, %d, %d )";
char buf[256]; 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 ); nPlayersT, nPlayersH, langCode );
logf( XW_LOGINFO, "passing %s", buf ); logf( XW_LOGINFO, "passing %s", buf );
PGresult* result = PQexec( m_pgconn, buf ); PGresult* result = PQexec( m_pgconn, buf );
PQclear( result );
#else #else
const char* command = "INSERT INTO games (cookie, connName, ntotal, nhere, lang) " const char* command = "INSERT INTO games (cookie, connName, ntotal, nhere, lang) "
"VALUES( $1, $2, $3, $4, $5 )"; "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 ); 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: Schema:
CREATE TABLE games ( CREATE TABLE games (
cid integer PRIMARY KEY, cid integer,
cookie VARCHAR(32), cookie VARCHAR(32),
connName VARCHAR(64) UNIQUE, connName VARCHAR(64) UNIQUE PRIMARY KEY,
nTotal INTEGER, nTotal INTEGER,
nHere INTEGER, nHere INTEGER,
lang INTEGER, lang INTEGER,
ctime TIMESTAMP, ctime TIMESTAMP,
mtime TIMESTAMP mtime TIMESTAMP
); );
*/ */

View file

@ -26,15 +26,22 @@
class DBMgr { class DBMgr {
public: public:
DBMgr(); static DBMgr* Get();
~DBMgr(); ~DBMgr();
void AddNew( const char* cookie, const char* connName, CookieID cid, void AddNew( const char* cookie, const char* connName, CookieID cid,
int langCode, int nPlayersT, int nPlayersH ); 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: private:
DBMgr();
PGconn* m_pgconn; PGconn* m_pgconn;
int m_nextCID; //int m_nextCID;
}; /* DBMgr */ }; /* DBMgr */