Don't join a game that's been idle for more than two weeks

The two weeks is configurable. But the idea is somebody creates a game
with a random name but it's never joined. Eventually the name is used
again and the first to connect joins the existing game that will never
progress. I could just prune from the DB, but this is less work ongoing.
This commit is contained in:
Eric House 2020-01-10 16:43:48 -08:00
parent dfb33c01af
commit 4fa1b3faf4
4 changed files with 28 additions and 11 deletions

View file

@ -254,9 +254,9 @@ CRefMgr::getMakeCookieRef( const char* cookie, int nPlayersH, int nPlayersT,
wantsPublic, connNameBuf,
sizeof(connNameBuf), &alreadyHere, &cid );
if ( !*seenSeed ) {
cid = m_db->FindOpen( cookie, langCode, nPlayersT, nPlayersH,
wantsPublic, connNameBuf, sizeof(connNameBuf),
&alreadyHere );
cid = m_db->FindRecentOpen( cookie, langCode, nPlayersT, nPlayersH,
wantsPublic, connNameBuf, sizeof(connNameBuf),
&alreadyHere );
}
if ( cid > 0 ) {

View file

@ -66,6 +66,12 @@ DBMgr::DBMgr()
m_useB64 = tmp != 0;
logf( XW_LOGINFO, "%s: m_useB64=%d", __func__, m_useB64 );
if ( !RelayConfigs::GetConfigs()->GetValueFor( "NEWGAME_INTERVAL",
m_interval,
sizeof(m_interval) ) ) {
assert(0);
}
pthread_key_create( &m_conn_key, destr_function );
pthread_mutex_init( &m_haveNoMessagesMutex, NULL );
@ -330,20 +336,21 @@ DBMgr::SeenSeed( const char* cookie, unsigned short seed,
}
CookieID
DBMgr::FindOpen( const char* cookie, int lang, int nPlayersT, int nPlayersH,
bool wantsPublic, char* connNameBuf, int bufLen,
int* nPlayersHP )
DBMgr::FindRecentOpen( const char* cookie, int lang, int nPlayersT, int nPlayersH,
bool wantsPublic, char* connNameBuf, int bufLen,
int* nPlayersHP )
{
QueryBuilder qb;
qb.appendQueryf("SELECT cid, connName, sum_array(nPerDevice) FROM "
GAMES_TABLE
" WHERE NOT dead"
" AND now() - interval '%s' < ANY(mtimes) "
" AND room ILIKE $$"
" AND lang = $$"
" AND nTotal = $$"
" AND $$ <= nTotal-sum_array(nPerDevice)"
" AND $$ = pub"
" LIMIT 1")
" LIMIT 1", m_interval )
.appendParam(cookie)
.appendParam(lang)
.appendParam(nPlayersT)
@ -366,7 +373,7 @@ DBMgr::FindOpen( const char* cookie, int lang, int nPlayersT, int nPlayersH,
PQclear( result );
logf( XW_LOGINFO, "%s=>%d", __func__, cid );
return cid;
} /* FindOpen */
} /* FindRecentOpen */
bool
DBMgr::AllDevsAckd( const char* const connName )

View file

@ -93,9 +93,9 @@ class DBMgr {
char* connNameBuf, int bufLen, int* nPlayersHP,
CookieID* cid );
CookieID FindOpen( const char* cookie, int lang, int nPlayersT,
int nPlayersH, bool wantsPublic,
char* connNameBuf, int bufLen, int* nPlayersHP );
CookieID FindRecentOpen( const char* cookie, int lang, int nPlayersT,
int nPlayersH, bool wantsPublic,
char* connNameBuf, int bufLen, int* nPlayersHP );
bool AllDevsAckd( const char* const connName );
DevIDRelay RegisterDevice( const DevID* host );
@ -196,6 +196,8 @@ class DBMgr {
pthread_key_t m_conn_key;
bool m_useB64;
char m_interval[64];
pthread_mutex_t m_haveNoMessagesMutex;
set<DevIDRelay> m_haveNoMessagesDevID;
set<StrWPF> m_haveNoMessagesConnname;

View file

@ -76,6 +76,14 @@ DB_PORT=5432
LOGLEVEL=0
LOGFILE_PATH=./xwrelay.log
# How long after an unjoined game has last been heard from will we
# refuse to add another device to it? The problem I'm addressing is
# that with 16-bit ints being used as room names on Android sometimes
# a new game si generated with the same room as a game whose creating
# device has been in a landfill for years. Passed into a sql interval
# statement, e.g. WHERE ctime >= now() - interval '2 weeks'
NEWGAME_INTERVAL=14 days
# Delay sending packets so devices/emulators on the same machine as
# relay have a bit more natural experience
# SEND_DELAY_MILLIS=500