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 );
addHost = true;
DBMgr::Get()->AddPlayers( ConnName(), nPlayersH );
/* if ( m_nPlayersHere == m_nPlayersSought ) { /\* complete! *\/ */
/* newEvt = XWE_ALLHERE; */
/* } else { */

View file

@ -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

View file

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

View file

@ -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
);
*/

View file

@ -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 */