mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-01-15 15:41:24 +01:00
pass addresses to game on creation
Current networking, based on invitations rather than a relay that plays matchmaker, allows host to know its address when a game is created, and for guest to know its host's address in addition. Enforcing this makes inviting and rematching in common code (coming soon) easier. Big change on Android is I used to create a new game prior to passing it to GameConfigDelegate, but now I have to wait for user to configure (including choosing how to communicate) before I can create it.
This commit is contained in:
parent
ca2d97e647
commit
674c811a09
12 changed files with 287 additions and 118 deletions
|
@ -64,13 +64,16 @@ public class GameConfigDelegate extends DelegateBase
|
|||
implements View.OnClickListener, XWListItem.DeleteCallback {
|
||||
private static final String TAG = GameConfigDelegate.class.getSimpleName();
|
||||
|
||||
private static final String INTENT_FORRESULT_NEWGAME = "newgame";
|
||||
|
||||
private static final String INTENT_FORRESULT_SOLO = "solo";
|
||||
private static final String WHICH_PLAYER = "WHICH_PLAYER";
|
||||
private static final String LOCAL_GI = "LOCAL_GI";
|
||||
private static final String LOCAL_TYPES = "LOCAL_TYPES";
|
||||
private static final String DIS_MAP = "DIS_MAP";
|
||||
|
||||
static final String INTENT_KEY_GI = "key_gi";
|
||||
static final String INTENT_KEY_CAR = "key_car";
|
||||
static final String INTENT_KEY_NAME = "key_name";
|
||||
|
||||
private Activity m_activity;
|
||||
private CheckBox m_gameLockedCheck;
|
||||
private boolean m_isLocked;
|
||||
|
@ -84,6 +87,7 @@ public class GameConfigDelegate extends DelegateBase
|
|||
private Spinner m_playerDictSpinner;
|
||||
private long m_rowid;
|
||||
private boolean m_isNewGame;
|
||||
private boolean m_newGameIsSolo; // only used if m_isNewGame is true
|
||||
private CurGameInfo m_gi;
|
||||
private CurGameInfo m_giOrig;
|
||||
private JNIThread m_jniThread;
|
||||
|
@ -97,7 +101,6 @@ public class GameConfigDelegate extends DelegateBase
|
|||
private String m_browseText;
|
||||
private LinearLayout m_playerLayout;
|
||||
private CommsAddrRec m_carOrig;
|
||||
private CommsAddrRec[] m_remoteAddrs;
|
||||
private CommsAddrRec m_car;
|
||||
private CommonPrefs m_cp;
|
||||
private boolean m_gameStarted = false;
|
||||
|
@ -482,8 +485,8 @@ public class GameConfigDelegate extends DelegateBase
|
|||
|
||||
Bundle args = getArguments();
|
||||
m_rowid = args.getLong( GameUtils.INTENT_KEY_ROWID, DBUtils.ROWID_NOTFOUND );
|
||||
Assert.assertTrue( DBUtils.ROWID_NOTFOUND != m_rowid );
|
||||
m_isNewGame = args.getBoolean( INTENT_FORRESULT_NEWGAME, false );
|
||||
m_newGameIsSolo = args.getBoolean( INTENT_FORRESULT_SOLO, false );
|
||||
m_isNewGame = DBUtils.ROWID_NOTFOUND == m_rowid;
|
||||
|
||||
m_addPlayerButton = (Button)findViewById(R.id.add_player);
|
||||
m_addPlayerButton.setOnClickListener( this );
|
||||
|
@ -510,7 +513,9 @@ public class GameConfigDelegate extends DelegateBase
|
|||
@Override
|
||||
protected void onResume()
|
||||
{
|
||||
m_jniThread = JNIThread.getRetained( m_rowid );
|
||||
if ( !m_isNewGame ) {
|
||||
m_jniThread = JNIThread.getRetained( m_rowid );
|
||||
}
|
||||
super.onResume();
|
||||
loadGame();
|
||||
}
|
||||
|
@ -565,8 +570,13 @@ public class GameConfigDelegate extends DelegateBase
|
|||
{
|
||||
if ( null == m_giOrig ) {
|
||||
m_giOrig = new CurGameInfo( m_activity );
|
||||
if ( m_isNewGame ) {
|
||||
m_giOrig.addDefaults( m_activity, m_newGameIsSolo );
|
||||
}
|
||||
|
||||
if ( null != m_jniThread ) {
|
||||
if ( m_isNewGame ) {
|
||||
loadGame( null );
|
||||
} else if ( null != m_jniThread ) {
|
||||
try ( XwJNI.GamePtr gamePtr = m_jniThread
|
||||
.getGamePtr().retain() ) {
|
||||
loadGame( gamePtr );
|
||||
|
@ -587,11 +597,14 @@ public class GameConfigDelegate extends DelegateBase
|
|||
// Exists only to be called from inside two try-with-resource blocks above
|
||||
private void loadGame( XwJNI.GamePtr gamePtr )
|
||||
{
|
||||
if ( null == gamePtr ) {
|
||||
if ( null == gamePtr && !m_isNewGame ) {
|
||||
Assert.failDbg();
|
||||
} else {
|
||||
m_gameStarted = XwJNI.model_getNMoves( gamePtr ) > 0
|
||||
|| XwJNI.comms_isConnected( gamePtr );
|
||||
m_gameStarted = !m_isNewGame;
|
||||
if ( m_gameStarted ) {
|
||||
m_gameStarted = XwJNI.model_getNMoves( gamePtr ) > 0
|
||||
|| XwJNI.comms_isConnected( gamePtr );
|
||||
}
|
||||
|
||||
if ( m_gameStarted ) {
|
||||
if ( null == m_gameLockedCheck ) {
|
||||
|
@ -607,11 +620,16 @@ public class GameConfigDelegate extends DelegateBase
|
|||
m_gi = new CurGameInfo( m_giOrig );
|
||||
}
|
||||
|
||||
if ( XwJNI.game_hasComms( gamePtr ) ) {
|
||||
m_carOrig = XwJNI.comms_getAddr( gamePtr );
|
||||
m_remoteAddrs = XwJNI.comms_getAddrs( gamePtr );
|
||||
if ( m_isNewGame ) {
|
||||
if ( m_newGameIsSolo ) {
|
||||
m_carOrig = new CommsAddrRec(); // empty
|
||||
} else {
|
||||
m_carOrig = CommsAddrRec.getSelfAddr( m_activity );
|
||||
}
|
||||
} else if ( XwJNI.game_hasComms( gamePtr ) ) {
|
||||
m_carOrig = XwJNI.comms_getSelfAddr( gamePtr );
|
||||
} else if ( !localOnlyGame() ) {
|
||||
m_carOrig = XwJNI.comms_getInitialAddr();
|
||||
m_carOrig = CommsAddrRec.getSelfAddr( m_activity );
|
||||
} else {
|
||||
// Leaving this null breaks stuff: an empty set, rather than a
|
||||
// null one, represents a standalone game
|
||||
|
@ -623,8 +641,10 @@ public class GameConfigDelegate extends DelegateBase
|
|||
m_conTypes = (CommsConnTypeSet)m_carOrig.conTypes.clone();
|
||||
}
|
||||
|
||||
buildDisabledsMap( gamePtr );
|
||||
setDisableds();
|
||||
if ( !m_isNewGame ) {
|
||||
buildDisabledsMap( gamePtr );
|
||||
setDisableds();
|
||||
}
|
||||
|
||||
m_car = new CommsAddrRec( m_carOrig );
|
||||
|
||||
|
@ -667,17 +687,17 @@ public class GameConfigDelegate extends DelegateBase
|
|||
}
|
||||
m_traysizeSpinner
|
||||
.setOnItemSelectedListener( new Utils.OnNothingSelDoesNothing() {
|
||||
@Override
|
||||
public void onItemSelected( AdapterView<?> parent, View spinner,
|
||||
int position, long id ) {
|
||||
if ( curSel[0] != position ) {
|
||||
curSel[0] = position;
|
||||
makeNotAgainBuilder( R.string.key_na_traysize,
|
||||
R.string.not_again_traysize )
|
||||
.show();
|
||||
@Override
|
||||
public void onItemSelected( AdapterView<?> parent, View spinner,
|
||||
int position, long id ) {
|
||||
if ( curSel[0] != position ) {
|
||||
curSel[0] = position;
|
||||
makeNotAgainBuilder( R.string.key_na_traysize,
|
||||
R.string.not_again_traysize )
|
||||
.show();
|
||||
}
|
||||
}
|
||||
}
|
||||
} );
|
||||
} );
|
||||
|
||||
}
|
||||
} // loadGame
|
||||
|
@ -774,9 +794,6 @@ public class GameConfigDelegate extends DelegateBase
|
|||
PrefsDelegate.launch( m_activity );
|
||||
break;
|
||||
case DELETE_AND_EXIT:
|
||||
if ( m_isNewGame ) {
|
||||
deleteGame();
|
||||
}
|
||||
closeNoSave();
|
||||
break;
|
||||
case ASKED_PHONE_STATE:
|
||||
|
@ -887,7 +904,14 @@ public class GameConfigDelegate extends DelegateBase
|
|||
if ( !m_haveClosed ) {
|
||||
m_haveClosed = true;
|
||||
Intent intent = new Intent();
|
||||
intent.putExtra( GameUtils.INTENT_KEY_ROWID, m_rowid );
|
||||
if ( m_isNewGame ) {
|
||||
intent.putExtra( INTENT_KEY_GI, m_gi );
|
||||
intent.putExtra( INTENT_KEY_CAR, m_car );
|
||||
// Game name should be a field in this layout
|
||||
intent.putExtra( INTENT_KEY_NAME, "Config Me" );
|
||||
} else {
|
||||
intent.putExtra( GameUtils.INTENT_KEY_ROWID, m_rowid );
|
||||
}
|
||||
setResult( Activity.RESULT_OK, intent );
|
||||
finish();
|
||||
}
|
||||
|
@ -907,9 +931,7 @@ public class GameConfigDelegate extends DelegateBase
|
|||
{
|
||||
boolean consumed = false;
|
||||
if ( ! isFinishing() && null != m_gi ) {
|
||||
if ( m_isNewGame ) {
|
||||
deleteGame();
|
||||
} else {
|
||||
if ( !m_isNewGame ) {
|
||||
saveChanges();
|
||||
if ( !m_gameStarted ) { // no confirm needed
|
||||
applyChanges( true );
|
||||
|
@ -932,11 +954,6 @@ public class GameConfigDelegate extends DelegateBase
|
|||
return (GameConfigDelegate)super.curThis();
|
||||
}
|
||||
|
||||
private void deleteGame()
|
||||
{
|
||||
GameUtils.deleteGame( m_activity, m_rowid, false, false );
|
||||
}
|
||||
|
||||
private void loadPlayersList()
|
||||
{
|
||||
if ( !isFinishing() ) {
|
||||
|
@ -1170,7 +1187,7 @@ public class GameConfigDelegate extends DelegateBase
|
|||
|
||||
private void setDisableds()
|
||||
{
|
||||
if ( BuildConfig.DEBUG && !localOnlyGame() ) {
|
||||
if ( BuildConfig.DEBUG && null != m_disabMap && !localOnlyGame() ) {
|
||||
LinearLayout disableds = (LinearLayout)findViewById( R.id.disableds );
|
||||
disableds.setVisibility( View.VISIBLE );
|
||||
|
||||
|
@ -1272,14 +1289,16 @@ public class GameConfigDelegate extends DelegateBase
|
|||
|
||||
private void applyChanges( GameLock lock, boolean forceNew )
|
||||
{
|
||||
GameUtils.applyChanges( m_activity, m_gi, m_car, m_disabMap,
|
||||
lock, forceNew );
|
||||
DBUtils.saveThumbnail( m_activity, lock, null ); // clear it
|
||||
if ( !m_isNewGame ) {
|
||||
GameUtils.applyChanges( m_activity, m_gi, m_car, m_disabMap,
|
||||
lock, forceNew );
|
||||
DBUtils.saveThumbnail( m_activity, lock, null ); // clear it
|
||||
}
|
||||
}
|
||||
|
||||
private void applyChanges( boolean forceNew )
|
||||
{
|
||||
if ( !isFinishing() ) {
|
||||
if ( !m_isNewGame && !isFinishing() ) {
|
||||
if ( null != m_jniThread ) {
|
||||
applyChanges( m_jniThread.getLock(), forceNew );
|
||||
} else {
|
||||
|
@ -1295,28 +1314,50 @@ public class GameConfigDelegate extends DelegateBase
|
|||
@Override
|
||||
protected void setTitle()
|
||||
{
|
||||
int strID;
|
||||
if ( null != m_conTypes && 0 < m_conTypes.size() ) {
|
||||
strID = R.string.title_gamenet_config_fmt;
|
||||
String title;
|
||||
if ( m_isNewGame ) {
|
||||
int strID = m_newGameIsSolo
|
||||
? R.string.new_game
|
||||
: R.string.new_game_networked;
|
||||
title = getString( strID );
|
||||
} else {
|
||||
strID = R.string.title_game_config_fmt;
|
||||
int strID;
|
||||
if ( null != m_conTypes && 0 < m_conTypes.size() ) {
|
||||
strID = R.string.title_gamenet_config_fmt;
|
||||
} else {
|
||||
strID = R.string.title_game_config_fmt;
|
||||
}
|
||||
String name = GameUtils.getName( m_activity, m_rowid );
|
||||
title = getString( strID, name );
|
||||
}
|
||||
String name = GameUtils.getName( m_activity, m_rowid );
|
||||
setTitle( getString( strID, name ) );
|
||||
setTitle( title );
|
||||
}
|
||||
|
||||
private boolean localOnlyGame()
|
||||
{
|
||||
return DeviceRole.SERVER_STANDALONE == m_gi.serverRole; // m_giOrig is null...
|
||||
boolean result = DeviceRole.SERVER_STANDALONE == m_gi.serverRole;
|
||||
// Log.d( TAG, "localOnlyGame() => %b", result );
|
||||
return result;
|
||||
}
|
||||
|
||||
public static void editForResult( Delegator delegator,
|
||||
RequestCode requestCode,
|
||||
long rowID, boolean newGame )
|
||||
long rowID )
|
||||
{
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putLong( GameUtils.INTENT_KEY_ROWID, rowID );
|
||||
bundle.putBoolean( INTENT_FORRESULT_NEWGAME, newGame );
|
||||
|
||||
delegator
|
||||
.addFragmentForResult( GameConfigFrag.newInstance( delegator ),
|
||||
bundle, requestCode );
|
||||
}
|
||||
|
||||
public static void configNewForResult( Delegator delegator,
|
||||
RequestCode requestCode,
|
||||
boolean solo )
|
||||
{
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putBoolean( INTENT_FORRESULT_SOLO, solo );
|
||||
|
||||
delegator
|
||||
.addFragmentForResult( GameConfigFrag.newInstance( delegator ),
|
||||
|
|
|
@ -133,26 +133,24 @@ public class GameUtils {
|
|||
boolean juggle )
|
||||
{
|
||||
CurGameInfo gi = new CurGameInfo( context );
|
||||
CommsAddrRec addr = null;
|
||||
CommsAddrRec selfAddr = null;
|
||||
CommsAddrRec hostAddr = null;
|
||||
|
||||
try ( GamePtr gamePtr = loadMakeGame( context, gi, lockSrc ) ) {
|
||||
if ( XwJNI.game_hasComms( gamePtr ) ) {
|
||||
addr = XwJNI.comms_getAddr( gamePtr );
|
||||
selfAddr = XwJNI.comms_getSelfAddr( gamePtr );
|
||||
hostAddr = XwJNI.comms_getHostAddr( gamePtr );
|
||||
}
|
||||
}
|
||||
|
||||
try ( GamePtr gamePtr = XwJNI
|
||||
.initNew( gi, (UtilCtxt)null, (DrawCtx)null,
|
||||
.initNew( gi, selfAddr, hostAddr, (UtilCtxt)null, (DrawCtx)null,
|
||||
CommonPrefs.get( context ), (TransportProcs)null ) ) {
|
||||
|
||||
if ( juggle ) {
|
||||
gi.juggle();
|
||||
}
|
||||
|
||||
if ( null != addr ) {
|
||||
XwJNI.comms_augmentHostAddr( gamePtr, addr );
|
||||
}
|
||||
|
||||
if ( null == lockDest ) {
|
||||
if ( DBUtils.GROUPID_UNSPEC == groupID ) {
|
||||
groupID = DBUtils.getGroupForGame( context, lockSrc.getRowid() );
|
||||
|
@ -414,7 +412,11 @@ public class GameUtils {
|
|||
CommonPrefs.get(context),
|
||||
tp );
|
||||
if ( null == gamePtr ) {
|
||||
gamePtr = XwJNI.initNew( gi, (UtilCtxt)null, null,
|
||||
Assert.assertTrueNR( gi.serverRole != DeviceRole.SERVER_ISCLIENT );
|
||||
CommsAddrRec selfAddr = CommsAddrRec.getSelfAddr( context );
|
||||
CommsAddrRec hostAddr = null;
|
||||
gamePtr = XwJNI.initNew( gi, selfAddr, hostAddr,
|
||||
(UtilCtxt)null, (DrawCtx)null,
|
||||
CommonPrefs.get(context), null );
|
||||
}
|
||||
}
|
||||
|
@ -682,7 +684,23 @@ public class GameUtils {
|
|||
// Will need to add a setNPlayers() method to gi to make this
|
||||
// work
|
||||
Assert.assertTrue( gi.nPlayers == nPlayersT );
|
||||
rowid = saveNew( context, gi, groupID, gameName );
|
||||
return makeNewMultiGame( context, sink, gi, util, groupID, gameName, addr );
|
||||
}
|
||||
|
||||
public static long makeNewMultiGame( Context context, CurGameInfo gi,
|
||||
long groupID, String gameName,
|
||||
CommsAddrRec selfAddr )
|
||||
{
|
||||
return makeNewMultiGame( context, (MultiMsgSink)null, gi, (UtilCtxt)null,
|
||||
groupID, gameName, selfAddr );
|
||||
}
|
||||
|
||||
private static long makeNewMultiGame( Context context, MultiMsgSink sink,
|
||||
CurGameInfo gi, UtilCtxt util,
|
||||
long groupID, String gameName,
|
||||
CommsAddrRec selfAddr )
|
||||
{
|
||||
long rowid = saveNew( context, gi, groupID, gameName );
|
||||
if ( null != sink ) {
|
||||
sink.setRowID( rowid );
|
||||
}
|
||||
|
@ -692,7 +710,9 @@ public class GameUtils {
|
|||
// succeed because we just created the rowid.
|
||||
try ( GameLock lock = GameLock.tryLock( rowid ) ) {
|
||||
Assert.assertNotNull( lock );
|
||||
applyChanges( context, sink, gi, util, addr, null, lock, false );
|
||||
applyChanges( context, sink, gi, util, selfAddr,
|
||||
(Map<CommsConnType, boolean[]>)null,
|
||||
lock, false /*forceNew*/ );
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1138,7 +1158,7 @@ public class GameUtils {
|
|||
}
|
||||
|
||||
if ( forceNew || !madeGame ) {
|
||||
try ( GamePtr gamePtr = XwJNI.initNew( gi, util, (DrawCtx)null,
|
||||
try ( GamePtr gamePtr = XwJNI.initNew( gi, car, null, util, (DrawCtx)null,
|
||||
cp, sink ) ) {
|
||||
if ( null != gamePtr ) {
|
||||
applyChanges( context, sink, gi, car, disab, lock, gamePtr );
|
||||
|
@ -1152,10 +1172,6 @@ public class GameUtils {
|
|||
Map<CommsConnType, boolean[]> disab,
|
||||
GameLock lock, GamePtr gamePtr )
|
||||
{
|
||||
if ( null != car ) {
|
||||
XwJNI.comms_augmentHostAddr( gamePtr, car );
|
||||
}
|
||||
|
||||
if ( BuildConfig.DEBUG && null != disab ) {
|
||||
for ( CommsConnType typ : disab.keySet() ) {
|
||||
boolean[] bools = disab.get( typ );
|
||||
|
|
|
@ -1672,7 +1672,18 @@ public class GamesListDelegate extends ListDelegateBase
|
|||
if ( !cancelled ) {
|
||||
long rowID = data.getLongExtra( GameUtils.INTENT_KEY_ROWID,
|
||||
ROWID_NOTFOUND );
|
||||
launchGame( rowID );
|
||||
if ( ROWID_NOTFOUND != rowID ) {
|
||||
launchGame( rowID );
|
||||
} else { // new game case?
|
||||
CurGameInfo gi = (CurGameInfo)
|
||||
data.getSerializableExtra( GameConfigDelegate.INTENT_KEY_GI );
|
||||
CommsAddrRec selfAddr = (CommsAddrRec)
|
||||
data.getSerializableExtra( GameConfigDelegate.INTENT_KEY_CAR );
|
||||
String name = data.getStringExtra( GameConfigDelegate.INTENT_KEY_NAME );
|
||||
long rowid = GameUtils.makeNewMultiGame( m_activity, gi, DBUtils.GROUPID_UNSPEC,
|
||||
name, selfAddr );
|
||||
launchGame( rowid );
|
||||
}
|
||||
}
|
||||
break;
|
||||
case STORE_DATA_FILE:
|
||||
|
@ -2181,7 +2192,7 @@ public class GamesListDelegate extends ListDelegateBase
|
|||
case R.id.games_game_config:
|
||||
GameConfigDelegate.editForResult( getDelegator(),
|
||||
RequestCode.CONFIG_GAME,
|
||||
selRowIDs[0], false );
|
||||
selRowIDs[0] );
|
||||
break;
|
||||
|
||||
case R.id.games_game_move:
|
||||
|
@ -3113,31 +3124,25 @@ public class GamesListDelegate extends ListDelegateBase
|
|||
boolean skipAsk, CommsAddrRec addr )
|
||||
{
|
||||
if ( skipAsk || !askingChangeName( name, doConfigure ) ) {
|
||||
long rowID;
|
||||
long groupID = 1 == m_mySIS.selGroupIDs.size()
|
||||
? m_mySIS.selGroupIDs.iterator().next() : DBUtils.GROUPID_UNSPEC;
|
||||
|
||||
// Ideally we'd check here whether user has set player name.
|
||||
|
||||
if ( m_mySIS.nextIsSolo ) {
|
||||
rowID = GameUtils.saveNew( m_activity,
|
||||
new CurGameInfo( m_activity ),
|
||||
groupID, name );
|
||||
} else {
|
||||
rowID = GameUtils.makeNewMultiGame( m_activity, groupID, name );
|
||||
}
|
||||
|
||||
if ( null != addr ) {
|
||||
DBUtils.addRematchInfo( m_activity, rowID, addr );
|
||||
}
|
||||
|
||||
// If we're configuring, we don't create a game yet.
|
||||
if ( doConfigure ) {
|
||||
// configure it
|
||||
GameConfigDelegate.editForResult( getDelegator(),
|
||||
RequestCode.CONFIG_GAME,
|
||||
rowID, true );
|
||||
GameConfigDelegate.configNewForResult( getDelegator(),
|
||||
RequestCode.CONFIG_GAME,
|
||||
m_mySIS.nextIsSolo );
|
||||
} else {
|
||||
// launch it
|
||||
long rowID;
|
||||
long groupID = 1 == m_mySIS.selGroupIDs.size()
|
||||
? m_mySIS.selGroupIDs.iterator().next()
|
||||
: DBUtils.GROUPID_UNSPEC;
|
||||
|
||||
if ( m_mySIS.nextIsSolo ) {
|
||||
rowID = GameUtils.saveNew( m_activity,
|
||||
new CurGameInfo( m_activity ),
|
||||
groupID, name );
|
||||
} else {
|
||||
rowID = GameUtils.makeNewMultiGame( m_activity, groupID, name );
|
||||
}
|
||||
GameUtils.launchGame( getDelegator(), rowID );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -415,6 +415,42 @@ public class CommsAddrRec implements Serializable {
|
|||
return matter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
if ( BuildConfig.NON_RELEASE ) {
|
||||
ArrayList<String> list = new ArrayList<>();
|
||||
for ( CommsConnType typ : conTypes ) {
|
||||
String elem;
|
||||
switch ( typ ) {
|
||||
case COMMS_CONN_MQTT:
|
||||
elem = String.format( "%s: %s", typ, mqtt_devID );
|
||||
break;
|
||||
case COMMS_CONN_SMS:
|
||||
elem = String.format( "%s: {phone: %s, port: %d}",
|
||||
typ, sms_phone, sms_port );
|
||||
break;
|
||||
default:
|
||||
elem = typ.toString();
|
||||
break;
|
||||
}
|
||||
list.add( elem );
|
||||
}
|
||||
return "{" + TextUtils.join(", ", list) + "}";
|
||||
} else {
|
||||
return super.toString();
|
||||
}
|
||||
}
|
||||
|
||||
public static CommsAddrRec getSelfAddr( Context context )
|
||||
{
|
||||
CommsAddrRec result = new CommsAddrRec();
|
||||
CommsConnTypeSet types = XWPrefs.getAddrTypes( context );
|
||||
result.populate( context, types );
|
||||
Log.d( TAG, "getSelfAddr() => %s", result );
|
||||
return result;
|
||||
}
|
||||
|
||||
private void copyFrom( CommsAddrRec src )
|
||||
{
|
||||
conTypes = src.conTypes;
|
||||
|
|
|
@ -187,6 +187,7 @@ public class CurGameInfo implements Serializable {
|
|||
.append( ", " );
|
||||
}
|
||||
sb.append( "], gameID: ").append( gameID )
|
||||
.append( ", role: ").append( serverRole )
|
||||
.append( ", hashCode: ").append( hashCode() )
|
||||
.append( ", timerEnabled: ").append( timerEnabled )
|
||||
.append( ", gameSeconds: ").append( gameSeconds )
|
||||
|
@ -297,6 +298,22 @@ public class CurGameInfo implements Serializable {
|
|||
}
|
||||
}
|
||||
|
||||
public CurGameInfo addDefaults( Context context, boolean standalone )
|
||||
{
|
||||
setLang( context, null );
|
||||
nPlayers = 2;
|
||||
players[0] = new LocalPlayer( context, 0 );
|
||||
players[1] = new LocalPlayer( context, 1 );
|
||||
if ( standalone ) {
|
||||
players[1].setIsRobot( true );
|
||||
} else {
|
||||
players[1].isLocal = false;
|
||||
}
|
||||
setServerRole( standalone ?
|
||||
DeviceRole.SERVER_STANDALONE : DeviceRole.SERVER_ISSERVER );
|
||||
return this;
|
||||
}
|
||||
|
||||
/** return true if any of the changes made would invalide a game
|
||||
* in progress, i.e. require that it be restarted with the new
|
||||
* params. E.g. changing a player to a robot is harmless for a
|
||||
|
|
|
@ -233,7 +233,19 @@ public class JNIThread extends Thread implements AutoCloseable {
|
|||
utils, null, cp, m_xport );
|
||||
}
|
||||
if ( null == m_jniGamePtr ) {
|
||||
m_jniGamePtr = XwJNI.initNew( m_gi, utils, null, cp, m_xport );
|
||||
// I don't think games get created here. If they do, I
|
||||
// need to get the selfAddr from somewhere other than
|
||||
// generic defaults, as the user should have configured an
|
||||
// address for the game.
|
||||
Assert.assertTrueNR( m_gi.serverRole != DeviceRole.SERVER_ISCLIENT );
|
||||
Assert.failDbg();
|
||||
CommsAddrRec selfAddr = null;
|
||||
CommsAddrRec hostAddr = null;
|
||||
if ( m_gi.serverRole == DeviceRole.SERVER_ISSERVER ) {
|
||||
selfAddr = CommsAddrRec.getSelfAddr( context );
|
||||
}
|
||||
m_jniGamePtr = XwJNI.initNew( m_gi, selfAddr, hostAddr,
|
||||
utils, null, cp, m_xport );
|
||||
}
|
||||
Assert.assertNotNull( m_jniGamePtr );
|
||||
notifyAll();
|
||||
|
|
|
@ -105,7 +105,8 @@ public class LocalPlayer implements Serializable {
|
|||
{
|
||||
String result;
|
||||
if ( BuildConfig.DEBUG ) {
|
||||
result = String.format( "{name: %s, isLocal: %b}", name, isLocal );
|
||||
result = String.format( "{name: %s, isLocal: %b, robotIQ: %d}",
|
||||
name, isLocal, robotIQ );
|
||||
} else {
|
||||
result = super.toString();
|
||||
}
|
||||
|
|
|
@ -33,6 +33,7 @@ import org.eehouse.android.xw4.Quarantine;
|
|||
import org.eehouse.android.xw4.Utils.ISOCode;
|
||||
import org.eehouse.android.xw4.Utils;
|
||||
import org.eehouse.android.xw4.jni.CommsAddrRec.CommsConnType;
|
||||
import org.eehouse.android.xw4.jni.CurGameInfo.DeviceRole;
|
||||
|
||||
// Collection of native methods and a bit of state
|
||||
public class XwJNI {
|
||||
|
@ -287,7 +288,6 @@ public class XwJNI {
|
|||
return haveLocaleToLc( isoCode.toString(), lc );
|
||||
}
|
||||
|
||||
public static native CommsAddrRec comms_getInitialAddr();
|
||||
public static native String comms_getUUID();
|
||||
public static native String lcToLocale( int lc );
|
||||
public static native boolean haveLocaleToLc( String isoCodeStr, int[] lc );
|
||||
|
@ -317,11 +317,12 @@ public class XwJNI {
|
|||
}
|
||||
|
||||
public static synchronized GamePtr
|
||||
initNew( CurGameInfo gi, UtilCtxt util, DrawCtx draw,
|
||||
CommonPrefs cp, TransportProcs procs )
|
||||
initNew( CurGameInfo gi, CommsAddrRec selfAddr, CommsAddrRec hostAddr,
|
||||
UtilCtxt util, DrawCtx draw, CommonPrefs cp, TransportProcs procs )
|
||||
{
|
||||
Assert.assertTrueNR( null != selfAddr || gi.serverRole == DeviceRole.SERVER_STANDALONE );
|
||||
GamePtr gamePtr = initGameJNI( 0 );
|
||||
game_makeNewGame( gamePtr, gi, util, draw, cp, procs );
|
||||
game_makeNewGame( gamePtr, gi, selfAddr, hostAddr, util, draw, cp, procs );
|
||||
return gamePtr;
|
||||
}
|
||||
|
||||
|
@ -333,6 +334,8 @@ public class XwJNI {
|
|||
|
||||
private static native void game_makeNewGame( GamePtr gamePtr,
|
||||
CurGameInfo gi,
|
||||
CommsAddrRec selfAddr,
|
||||
CommsAddrRec hostAddr,
|
||||
UtilCtxt util,
|
||||
DrawCtx draw, CommonPrefs cp,
|
||||
TransportProcs procs );
|
||||
|
@ -498,9 +501,9 @@ public class XwJNI {
|
|||
public static native void comms_start( GamePtr gamePtr );
|
||||
public static native void comms_stop( GamePtr gamePtr );
|
||||
public static native void comms_resetSame( GamePtr gamePtr );
|
||||
public static native CommsAddrRec comms_getAddr( GamePtr gamePtr );
|
||||
public static native CommsAddrRec comms_getSelfAddr( GamePtr gamePtr );
|
||||
public static native CommsAddrRec comms_getHostAddr( GamePtr gamePtr );
|
||||
public static native CommsAddrRec[] comms_getAddrs( GamePtr gamePtr );
|
||||
public static native void comms_augmentHostAddr( GamePtr gamePtr, CommsAddrRec addr );
|
||||
public static native void comms_dropHostAddr( GamePtr gamePtr, CommsConnType typ );
|
||||
public static native int comms_resendAll( GamePtr gamePtr, boolean force,
|
||||
CommsConnType filter,
|
||||
|
|
|
@ -603,6 +603,7 @@ addrTypesToJ( JNIEnv* env, const CommsAddrRec* addr )
|
|||
void
|
||||
getJAddrRec( JNIEnv* env, CommsAddrRec* addr, jobject jaddr )
|
||||
{
|
||||
XP_MEMSET( addr, 0, sizeof(*addr) );
|
||||
/* Iterate over types in the set in jaddr, and for each call
|
||||
addr_addType() and then copy in the types. */
|
||||
jobject jtypeset = getObjectField( env, jaddr, "conTypes",
|
||||
|
|
|
@ -959,17 +959,6 @@ Java_org_eehouse_android_xw4_jni_XwJNI_nli_1from_1stream
|
|||
return jnli;
|
||||
}
|
||||
|
||||
JNIEXPORT jobject JNICALL
|
||||
Java_org_eehouse_android_xw4_jni_XwJNI_comms_1getInitialAddr
|
||||
( JNIEnv* env, jclass C )
|
||||
{
|
||||
CommsAddrRec addr;
|
||||
comms_getInitialAddr( &addr );
|
||||
jobject jaddr = makeObjectEmptyConst( env, PKG_PATH("jni/CommsAddrRec") );
|
||||
setJAddrRec( env, jaddr, &addr );
|
||||
return jaddr;
|
||||
}
|
||||
|
||||
JNIEXPORT jstring JNICALL
|
||||
Java_org_eehouse_android_xw4_jni_XwJNI_comms_1getUUID
|
||||
( JNIEnv* env, jclass C )
|
||||
|
@ -1388,8 +1377,8 @@ Java_org_eehouse_android_xw4_jni_XwJNI_envDone
|
|||
JNIEXPORT void JNICALL
|
||||
Java_org_eehouse_android_xw4_jni_XwJNI_game_1makeNewGame
|
||||
( JNIEnv* env, jclass C, GamePtrType gamePtr, jobject j_gi,
|
||||
jobject j_util, jobject j_draw, jobject j_cp,
|
||||
jobject j_procs )
|
||||
jobject j_selfAddr, jobject j_hostAddr, jobject j_util, jobject j_draw,
|
||||
jobject j_cp, jobject j_procs )
|
||||
{
|
||||
XWJNI_START_GLOBALS();
|
||||
CurGameInfo* gi = makeGI( MPPARM(mpool) env, j_gi );
|
||||
|
@ -1410,8 +1399,25 @@ Java_org_eehouse_android_xw4_jni_XwJNI_game_1makeNewGame
|
|||
CommonPrefs cp = {0};
|
||||
loadCommonPrefs( env, &cp, j_cp );
|
||||
|
||||
game_makeNewGame( MPPARM(mpool) env, &state->game, gi,
|
||||
globals->util, dctx, &cp, globals->xportProcs );
|
||||
CommsAddrRec selfAddr;
|
||||
CommsAddrRec* selfAddrP = NULL;
|
||||
if ( !!j_selfAddr ) {
|
||||
getJAddrRec( env, &selfAddr, j_selfAddr );
|
||||
selfAddrP = &selfAddr;
|
||||
}
|
||||
|
||||
CommsAddrRec hostAddr;
|
||||
CommsAddrRec* hostAddrP = NULL;
|
||||
if ( !!j_hostAddr ) {
|
||||
XP_ASSERT( gi->serverRole == SERVER_ISCLIENT );
|
||||
getJAddrRec( env, &hostAddr, j_hostAddr );
|
||||
hostAddrP = &hostAddr;
|
||||
} else {
|
||||
XP_ASSERT( gi->serverRole != SERVER_ISCLIENT );
|
||||
}
|
||||
|
||||
game_makeNewGame( MPPARM(mpool) env, &state->game, gi, selfAddrP,
|
||||
hostAddrP, globals->util, dctx, &cp, globals->xportProcs );
|
||||
XWJNI_END();
|
||||
} /* makeNewGame */
|
||||
|
||||
|
@ -2163,20 +2169,37 @@ Java_org_eehouse_android_xw4_jni_XwJNI_comms_1resetSame
|
|||
}
|
||||
|
||||
JNIEXPORT jobject JNICALL
|
||||
Java_org_eehouse_android_xw4_jni_XwJNI_comms_1getAddr
|
||||
Java_org_eehouse_android_xw4_jni_XwJNI_comms_1getSelfAddr
|
||||
(JNIEnv* env, jclass C, GamePtrType gamePtr )
|
||||
{
|
||||
jobject jaddr;
|
||||
XWJNI_START();
|
||||
XP_ASSERT( state->game.comms );
|
||||
CommsAddrRec addr;
|
||||
comms_getAddr( state->game.comms, &addr );
|
||||
comms_getSelfAddr( state->game.comms, &addr );
|
||||
jaddr = makeObjectEmptyConst( env, PKG_PATH("jni/CommsAddrRec") );
|
||||
setJAddrRec( env, jaddr, &addr );
|
||||
XWJNI_END();
|
||||
return jaddr;
|
||||
}
|
||||
|
||||
JNIEXPORT jobject JNICALL
|
||||
Java_org_eehouse_android_xw4_jni_XwJNI_comms_1getHostAddr
|
||||
(JNIEnv* env, jclass C, GamePtrType gamePtr )
|
||||
{
|
||||
LOG_FUNC();
|
||||
jobject jaddr = NULL;
|
||||
XWJNI_START();
|
||||
XP_ASSERT( state->game.comms );
|
||||
CommsAddrRec addr;
|
||||
if ( comms_getHostAddr( state->game.comms, &addr ) ) {
|
||||
jaddr = makeObjectEmptyConst( env, PKG_PATH("jni/CommsAddrRec") );
|
||||
setJAddrRec( env, jaddr, &addr );
|
||||
}
|
||||
XWJNI_END();
|
||||
return jaddr;
|
||||
}
|
||||
|
||||
JNIEXPORT jobjectArray JNICALL
|
||||
Java_org_eehouse_android_xw4_jni_XwJNI_comms_1getAddrs
|
||||
( JNIEnv* env, jclass C, GamePtrType gamePtr )
|
||||
|
@ -2265,7 +2288,7 @@ Java_org_eehouse_android_xw4_jni_XwJNI_game_1summarize
|
|||
setInt( env, jsummary, "nPacketsPending", summary.nPacketsPending );
|
||||
|
||||
CommsAddrRec addr;
|
||||
comms_getAddr( comms, &addr );
|
||||
comms_getSelfAddr( comms, &addr );
|
||||
setTypeSetFieldIn( env, &addr, jsummary, "conTypes" );
|
||||
|
||||
CommsConnType typ;
|
||||
|
|
|
@ -1159,6 +1159,20 @@ comms_getSelfAddr( const CommsCtxt* comms, CommsAddrRec* addr )
|
|||
XP_MEMCPY( addr, &comms->selfAddr, sizeof(*addr) );
|
||||
} /* comms_getAddr */
|
||||
|
||||
XP_Bool
|
||||
comms_getHostAddr( const CommsCtxt* comms, CommsAddrRec* addr )
|
||||
{
|
||||
XP_ASSERT( !!comms );
|
||||
XP_Bool haveAddr = !comms->isServer
|
||||
&& !!comms->recs
|
||||
&& !comms->recs->next
|
||||
;
|
||||
if ( haveAddr ) {
|
||||
XP_MEMCPY( addr, &comms->recs->addr, sizeof(*addr) );
|
||||
}
|
||||
return haveAddr;
|
||||
} /* comms_getAddr */
|
||||
|
||||
void
|
||||
comms_augmentHostAddr( CommsCtxt* comms, XWEnv xwe, const CommsAddrRec* addr )
|
||||
{
|
||||
|
|
|
@ -168,7 +168,7 @@ void comms_destroy( CommsCtxt* comms, XWEnv xwe );
|
|||
void comms_setConnID( CommsCtxt* comms, XP_U32 connID );
|
||||
|
||||
void comms_getSelfAddr( const CommsCtxt* comms, CommsAddrRec* selfAddr );
|
||||
void comms_getHostAddr( const CommsCtxt* comms, CommsAddrRec* hostAddr );
|
||||
XP_Bool comms_getHostAddr( const CommsCtxt* comms, CommsAddrRec* hostAddr );
|
||||
void comms_augmentHostAddr( CommsCtxt* comms, XWEnv xwe, const CommsAddrRec* addr );
|
||||
void comms_addMQTTDevID( CommsCtxt* comms, XP_PlayerAddr channelNo,
|
||||
const MQTTDevID* devID );
|
||||
|
|
Loading…
Reference in a new issue