sending invites works

though play after it still glitchy
This commit is contained in:
Eric House 2021-02-19 17:03:14 -08:00
parent 43874a6a5d
commit 79433de160

View file

@ -75,6 +75,7 @@
#define BUTTON_UNDO "Undo" #define BUTTON_UNDO "Undo"
#define BUTTON_REDO "Redo" #define BUTTON_REDO "Redo"
#define BUTTON_VALS "Vals" #define BUTTON_VALS "Vals"
#define BUTTON_INVITE "Invite"
#define BUTTON_EXIT "Quit" #define BUTTON_EXIT "Quit"
#define BUTTON_GAME_NEW "New Game" #define BUTTON_GAME_NEW "New Game"
@ -252,32 +253,37 @@ call_alert( const char* msg )
call_dialog( msg, buttons, NULL, NULL ); call_dialog( msg, buttons, NULL, NULL );
} }
static bool
sendStreamToDev( XWStreamCtxt* stream, const MQTTDevID* devID )
{
XP_S16 nSent = -1;
XP_UCHAR topic[64];
formatMQTTTopic( devID, topic, sizeof(topic) );
XP_U16 streamLen = stream_getSize( stream );
bool success = call_mqttSend( topic, stream_getPtr( stream ), streamLen );
stream_destroy( stream, NULL );
LOG_RETURNF("%d", nSent);
return success;
}
static XP_S16 static XP_S16
send_msg( XWEnv xwe, const XP_U8* buf, XP_U16 len, send_msg( XWEnv xwe, const XP_U8* buf, XP_U16 len,
const XP_UCHAR* msgNo, const CommsAddrRec* addr, const XP_UCHAR* msgNo, const CommsAddrRec* addr,
CommsConnType conType, XP_U32 gameID, void* closure ) CommsConnType conType, XP_U32 gameID, void* closure )
{ {
XP_S16 nSent = -1; XP_S16 nSent = -1;
LOG_FUNC();
Globals* globals = (Globals*)closure; Globals* globals = (Globals*)closure;
if ( addr_hasType( addr, COMMS_CONN_MQTT ) ) { if ( addr_hasType( addr, COMMS_CONN_MQTT ) ) {
MQTTDevID devID = addr->u.mqtt.devID; // MQTTDevID devID = addr->u.mqtt.devID;
XWStreamCtxt* stream = mem_stream_make_raw( MPPARM(globals->mpool) XWStreamCtxt* stream = mem_stream_make_raw( MPPARM(globals->mpool)
globals->vtMgr ); globals->vtMgr );
dvc_makeMQTTMessage( globals->dutil, NULL, stream, dvc_makeMQTTMessage( globals->dutil, NULL, stream,
gameID, buf, len ); gameID, buf, len );
if ( sendStreamToDev( stream, &addr->u.mqtt.devID ) ) {
XP_UCHAR topic[64];
formatMQTTTopic( &devID, topic, sizeof(topic) );
XP_U16 streamLen = stream_getSize( stream );
if ( call_mqttSend( topic, stream_getPtr( stream ), streamLen ) ) {
nSent = len; nSent = len;
} }
stream_destroy( stream, NULL );
} }
LOG_RETURNF( "%d", nSent ); LOG_RETURNF( "%d", nSent );
@ -316,6 +322,35 @@ formatNameKey( char* buf, size_t len, int gameID )
snprintf( buf, len, KEY_NAME_PREFIX "%X", gameID ); snprintf( buf, len, KEY_NAME_PREFIX "%X", gameID );
} }
static void
makeSelfAddr( Globals* globals, CommsAddrRec* addr )
{
addr_setType( addr, COMMS_CONN_MQTT );
dvc_getMQTTDevID( globals->dutil, NULL, &addr->u.mqtt.devID );
}
static void
onGotInviteeID( void* closure, const char* mqttid )
{
MQTTDevID remoteDevID;
if ( strToMQTTCDevID( mqttid, &remoteDevID ) ) {
Globals* globals = (Globals*)closure;
CommsAddrRec myAddr = {0};
makeSelfAddr( globals, &myAddr );
NetLaunchInfo nli = {0}; /* include everything!!! */
nli_init( &nli, &globals->gs.gi, &myAddr, 1, 1 );
XWStreamCtxt* stream = mem_stream_make_raw( MPPARM(globals->mpool)
globals->vtMgr );
dvc_makeMQTTInvite( globals->dutil, NULL, stream, &nli );
sendStreamToDev( stream, &remoteDevID );
} else {
call_alert( "MQTT id looks badly formed" );
}
}
static void static void
onGameButton( void* closure, const char* button ) onGameButton( void* closure, const char* button )
{ {
@ -344,6 +379,9 @@ onGameButton( void* closure, const char* button )
} else if ( 0 == strcmp(button, BUTTON_VALS) ) { } else if ( 0 == strcmp(button, BUTTON_VALS) ) {
globals->cp.tvType = (globals->cp.tvType + 1) % TVT_N_ENTRIES; globals->cp.tvType = (globals->cp.tvType + 1) % TVT_N_ENTRIES;
draw = board_prefsChanged( board, &globals->cp ); draw = board_prefsChanged( board, &globals->cp );
} else if ( 0 == strcmp(button, BUTTON_INVITE) ) {
call_get_string( "Invitee's MQTT Device ID?", "",
onGotInviteeID, globals );
} else if ( 0 == strcmp(button, BUTTON_EXIT) ) { } else if ( 0 == strcmp(button, BUTTON_EXIT) ) {
doExit( globals ); doExit( globals );
} }
@ -384,6 +422,11 @@ updateGameButtons( Globals* globals )
} }
buttons[cur++] = BUTTON_VALS; buttons[cur++] = BUTTON_VALS;
XP_U16 nPending = server_getPendingRegs( globals->gs.game.server );
if ( 0 < nPending ) {
buttons[cur++] = BUTTON_INVITE;
}
} }
buttons[cur++] = NULL; buttons[cur++] = NULL;
@ -415,6 +458,7 @@ cleanupGame( Globals* globals )
{ {
if ( !!globals->gs.util ) { if ( !!globals->gs.util ) {
game_dispose( &globals->gs.game, NULL ); game_dispose( &globals->gs.game, NULL );
gi_disposePlayerInfo( MPPARM(globals->mpool) &globals->gs.gi );
wasm_util_destroy( globals->gs.util ); wasm_util_destroy( globals->gs.util );
XP_MEMSET( &globals->gs, 0, sizeof(globals->gs) ); XP_MEMSET( &globals->gs, 0, sizeof(globals->gs) );
} }
@ -723,6 +767,9 @@ loadAndDraw( Globals* globals, const NetLaunchInfo* invite,
globals->gs.gi.phoniesAction = PHONIES_WARN; globals->gs.gi.phoniesAction = PHONIES_WARN;
globals->gs.gi.hintsNotAllowed = !!params && params->hintsNotAllowed || false; globals->gs.gi.hintsNotAllowed = !!params && params->hintsNotAllowed || false;
globals->gs.gi.gameID = 0; globals->gs.gi.gameID = 0;
globals->gs.gi.dictLang = 1; /* English only for now */
replaceStringIfDifferent( globals->mpool, &globals->gs.gi.dictName,
"CollegeEng_2to8" );
globals->gs.gi.nPlayers = 2; globals->gs.gi.nPlayers = 2;
globals->gs.gi.boardSize = 15; globals->gs.gi.boardSize = 15;
globals->gs.gi.players[0].name = copyString( globals->mpool, "Player 1" ); /* FIXME */ globals->gs.gi.players[0].name = copyString( globals->mpool, "Player 1" ); /* FIXME */
@ -743,6 +790,11 @@ loadAndDraw( Globals* globals, const NetLaunchInfo* invite,
globals->gs.util, globals->draw, globals->gs.util, globals->draw,
&globals->cp, &globals->procs ); &globals->cp, &globals->procs );
nameGame( globals ); nameGame( globals );
if ( !!globals->gs.game.comms ) {
CommsAddrRec addr = {0};
makeSelfAddr( globals, &addr );
comms_augmentHostAddr( globals->gs.game.comms, NULL, &addr );
}
} }
startGame( globals, NULL ); startGame( globals, NULL );
} }