add test for email-style invitation

This commit is contained in:
Eric House 2024-08-09 10:48:27 -07:00
parent 6092ad2d4b
commit 2f8164d8c7
8 changed files with 91 additions and 10 deletions

View file

@ -76,7 +76,8 @@ nli_init( NetLaunchInfo* nli, const CurGameInfo* gi, const CommsAddrRec* addr,
}
void
nliToGI( const NetLaunchInfo* nli, XWEnv xwe, XW_UtilCtxt* util, CurGameInfo* gi )
nliToGI( const NetLaunchInfo* nli, XWEnv xwe, XW_UtilCtxt* util,
CurGameInfo* gi )
{
gi_setNPlayers( gi, xwe, util, nli->nPlayersT, nli->nPlayersH );
gi->gameID = nli->gameID;

View file

@ -113,6 +113,8 @@ STATtoStr(STAT stat)
CASESTR(STAT_NEW_THREE);
CASESTR(STAT_NEW_FOUR);
CASESTR(STAT_NEW_REMATCH);
CASESTR(STAT_SMS_SENT);
CASESTR(STAT_SMS_RCVD);
default:
XP_ASSERT(0);
}

View file

@ -38,6 +38,9 @@ typedef enum {
STAT_NEW_FOUR,
STAT_NEW_REMATCH,
STAT_SMS_RCVD,
STAT_SMS_SENT,
STAT_NSTATS,
} STAT;

View file

