track device seeds in db.

This commit is contained in:
Eric House 2010-09-28 18:06:50 -07:00
parent a6f5ea5f4a
commit bc65377cfd
4 changed files with 33 additions and 45 deletions

View file

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

View file

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

View file

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

View file

@ -18,6 +18,18 @@ usage() {
make_db() {
createdb xwgames
cat | psql xwgames --file - <<EOF
create or replace function sum_array( DECIMAL [] )
returns decimal
as \$\$
select sum(\$1[i])
from generate_series(
array_lower(\$1,1),
array_upper(\$1,1)
) g(i);
\$\$ language sql immutable;
EOF
cat | psql xwgames --file - <<EOF
CREATE TABLE games (
cid integer,
@ -27,6 +39,7 @@ pub BOOLEAN,
connName VARCHAR(64) UNIQUE PRIMARY KEY,
nTotal INTEGER,
nPerDevice INTEGER[],
seeds INTEGER[],
ctime TIMESTAMP,
mtime TIMESTAMP
);