2021-02-16 14:59:54 +01:00
|
|
|
/* -*- compile-command: "cd ../wasm && make MEMDEBUG=TRUE install -j3"; -*- */
|
2021-02-06 17:54:08 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2021 by Eric House (xwords@eehouse.org). All rights reserved.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
2021-02-01 17:30:34 +01:00
|
|
|
|
2021-02-26 05:39:08 +01:00
|
|
|
/* TODO
|
|
|
|
*
|
|
|
|
* Downloading wordlists. Will likely need to fetch and store them as base64
|
|
|
|
* data, meaning I'll need a working btoa to use them. Here's one way to download:
|
|
|
|
* https://emscripten.org/docs/api_reference/fetch.html
|
|
|
|
*/
|
|
|
|
|
2021-02-02 22:49:27 +01:00
|
|
|
#include <sys/time.h>
|
2021-02-01 17:30:34 +01:00
|
|
|
#include <stdio.h>
|
2021-02-02 05:13:25 +01:00
|
|
|
#include <stdarg.h>
|
2021-02-01 17:30:34 +01:00
|
|
|
#include <SDL2/SDL.h>
|
2021-02-02 18:08:41 +01:00
|
|
|
#include <SDL2/SDL_ttf.h>
|
2021-02-01 17:30:34 +01:00
|
|
|
#include <emscripten.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2021-02-02 05:13:25 +01:00
|
|
|
#include "game.h"
|
2021-02-04 01:19:16 +01:00
|
|
|
#include "device.h"
|
2021-02-02 05:13:25 +01:00
|
|
|
#include "mempool.h"
|
2021-02-07 21:48:06 +01:00
|
|
|
#include "nli.h"
|
|
|
|
#include "strutils.h"
|
2021-02-10 23:16:22 +01:00
|
|
|
#include "movestak.h"
|
2021-02-24 01:00:19 +01:00
|
|
|
#include "knownplyr.h"
|
2021-02-02 05:13:25 +01:00
|
|
|
|
2021-02-02 21:04:31 +01:00
|
|
|
#include "main.h"
|
2021-02-02 05:13:25 +01:00
|
|
|
#include "wasmdraw.h"
|
|
|
|
#include "wasmutil.h"
|
|
|
|
#include "wasmdutil.h"
|
|
|
|
#include "wasmdict.h"
|
2021-02-17 00:32:33 +01:00
|
|
|
#include "wasmasm.h"
|
2021-02-02 05:13:25 +01:00
|
|
|
|
2021-02-01 17:30:34 +01:00
|
|
|
#ifdef __EMSCRIPTEN__
|
|
|
|
#include <emscripten.h>
|
|
|
|
#endif
|
|
|
|
|
2021-02-02 05:13:25 +01:00
|
|
|
#define WASM_BOARD_LEFT 0
|
|
|
|
#define WASM_HOR_SCORE_TOP 0
|
2021-02-02 21:04:31 +01:00
|
|
|
|
|
|
|
#define WINDOW_WIDTH 400
|
|
|
|
#define WINDOW_HEIGHT 600
|
2021-02-07 02:36:53 +01:00
|
|
|
#define BDWIDTH WINDOW_WIDTH
|
|
|
|
#define BDHEIGHT WINDOW_HEIGHT
|
2021-02-01 17:30:34 +01:00
|
|
|
|
2021-02-23 17:35:50 +01:00
|
|
|
#define KEY_LAST_GID "cur_game"
|
2021-02-17 00:32:33 +01:00
|
|
|
#define KEY_PLAYER_NAME "player_name"
|
2021-02-27 00:33:03 +01:00
|
|
|
#define KEY_GAME "game_data"
|
|
|
|
#define KEY_NAME "game_name"
|
|
|
|
#define KEY_NEXT_GAME "next_game"
|
2021-02-19 20:19:16 +01:00
|
|
|
|
2021-02-13 19:37:47 +01:00
|
|
|
#define BUTTON_OK "OK"
|
|
|
|
#define BUTTON_CANCEL "Cancel"
|
|
|
|
|
2021-02-19 04:20:20 +01:00
|
|
|
#define BUTTONS_ID_GAME "game_buttons"
|
|
|
|
#define BUTTONS_ID_DEVICE "device_buttons"
|
|
|
|
|
|
|
|
#define BUTTON_HINTDOWN "Prev Hint"
|
|
|
|
#define BUTTON_HINTUP "Next Hint"
|
|
|
|
#define BUTTON_TRADE "Trade"
|
|
|
|
#define BUTTON_STOPTRADE "Cancel Trade"
|
|
|
|
#define BUTTON_COMMIT "Commit"
|
|
|
|
#define BUTTON_FLIP "Flip"
|
|
|
|
#define BUTTON_REDO "Redo"
|
|
|
|
#define BUTTON_VALS "Vals"
|
2021-02-20 02:03:14 +01:00
|
|
|
#define BUTTON_INVITE "Invite"
|
2021-02-19 04:20:20 +01:00
|
|
|
|
|
|
|
#define BUTTON_GAME_NEW "New Game"
|
|
|
|
#define BUTTON_GAME_OPEN "Open Game"
|
|
|
|
#define BUTTON_GAME_RENAME "Rename Game"
|
|
|
|
#define BUTTON_GAME_DELETE "Delete Game"
|
2021-02-27 06:09:58 +01:00
|
|
|
#define BUTTON_NAME "My Name"
|
2021-02-19 20:19:16 +01:00
|
|
|
#define MAX_BUTTONS 20 /* not sure what's safe here */
|
|
|
|
|
2021-02-19 22:18:53 +01:00
|
|
|
typedef struct _NewGameParams {
|
|
|
|
bool isRobotNotRemote;
|
|
|
|
bool hintsNotAllowed;
|
|
|
|
} NewGameParams;
|
2021-02-19 04:20:20 +01:00
|
|
|
|
2021-02-23 17:35:50 +01:00
|
|
|
static void updateScreen( GameState* gs, bool doSave );
|
2021-02-27 00:45:52 +01:00
|
|
|
static void updateDeviceButtons( Globals* globals );
|
2021-02-23 17:35:50 +01:00
|
|
|
static void clearScreen( Globals* globals );
|
|
|
|
static GameState* newGameState( Globals* globals );
|
|
|
|
static GameState* getSavedGame( Globals* globals, int gameID );
|
2021-02-19 05:30:24 +01:00
|
|
|
static void loadAndDraw( Globals* globals, const NetLaunchInfo* invite,
|
2021-02-23 17:35:50 +01:00
|
|
|
const char* gameID, NewGameParams* params );
|
|
|
|
static GameState* getCurGame( Globals* globals );
|
2021-02-23 19:49:09 +01:00
|
|
|
static void nameGame( GameState* gs, const char* name );
|
2021-02-23 17:35:50 +01:00
|
|
|
static void ensureName( GameState* gs );
|
|
|
|
static void loadName( GameState* gs );
|
|
|
|
static void saveName( GameState* gs );
|
2021-02-24 01:00:19 +01:00
|
|
|
static bool isVisible( GameState* gs );
|
2021-02-28 21:02:20 +01:00
|
|
|
static int countDicts( Globals* globals );
|
2021-02-19 04:20:20 +01:00
|
|
|
|
2021-02-27 20:06:43 +01:00
|
|
|
typedef void (*BinProc)(void* closure, const uint8_t* data, int len );
|
|
|
|
|
|
|
|
EM_JS(void, call_get_dict, (void* closure), {
|
|
|
|
getDict(closure);
|
|
|
|
});
|
|
|
|
|
2021-02-23 17:35:50 +01:00
|
|
|
EM_JS(void, show_name, (const char* name), {
|
|
|
|
let jsname = UTF8ToString(name);
|
|
|
|
document.getElementById('gamename').textContent = jsname;
|
|
|
|
});
|
2021-02-13 19:37:47 +01:00
|
|
|
|
|
|
|
EM_JS(void, call_dialog, (const char* str, const char** but_strs,
|
2021-02-21 19:07:51 +01:00
|
|
|
StringProc proc, void* closure), {
|
2021-02-13 19:37:47 +01:00
|
|
|
var buttons = [];
|
2021-02-18 03:58:46 +01:00
|
|
|
for ( let ii = 0; ; ++ii ) {
|
2021-02-13 19:37:47 +01:00
|
|
|
const mem = HEAP32[(but_strs + (ii * 4)) >> 2];
|
|
|
|
if ( 0 == mem ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
const str = UTF8ToString(mem);
|
|
|
|
buttons.push(str);
|
|
|
|
}
|
|
|
|
nbDialog(UTF8ToString(str), buttons, proc, closure);
|
|
|
|
} );
|
|
|
|
|
2021-02-18 03:58:46 +01:00
|
|
|
EM_JS(void, call_pickBlank, (const char* msg, const char** strs, int nStrs,
|
2021-02-21 19:07:51 +01:00
|
|
|
StringProc proc, void* closure), {
|
2021-02-18 03:58:46 +01:00
|
|
|
var buttons = [];
|
|
|
|
for ( let ii = 0; ii < nStrs; ++ii ) {
|
|
|
|
const mem = HEAP32[(strs + (ii * 4)) >> 2];
|
|
|
|
const str = UTF8ToString(mem);
|
|
|
|
buttons.push(str);
|
|
|
|
}
|
|
|
|
nbBlankPick(UTF8ToString(msg), buttons, proc, closure);
|
2021-02-19 05:30:24 +01:00
|
|
|
} );
|
|
|
|
|
2021-02-27 00:33:03 +01:00
|
|
|
EM_JS(void, call_pickGame, (const char* msg, char** ids, char** names,
|
|
|
|
int nEntries, StringProc proc, void* closure), {
|
|
|
|
let map = {};
|
|
|
|
for (let ii = 0; ii < nEntries; ++ii ) {
|
|
|
|
const idsMem = HEAP32[(ids + (ii * 4)) >> 2];
|
|
|
|
const namesMem = HEAP32[(names + (ii * 4)) >> 2];
|
|
|
|
let id = UTF8ToString(idsMem);
|
|
|
|
map[id] = UTF8ToString(namesMem);
|
|
|
|
console.log('added ' + id + ' -> ' + map[id]);
|
2021-02-19 05:30:24 +01:00
|
|
|
}
|
|
|
|
|
2021-02-19 20:19:16 +01:00
|
|
|
nbGamePick(UTF8ToString(msg), map, proc, closure);
|
2021-02-19 05:30:24 +01:00
|
|
|
} );
|
2021-02-18 03:58:46 +01:00
|
|
|
|
2021-02-16 23:25:22 +01:00
|
|
|
EM_JS(void, call_get_string, (const char* msg, const char* dflt,
|
2021-02-21 19:07:51 +01:00
|
|
|
StringProc proc, void* closure), {
|
2021-02-16 23:25:22 +01:00
|
|
|
let jsMgs = UTF8ToString(msg);
|
|
|
|
let jsDflt = UTF8ToString(dflt);
|
|
|
|
nbGetString( jsMgs, jsDflt, proc, closure );
|
|
|
|
} );
|
|
|
|
|
2021-02-27 20:06:43 +01:00
|
|
|
EM_JS(void, call_setup, (void* closure, const char* devid,
|
|
|
|
const char* gitrev, int now,
|
|
|
|
StringProc conflictProc,
|
|
|
|
StringProc focussedProc,
|
|
|
|
BinProc msgProc), {
|
2021-02-22 06:59:03 +01:00
|
|
|
let jsgr = UTF8ToString(gitrev);
|
2021-02-27 20:06:43 +01:00
|
|
|
jssetup(closure, UTF8ToString(devid), jsgr, now,
|
|
|
|
conflictProc, focussedProc, msgProc);
|
2021-02-22 06:59:03 +01:00
|
|
|
});
|
2021-02-07 21:48:06 +01:00
|
|
|
|
2021-02-10 03:56:13 +01:00
|
|
|
EM_JS(bool, call_mqttSend, (const char* topic, const uint8_t* ptr, int len), {
|
2021-02-07 21:48:06 +01:00
|
|
|
let topStr = UTF8ToString(topic);
|
|
|
|
let buffer = new Uint8Array(Module.HEAPU8.buffer, ptr, len);
|
2021-02-10 03:56:13 +01:00
|
|
|
return mqttSend(topStr, buffer);
|
2021-02-07 21:48:06 +01:00
|
|
|
});
|
2021-02-02 23:52:25 +01:00
|
|
|
|
2021-02-10 22:21:28 +01:00
|
|
|
typedef void (*JSCallback)(void* closure);
|
|
|
|
|
|
|
|
EM_JS(void, jscallback_set, (JSCallback proc, void* closure, int inMS), {
|
|
|
|
let timerproc = function(closure) {
|
2021-02-21 19:07:51 +01:00
|
|
|
ccall('cbckVoid', null, ['number', 'number'], [proc, closure]);
|
2021-02-10 22:21:28 +01:00
|
|
|
};
|
|
|
|
setTimeout( timerproc, inMS, closure );
|
|
|
|
});
|
|
|
|
|
2021-02-11 00:46:52 +01:00
|
|
|
EM_JS(void, setButtonText, (const char* id, const char* text), {
|
|
|
|
let jsid = UTF8ToString(id);
|
|
|
|
let jstext = UTF8ToString(text);
|
|
|
|
document.getElementById(jsid).textContent = jstext;
|
|
|
|
});
|
|
|
|
|
2021-02-19 04:20:20 +01:00
|
|
|
EM_JS(void, setButtons, (const char* id, const char** bstrs,
|
2021-02-21 19:07:51 +01:00
|
|
|
StringProc proc, void* closure), {
|
2021-02-19 04:20:20 +01:00
|
|
|
var buttons = [];
|
|
|
|
for ( let ii = 0; ; ++ii ) {
|
|
|
|
const mem = HEAP32[(bstrs + (ii * 4)) >> 2];
|
|
|
|
if ( 0 == mem ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
const str = UTF8ToString(mem);
|
|
|
|
buttons.push(str);
|
|
|
|
}
|
|
|
|
setDivButtons(UTF8ToString(id), buttons, proc, closure);
|
|
|
|
});
|
|
|
|
|
2021-02-19 22:18:53 +01:00
|
|
|
EM_JS(void, callNewGame, (const char* msg, void* closure), {
|
|
|
|
let jsmsg = UTF8ToString(msg);
|
|
|
|
nbGetNewGame(closure, jsmsg);
|
|
|
|
});
|
|
|
|
|
2021-02-15 23:59:44 +01:00
|
|
|
typedef void (*ConfirmProc)( void* closure, bool confirmed );
|
|
|
|
|
|
|
|
typedef struct _ConfirmState {
|
|
|
|
Globals* globals;
|
|
|
|
ConfirmProc proc;
|
|
|
|
void* closure;
|
|
|
|
} ConfirmState;
|
|
|
|
|
|
|
|
static void
|
|
|
|
onConfirmed( void* closure, const char* button )
|
|
|
|
{
|
|
|
|
bool confirmed = 0 == strcmp( button, BUTTON_OK );
|
|
|
|
ConfirmState* cs = (ConfirmState*)closure;
|
|
|
|
(*cs->proc)( cs->closure, confirmed );
|
|
|
|
XP_FREE( cs->globals->mpool, cs );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
call_confirm( Globals* globals, const char* msg,
|
|
|
|
ConfirmProc proc, void* closure )
|
|
|
|
{
|
|
|
|
const char* buttons[] = { BUTTON_CANCEL, BUTTON_OK, NULL };
|
|
|
|
ConfirmState* cs = XP_MALLOC( globals->mpool, sizeof(*cs) );
|
|
|
|
cs->globals = globals;
|
|
|
|
cs->proc = proc;
|
|
|
|
cs->closure = closure;
|
|
|
|
call_dialog( msg, buttons, onConfirmed, cs );
|
|
|
|
}
|
|
|
|
|
2021-02-13 19:37:47 +01:00
|
|
|
static void
|
|
|
|
call_alert( const char* msg )
|
|
|
|
{
|
|
|
|
const char* buttons[] = { BUTTON_OK, NULL };
|
|
|
|
call_dialog( msg, buttons, NULL, NULL );
|
|
|
|
}
|
|
|
|
|
2021-02-20 02:03:14 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-02-07 21:48:06 +01:00
|
|
|
static XP_S16
|
|
|
|
send_msg( XWEnv xwe, const XP_U8* buf, XP_U16 len,
|
|
|
|
const XP_UCHAR* msgNo, const CommsAddrRec* addr,
|
|
|
|
CommsConnType conType, XP_U32 gameID, void* closure )
|
|
|
|
{
|
|
|
|
XP_S16 nSent = -1;
|
|
|
|
Globals* globals = (Globals*)closure;
|
|
|
|
|
|
|
|
if ( addr_hasType( addr, COMMS_CONN_MQTT ) ) {
|
2021-02-20 02:03:14 +01:00
|
|
|
// MQTTDevID devID = addr->u.mqtt.devID;
|
2021-02-07 21:48:06 +01:00
|
|
|
XWStreamCtxt* stream = mem_stream_make_raw( MPPARM(globals->mpool)
|
|
|
|
globals->vtMgr );
|
|
|
|
dvc_makeMQTTMessage( globals->dutil, NULL, stream,
|
|
|
|
gameID, buf, len );
|
2021-02-20 02:03:14 +01:00
|
|
|
if ( sendStreamToDev( stream, &addr->u.mqtt.devID ) ) {
|
2021-02-10 03:56:13 +01:00
|
|
|
nSent = len;
|
|
|
|
}
|
2021-02-07 21:48:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
LOG_RETURNF( "%d", nSent );
|
|
|
|
return nSent;
|
|
|
|
}
|
|
|
|
|
2021-02-19 20:19:16 +01:00
|
|
|
static void
|
|
|
|
formatGameID( char* buf, size_t len, int gameID )
|
|
|
|
{
|
2021-02-27 00:33:03 +01:00
|
|
|
snprintf( buf, len, "gid=%X", gameID );
|
2021-02-19 20:19:16 +01:00
|
|
|
}
|
|
|
|
|
2021-02-23 17:35:50 +01:00
|
|
|
static void
|
|
|
|
unformatGameID( int* gameID, const char* gameIDStr )
|
|
|
|
{
|
2021-02-27 00:33:03 +01:00
|
|
|
sscanf( gameIDStr, "gid=%X", gameID );
|
2021-02-23 17:35:50 +01:00
|
|
|
XP_LOGFF( "unformatGameID(%s) => %X", gameIDStr, *gameID );
|
|
|
|
}
|
|
|
|
|
2021-02-20 02:03:14 +01:00
|
|
|
static void
|
|
|
|
makeSelfAddr( Globals* globals, CommsAddrRec* addr )
|
|
|
|
{
|
|
|
|
addr_setType( addr, COMMS_CONN_MQTT );
|
|
|
|
dvc_getMQTTDevID( globals->dutil, NULL, &addr->u.mqtt.devID );
|
|
|
|
}
|
|
|
|
|
2021-02-24 01:00:19 +01:00
|
|
|
static void
|
|
|
|
sendInviteTo(GameState* gs, const MQTTDevID* remoteDevID)
|
|
|
|
{
|
|
|
|
Globals* globals = gs->globals;
|
|
|
|
CommsAddrRec myAddr = {0};
|
|
|
|
makeSelfAddr( globals, &myAddr );
|
|
|
|
|
|
|
|
NetLaunchInfo nli = {0}; /* include everything!!! */
|
|
|
|
nli_init( &nli, &gs->gi, &myAddr, 1, 1 );
|
|
|
|
nli_setGameName( &nli, gs->gameName );
|
|
|
|
|
|
|
|
|
|
|
|
XWStreamCtxt* stream = mem_stream_make_raw( MPPARM(globals->mpool)
|
|
|
|
globals->vtMgr );
|
|
|
|
dvc_makeMQTTInvite( globals->dutil, NULL, stream, &nli );
|
|
|
|
|
|
|
|
sendStreamToDev( stream, remoteDevID );
|
|
|
|
}
|
|
|
|
|
2021-02-20 02:03:14 +01:00
|
|
|
static void
|
|
|
|
onGotInviteeID( void* closure, const char* mqttid )
|
|
|
|
{
|
|
|
|
MQTTDevID remoteDevID;
|
|
|
|
if ( strToMQTTCDevID( mqttid, &remoteDevID ) ) {
|
2021-02-23 17:35:50 +01:00
|
|
|
CAST_GS(GameState*, gs, closure);
|
2021-02-24 01:00:19 +01:00
|
|
|
sendInviteTo( gs, &remoteDevID );
|
|
|
|
} else {
|
|
|
|
call_alert( "MQTT id looks badly formed" );
|
|
|
|
}
|
|
|
|
}
|
2021-02-20 02:03:14 +01:00
|
|
|
|
2021-02-24 01:00:19 +01:00
|
|
|
typedef struct _KnownSelState {
|
|
|
|
GameState* gs;
|
|
|
|
const char** names;
|
|
|
|
XP_U16 nNames;
|
|
|
|
} KnownSelState;
|
2021-02-20 02:03:14 +01:00
|
|
|
|
2021-02-24 01:00:19 +01:00
|
|
|
static void
|
|
|
|
onKnownSelected( void* closure, const char* name )
|
|
|
|
{
|
|
|
|
XP_LOGFF( "(name=%s)", name );
|
|
|
|
KnownSelState* kss = (KnownSelState*)closure;
|
|
|
|
GameState* gs = kss->gs;
|
|
|
|
if ( 0 == strcmp( BUTTON_CANCEL, name ) ) {
|
|
|
|
call_get_string( "Invitee's MQTT Device ID?", "",
|
|
|
|
onGotInviteeID, gs );
|
2021-02-20 02:03:14 +01:00
|
|
|
} else {
|
2021-02-24 01:00:19 +01:00
|
|
|
CommsAddrRec addr;
|
|
|
|
if ( kplr_getAddr( gs->globals->dutil, NULL, name, &addr, NULL ) ) {
|
|
|
|
sendInviteTo(gs, &addr.u.mqtt.devID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
XP_FREE( gs->globals->mpool, kss->names );
|
|
|
|
XP_FREE( gs->globals->mpool, kss );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
handleInvite( GameState* gs )
|
|
|
|
{
|
|
|
|
if ( isVisible(gs) ) {
|
|
|
|
Globals* globals = gs->globals;
|
|
|
|
XW_DUtilCtxt* dutil = globals->dutil;
|
|
|
|
if ( kplr_havePlayers( dutil, NULL ) ) {
|
|
|
|
KnownSelState* kss = XP_MALLOC( globals->mpool, sizeof(*kss) );
|
|
|
|
kss->gs = gs;
|
|
|
|
kss->nNames = 0;
|
|
|
|
kplr_getNames( dutil, NULL, NULL, &kss->nNames );
|
|
|
|
kss->names = XP_CALLOC( globals->mpool,
|
|
|
|
(kss->nNames + 2) * sizeof(kss->names[0]) );
|
|
|
|
kplr_getNames( dutil, NULL, kss->names, &kss->nNames );
|
|
|
|
kss->names[kss->nNames] = BUTTON_CANCEL;
|
|
|
|
XP_ASSERT( NULL == kss->names[kss->nNames+1] );
|
|
|
|
const char* msg = "Pick an invitee you've played before, "
|
|
|
|
"or cancel to enter manually.";
|
|
|
|
call_dialog( msg, kss->names, onKnownSelected, kss );
|
|
|
|
} else {
|
|
|
|
call_get_string( "Invitee's MQTT Device ID?", "",
|
|
|
|
onGotInviteeID, gs );
|
|
|
|
}
|
2021-02-20 02:03:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-19 04:20:20 +01:00
|
|
|
static void
|
|
|
|
onGameButton( void* closure, const char* button )
|
|
|
|
{
|
|
|
|
if ( !!button ) {
|
2021-02-23 17:35:50 +01:00
|
|
|
CAST_GS(GameState*, gs, closure);
|
2021-02-19 04:20:20 +01:00
|
|
|
|
|
|
|
XP_Bool draw = XP_FALSE;
|
2021-02-23 17:35:50 +01:00
|
|
|
BoardCtxt* board = gs->game.board;
|
2021-02-19 04:20:20 +01:00
|
|
|
XP_Bool redo;
|
|
|
|
|
|
|
|
if ( 0 == strcmp(button, BUTTON_HINTDOWN ) ) {
|
|
|
|
draw = board_requestHint( board, NULL, XP_TRUE, &redo );
|
|
|
|
} else if ( 0 == strcmp(button, BUTTON_HINTUP) ) {
|
|
|
|
draw = board_requestHint( board, NULL, XP_FALSE, &redo );
|
|
|
|
} else if ( 0 == strcmp(button, BUTTON_TRADE ) ) {
|
|
|
|
draw = board_beginTrade( board, NULL );
|
|
|
|
} else if ( 0 == strcmp(button, BUTTON_STOPTRADE ) ) {
|
|
|
|
draw = board_endTrade( board );
|
|
|
|
} else if ( 0 == strcmp(button, BUTTON_COMMIT) ) {
|
|
|
|
draw = board_commitTurn( board, NULL, XP_FALSE, XP_FALSE, NULL );
|
|
|
|
} else if ( 0 == strcmp(button, BUTTON_FLIP) ) {
|
|
|
|
draw = board_flip( board );
|
|
|
|
} else if ( 0 == strcmp(button, BUTTON_REDO) ) {
|
|
|
|
draw = board_redoReplacedTiles( board, NULL )
|
|
|
|
|| board_replaceTiles( board, NULL );
|
|
|
|
} else if ( 0 == strcmp(button, BUTTON_VALS) ) {
|
2021-02-23 17:35:50 +01:00
|
|
|
Globals* globals = gs->globals;
|
2021-02-19 04:20:20 +01:00
|
|
|
globals->cp.tvType = (globals->cp.tvType + 1) % TVT_N_ENTRIES;
|
|
|
|
draw = board_prefsChanged( board, &globals->cp );
|
2021-02-20 02:03:14 +01:00
|
|
|
} else if ( 0 == strcmp(button, BUTTON_INVITE) ) {
|
2021-02-24 01:00:19 +01:00
|
|
|
handleInvite(gs);
|
2021-02-19 04:20:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( draw ) {
|
2021-02-23 17:35:50 +01:00
|
|
|
updateScreen( gs, true );
|
2021-02-19 04:20:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-02-24 02:05:13 +01:00
|
|
|
updateGameButtons( Globals* globals )
|
2021-02-19 04:20:20 +01:00
|
|
|
{
|
2021-02-24 02:05:13 +01:00
|
|
|
GameState* gs = getCurGame(globals);
|
2021-02-19 04:20:20 +01:00
|
|
|
const char* buttons[MAX_BUTTONS];
|
|
|
|
int cur = 0;
|
|
|
|
|
2021-02-24 02:05:13 +01:00
|
|
|
if ( gs && !!gs->util ) {
|
2021-02-23 17:35:50 +01:00
|
|
|
XP_U16 nPending = server_getPendingRegs( gs->game.server );
|
2021-02-20 16:27:43 +01:00
|
|
|
if ( 0 < nPending ) {
|
|
|
|
buttons[cur++] = BUTTON_INVITE;
|
|
|
|
} else {
|
|
|
|
GameStateInfo gsi;
|
2021-02-23 17:35:50 +01:00
|
|
|
game_getState( &gs->game, NULL, &gsi );
|
2021-02-19 04:20:20 +01:00
|
|
|
|
2021-02-20 16:27:43 +01:00
|
|
|
if ( gsi.canHint ) {
|
|
|
|
buttons[cur++] = BUTTON_HINTDOWN;
|
|
|
|
buttons[cur++] = BUTTON_HINTUP;
|
|
|
|
}
|
2021-02-19 04:20:20 +01:00
|
|
|
|
2021-02-20 16:27:43 +01:00
|
|
|
if ( gsi.inTrade ) {
|
|
|
|
buttons[cur++] = BUTTON_STOPTRADE;
|
|
|
|
} else if ( gsi.canTrade ) {
|
|
|
|
buttons[cur++] = BUTTON_TRADE;
|
|
|
|
}
|
|
|
|
buttons[cur++] = BUTTON_COMMIT;
|
|
|
|
buttons[cur++] = BUTTON_FLIP;
|
2021-02-19 04:20:20 +01:00
|
|
|
|
2021-02-20 16:27:43 +01:00
|
|
|
if ( gsi.canRedo ) {
|
|
|
|
buttons[cur++] = BUTTON_REDO;
|
|
|
|
}
|
2021-02-20 02:03:14 +01:00
|
|
|
|
2021-02-20 16:27:43 +01:00
|
|
|
buttons[cur++] = BUTTON_VALS;
|
2021-02-20 02:03:14 +01:00
|
|
|
}
|
2021-02-19 22:18:53 +01:00
|
|
|
}
|
2021-02-19 04:20:20 +01:00
|
|
|
|
2021-02-24 02:05:13 +01:00
|
|
|
buttons[cur++] = NULL;
|
2021-02-23 17:35:50 +01:00
|
|
|
setButtons( BUTTONS_ID_GAME, buttons, onGameButton, gs );
|
2021-02-19 04:20:20 +01:00
|
|
|
}
|
|
|
|
|
2021-02-28 21:02:20 +01:00
|
|
|
static void
|
|
|
|
showName( GameState* gs )
|
|
|
|
{
|
|
|
|
const char* title = gs->gameName;
|
|
|
|
char buf[64];
|
2021-03-01 06:31:04 +01:00
|
|
|
if ( true || 1 < countDicts( gs->globals ) ) {
|
2021-02-28 21:02:20 +01:00
|
|
|
sprintf( buf, "%s (%s)", gs->gameName, lcToLocale(gs->gi.dictLang) );
|
|
|
|
title = buf;
|
|
|
|
}
|
|
|
|
show_name( title );
|
|
|
|
}
|
|
|
|
|
2021-02-27 00:33:03 +01:00
|
|
|
static void
|
|
|
|
onGameRanamed( void* closure, const char* newName )
|
|
|
|
{
|
|
|
|
if ( !!newName ) {
|
|
|
|
CAST_GS(GameState*, gs, closure);
|
|
|
|
nameGame( gs, newName );
|
2021-02-28 21:02:20 +01:00
|
|
|
showName( gs );
|
2021-02-27 00:33:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct _NameIterState {
|
|
|
|
Globals* globals;
|
|
|
|
int count;
|
|
|
|
char** names;
|
|
|
|
char** ids;
|
|
|
|
} NameIterState;
|
|
|
|
|
2021-02-19 05:30:24 +01:00
|
|
|
static void
|
|
|
|
onGameChosen( void* closure, const char* key )
|
|
|
|
{
|
2021-02-27 00:33:03 +01:00
|
|
|
NameIterState* nis = (NameIterState*)closure;
|
|
|
|
Globals* globals = nis->globals;
|
|
|
|
|
2021-02-23 20:59:49 +01:00
|
|
|
/* To be safe, let's make sure the game exists. We don't want to create
|
|
|
|
* another if somehow it doesn't */
|
|
|
|
int gameID;
|
|
|
|
unformatGameID( &gameID, key );
|
|
|
|
if ( !!getSavedGame(globals, gameID) ) {
|
|
|
|
loadAndDraw( globals, NULL, key, NULL );
|
|
|
|
}
|
2021-02-27 00:33:03 +01:00
|
|
|
|
|
|
|
XP_FREE( globals->mpool, nis->names );
|
|
|
|
XP_FREE( globals->mpool, nis->ids );
|
|
|
|
XP_FREE( globals->mpool, nis );
|
2021-02-27 00:45:52 +01:00
|
|
|
|
|
|
|
updateDeviceButtons( globals );
|
2021-02-27 00:33:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static XP_Bool
|
2021-02-28 06:16:01 +01:00
|
|
|
onOneIndx( void* closure, const XP_UCHAR* indx )
|
2021-02-27 00:33:03 +01:00
|
|
|
{
|
2021-02-28 06:16:01 +01:00
|
|
|
XP_LOGFF( "(indx: %s)", indx );
|
2021-02-27 00:33:03 +01:00
|
|
|
NameIterState* nis = (NameIterState*)closure;
|
|
|
|
Globals* globals = nis->globals;
|
2021-02-28 21:02:20 +01:00
|
|
|
int cur = nis->count++;
|
2021-02-27 00:33:03 +01:00
|
|
|
nis->names = XP_REALLOC( globals->mpool, nis->names,
|
|
|
|
nis->count * sizeof(nis->names[0]) );
|
2021-02-28 06:16:01 +01:00
|
|
|
|
|
|
|
XP_U32 valLen;
|
|
|
|
dutil_loadIndxPtr( globals->dutil, NULL, KEY_NAME, indx, NULL, &valLen );
|
|
|
|
uint8_t val[valLen];
|
|
|
|
dutil_loadIndxPtr( globals->dutil, NULL, KEY_NAME, indx, val, &valLen );
|
2021-02-28 21:02:20 +01:00
|
|
|
nis->names[cur] = XP_MALLOC( globals->mpool, valLen );
|
|
|
|
XP_MEMCPY( nis->names[cur], val, valLen );
|
2021-02-27 00:33:03 +01:00
|
|
|
|
|
|
|
nis->ids = XP_REALLOC( globals->mpool, nis->ids,
|
|
|
|
nis->count * sizeof(nis->ids[0]) );
|
2021-02-28 21:02:20 +01:00
|
|
|
nis->ids[cur] = XP_MALLOC( globals->mpool, 1 + strlen(indx) );
|
|
|
|
strcpy( nis->ids[cur], indx );
|
2021-02-27 00:33:03 +01:00
|
|
|
|
|
|
|
return true; /* keep going */
|
2021-02-19 05:30:24 +01:00
|
|
|
}
|
|
|
|
|
2021-02-19 20:19:16 +01:00
|
|
|
static void
|
2021-02-27 00:33:03 +01:00
|
|
|
pickGame( Globals* globals )
|
2021-02-19 20:19:16 +01:00
|
|
|
{
|
2021-02-27 00:33:03 +01:00
|
|
|
XW_DUtilCtxt* dutil = globals->dutil;
|
|
|
|
NameIterState* nis = XP_CALLOC( globals->mpool, sizeof(*nis) );
|
|
|
|
nis->globals = globals;
|
|
|
|
|
|
|
|
dutil_forEachIndx( dutil, NULL, KEY_NAME, onOneIndx, nis );
|
|
|
|
|
|
|
|
const char* msg = "Choose game to open:";
|
|
|
|
call_pickGame(msg, nis->ids, nis->names, nis->count, onGameChosen, nis);
|
2021-02-19 20:19:16 +01:00
|
|
|
}
|
|
|
|
|
2021-02-19 22:18:53 +01:00
|
|
|
static void
|
2021-02-23 17:35:50 +01:00
|
|
|
cleanupGame( GameState* gs )
|
|
|
|
{
|
|
|
|
if ( !!gs->util ) {
|
|
|
|
game_dispose( &gs->game, NULL );
|
|
|
|
gi_disposePlayerInfo( MPPARM(gs->globals->mpool) &gs->gi );
|
|
|
|
wasm_util_destroy( gs->util );
|
|
|
|
gs->util = NULL;
|
|
|
|
// XP_MEMSET( &gs, 0, sizeof(globals->gs) );
|
2021-02-19 22:18:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-02-23 17:35:50 +01:00
|
|
|
deleteGame( GameState* gs )
|
2021-02-19 22:18:53 +01:00
|
|
|
{
|
2021-02-24 01:13:44 +01:00
|
|
|
Globals* globals = gs->globals;
|
2021-02-24 02:05:13 +01:00
|
|
|
if ( globals->curGame == gs ) {
|
|
|
|
globals->curGame = NULL;
|
|
|
|
}
|
|
|
|
updateGameButtons( globals );
|
|
|
|
|
2021-02-23 17:35:50 +01:00
|
|
|
int gameID = gs->gi.gameID; /* remember it */
|
|
|
|
cleanupGame( gs );
|
2021-02-19 22:18:53 +01:00
|
|
|
|
2021-02-24 01:13:44 +01:00
|
|
|
// Remove from linked list, but don't actually free because could live in
|
|
|
|
// a js-side object still. Needs refcounting or somesuch
|
|
|
|
GameState** prev = &globals->games;
|
|
|
|
for ( GameState* cur = globals->games; !!cur; cur = cur->next ) {
|
|
|
|
if ( gs == cur ) {
|
|
|
|
*prev = cur->next;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
prev = &cur->next;
|
|
|
|
}
|
|
|
|
|
2021-02-27 00:33:03 +01:00
|
|
|
char indx[16];
|
|
|
|
formatGameID( indx, sizeof(indx), gameID );
|
|
|
|
dutil_removeAllIndx( globals->dutil, indx );
|
2021-02-19 22:18:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
onDeleteConfirmed( void* closure, bool confirmed )
|
|
|
|
{
|
|
|
|
if ( confirmed ) {
|
2021-02-23 17:35:50 +01:00
|
|
|
CAST_GS(GameState*, gs, closure);
|
|
|
|
Globals* globals = gs->globals;
|
|
|
|
deleteGame( gs );
|
|
|
|
clearScreen( globals );
|
2021-02-27 00:45:52 +01:00
|
|
|
updateDeviceButtons( globals );
|
2021-02-19 22:18:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-27 06:09:58 +01:00
|
|
|
static void
|
|
|
|
onPlayerNamed( void* closure, const char* name )
|
|
|
|
{
|
|
|
|
CAST_GLOB(Globals*, globals, closure);
|
|
|
|
if ( !!name ) {
|
2021-03-03 06:10:59 +01:00
|
|
|
strncpy( globals->playerName, name, VSIZE(globals->playerName) );
|
|
|
|
dutil_startStore( globals->dutil, NULL, KEY_PLAYER_NAME,
|
|
|
|
name, 1 + strlen(name), NULL, NULL );
|
2021-02-27 06:09:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-19 04:20:20 +01:00
|
|
|
static void
|
|
|
|
onDeviceButton( void* closure, const char* button )
|
|
|
|
{
|
2021-02-23 17:35:50 +01:00
|
|
|
CAST_GLOB(Globals*, globals, closure);
|
2021-02-19 04:20:20 +01:00
|
|
|
XP_LOGFF( "(button=%s)", button );
|
|
|
|
if ( 0 == strcmp(button, BUTTON_GAME_NEW) ) {
|
2021-02-19 22:18:53 +01:00
|
|
|
callNewGame("Configure your new game", globals);
|
2021-02-19 04:20:20 +01:00
|
|
|
} else if ( 0 == strcmp(button, BUTTON_GAME_OPEN) ) {
|
2021-02-27 00:33:03 +01:00
|
|
|
pickGame( globals );
|
2021-02-19 04:20:20 +01:00
|
|
|
} else if ( 0 == strcmp(button, BUTTON_GAME_RENAME ) ) {
|
2021-02-23 17:35:50 +01:00
|
|
|
GameState* curGS = getCurGame( globals );
|
|
|
|
ensureName( curGS );
|
|
|
|
call_get_string( "Rename your game", curGS->gameName,
|
|
|
|
onGameRanamed, curGS );
|
2021-02-19 04:20:20 +01:00
|
|
|
} else if ( 0 == strcmp(button, BUTTON_GAME_DELETE) ) {
|
2021-02-23 17:35:50 +01:00
|
|
|
GameState* curGS = getCurGame( globals );
|
2021-02-19 22:18:53 +01:00
|
|
|
char msg[256];
|
|
|
|
snprintf( msg, sizeof(msg), "Are you sure you want to delete the game \"%s\"?"
|
|
|
|
"\nThis action cannot be undone.",
|
2021-02-23 17:35:50 +01:00
|
|
|
curGS->gameName );
|
|
|
|
call_confirm( globals, msg, onDeleteConfirmed, curGS );
|
2021-02-27 06:09:58 +01:00
|
|
|
} else if ( 0 == strcmp(button, BUTTON_NAME ) ) {
|
2021-03-03 06:10:59 +01:00
|
|
|
call_get_string( "Set your (local) player name", globals->playerName,
|
2021-02-27 06:09:58 +01:00
|
|
|
onPlayerNamed, globals );
|
2021-02-19 04:20:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-27 00:45:52 +01:00
|
|
|
static XP_Bool
|
2021-02-28 06:16:01 +01:00
|
|
|
upCounter( void* closure, const XP_UCHAR* indx )
|
2021-02-27 00:45:52 +01:00
|
|
|
{
|
|
|
|
XP_LOGFF("(indx: %s)", indx);
|
|
|
|
int* intp = (int*)closure;
|
|
|
|
++*intp;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
countGames( Globals* globals )
|
|
|
|
{
|
|
|
|
int nFound = 0;
|
|
|
|
dutil_forEachIndx( globals->dutil, NULL, KEY_NAME, upCounter, &nFound );
|
|
|
|
return nFound;
|
|
|
|
}
|
|
|
|
|
2021-02-28 06:16:01 +01:00
|
|
|
static XP_Bool
|
|
|
|
onOneDictCount( void* closure, const XP_UCHAR* indx )
|
|
|
|
{
|
|
|
|
int* ip = (int*)closure;
|
|
|
|
++*ip;
|
|
|
|
return XP_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
countDicts( Globals* globals )
|
|
|
|
{
|
|
|
|
int count = 0;
|
|
|
|
dutil_forEachIndx( globals->dutil, NULL, KEY_DICTS, onOneDictCount, &count );
|
|
|
|
LOG_RETURNF( "%d", count );
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2021-02-19 04:20:20 +01:00
|
|
|
static void
|
|
|
|
updateDeviceButtons( Globals* globals )
|
|
|
|
{
|
|
|
|
const char* buttons[MAX_BUTTONS];
|
|
|
|
int cur = 0;
|
2021-02-28 06:16:01 +01:00
|
|
|
if ( 0 < countDicts( globals ) ) {
|
|
|
|
buttons[cur++] = BUTTON_GAME_NEW;
|
|
|
|
}
|
2021-02-27 00:45:52 +01:00
|
|
|
if ( 0 < countGames(globals) ) {
|
|
|
|
buttons[cur++] = BUTTON_GAME_OPEN;
|
|
|
|
}
|
|
|
|
if ( !!getCurGame( globals ) ) {
|
|
|
|
buttons[cur++] = BUTTON_GAME_RENAME;
|
|
|
|
buttons[cur++] = BUTTON_GAME_DELETE;
|
|
|
|
}
|
2021-02-27 06:09:58 +01:00
|
|
|
buttons[cur++] = BUTTON_NAME;
|
2021-02-19 04:20:20 +01:00
|
|
|
buttons[cur++] = NULL;
|
|
|
|
|
|
|
|
setButtons( BUTTONS_ID_DEVICE, buttons, onDeviceButton, globals );
|
2021-02-11 00:46:52 +01:00
|
|
|
}
|
|
|
|
|
2021-02-21 07:20:21 +01:00
|
|
|
static void
|
|
|
|
onConflict( void* closure, const char* ignored )
|
|
|
|
{
|
2021-02-25 15:43:26 +01:00
|
|
|
CAST_GLOB(Globals*, globals, closure);
|
2021-02-21 07:20:21 +01:00
|
|
|
call_alert( "Control passed to another tab" );
|
|
|
|
XP_MEMSET( globals, 0, sizeof(*globals) ); /* stop everything :-) */
|
|
|
|
}
|
|
|
|
|
2021-02-25 15:43:26 +01:00
|
|
|
static void
|
|
|
|
onFocussed( void* closure, const char* ignored )
|
|
|
|
{
|
|
|
|
XP_LOGFF("Need to refresh...");
|
|
|
|
/* This hasn't worked.... */
|
|
|
|
/* CAST_GLOB(Globals*, globals, closure); */
|
|
|
|
/* GameState* gs = getCurGame( globals ); */
|
|
|
|
/* if ( !!gs ) { */
|
|
|
|
/* board_invalAll( gs->game.board ); */
|
|
|
|
/* updateScreen( gs, false ); */
|
|
|
|
/* } */
|
|
|
|
}
|
|
|
|
|
2021-02-27 20:06:43 +01:00
|
|
|
static void
|
|
|
|
onMqttMsg(void* closure, const uint8_t* data, int len )
|
|
|
|
{
|
|
|
|
CAST_GLOB(Globals*, globals, closure);
|
|
|
|
dvc_parseMQTTPacket( globals->dutil, NULL, data, len );
|
|
|
|
}
|
|
|
|
|
2021-02-02 05:13:25 +01:00
|
|
|
static void
|
2021-02-04 00:22:08 +01:00
|
|
|
initDeviceGlobals( Globals* globals )
|
2021-02-02 05:13:25 +01:00
|
|
|
{
|
|
|
|
globals->cp.showBoardArrow = XP_TRUE;
|
2021-02-02 22:49:27 +01:00
|
|
|
globals->cp.allowPeek = XP_TRUE;
|
2021-02-20 02:49:20 +01:00
|
|
|
globals->cp.showRobotScores = XP_TRUE;
|
2021-02-03 04:20:26 +01:00
|
|
|
globals->cp.sortNewTiles = XP_TRUE;
|
2021-02-12 03:31:31 +01:00
|
|
|
globals->cp.showColors = XP_TRUE;
|
2021-02-02 05:13:25 +01:00
|
|
|
|
2021-02-07 21:48:06 +01:00
|
|
|
globals->procs.send = send_msg;
|
|
|
|
globals->procs.closure = globals;
|
|
|
|
|
2021-02-11 01:46:15 +01:00
|
|
|
#ifdef MEMDEBUG
|
2021-02-04 00:22:08 +01:00
|
|
|
globals->mpool = mpool_make( "wasm" );
|
2021-02-11 01:46:15 +01:00
|
|
|
#endif
|
|
|
|
globals->vtMgr = make_vtablemgr( MPPARM_NOCOMMA(globals->mpool) );
|
|
|
|
globals->dutil = wasm_dutil_make( MPPARM(globals->mpool) globals->vtMgr, globals );
|
2021-02-08 01:31:32 +01:00
|
|
|
globals->dictMgr = dmgr_make( MPPARM_NOCOMMA(globals->mpool) );
|
2021-02-27 06:09:58 +01:00
|
|
|
|
2021-02-04 00:22:08 +01:00
|
|
|
globals->draw = wasm_draw_make( MPPARM(globals->mpool)
|
|
|
|
WINDOW_WIDTH, WINDOW_HEIGHT );
|
2021-02-04 01:19:16 +01:00
|
|
|
|
|
|
|
MQTTDevID devID;
|
|
|
|
dvc_getMQTTDevID( globals->dutil, NULL, &devID );
|
2021-02-07 21:48:06 +01:00
|
|
|
XP_UCHAR buf[32];
|
|
|
|
XP_SNPRINTF( buf, VSIZE(buf), MQTTDevID_FMT, devID );
|
|
|
|
XP_LOGFF( "got mqtt devID: %s", buf );
|
2021-02-22 06:59:03 +01:00
|
|
|
int now = dutil_getCurSeconds( globals->dutil, NULL );
|
2021-02-27 20:06:43 +01:00
|
|
|
call_setup( globals, buf, GITREV, now, onConflict, onFocussed, onMqttMsg );
|
|
|
|
|
2021-02-28 06:16:01 +01:00
|
|
|
if ( 0 == countDicts( globals ) ) {
|
|
|
|
call_get_dict( globals );
|
|
|
|
}
|
2021-02-07 21:48:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-02-23 17:35:50 +01:00
|
|
|
storeCurOpen( GameState* gs )
|
2021-02-07 21:48:06 +01:00
|
|
|
{
|
2021-03-03 06:10:59 +01:00
|
|
|
dutil_startStore( gs->globals->dutil, NULL, KEY_LAST_GID,
|
|
|
|
&gs->gi.gameID, sizeof(gs->gi.gameID),
|
|
|
|
NULL, NULL );
|
2021-02-23 17:35:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
startGame( GameState* gs, const char* name )
|
|
|
|
{
|
|
|
|
gs->globals->curGame = gs;
|
|
|
|
ensureName( gs );
|
|
|
|
XP_LOGFF( "changed curGame to %s", gs->gameName );
|
2021-02-28 21:02:20 +01:00
|
|
|
showName( gs );
|
2021-02-23 17:35:50 +01:00
|
|
|
storeCurOpen( gs );
|
|
|
|
|
2021-02-07 21:48:06 +01:00
|
|
|
BoardDims dims;
|
2021-02-23 17:35:50 +01:00
|
|
|
board_figureLayout( gs->game.board, NULL, &gs->gi,
|
2021-02-07 21:48:06 +01:00
|
|
|
WASM_BOARD_LEFT, WASM_HOR_SCORE_TOP, BDWIDTH, BDHEIGHT,
|
|
|
|
110, 150, 200, BDWIDTH-25, BDWIDTH/15, BDHEIGHT/15,
|
|
|
|
XP_FALSE, &dims );
|
2021-02-23 17:35:50 +01:00
|
|
|
board_applyLayout( gs->game.board, NULL, &dims );
|
2021-02-07 21:48:06 +01:00
|
|
|
|
2021-02-23 17:35:50 +01:00
|
|
|
board_invalAll( gs->game.board ); /* redraw screen on loading new game */
|
|
|
|
|
|
|
|
if ( SERVER_ISCLIENT == gs->gi.serverRole ) {
|
2021-02-17 00:32:33 +01:00
|
|
|
if ( !!name ) {
|
2021-02-23 17:35:50 +01:00
|
|
|
replaceStringIfDifferent( gs->globals->mpool,
|
|
|
|
&gs->gi.players[0].name,
|
2021-02-17 00:32:33 +01:00
|
|
|
name );
|
|
|
|
}
|
2021-02-23 17:35:50 +01:00
|
|
|
server_initClientConnection( gs->game.server, NULL );
|
2021-02-07 21:48:06 +01:00
|
|
|
}
|
|
|
|
|
2021-02-23 17:35:50 +01:00
|
|
|
(void)server_do( gs->game.server, NULL ); /* assign tiles, etc. */
|
|
|
|
if ( !!gs->game.comms ) {
|
|
|
|
comms_resendAll( gs->game.comms, NULL, COMMS_CONN_MQTT, XP_TRUE );
|
2021-02-10 02:53:30 +01:00
|
|
|
}
|
2021-02-07 21:48:06 +01:00
|
|
|
|
2021-02-23 17:35:50 +01:00
|
|
|
updateScreen( gs, true );
|
2021-02-07 21:48:06 +01:00
|
|
|
LOG_RETURN_VOID();
|
2021-02-04 00:22:08 +01:00
|
|
|
}
|
|
|
|
|
2021-02-23 17:35:50 +01:00
|
|
|
static GameState*
|
|
|
|
newFromInvite( Globals* globals, const NetLaunchInfo* invite )
|
2021-02-15 23:59:44 +01:00
|
|
|
{
|
2021-02-23 17:35:50 +01:00
|
|
|
GameState* gs = newGameState(globals);
|
|
|
|
gs->util = wasm_util_make( MPPARM(globals->mpool) &gs->gi,
|
|
|
|
globals->dutil, gs );
|
2021-02-15 23:59:44 +01:00
|
|
|
|
2021-02-20 18:04:57 +01:00
|
|
|
game_makeFromInvite( MPPARM(globals->mpool) NULL, invite,
|
2021-03-03 06:10:59 +01:00
|
|
|
&gs->game, &gs->gi, globals->playerName,
|
2021-02-23 17:35:50 +01:00
|
|
|
gs->util, globals->draw,
|
2021-02-20 18:04:57 +01:00
|
|
|
&globals->cp, &globals->procs );
|
2021-02-23 18:04:56 +01:00
|
|
|
if ( invite->gameName[0] ) {
|
2021-02-23 19:49:09 +01:00
|
|
|
nameGame( gs, invite->gameName );
|
2021-02-23 18:04:56 +01:00
|
|
|
}
|
2021-02-23 17:35:50 +01:00
|
|
|
ensureName( gs );
|
|
|
|
return gs;
|
|
|
|
}
|
2021-02-15 23:59:44 +01:00
|
|
|
|
2021-02-23 17:35:50 +01:00
|
|
|
/* If you launch a URL that encodes an invitation, you'll get here. If it's
|
|
|
|
* the first time (the game hasn't been created yet) you'll get a new game
|
|
|
|
* that connects to the host. If you've already created the game, you'll be
|
|
|
|
* taken to it in whatever state it's in. If you've deleted the game, bad
|
|
|
|
* situation: you'll get a new game that will be unable to connect to any
|
|
|
|
* host.
|
|
|
|
*/
|
|
|
|
static GameState*
|
|
|
|
gameFromInvite( Globals* globals, const NetLaunchInfo* invite )
|
|
|
|
{
|
|
|
|
bool needsLoad = true;
|
|
|
|
GameState* gs = getSavedGame( globals, invite->gameID );
|
|
|
|
if ( !gs ) {
|
|
|
|
if ( invite->lang == 1 ) {
|
|
|
|
gs = newFromInvite( globals, invite );
|
|
|
|
} else {
|
|
|
|
call_alert( "Invitations are only supported for play in English right now." );
|
|
|
|
}
|
2021-02-20 18:04:57 +01:00
|
|
|
}
|
2021-02-23 17:35:50 +01:00
|
|
|
return gs;
|
2021-02-20 18:04:57 +01:00
|
|
|
}
|
2021-02-15 23:59:44 +01:00
|
|
|
|
2021-02-23 17:35:50 +01:00
|
|
|
static GameState*
|
|
|
|
newGameState( Globals* globals )
|
2021-02-20 18:04:57 +01:00
|
|
|
{
|
2021-02-23 17:35:50 +01:00
|
|
|
GameState* gs = XP_CALLOC( globals->mpool, sizeof(*gs) );
|
|
|
|
gs->globals = globals;
|
|
|
|
#ifdef DEBUG
|
|
|
|
gs->_GUARD = GUARD_GS;
|
|
|
|
#endif
|
|
|
|
gs->next = globals->games;
|
|
|
|
globals->games = gs;
|
2021-02-20 18:04:57 +01:00
|
|
|
|
2021-02-23 17:35:50 +01:00
|
|
|
return gs;
|
|
|
|
}
|
2021-02-15 23:59:44 +01:00
|
|
|
|
2021-02-23 17:35:50 +01:00
|
|
|
static GameState*
|
|
|
|
getCurGame( Globals* globals )
|
|
|
|
{
|
|
|
|
return globals->curGame;
|
2021-02-15 23:59:44 +01:00
|
|
|
}
|
|
|
|
|
2021-02-23 17:35:50 +01:00
|
|
|
static void
|
|
|
|
removeGameState( GameState* gs )
|
2021-02-10 05:59:33 +01:00
|
|
|
{
|
2021-02-23 17:35:50 +01:00
|
|
|
XP_ASSERT(0);
|
|
|
|
}
|
2021-02-17 00:32:33 +01:00
|
|
|
|
2021-02-23 17:35:50 +01:00
|
|
|
static GameState*
|
|
|
|
getSavedGame( Globals* globals, int gameID )
|
|
|
|
{
|
|
|
|
GameState* gs;
|
|
|
|
|
|
|
|
for ( gs = globals->games; !!gs; gs = gs->next ) {
|
|
|
|
if ( gameID == gs->gi.gameID ) {
|
|
|
|
break;
|
2021-02-10 21:18:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-23 17:35:50 +01:00
|
|
|
if ( !gs ) {
|
|
|
|
gs = newGameState( globals );
|
2021-02-10 21:18:15 +01:00
|
|
|
|
2021-02-23 17:35:50 +01:00
|
|
|
XP_LOGFF( "gameID: %X", gameID );
|
|
|
|
bool loaded = false;
|
|
|
|
XWStreamCtxt* stream = mem_stream_make_raw( MPPARM(globals->mpool)
|
|
|
|
globals->vtMgr );
|
2021-02-27 00:33:03 +01:00
|
|
|
char indx[16];
|
|
|
|
formatGameID( indx, sizeof(indx), gameID );
|
|
|
|
dutil_loadIndxStream( globals->dutil, NULL, KEY_GAME, NULL, indx, stream );
|
2021-02-23 17:35:50 +01:00
|
|
|
if ( 0 < stream_getSize( stream ) ) {
|
|
|
|
XP_ASSERT( !gs->util );
|
|
|
|
gs->util = wasm_util_make( MPPARM(globals->mpool) &gs->gi,
|
|
|
|
globals->dutil, gs );
|
|
|
|
|
|
|
|
XP_LOGFF( "there's a saved game!!" );
|
|
|
|
loaded = game_makeFromStream( MPPARM(globals->mpool) NULL, stream,
|
|
|
|
&gs->game, &gs->gi,
|
|
|
|
gs->util, globals->draw,
|
|
|
|
&globals->cp, &globals->procs );
|
|
|
|
|
|
|
|
if ( loaded ) {
|
|
|
|
loadName( gs );
|
|
|
|
ensureName( gs );
|
|
|
|
updateScreen( gs, false );
|
|
|
|
} else {
|
|
|
|
removeGameState( gs );
|
|
|
|
}
|
|
|
|
} else {
|
2021-02-27 00:33:03 +01:00
|
|
|
XP_LOGFF( "ERROR: no saved data for key %s", indx );
|
2021-02-23 17:35:50 +01:00
|
|
|
XP_ASSERT( globals->games == gs );
|
|
|
|
globals->games = gs->next;
|
|
|
|
XP_FREE( globals->mpool, gs );
|
|
|
|
gs = NULL;
|
2021-02-10 21:18:15 +01:00
|
|
|
}
|
2021-02-23 17:35:50 +01:00
|
|
|
stream_destroy( stream, NULL );
|
2021-02-10 21:18:15 +01:00
|
|
|
}
|
2021-02-23 17:35:50 +01:00
|
|
|
XP_LOGFF( "(%X) => %p", gameID, gs );
|
|
|
|
return gs;
|
2021-02-10 05:59:33 +01:00
|
|
|
}
|
|
|
|
|
2021-02-19 05:30:24 +01:00
|
|
|
static void
|
2021-02-23 17:35:50 +01:00
|
|
|
saveName( GameState* gs )
|
2021-02-19 20:19:16 +01:00
|
|
|
{
|
2021-02-27 00:33:03 +01:00
|
|
|
char indx[32];
|
|
|
|
formatGameID( indx, sizeof(indx), gs->gi.gameID );
|
|
|
|
dutil_storeIndxPtr( gs->globals->dutil, NULL, KEY_NAME, indx,
|
|
|
|
(XP_U8*)gs->gameName, 1 + strlen(gs->gameName) );
|
2021-02-19 20:19:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-02-23 17:35:50 +01:00
|
|
|
loadName( GameState* gs )
|
2021-02-19 20:19:16 +01:00
|
|
|
{
|
2021-02-27 00:33:03 +01:00
|
|
|
char indx[32];
|
|
|
|
formatGameID( indx, sizeof(indx), gs->gi.gameID );
|
2021-02-23 19:49:09 +01:00
|
|
|
|
2021-02-27 00:33:03 +01:00
|
|
|
XP_U32 len = sizeof(gs->gameName);
|
|
|
|
dutil_loadIndxPtr( gs->globals->dutil, NULL, KEY_NAME, indx,
|
|
|
|
(XP_U8*)gs->gameName, &len );
|
2021-02-19 20:19:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-02-23 17:35:50 +01:00
|
|
|
ensureName( GameState* gs )
|
2021-02-19 20:19:16 +01:00
|
|
|
{
|
2021-02-23 17:35:50 +01:00
|
|
|
if ( '\0' == gs->gameName[0] ) {
|
2021-02-23 19:49:09 +01:00
|
|
|
nameGame( gs, NULL );
|
2021-02-19 20:19:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-27 00:33:03 +01:00
|
|
|
static int
|
|
|
|
getNextGameNo( Globals* globals )
|
|
|
|
{
|
|
|
|
int val = 0;
|
|
|
|
XP_U32 len = sizeof(val);
|
|
|
|
dutil_loadPtr( globals->dutil, NULL, KEY_NEXT_GAME, NULL, (XP_U8*)&val, &len );
|
|
|
|
++val;
|
|
|
|
dutil_storePtr( globals->dutil, NULL, KEY_NEXT_GAME, (XP_U8*)&val, sizeof(val) );
|
|
|
|
XP_LOGFF( "getNextGameNo() => %d", val );
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2021-02-19 20:19:16 +01:00
|
|
|
static void
|
2021-02-23 19:49:09 +01:00
|
|
|
nameGame( GameState* gs, const char* name )
|
2021-02-19 05:30:24 +01:00
|
|
|
{
|
2021-02-23 19:49:09 +01:00
|
|
|
if ( !!name ) {
|
|
|
|
snprintf( gs->gameName, sizeof(gs->gameName), "%s", name );
|
|
|
|
} else {
|
|
|
|
snprintf( gs->gameName, sizeof(gs->gameName),
|
2021-02-27 00:33:03 +01:00
|
|
|
"Game %d", getNextGameNo(gs->globals) );
|
2021-02-23 19:49:09 +01:00
|
|
|
}
|
|
|
|
saveName( gs );
|
2021-02-23 17:35:50 +01:00
|
|
|
XP_LOGFF( "named game: %s", gs->gameName );
|
2021-02-19 05:30:24 +01:00
|
|
|
}
|
|
|
|
|
2021-02-28 06:16:01 +01:00
|
|
|
typedef struct _FindOneState {
|
|
|
|
Globals* globals;
|
|
|
|
DictionaryCtxt* dict;
|
|
|
|
} FindOneState;
|
|
|
|
|
|
|
|
static XP_Bool
|
|
|
|
onOneDict( void* closure, const XP_UCHAR* indx )
|
|
|
|
{
|
|
|
|
XP_LOGFF( "indx: %s", indx );
|
|
|
|
FindOneState* fos = (FindOneState*)closure;
|
|
|
|
XW_DUtilCtxt* dutil = fos->globals->dutil;
|
|
|
|
|
2021-02-28 21:02:20 +01:00
|
|
|
XP_U32 len;
|
2021-02-28 06:16:01 +01:00
|
|
|
dutil_loadIndxPtr( dutil, NULL, KEY_DICTS, indx, NULL, &len );
|
|
|
|
uint8_t* ptr = XP_MALLOC( fos->globals->mpool, len );
|
|
|
|
dutil_loadIndxPtr( dutil, NULL, KEY_DICTS, indx, ptr, &len );
|
|
|
|
XP_ASSERT( !fos->dict );
|
|
|
|
fos->dict = wasm_dictionary_make( fos->globals, NULL, indx, ptr, len );
|
|
|
|
return XP_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static DictionaryCtxt*
|
|
|
|
loadAnyDict( Globals* globals )
|
|
|
|
{
|
|
|
|
FindOneState fos = {.globals = globals,
|
|
|
|
};
|
|
|
|
dutil_forEachIndx( globals->dutil, NULL, KEY_DICTS, onOneDict, &fos );
|
|
|
|
LOG_RETURNF( "%p", fos.dict );
|
|
|
|
return fos.dict;
|
|
|
|
}
|
|
|
|
|
2021-02-04 00:22:08 +01:00
|
|
|
static void
|
2021-02-10 21:18:15 +01:00
|
|
|
loadAndDraw( Globals* globals, const NetLaunchInfo* invite,
|
2021-02-23 17:35:50 +01:00
|
|
|
const char* gameIDStr, NewGameParams* params )
|
2021-02-04 00:22:08 +01:00
|
|
|
{
|
2021-02-23 17:35:50 +01:00
|
|
|
XP_LOGFF( "(gameIDStr: %s)", gameIDStr );
|
|
|
|
GameState* gs = NULL;
|
2021-02-04 00:22:08 +01:00
|
|
|
|
2021-02-10 21:18:15 +01:00
|
|
|
bool haveGame;
|
2021-02-23 17:35:50 +01:00
|
|
|
if ( !params ) {
|
2021-02-10 21:18:15 +01:00
|
|
|
/* First, load any saved game. We need it e.g. to confirm that an incoming
|
|
|
|
invite is a dup and should be dropped. */
|
2021-02-27 00:33:03 +01:00
|
|
|
if ( !!gameIDStr && gameIDStr[0] ) {
|
2021-02-23 17:35:50 +01:00
|
|
|
int gameID;
|
|
|
|
unformatGameID( &gameID, gameIDStr );
|
|
|
|
gs = getSavedGame( globals, gameID );
|
2021-02-19 05:30:24 +01:00
|
|
|
}
|
2021-02-23 17:35:50 +01:00
|
|
|
if ( !!invite ) { /* overwrite gs is ok: we'll likely want both games */
|
|
|
|
gs = gameFromInvite( globals, invite );
|
2021-02-10 21:18:15 +01:00
|
|
|
}
|
|
|
|
}
|
2021-02-02 05:13:25 +01:00
|
|
|
|
2021-02-23 17:35:50 +01:00
|
|
|
if ( !gs ) {
|
2021-02-28 06:16:01 +01:00
|
|
|
DictionaryCtxt* dict = loadAnyDict( globals );
|
|
|
|
if ( !!dict ) {
|
|
|
|
gs = newGameState( globals );
|
|
|
|
gs->gi.serverRole = !!params && !params->isRobotNotRemote
|
|
|
|
? SERVER_ISSERVER : SERVER_STANDALONE;
|
|
|
|
|
|
|
|
gs->gi.phoniesAction = PHONIES_WARN;
|
|
|
|
gs->gi.hintsNotAllowed = !!params && params->hintsNotAllowed || false;
|
|
|
|
gs->gi.gameID = 0;
|
|
|
|
gs->gi.dictLang = dict_getLangCode(dict);
|
|
|
|
replaceStringIfDifferent( globals->mpool, &gs->gi.dictName,
|
|
|
|
dict_getShortName(dict) );
|
|
|
|
gs->gi.nPlayers = 2;
|
|
|
|
gs->gi.boardSize = 15;
|
2021-03-03 06:10:59 +01:00
|
|
|
gs->gi.players[0].name = copyString( globals->mpool, globals->playerName );
|
2021-02-28 06:16:01 +01:00
|
|
|
gs->gi.players[0].isLocal = XP_TRUE;
|
|
|
|
gs->gi.players[0].robotIQ = 0;
|
|
|
|
|
|
|
|
gs->gi.players[1].name = copyString( globals->mpool, "Robot" );
|
|
|
|
gs->gi.players[1].isLocal = !!params ? params->isRobotNotRemote : true;
|
|
|
|
XP_LOGFF( "set isLocal[1]: %d", gs->gi.players[1].isLocal );
|
|
|
|
gs->gi.players[1].robotIQ = 99; /* doesn't matter if remote */
|
2021-02-23 19:49:09 +01:00
|
|
|
|
2021-02-28 06:16:01 +01:00
|
|
|
gs->util = wasm_util_make( MPPARM(globals->mpool) &gs->gi,
|
|
|
|
globals->dutil, gs );
|
|
|
|
|
|
|
|
XP_LOGFF( "calling game_makeNewGame()" );
|
|
|
|
game_makeNewGame( MPPARM(globals->mpool) NULL,
|
|
|
|
&gs->game, &gs->gi,
|
|
|
|
gs->util, globals->draw,
|
|
|
|
&globals->cp, &globals->procs );
|
|
|
|
|
|
|
|
ensureName( gs );
|
|
|
|
if ( !!gs->game.comms ) {
|
|
|
|
CommsAddrRec addr = {0};
|
|
|
|
makeSelfAddr( globals, &addr );
|
|
|
|
comms_augmentHostAddr( gs->game.comms, NULL, &addr );
|
|
|
|
}
|
|
|
|
dict_unref( dict, NULL );
|
2021-02-20 02:03:14 +01:00
|
|
|
}
|
2021-02-06 18:23:39 +01:00
|
|
|
}
|
2021-02-28 06:16:01 +01:00
|
|
|
if ( !!gs ) {
|
|
|
|
startGame( gs, NULL );
|
|
|
|
}
|
2021-02-23 17:35:50 +01:00
|
|
|
LOG_RETURN_VOID();
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
isVisible( GameState* gs )
|
|
|
|
{
|
|
|
|
return getCurGame(gs->globals) == gs;
|
2021-02-07 21:48:06 +01:00
|
|
|
}
|
2021-02-02 05:13:25 +01:00
|
|
|
|
2021-02-07 21:48:06 +01:00
|
|
|
void
|
|
|
|
main_gameFromInvite( Globals* globals, const NetLaunchInfo* invite )
|
|
|
|
{
|
2021-02-23 17:35:50 +01:00
|
|
|
GameState* gs = gameFromInvite( globals, invite );
|
|
|
|
if ( gs ) {
|
|
|
|
startGame( gs, NULL );
|
2021-02-07 21:48:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-20 18:04:57 +01:00
|
|
|
typedef struct _OpenForMessageState {
|
|
|
|
Globals* globals;
|
|
|
|
int gameID;
|
|
|
|
CommsAddrRec from;
|
|
|
|
XWStreamCtxt* stream;
|
|
|
|
} OpenForMessageState;
|
|
|
|
|
2021-02-08 01:31:32 +01:00
|
|
|
void
|
|
|
|
main_onGameMessage( Globals* globals, XP_U32 gameID,
|
|
|
|
const CommsAddrRec* from, XWStreamCtxt* stream )
|
|
|
|
{
|
2021-02-23 17:35:50 +01:00
|
|
|
GameState* gs = getSavedGame( globals, gameID );
|
|
|
|
if ( !!gs ) {
|
|
|
|
if ( game_receiveMessage( &gs->game, NULL, stream, from ) ) {
|
|
|
|
updateScreen( gs, true );
|
2021-02-15 23:59:44 +01:00
|
|
|
}
|
2021-02-23 17:35:50 +01:00
|
|
|
} else {
|
|
|
|
char msg[128];
|
2021-02-24 15:51:03 +01:00
|
|
|
snprintf( msg, sizeof(msg), "Dropping move for deleted game (id: %X/%d)",
|
|
|
|
gameID, gameID );
|
2021-02-23 17:35:50 +01:00
|
|
|
call_alert( msg );
|
2021-02-24 15:51:03 +01:00
|
|
|
|
|
|
|
XWStreamCtxt* stream = mem_stream_make_raw( MPPARM(globals->mpool)
|
|
|
|
globals->vtMgr );
|
|
|
|
dvc_makeMQTTNoSuchGame( globals->dutil, NULL, stream, gameID );
|
|
|
|
sendStreamToDev( stream, &from->u.mqtt.devID );
|
2021-02-08 01:31:32 +01:00
|
|
|
}
|
2021-02-20 16:27:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
main_onGameGone( Globals* globals, XP_U32 gameID )
|
|
|
|
{
|
2021-02-23 17:35:50 +01:00
|
|
|
GameState* gs = getSavedGame( globals, gameID );
|
|
|
|
if ( !!gs ) {
|
|
|
|
const char* msg = "This game has been deleted on the remote device. "
|
|
|
|
"Delete here too?";
|
|
|
|
call_confirm( globals, msg, onDeleteConfirmed, gs );
|
|
|
|
}
|
2021-02-08 01:31:32 +01:00
|
|
|
}
|
|
|
|
|
2021-02-07 21:48:06 +01:00
|
|
|
void
|
|
|
|
main_sendOnClose( XWStreamCtxt* stream, XWEnv env, void* closure )
|
|
|
|
{
|
2021-02-23 17:35:50 +01:00
|
|
|
CAST_GS(GameState*, gs, closure );
|
2021-02-07 21:48:06 +01:00
|
|
|
XP_LOGFF( "called with msg of len %d", stream_getSize(stream) );
|
2021-02-23 17:35:50 +01:00
|
|
|
(void)comms_send( gs->game.comms, NULL, stream );
|
2021-02-02 05:13:25 +01:00
|
|
|
}
|
2021-02-01 17:30:34 +01:00
|
|
|
|
2021-02-10 23:16:22 +01:00
|
|
|
void
|
2021-02-23 17:35:50 +01:00
|
|
|
main_playerScoreHeld( GameState* gs, XP_U16 player )
|
2021-02-10 23:16:22 +01:00
|
|
|
{
|
|
|
|
LastMoveInfo lmi;
|
|
|
|
XP_UCHAR buf[128];
|
2021-02-23 17:35:50 +01:00
|
|
|
if ( model_getPlayersLastScore( gs->game.model, NULL, player, &lmi ) ) {
|
2021-02-10 23:16:22 +01:00
|
|
|
switch ( lmi.moveType ) {
|
|
|
|
case ASSIGN_TYPE:
|
|
|
|
XP_SNPRINTF( buf, sizeof(buf), "Tiles assigned to %s", lmi.names[0] );
|
|
|
|
break;
|
|
|
|
case MOVE_TYPE:
|
|
|
|
XP_SNPRINTF( buf, sizeof(buf), "%s formed %s for %d points", lmi.names[0],
|
|
|
|
lmi.word, lmi.score );
|
|
|
|
break;
|
|
|
|
case TRADE_TYPE:
|
|
|
|
XP_SNPRINTF( buf, sizeof(buf), "%s traded %d tiles", lmi.names[0],
|
|
|
|
lmi.nTiles );
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
buf[0] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( buf[0] ) {
|
|
|
|
call_alert( buf );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-17 02:22:50 +01:00
|
|
|
void
|
2021-02-23 17:35:50 +01:00
|
|
|
main_showRemaining( GameState* gs )
|
2021-02-17 02:22:50 +01:00
|
|
|
{
|
2021-02-23 17:35:50 +01:00
|
|
|
XWStreamCtxt* stream = mem_stream_make_raw( MPPARM(gs->globals->mpool)
|
|
|
|
gs->globals->vtMgr );
|
|
|
|
board_formatRemainingTiles( gs->game.board, NULL, stream );
|
2021-02-17 02:22:50 +01:00
|
|
|
stream_putU8( stream, 0 );
|
|
|
|
call_alert( (const XP_UCHAR*)stream_getPtr( stream ) );
|
|
|
|
stream_destroy( stream, NULL );
|
|
|
|
}
|
2021-02-13 21:16:09 +01:00
|
|
|
|
2021-02-23 17:35:50 +01:00
|
|
|
static void
|
|
|
|
openConfirmed(void* closure, bool confirmed)
|
|
|
|
{
|
|
|
|
if ( confirmed ) {
|
|
|
|
CAST_GS(GameState*, gs, closure);
|
|
|
|
char key[16];
|
|
|
|
formatGameID( key, sizeof(key), gs->gi.gameID );
|
|
|
|
loadAndDraw( gs->globals, NULL, key, NULL );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
main_turnChanged( GameState* gs, int newTurn )
|
|
|
|
{
|
|
|
|
if ( 0 <= newTurn && !isVisible(gs) && gs->gi.players[newTurn].isLocal ) {
|
|
|
|
char msg[128];
|
|
|
|
snprintf( msg, sizeof(msg),
|
|
|
|
"It's your turn in background game \"%s\". Would you like to open it now?",
|
|
|
|
gs->gameName );
|
|
|
|
call_confirm( gs->globals, msg, openConfirmed, gs );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-18 03:58:46 +01:00
|
|
|
typedef struct _BlankPickState {
|
2021-02-23 17:35:50 +01:00
|
|
|
GameState* gs;
|
2021-02-18 03:58:46 +01:00
|
|
|
int col, row;
|
|
|
|
int nTiles;
|
|
|
|
int playerNum;
|
|
|
|
} BlankPickState;
|
|
|
|
|
|
|
|
static void
|
2021-02-23 21:12:32 +01:00
|
|
|
onBlankPicked( void* closure, const char* str )
|
2021-02-18 03:58:46 +01:00
|
|
|
{
|
2021-02-23 21:12:32 +01:00
|
|
|
XP_LOGFF( "index: %s", str );
|
2021-02-18 03:58:46 +01:00
|
|
|
BlankPickState* bps = (BlankPickState*)closure;
|
2021-02-23 17:35:50 +01:00
|
|
|
GameState* gs = bps->gs;
|
|
|
|
Globals* globals = gs->globals;
|
2021-02-18 03:58:46 +01:00
|
|
|
|
2021-02-23 21:12:32 +01:00
|
|
|
int indx = atoi(str);
|
|
|
|
if ( 0 <= indx && indx < bps->nTiles
|
|
|
|
&& board_setBlankValue( gs->game.board, bps->playerNum,
|
|
|
|
bps->col, bps->row, indx ) ) {
|
2021-02-23 17:35:50 +01:00
|
|
|
updateScreen( gs, true );
|
2021-02-18 03:58:46 +01:00
|
|
|
}
|
|
|
|
|
2021-02-23 17:35:50 +01:00
|
|
|
XP_FREE( globals->mpool, bps );
|
2021-02-18 03:58:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-02-23 17:35:50 +01:00
|
|
|
main_pickBlank( GameState* gs, int playerNum, int col, int row,
|
2021-02-18 03:58:46 +01:00
|
|
|
const char** tileFaces, int nTiles )
|
|
|
|
{
|
2021-02-23 17:35:50 +01:00
|
|
|
BlankPickState* bps = XP_MALLOC( gs->globals->mpool, sizeof(*bps) );
|
|
|
|
bps->gs = gs;
|
2021-02-18 03:58:46 +01:00
|
|
|
bps->row = row;
|
|
|
|
bps->col = col;
|
|
|
|
bps->playerNum = playerNum;
|
|
|
|
bps->nTiles = nTiles;
|
2021-02-23 21:12:32 +01:00
|
|
|
call_pickBlank( "Pick a tile for your blank", tileFaces, nTiles,
|
2021-02-18 03:58:46 +01:00
|
|
|
onBlankPicked, bps );
|
|
|
|
}
|
|
|
|
|
2021-02-13 21:16:09 +01:00
|
|
|
void
|
2021-02-23 17:35:50 +01:00
|
|
|
main_showGameOver( GameState* gs )
|
|
|
|
{
|
|
|
|
if ( isVisible(gs) ) {
|
|
|
|
XWStreamCtxt* stream = mem_stream_make_raw( MPPARM(gs->globals->mpool)
|
|
|
|
gs->globals->vtMgr );
|
|
|
|
server_writeFinalScores( gs->game.server, NULL, stream );
|
|
|
|
stream_putU8( stream, 0 );
|
|
|
|
call_alert( (const XP_UCHAR*)stream_getPtr( stream ) );
|
|
|
|
stream_destroy( stream, NULL );
|
|
|
|
}
|
2021-02-13 21:16:09 +01:00
|
|
|
}
|
|
|
|
|
2021-02-02 22:49:27 +01:00
|
|
|
static time_t
|
|
|
|
getCurMS()
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday( &tv, NULL );
|
|
|
|
time_t result = tv.tv_sec * 1000; /* convert to millis */
|
|
|
|
result += tv.tv_usec / 1000; /* convert to millis too */
|
|
|
|
// LOG_RETURNF( "%x", result );
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-02-10 23:16:22 +01:00
|
|
|
void
|
2021-02-23 17:35:50 +01:00
|
|
|
main_clear_timer( GameState* gs, XWTimerReason why )
|
2021-02-02 22:49:27 +01:00
|
|
|
{
|
2021-02-10 23:16:22 +01:00
|
|
|
XP_LOGFF( "why: %d" );
|
2021-02-10 23:25:25 +01:00
|
|
|
// XP_ASSERT(0); fires when start new game
|
2021-02-03 04:20:26 +01:00
|
|
|
}
|
|
|
|
|
2021-02-10 23:16:22 +01:00
|
|
|
typedef struct _TimerClosure {
|
2021-02-23 17:35:50 +01:00
|
|
|
GameState* gs;
|
2021-02-10 23:16:22 +01:00
|
|
|
XWTimerReason why;
|
|
|
|
XWTimerProc proc;
|
|
|
|
void* closure;
|
|
|
|
} TimerClosure;
|
2021-02-02 22:49:27 +01:00
|
|
|
|
2021-02-10 23:16:22 +01:00
|
|
|
static void
|
|
|
|
onTimerFired( void* closure )
|
2021-02-07 21:48:06 +01:00
|
|
|
{
|
2021-02-10 23:16:22 +01:00
|
|
|
LOG_FUNC();
|
|
|
|
TimerClosure* tc = (TimerClosure*)closure;
|
|
|
|
XP_Bool draw = (*tc->proc)( tc->closure, NULL, tc->why );
|
|
|
|
if ( draw ) {
|
2021-02-23 17:35:50 +01:00
|
|
|
updateScreen( tc->gs, true );
|
2021-02-10 23:16:22 +01:00
|
|
|
}
|
2021-02-23 17:35:50 +01:00
|
|
|
XP_FREE( tc->gs->globals->mpool, tc );
|
2021-02-07 21:48:06 +01:00
|
|
|
}
|
|
|
|
|
2021-02-02 22:49:27 +01:00
|
|
|
void
|
2021-02-23 17:35:50 +01:00
|
|
|
main_set_timer( GameState* gs, XWTimerReason why, XP_U16 when,
|
2021-02-02 22:49:27 +01:00
|
|
|
XWTimerProc proc, void* closure )
|
|
|
|
{
|
2021-02-10 23:16:22 +01:00
|
|
|
XP_LOGFF( "why: %d", why );
|
2021-02-23 17:35:50 +01:00
|
|
|
TimerClosure* tc = XP_MALLOC( gs->globals->mpool, sizeof(*tc) );
|
|
|
|
tc->gs = gs;
|
2021-02-10 23:16:22 +01:00
|
|
|
tc->proc = proc;
|
|
|
|
tc->closure = closure;
|
|
|
|
tc->why = why;
|
|
|
|
|
|
|
|
if ( 0 == when ) {
|
|
|
|
when = 1;
|
|
|
|
}
|
|
|
|
when *= 1000; /* convert to ms */
|
2021-02-02 22:49:27 +01:00
|
|
|
|
2021-02-10 23:16:22 +01:00
|
|
|
jscallback_set( onTimerFired, tc, when );
|
2021-02-02 22:49:27 +01:00
|
|
|
}
|
|
|
|
|
2021-02-13 19:37:47 +01:00
|
|
|
typedef struct _QueryState {
|
2021-02-23 17:35:50 +01:00
|
|
|
GameState* gs;
|
2021-02-13 19:37:47 +01:00
|
|
|
QueryProc proc;
|
|
|
|
void* closure;
|
|
|
|
} QueryState;
|
|
|
|
|
|
|
|
static void
|
|
|
|
onQueryCalled( void* closure, const char* button )
|
|
|
|
{
|
|
|
|
QueryState* qs = (QueryState*)closure;
|
|
|
|
bool ok = 0 == strcmp( button, BUTTON_OK );
|
|
|
|
(*qs->proc)( qs->closure, ok );
|
2021-02-23 17:35:50 +01:00
|
|
|
XP_FREE( qs->gs->globals->mpool, qs );
|
2021-02-13 19:37:47 +01:00
|
|
|
}
|
|
|
|
|
2021-02-02 23:52:25 +01:00
|
|
|
void
|
2021-02-23 17:35:50 +01:00
|
|
|
main_query( GameState* gs, const XP_UCHAR* query, QueryProc proc, void* closure )
|
2021-02-02 23:52:25 +01:00
|
|
|
{
|
2021-02-23 17:35:50 +01:00
|
|
|
if ( isVisible(gs) ) {
|
|
|
|
QueryState* qs = XP_MALLOC( gs->globals->mpool, sizeof(*qs) );
|
|
|
|
qs->proc = proc;
|
|
|
|
qs->closure = closure;
|
|
|
|
qs->gs = gs;
|
2021-02-13 19:37:47 +01:00
|
|
|
|
2021-02-23 17:35:50 +01:00
|
|
|
const char* buttons[] = { BUTTON_CANCEL, BUTTON_OK, NULL };
|
|
|
|
call_dialog( query, buttons, onQueryCalled, qs );
|
|
|
|
}
|
2021-02-02 23:52:25 +01:00
|
|
|
}
|
|
|
|
|
2021-02-03 04:20:26 +01:00
|
|
|
void
|
2021-02-23 17:35:50 +01:00
|
|
|
main_alert( GameState* gs, const XP_UCHAR* msg )
|
2021-02-03 04:20:26 +01:00
|
|
|
{
|
2021-02-23 17:35:50 +01:00
|
|
|
if ( isVisible(gs) ) {
|
|
|
|
call_alert( msg );
|
|
|
|
}
|
2021-02-03 04:20:26 +01:00
|
|
|
}
|
|
|
|
|
2021-02-10 22:21:28 +01:00
|
|
|
typedef struct _IdleClosure {
|
2021-02-23 17:35:50 +01:00
|
|
|
GameState* gs;
|
2021-02-10 22:21:28 +01:00
|
|
|
IdleProc proc;
|
|
|
|
void* closure;
|
|
|
|
} IdleClosure;
|
|
|
|
|
|
|
|
static void
|
|
|
|
onIdleFired( void* closure )
|
|
|
|
{
|
|
|
|
LOG_FUNC();
|
|
|
|
IdleClosure* ic = (IdleClosure*)closure;
|
|
|
|
XP_Bool draw = (*ic->proc)(ic->closure);
|
|
|
|
if ( draw ) {
|
2021-02-23 17:35:50 +01:00
|
|
|
updateScreen( ic->gs, true );
|
2021-02-10 22:21:28 +01:00
|
|
|
}
|
2021-02-23 17:35:50 +01:00
|
|
|
XP_FREE( ic->gs->globals->mpool, ic );
|
2021-02-10 22:21:28 +01:00
|
|
|
}
|
|
|
|
|
2021-02-03 04:20:26 +01:00
|
|
|
void
|
2021-02-23 17:35:50 +01:00
|
|
|
main_set_idle( GameState* gs, IdleProc proc, void* closure )
|
2021-02-03 04:20:26 +01:00
|
|
|
{
|
2021-02-10 22:21:28 +01:00
|
|
|
LOG_FUNC();
|
2021-02-23 17:35:50 +01:00
|
|
|
IdleClosure* ic = XP_MALLOC( gs->globals->mpool, sizeof(*ic) );
|
|
|
|
ic->gs = gs;
|
2021-02-10 22:21:28 +01:00
|
|
|
ic->proc = proc;
|
|
|
|
ic->closure = closure;
|
|
|
|
|
|
|
|
jscallback_set( onIdleFired, ic, 0 );
|
2021-02-03 04:20:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static XP_Bool
|
2021-02-23 17:35:50 +01:00
|
|
|
checkForEvent( GameState* gs )
|
2021-02-02 21:04:31 +01:00
|
|
|
{
|
|
|
|
XP_Bool handled;
|
|
|
|
XP_Bool draw = XP_FALSE;
|
2021-02-23 17:35:50 +01:00
|
|
|
BoardCtxt* board = gs->game.board;
|
2021-02-02 23:19:57 +01:00
|
|
|
|
2021-02-02 21:04:31 +01:00
|
|
|
SDL_Event event;
|
|
|
|
if ( SDL_PollEvent(&event) ) {
|
|
|
|
switch ( event.type ) {
|
|
|
|
case SDL_MOUSEBUTTONDOWN:
|
|
|
|
draw = event.button.button == SDL_BUTTON_LEFT
|
2021-02-02 23:19:57 +01:00
|
|
|
&& board_handlePenDown( board, NULL,
|
2021-02-02 21:04:31 +01:00
|
|
|
event.button.x, event.button.y,
|
|
|
|
&handled );
|
|
|
|
break;
|
|
|
|
case SDL_MOUSEBUTTONUP:
|
|
|
|
draw = event.button.button == SDL_BUTTON_LEFT
|
2021-02-02 23:19:57 +01:00
|
|
|
&& board_handlePenUp( board, NULL,
|
2021-02-02 21:04:31 +01:00
|
|
|
event.button.x, event.button.y );
|
|
|
|
break;
|
2021-02-02 23:19:57 +01:00
|
|
|
case SDL_MOUSEMOTION:
|
|
|
|
draw = board_handlePenMove( board, NULL,
|
|
|
|
event.motion.x, event.motion.y );
|
|
|
|
break;
|
2021-02-02 21:04:31 +01:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// XP_LOGFF( "draw: %d", draw );
|
2021-02-03 04:20:26 +01:00
|
|
|
return draw;
|
|
|
|
}
|
|
|
|
|
2021-02-03 07:10:26 +01:00
|
|
|
static void
|
2021-02-23 17:35:50 +01:00
|
|
|
clearScreen( Globals* globals )
|
2021-02-03 07:10:26 +01:00
|
|
|
{
|
|
|
|
SDL_RenderClear( globals->renderer );
|
|
|
|
SDL_RenderPresent( globals->renderer );
|
2021-02-23 17:35:50 +01:00
|
|
|
}
|
2021-02-06 17:54:08 +01:00
|
|
|
|
2021-02-23 17:35:50 +01:00
|
|
|
static void
|
|
|
|
updateScreen( GameState* gs, bool doSave )
|
|
|
|
{
|
|
|
|
Globals* globals = gs->globals;
|
|
|
|
if ( gs == getCurGame(globals) ) {
|
|
|
|
SDL_RenderClear( globals->renderer );
|
|
|
|
if ( !!gs->game.board ) {
|
|
|
|
board_draw( gs->game.board, NULL );
|
|
|
|
wasm_draw_render( globals->draw, globals->renderer );
|
|
|
|
}
|
|
|
|
SDL_RenderPresent( globals->renderer );
|
|
|
|
|
|
|
|
} else {
|
|
|
|
XP_LOGFF( "not drawing %s; not visible", gs->gameName );
|
|
|
|
}
|
2021-02-19 04:20:20 +01:00
|
|
|
|
2021-02-24 02:05:13 +01:00
|
|
|
updateGameButtons( globals );
|
|
|
|
|
2021-02-06 17:54:08 +01:00
|
|
|
/* Let's save state here too, though likely too often */
|
2021-02-10 21:18:15 +01:00
|
|
|
if ( doSave ) {
|
|
|
|
XWStreamCtxt* stream = mem_stream_make_raw( MPPARM(globals->mpool)
|
|
|
|
globals->vtMgr );
|
2021-02-23 17:35:50 +01:00
|
|
|
game_saveToStream( &gs->game, NULL, &gs->gi,
|
|
|
|
stream, ++gs->saveToken );
|
2021-02-19 20:19:16 +01:00
|
|
|
|
2021-02-27 00:33:03 +01:00
|
|
|
char indx[32];
|
|
|
|
formatGameID( indx, sizeof(indx), gs->gi.gameID );
|
|
|
|
dutil_storeIndxStream( globals->dutil, NULL, KEY_GAME, indx, stream );
|
2021-02-10 21:18:15 +01:00
|
|
|
stream_destroy( stream, NULL );
|
2021-02-23 17:35:50 +01:00
|
|
|
game_saveSucceeded( &gs->game, NULL, gs->saveToken );
|
2021-02-10 21:18:15 +01:00
|
|
|
}
|
2021-02-03 07:10:26 +01:00
|
|
|
}
|
|
|
|
|
2021-02-20 02:49:20 +01:00
|
|
|
void
|
2021-02-23 17:35:50 +01:00
|
|
|
main_updateScreen( GameState* gs )
|
2021-02-20 02:49:20 +01:00
|
|
|
{
|
2021-02-23 17:35:50 +01:00
|
|
|
updateScreen( gs, true );
|
2021-02-20 02:49:20 +01:00
|
|
|
}
|
|
|
|
|
2021-02-03 04:20:26 +01:00
|
|
|
static void
|
|
|
|
looper( void* closure )
|
|
|
|
{
|
|
|
|
Globals* globals = (Globals*)closure;
|
2021-02-23 17:35:50 +01:00
|
|
|
GameState* gs = getCurGame( globals );
|
|
|
|
if ( !!gs ) {
|
|
|
|
if ( checkForEvent( gs ) ) {
|
|
|
|
updateScreen( gs, true );
|
|
|
|
}
|
2021-02-03 07:10:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-10 05:59:33 +01:00
|
|
|
static bool
|
2021-02-23 17:35:50 +01:00
|
|
|
inviteFromArgv( Globals* globals, NetLaunchInfo* nlip,
|
|
|
|
int argc, const char** argv )
|
2021-02-10 05:59:33 +01:00
|
|
|
{
|
|
|
|
LOG_FUNC();
|
|
|
|
CurGameInfo gi = {0};
|
|
|
|
CommsAddrRec addr = {0};
|
|
|
|
MQTTDevID mqttDevID = 0;
|
|
|
|
XP_U16 nPlayersH = 0;
|
|
|
|
XP_U16 forceChannel = 0;
|
|
|
|
const XP_UCHAR* gameName = NULL;
|
|
|
|
const XP_UCHAR* inviteID = NULL;
|
|
|
|
|
|
|
|
for ( int ii = 0; ii < argc; ++ii ) {
|
|
|
|
const char* argp = argv[ii];
|
|
|
|
char* param = strchr(argp, '=');
|
|
|
|
if ( !param ) { /* no '='? */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
char arg[8];
|
|
|
|
int argLen = param - argp;
|
|
|
|
XP_MEMCPY( arg, argp, argLen );
|
|
|
|
arg[argLen] = '\0';
|
|
|
|
++param; /* skip the '=' */
|
|
|
|
|
|
|
|
if ( 0 == strcmp( "lang", arg ) ) {
|
|
|
|
gi.dictLang = atoi(param);
|
|
|
|
} else if ( 0 == strcmp( "np", arg ) ) {
|
|
|
|
gi.nPlayers = atoi(param);
|
|
|
|
} else if ( 0 == strcmp( "nh", arg ) ) {
|
|
|
|
nPlayersH = atoi(param);
|
|
|
|
} else if ( 0 == strcmp( "gid", arg ) ) {
|
|
|
|
gi.gameID = atoi(param);
|
|
|
|
} else if ( 0 == strcmp( "fc", arg ) ) {
|
|
|
|
gi.forceChannel = atoi(param);
|
|
|
|
} else if ( 0 == strcmp( "nm", arg ) ) {
|
|
|
|
gameName = param;
|
|
|
|
} else if ( 0 == strcmp( "id", arg ) ) {
|
|
|
|
inviteID = param;
|
|
|
|
} else if ( 0 == strcmp( "wl", arg ) ) {
|
|
|
|
replaceStringIfDifferent( globals->mpool, &gi.dictName, param );
|
|
|
|
} else if ( 0 == strcmp( "r2id", arg ) ) {
|
|
|
|
if ( strToMQTTCDevID( param, &addr.u.mqtt.devID ) ) {
|
|
|
|
addr_addType( &addr, COMMS_CONN_MQTT );
|
|
|
|
} else {
|
|
|
|
XP_LOGFF( "bad devid %s", param );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
XP_LOGFF( "dropping arg %s, param %s", arg, param );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool success = 0 < nPlayersH &&
|
|
|
|
addr_hasType( &addr, COMMS_CONN_MQTT );
|
|
|
|
|
|
|
|
if ( success ) {
|
|
|
|
nli_init( nlip, &gi, &addr, nPlayersH, forceChannel );
|
|
|
|
if ( !!gameName ) {
|
|
|
|
nli_setGameName( nlip, gameName );
|
|
|
|
}
|
|
|
|
if ( !!inviteID ) {
|
|
|
|
nli_setInviteID( nlip, inviteID );
|
|
|
|
}
|
|
|
|
LOGNLI( nlip );
|
|
|
|
}
|
|
|
|
gi_disposePlayerInfo( MPPARM(globals->mpool) &gi );
|
|
|
|
LOG_RETURNF( "%d", success );
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2021-03-03 06:10:59 +01:00
|
|
|
typedef struct _GameStartState {
|
|
|
|
Globals* globals;
|
|
|
|
NetLaunchInfo nli;
|
|
|
|
NetLaunchInfo* nlip;
|
|
|
|
char lastKey[16];
|
|
|
|
int nWanted;
|
|
|
|
} GameStartState;
|
|
|
|
|
|
|
|
/* Will get called twice, once with KEY_PLAYER_NAME and once with KEY_LAST_GID */
|
|
|
|
static void
|
|
|
|
onReqsLoaded(void* closure, const char* key, void* data, int dataLen)
|
|
|
|
{
|
|
|
|
GameStartState* gss = (GameStartState*)closure;
|
|
|
|
Globals* globals = gss->globals;
|
|
|
|
XP_LOGFF( "(key: %s, len: %d)", key, dataLen );
|
|
|
|
|
|
|
|
if ( 0 == strcmp( key, KEY_LAST_GID ) ) {
|
|
|
|
int gameID = 0;
|
|
|
|
if ( !!data && dataLen == sizeof(gameID) ) {
|
|
|
|
gameID = *(int*)data;
|
|
|
|
formatGameID( gss->lastKey, sizeof(gss->lastKey), gameID );
|
|
|
|
}
|
|
|
|
XP_LOGFF( "loaded KEY_LAST_GID: %s", gss->lastKey );
|
|
|
|
} else if ( 0 == strcmp( key, KEY_PLAYER_NAME ) ) {
|
|
|
|
if ( !!data && 0 < dataLen && dataLen < VSIZE(globals->playerName) ) {
|
|
|
|
XP_MEMCPY( globals->playerName, data, dataLen );
|
|
|
|
} else {
|
|
|
|
strcat( globals->playerName, "Player 1" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( 0 == --gss->nWanted ) {
|
|
|
|
loadAndDraw( globals, gss->nlip, gss->lastKey, NULL );
|
|
|
|
updateDeviceButtons( globals );
|
|
|
|
XP_FREE( globals->mpool, gss );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-06 18:23:39 +01:00
|
|
|
static void
|
2021-02-10 05:59:33 +01:00
|
|
|
initNoReturn( int argc, const char** argv )
|
2021-02-02 05:13:25 +01:00
|
|
|
{
|
2021-02-04 05:18:15 +01:00
|
|
|
time_t now = getCurMS();
|
|
|
|
srandom( now );
|
2021-02-06 17:54:08 +01:00
|
|
|
XP_LOGFF( "called(srandom( %x )", now );
|
2021-02-04 05:18:15 +01:00
|
|
|
|
2021-02-23 17:35:50 +01:00
|
|
|
Globals globals = {0}; // calloc(1, sizeof(*globals));
|
|
|
|
#ifdef DEBUG
|
|
|
|
globals._GUARD = GUARD_GLOB;
|
|
|
|
#endif
|
2021-02-04 00:22:08 +01:00
|
|
|
|
2021-03-03 06:10:59 +01:00
|
|
|
/* I don't think this ever gets popped off stack, but to be safe.... */
|
|
|
|
GameStartState* gss = XP_CALLOC( globals.mpool, sizeof(*gss) );
|
|
|
|
gss->globals = &globals;
|
|
|
|
|
|
|
|
if ( inviteFromArgv( &globals, &gss->nli, argc, argv ) ) {
|
|
|
|
gss->nlip = &gss->nli;
|
2021-02-10 05:59:33 +01:00
|
|
|
}
|
|
|
|
|
2021-02-02 21:04:31 +01:00
|
|
|
SDL_Init( SDL_INIT_EVENTS );
|
2021-02-02 18:08:41 +01:00
|
|
|
TTF_Init();
|
2021-02-02 21:04:31 +01:00
|
|
|
|
|
|
|
SDL_CreateWindowAndRenderer( WINDOW_WIDTH, WINDOW_HEIGHT, 0,
|
2021-02-23 17:35:50 +01:00
|
|
|
&globals.window, &globals.renderer );
|
2021-02-01 17:30:34 +01:00
|
|
|
|
2021-03-03 06:10:59 +01:00
|
|
|
/* wipe the canvas to background */
|
2021-02-23 17:35:50 +01:00
|
|
|
SDL_SetRenderDrawColor( globals.renderer, 155, 155, 155, 255 );
|
|
|
|
SDL_RenderClear( globals.renderer );
|
2021-02-04 00:22:08 +01:00
|
|
|
|
2021-02-23 17:35:50 +01:00
|
|
|
initDeviceGlobals( &globals );
|
2021-02-02 05:13:25 +01:00
|
|
|
|
2021-03-03 06:10:59 +01:00
|
|
|
/* Load the two bits we need from storage before starting */
|
|
|
|
dutil_startLoad( globals.dutil, NULL, KEY_PLAYER_NAME, onReqsLoaded, gss );
|
|
|
|
dutil_startLoad( globals.dutil, NULL, KEY_LAST_GID, onReqsLoaded, gss );
|
|
|
|
gss->nWanted = 2;
|
2021-02-19 04:20:20 +01:00
|
|
|
|
2021-02-23 17:35:50 +01:00
|
|
|
emscripten_set_main_loop_arg( looper, &globals, -1, 1 );
|
2021-02-04 00:22:08 +01:00
|
|
|
}
|
|
|
|
|
2021-02-15 23:59:44 +01:00
|
|
|
void
|
|
|
|
MQTTConnectedChanged( void* closure, bool connected )
|
|
|
|
{
|
|
|
|
XP_LOGFF( "connected=%d", connected);
|
2021-02-23 17:35:50 +01:00
|
|
|
if ( connected ) {
|
|
|
|
CAST_GLOB(Globals*, globals, closure);
|
|
|
|
GameState* gs = getCurGame( globals );
|
|
|
|
if ( !!gs && !!gs->game.comms ) {
|
|
|
|
comms_resendAll( gs->game.comms, NULL, COMMS_CONN_MQTT, XP_TRUE );
|
|
|
|
}
|
2021-02-15 23:59:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-07 21:48:06 +01:00
|
|
|
void
|
2021-02-27 20:06:43 +01:00
|
|
|
cbckBinary( BinProc proc, void* closure, int len, const uint8_t* msg )
|
2021-02-07 21:48:06 +01:00
|
|
|
{
|
2021-02-27 20:06:43 +01:00
|
|
|
(*proc)(closure, msg, len );
|
2021-02-07 21:48:06 +01:00
|
|
|
}
|
|
|
|
|
2021-02-10 22:21:28 +01:00
|
|
|
void
|
2021-02-21 19:07:51 +01:00
|
|
|
onNewGame( void* closure, bool opponentIsRobot )
|
2021-02-10 22:21:28 +01:00
|
|
|
{
|
2021-02-21 19:07:51 +01:00
|
|
|
Globals* globals = (Globals*)closure;
|
|
|
|
XP_LOGFF( "isRobot: %d", opponentIsRobot );
|
|
|
|
|
|
|
|
NewGameParams ngp = {0};
|
|
|
|
ngp.isRobotNotRemote = opponentIsRobot;
|
|
|
|
loadAndDraw( globals, NULL, NULL, &ngp );
|
2021-02-27 00:45:52 +01:00
|
|
|
updateDeviceButtons( globals );
|
2021-02-10 22:21:28 +01:00
|
|
|
}
|
|
|
|
|
2021-02-21 19:07:51 +01:00
|
|
|
/* Called from js with a proc and closure */
|
2021-02-13 19:37:47 +01:00
|
|
|
void
|
2021-02-21 19:07:51 +01:00
|
|
|
cbckVoid( JSCallback proc, void* closure )
|
2021-02-13 19:37:47 +01:00
|
|
|
{
|
2021-02-21 19:07:51 +01:00
|
|
|
LOG_FUNC();
|
|
|
|
(*proc)(closure);
|
2021-02-13 19:37:47 +01:00
|
|
|
}
|
|
|
|
|
2021-02-21 19:07:51 +01:00
|
|
|
/* Called from js with a proc, closure, and string */
|
2021-02-19 22:18:53 +01:00
|
|
|
void
|
2021-02-21 19:07:51 +01:00
|
|
|
cbckString( StringProc proc, void* closure, const char* str )
|
2021-02-19 22:18:53 +01:00
|
|
|
{
|
2021-02-21 19:07:51 +01:00
|
|
|
if ( !!proc ) {
|
|
|
|
(*proc)( closure, str );
|
|
|
|
}
|
2021-02-19 22:18:53 +01:00
|
|
|
}
|
|
|
|
|
2021-02-27 20:06:43 +01:00
|
|
|
void
|
|
|
|
gotDictBinary( void* closure, const char* xwd, const char* lang,
|
|
|
|
const char* lc, int len, uint8_t* data )
|
|
|
|
{
|
|
|
|
XP_LOGFF( "xwd: %s; lang: %s, lc: %s, len: %d", xwd, lang, lc, len );
|
2021-02-28 06:16:01 +01:00
|
|
|
|
|
|
|
CAST_GLOB(Globals*, globals, closure);
|
|
|
|
char key[64];
|
|
|
|
formatDictIndx( key, sizeof(key), lc, xwd );
|
|
|
|
dutil_storeIndxPtr( globals->dutil, NULL, KEY_DICTS, key, data, len );
|
|
|
|
|
|
|
|
updateDeviceButtons( globals );
|
2021-02-27 20:06:43 +01:00
|
|
|
}
|
|
|
|
|
2021-02-04 00:22:08 +01:00
|
|
|
int
|
2021-02-10 05:59:33 +01:00
|
|
|
main( int argc, const char** argv )
|
2021-02-04 00:22:08 +01:00
|
|
|
{
|
2021-02-06 18:23:39 +01:00
|
|
|
XP_LOGFF( "(argc=%d)", argc );
|
2021-02-10 05:59:33 +01:00
|
|
|
initNoReturn( argc, argv );
|
2021-02-01 17:30:34 +01:00
|
|
|
return 0;
|
|
|
|
}
|