@ -1474,6 +1474,12 @@ addInvitesWrapper( void* closure, XP_U32 gameID, XP_U16 nRemotes,
cb_addInvites( aGlobals->cbState, gameID, nRemotes, destAddrs );
}
static void
newGuestWrapper( void* closure, const NetLaunchInfo* nli )
{
inviteReceivedCurses( closure, nli );
}
static const CommonGlobals*
getForGameIDWrapper( void* closure, XP_U32 gameID )
{
@ -1578,6 +1584,7 @@ cursesmain( XP_Bool XP_UNUSED(isServer), LaunchParams* params )
.quit = quitWrapper,
.newGame = newGameWrapper,
.addInvites = addInvitesWrapper,
.newGuest = newGuestWrapper,
.makeMoveIf = makeMoveIfWrapper,
.getForGameID = getForGameIDWrapper,
.makeRematch = makeRematchWrapper,

View file

@ -123,6 +123,38 @@ inviteFromArgs( CmdWrapper* wr, cJSON* args )
return XP_TRUE;
}
static XP_Bool
newGuestFromArgs( CmdWrapper* wr, cJSON* args )
{
XP_U32 gameID = gidFromObject( args );
cJSON* tmp = cJSON_GetObjectItem( args, "nPlayersT" );
XP_U16 nPlayersT = tmp->valueint;
NetLaunchInfo nli = {.gameID = gameID,
.nPlayersT = nPlayersT,
.nPlayersH = 1,
};
cJSON* addr = cJSON_GetObjectItem( args, "addr" );
tmp = cJSON_GetObjectItem( addr, "mqtt" );
if ( !!tmp ) {
XP_STRCAT( nli.mqttDevID, tmp->valuestring );
types_addType( &nli._conTypes, COMMS_CONN_MQTT );
}
tmp = cJSON_GetObjectItem( addr, "sms" );
if ( !!tmp ) {
XP_STRCAT( nli.phone, tmp->valuestring );
types_addType( &nli._conTypes, COMMS_CONN_SMS );
}
tmp = cJSON_GetObjectItem( args, "dict" );
if ( !!tmp ) {
XP_STRCAT( nli.dict, tmp->valuestring );
}
(*wr->procs.newGuest)( wr->closure, &nli );
return XP_TRUE;
}
static XP_Bool
moveifFromArgs( CmdWrapper* wr, cJSON* args )
{
@ -419,6 +451,8 @@ on_incoming_signal( GSocketService* XP_UNUSED(service),
}
} else if ( 0 == strcmp( cmdStr, "invite" ) ) {
success = inviteFromArgs( wr, args );
} else if ( 0 == strcmp( cmdStr, "inviteRcvd" ) ) {
success = newGuestFromArgs( wr, args );
} else if ( 0 == strcmp( cmdStr, "moveIf" ) ) {
success = moveifFromArgs( wr, args );
} else if ( 0 == strcmp( cmdStr, "rematch" ) ) {

View file

@ -30,6 +30,7 @@ typedef struct _CmdWrapper {
XP_Bool (*newGame)( void* closure, CurGameInfo* gi, XP_U32* newGameID );
void (*addInvites)( void* closure, XP_U32 gameID, XP_U16 nRemotes,
const CommsAddrRec destAddrs[] );
void (*newGuest)( void* closure, const NetLaunchInfo* nli );
XP_Bool (*makeMoveIf)( void* closure, XP_U32 gameID, XP_Bool tryTrade );
const CommonGlobals* (*getForGameID)( void* closure, XP_U32 gameID );
XP_Bool (*makeRematch)( void* closure, XP_U32 gameID, RematchOrder ro,

View file

@ -31,6 +31,7 @@
#include "linuxutl.h"
#include "strutils.h"
#include "smsproto.h"
#include "stats.h"
#include "linuxmain.h"
#define SMS_DIR "/tmp/xw_sms"
@ -256,6 +257,7 @@ parseAndDispatch( LaunchParams* params, uint8_t* buf, int len,
(*storage->procs->msgReceived)( storage->procClosure, addr,
msg->gameID,
msg->data, msg->len );
sts_increment( params->dutil, NULL_XWE, STAT_SMS_RCVD );
break;
case INVITE: {
NetLaunchInfo nli = {};
@ -383,6 +385,7 @@ sendOrRetry( LaunchParams* params, SMSMsgArray* arr, SMS_CMD cmd,
// doSend( params, msg->data, msg->len, phone, port, gameID );
(void)write_fake_sms( params, msg->data, msg->len, msgNo,
phone, port );
sts_increment( params->dutil, NULL_XWE, STAT_SMS_SENT );
}
LinSMSData* storage = getStorage( params );
@ -514,7 +517,6 @@ check_for_files_once( gpointer data )
for ( ; ; ) {
lock_queue( storage );
uint8_t buf[256];
CommsAddrRec fromAddr = {};
XP_S16 nRead = -1;

View file

@ -441,7 +441,34 @@ class Device():
for guest in guests:
Device.getForPlayer(guest).expectInvite(newGid, rematchLevel)
# inviting means either causing host to send an in-game invitation
# (the way rematch works) or causing guest to register (as happens
# when email or SMS is used for invitations.)
def invite(self, game):
if bool(random.randint(0,1)): self.inviteOutOfBand(game)
else: self.inviteInBand(game)
def inviteOutOfBand(self, game):
# For each invitee, we need to make sure it exists, launch it,
# and then send it the equivalent of an emailed invitation.
nPlayersT = 1 + len(game.guestNames)
for name in game.guestNames:
invitee = Device.getForPlayer(name)
invitee.launchIfNot()
response = invitee._sendWaitReply('inviteRcvd', gid=game.gid,
nPlayersT=nPlayersT,
dict = self.args.DICTS[0],
addr = self._mkAddr(self),
)
if response['success']:
invitee.expectInvite(game.gid, game.rematchLevel)
game.needsInvite = False
def inviteInBand(self, game):
remotes = []
guestDevs = []
useRandomDevID = random.randint(0, 100) < self.args.BAD_INVITE_PCT
@ -449,14 +476,7 @@ class Device():
guestDev = Device.getForPlayer(game.guestNames[ii])
guestDevs.append(guestDev)
addr = {}
if self.args.WITH_MQTT:
if useRandomDevID:
mqttAddr = '{:016X}'.format(random.randint(1, 0x7FFFFFF))
else:
mqttAddr = guestDev.mqttDevID
addr['mqtt'] = mqttAddr
if self.args.WITH_SMS: addr['sms'] = guestDev.smsNumber
addr = self._mkAddr(guestDev, useRandomDevID)
remotes.append({'addr': addr})
response = self._sendWaitReply('invite', gid=game.gid,
@ -467,6 +487,17 @@ class Device():
guestDev.expectInvite(game.gid, game.rematchLevel)
game.needsInvite = useRandomDevID
def _mkAddr(self, dev, useRandomDevID=False):
addr = {}
if self.args.WITH_MQTT:
if useRandomDevID:
mqttAddr = '{:016X}'.format(random.randint(1, 0x7FFFFFF))
else:
mqttAddr = dev.mqttDevID
addr['mqtt'] = mqttAddr
if self.args.WITH_SMS: addr['sms'] = dev.smsNumber
return addr
def expectInvite(self, gid, rematchLevel):
self.guestGames.append(GuestGameInfo(self, gid, rematchLevel))
self.launchIfNot()