Save and restore per-player dicts; load games that have 'em correctly.

Robots default to BasEnglish dict and humans to CollegeEng.  Add new
per-game default for robot dict.  Still need to deal with language
changes and non-English case in general.
This commit is contained in:
Andy2 2011-04-11 06:42:17 -07:00
parent 96254ad8b5
commit c6cd60deef
12 changed files with 156 additions and 53 deletions

View file

@ -10,7 +10,7 @@ local_C_INCLUDES+= \
local_LDLIBS += -llog
# local_DEBUG = -DMEM_DEBUG -DDEBUG -DENABLE_LOGGING
local_DEBUG = -DMEM_DEBUG -DDEBUG -DENABLE_LOGGING
local_DEFINES += \
$(local_DEBUG) \
-DXWFEATURE_RELAY \

View file

@ -422,6 +422,30 @@ and_dictionary_make_empty( MPFORMAL JNIEnv* env, JNIUtilCtxt* jniutil )
return (DictionaryCtxt*)anddict;
}
void
makeDicts( MPFORMAL JNIEnv *env, JNIUtilCtxt* jniutil, PlayerDicts* dicts,
jobjectArray jdicts, jobjectArray jnames )
{
LOG_FUNC();
int ii;
jsize len = (*env)->GetArrayLength( env, jdicts );
XP_ASSERT( len == (*env)->GetArrayLength( env, jnames ) );
for ( ii = 0; ii < VSIZE(dicts->dicts); ++ii ) {
DictionaryCtxt* dict = NULL;
if ( ii < len ) {
jobject jdict = (*env)->GetObjectArrayElement( env, jdicts, ii );
jstring jname = (*env)->GetObjectArrayElement( env, jnames, ii );
dict = makeDict( MPPARM(mpool) env, jniutil, jdict, jname );
XP_ASSERT( !!dict );
(*env)->DeleteLocalRef( env, jdict );
(*env)->DeleteLocalRef( env, jname );
}
dicts->dicts[ii] = dict;
}
LOG_RETURN_VOID();
}
DictionaryCtxt*
makeDict( MPFORMAL JNIEnv *env, JNIUtilCtxt* jniutil, jbyteArray jbytes,
jstring jname )
@ -449,3 +473,16 @@ makeDict( MPFORMAL JNIEnv *env, JNIUtilCtxt* jniutil, jbyteArray jbytes,
return (DictionaryCtxt*)anddict;
}
void
destroyDicts( PlayerDicts* dicts )
{
DictionaryCtxt** ctxts = dicts->dicts;
for ( ; ; ) {
if ( !*ctxts ) {
break;
}
dict_destroy( *ctxts );
++ctxts;
}
}

View file

@ -31,6 +31,11 @@ dict_splitFaces( DictionaryCtxt* dict, const XP_U8* bytes,
DictionaryCtxt* makeDict( MPFORMAL JNIEnv *env, JNIUtilCtxt* jniutil,
jbyteArray bytes, jstring jname );
void makeDicts( MPFORMAL JNIEnv *env, JNIUtilCtxt* jniutil, PlayerDicts* dicts,
jobjectArray jdicts, jobjectArray jnames );
void destroyDicts( PlayerDicts* dicts );
DictionaryCtxt* and_dictionary_make_empty( MPFORMAL JNIEnv *env,
JNIUtilCtxt* jniutil );

View file

@ -380,7 +380,7 @@ JNIEXPORT void JNICALL
Java_org_eehouse_android_xw4_jni_XwJNI_game_1makeNewGame
( JNIEnv* env, jclass C, jint gamePtr, jobject j_gi, jobject j_util,
jobject jniu, jobject j_draw, jobject j_cp, jobject j_procs,
jbyteArray jDictBytes, jstring jDictName )
jobjectArray j_dicts, jobjectArray j_names )
{
XWJNI_START_GLOBALS();
CurGameInfo* gi = makeGI( MPPARM(mpool) env, j_gi );
@ -398,16 +398,15 @@ Java_org_eehouse_android_xw4_jni_XwJNI_game_1makeNewGame
game_makeNewGame( MPPARM(mpool) &state->game, gi, globals->util, dctx, &cp,
globals->xportProcs );
DictionaryCtxt* dict = makeDict( MPPARM(mpool) env, globals->jniutil,
jDictBytes, jDictName );
PlayerDicts dicts;
makeDicts( MPPARM(mpool) env, globals->jniutil, &dicts, j_dicts, j_names );
#ifdef STUBBED_DICT
if ( !dict ) {
XP_LOGF( "falling back to stubbed dict" );
dict = make_stubbed_dict( MPPARM_NOCOMMA(mpool) );
}
#endif
XP_ASSERT( !!dict );
model_setDictionary( state->game.model, dict );
model_setPlayerDicts( state->game.model, &dicts );
XWJNI_END();
} /* makeNewGame */
@ -439,19 +438,19 @@ JNIEXPORT void JNICALL Java_org_eehouse_android_xw4_jni_XwJNI_game_1dispose
JNIEXPORT jboolean JNICALL
Java_org_eehouse_android_xw4_jni_XwJNI_game_1makeFromStream
( JNIEnv* env, jclass C, jint gamePtr, jbyteArray jstream,
jobject /*out*/jgi, jbyteArray jdict, jstring jdictName, jobject jutil,
jobject jniu, jobject jdraw, jobject jcp, jobject jprocs )
( JNIEnv* env, jclass C, jint gamePtr, jbyteArray jstream, jobject /*out*/jgi,
jobjectArray jdicts, jobjectArray jdictNames, jobject jutil, jobject jniu,
jobject jdraw, jobject jcp, jobject jprocs )
{
jboolean result;
PlayerDicts dicts;
XWJNI_START_GLOBALS();
globals->gi = (CurGameInfo*)XP_CALLOC( mpool, sizeof(*globals->gi) );
globals->util = makeUtil( MPPARM(mpool) &state->env,
jutil, globals->gi, globals );
globals->jniutil = makeJNIUtil( MPPARM(mpool) &state->env, jniu );
DictionaryCtxt* dict = makeDict( MPPARM(mpool) env, globals->jniutil,
jdict, jdictName );
makeDicts( MPPARM(mpool) env, globals->jniutil, &dicts, jdicts, jdictNames );
globals->dctx = makeDraw( MPPARM(mpool) &state->env, jdraw );
globals->xportProcs = makeXportProcs( MPPARM(mpool) &state->env, jprocs );
@ -461,7 +460,7 @@ Java_org_eehouse_android_xw4_jni_XwJNI_game_1makeFromStream
CommonPrefs cp;
loadCommonPrefs( env, &cp, jcp );
result = game_makeFromStream( MPPARM(mpool) stream, &state->game,
globals->gi, dict, NULL,
globals->gi, NULL, &dicts,
globals->util, globals->dctx, &cp,
globals->xportProcs );
stream_destroy( stream );
@ -474,8 +473,7 @@ Java_org_eehouse_android_xw4_jni_XwJNI_game_1makeFromStream
} else {
destroyDraw( &globals->dctx );
destroyXportProcs( &globals->xportProcs );
dict_destroy( dict );
dict = NULL;
destroyDicts( &dicts );
destroyUtil( &globals->util );
destroyJNIUtil( &globals->jniutil );
destroyGI( MPPARM(mpool) &globals->gi );

View file

@ -37,6 +37,7 @@
<string name="key_board_size">key_board_size</string>
<string name="key_initial_player_minutes">key_initial_player_minutes</string>
<string name="key_default_dict">key_default_dict</string>
<string name="key_default_robodict">key_default_robodict</string>
<string name="key_default_phonies">key_default_phonies2</string>
<string name="key_default_timerenabled">key_default_timerenabled</string>
<string name="key_connect_frequency">key_connect_frequency</string>

View file

@ -269,7 +269,9 @@
<string name="network_behavior_summary">Settings that apply to
networked games</string>
<string name="default_dict">Game dictionary</string>
<string name="default_dict">Dictionary for humans</string>
<string name="default_robodict">Dictionary for robots</string>
<string name="default_phonies">Handle phonies</string>
<string name="phonies_spinner_prompt">How to handle \"phonies\"
(words not in dictionary)</string>

View file

@ -13,6 +13,12 @@
android:defaultValue="CollegeEng_2to8"
/>
<org.eehouse.android.xw4.DictListPreference
android:key="@string/key_default_robodict"
android:title="@string/default_robodict"
android:defaultValue="BasEnglish2to8"
/>
<CheckBoxPreference android:key="@string/key_init_hintsallowed"
android:title="@string/hints_allowed"
android:summary="@string/hints_allowed_sum"

View file

@ -995,8 +995,8 @@ public class BoardActivity extends XWActivity
XwJNI.gi_from_stream( m_gi, stream );
Utils.logf( "loadGame: dict name: %s", m_gi.dictName );
byte[] dictBytes = GameUtils.openDict( this, m_gi.dictName );
Assert.assertNotNull( dictBytes );
String[] dictNames = m_gi.dictNames();
byte[][] dictBytes = GameUtils.openDicts( this, dictNames );
m_jniGamePtr = XwJNI.initJNI();
if ( m_gi.serverRole != DeviceRole.SERVER_STANDALONE ) {
@ -1007,12 +1007,12 @@ public class BoardActivity extends XWActivity
CommonPrefs cp = CommonPrefs.get( this );
if ( null == stream ||
! XwJNI.game_makeFromStream( m_jniGamePtr, stream,
m_gi, dictBytes,
m_gi.dictName, m_utils, m_jniu,
m_gi, dictBytes, dictNames,
m_utils, m_jniu,
m_view, cp, m_xport ) ) {
XwJNI.game_makeNewGame( m_jniGamePtr, m_gi, m_utils, m_jniu,
m_view, cp, m_xport,
dictBytes, m_gi.dictName );
dictBytes, dictNames );
}
m_jniThread = new

View file

@ -172,7 +172,8 @@ public class GameUtils {
// loadMakeGame, if makinga new game, will add comms as long
// as DeviceRole.SERVER_STANDALONE != gi.serverRole
loadMakeGame( context, gamePtr, gi, lockSrc );
byte[] dictBytes = GameUtils.openDict( context, gi.dictName );
String[] dictNames = gi.dictNames();
byte[][] dictBytes = openDicts( context, dictNames );
if ( XwJNI.game_hasComms( gamePtr ) ) {
addr = new CommsAddrRec( context );
@ -190,7 +191,7 @@ public class GameUtils {
gamePtr = XwJNI.initJNI();
XwJNI.game_makeNewGame( gamePtr, gi, JNIUtilsImpl.get(),
CommonPrefs.get( context ), dictBytes,
gi.dictName );
dictNames );
if ( null != addr ) {
XwJNI.comms_setAddr( gamePtr, addr );
}
@ -285,17 +286,18 @@ public class GameUtils {
{
byte[] stream = savedGame( context, lock );
XwJNI.gi_from_stream( gi, stream );
byte[] dictBytes = GameUtils.openDict( context, gi.dictName );
String[] dictNames = gi.dictNames();
byte[][] dictBytes = openDicts( context, dictNames );
boolean madeGame = XwJNI.game_makeFromStream( gamePtr, stream,
JNIUtilsImpl.get(), gi,
dictBytes, gi.dictName,
dictBytes, dictNames,
util,
CommonPrefs.get(context));
if ( !madeGame ) {
XwJNI.game_makeNewGame( gamePtr, gi, JNIUtilsImpl.get(),
CommonPrefs.get(context), dictBytes,
gi.dictName );
dictNames );
}
}
@ -486,6 +488,22 @@ public class GameUtils {
return bytes;
}
public static byte[][] openDicts( Context context, String[] names )
{
byte[][] result = new byte[names.length][];
HashMap<String,byte[]> seen = new HashMap<String,byte[]>();
for ( int ii = 0; ii < names.length; ++ii ) {
String name = names[ii];
byte[] bytes = seen.get( name );
if ( null == bytes ) {
bytes = openDict( context, name );
seen.put( name, bytes );
}
result[ii] = bytes;
}
return result;
}
public static boolean saveDict( Context context, String name, InputStream in )
{
boolean success = false;
@ -618,23 +636,24 @@ public class GameUtils {
public static void replaceDict( Context context, String path,
String dict )
{
GameLock lock = new GameLock( path, true ).lock();
byte[] stream = savedGame( context, lock );
CurGameInfo gi = new CurGameInfo( context );
byte[] dictBytes = GameUtils.openDict( context, dict );
Assert.fail();
// GameLock lock = new GameLock( path, true ).lock();
// byte[] stream = savedGame( context, lock );
// CurGameInfo gi = new CurGameInfo( context );
// byte[] dictBytes = openDict( context, dict );
int gamePtr = XwJNI.initJNI();
XwJNI.game_makeFromStream( gamePtr, stream,
JNIUtilsImpl.get(), gi,
dictBytes, dict,
CommonPrefs.get( context ) );
gi.dictName = dict;
// int gamePtr = XwJNI.initJNI();
// XwJNI.game_makeFromStream( gamePtr, stream,
// JNIUtilsImpl.get(), gi,
// dictBytes, dict,
// CommonPrefs.get( context ) );
// gi.dictName = dict;
saveGame( context, gamePtr, gi, lock, false );
// saveGame( context, gamePtr, gi, lock, false );
summarizeAndClose( context, lock, gamePtr, gi );
// summarizeAndClose( context, lock, gamePtr, gi );
lock.unlock();
// lock.unlock();
}
public static void applyChanges( Context context, CurGameInfo gi,
@ -645,7 +664,8 @@ public class GameUtils {
// somesuch. But: do we have a way to save changes to a gi
// that don't reset the game, e.g. player name for standalone
// games?
byte[] dictBytes = GameUtils.openDict( context, gi.dictName );
String[] dictNames = gi.dictNames();
byte[][] dictBytes = openDicts( context, dictNames );
int gamePtr = XwJNI.initJNI();
boolean madeGame = false;
CommonPrefs cp = CommonPrefs.get( context );
@ -653,18 +673,18 @@ public class GameUtils {
if ( forceNew ) {
tellRelayDied( context, lock, true );
} else {
byte[] stream = GameUtils.savedGame( context, lock );
byte[] stream = savedGame( context, lock );
// Will fail if there's nothing in the stream but a gi.
madeGame = XwJNI.game_makeFromStream( gamePtr, stream,
JNIUtilsImpl.get(),
new CurGameInfo(context),
dictBytes, gi.dictName, cp );
dictBytes, dictNames, cp );
}
if ( forceNew || !madeGame ) {
gi.setInProgress( false );
XwJNI.game_makeNewGame( gamePtr, gi, JNIUtilsImpl.get(),
cp, dictBytes, gi.dictName );
cp, dictBytes, dictNames );
}
if ( null != car ) {

View file

@ -201,7 +201,7 @@ public class CommonPrefs {
}
}
public static String getDefaultDict( Context context )
public static String getDefaultHumanDict( Context context )
{
String value = getString( context, R.string.key_default_dict );
if ( value.equals("") || !GameUtils.dictExists( context, value ) ) {
@ -210,6 +210,15 @@ public class CommonPrefs {
return value;
}
public static String getDefaultRobotDict( Context context )
{
String value = getString( context, R.string.key_default_robodict );
if ( value.equals("") || !GameUtils.dictExists( context, value ) ) {
value = getDefaultHumanDict( context );
}
return value;
}
public static CurGameInfo.XWPhoniesChoice
getDefaultPhonies( Context context )
{

View file

@ -74,7 +74,7 @@ public class CurGameInfo {
players = new LocalPlayer[MAX_NUM_PLAYERS];
serverRole = isNetworked ? DeviceRole.SERVER_ISCLIENT
: DeviceRole.SERVER_STANDALONE;
dictName = CommonPrefs.getDefaultDict( context );
dictName = CommonPrefs.getDefaultHumanDict( context );
dictLang = DictLangCache.getDictLangCode( context, dictName );
hintsNotAllowed = !CommonPrefs.getDefaultHintsAllowed( context );
phoniesAction = CommonPrefs.getDefaultPhonies( context );
@ -235,6 +235,17 @@ public class CurGameInfo {
return names;
}
public String[] dictNames()
{
assignDicts();
String[] result = new String[nPlayers];
for ( int ii = 0; ii < nPlayers; ++ii ) {
result[ii] = players[ii].dictName;
}
return result;
}
public boolean addPlayer()
{
boolean added = nPlayers < MAX_NUM_PLAYERS;
@ -306,4 +317,18 @@ public class CurGameInfo {
}
return canJuggle;
}
private void assignDicts()
{
String humanDict = CommonPrefs.getDefaultHumanDict( m_context );
int lang = DictLangCache.getDictLangCode( m_context, humanDict );
Assert.assertTrue( lang == dictLang );
String robotDict = CommonPrefs.getDefaultRobotDict( m_context );
for ( int ii = 0; ii < nPlayers; ++ii ) {
LocalPlayer lp = players[ii];
if ( null == lp.dictName ) {
lp.dictName = lp.isRobot() ? robotDict : humanDict;
}
}
}
}

View file

@ -59,13 +59,13 @@ public class XwJNI {
JNIUtils jniu,
DrawCtx draw, CommonPrefs cp,
TransportProcs procs,
byte[] dict, String dictName );
byte[][] dicts, String[] dictNames );
public static native boolean game_makeFromStream( int gamePtr,
byte[] stream,
CurGameInfo gi,
byte[] dict,
String dictName,
byte[][] dicts,
String[] dictNames,
UtilCtxt util,
JNIUtils jniu,
DrawCtx draw,
@ -76,19 +76,19 @@ public class XwJNI {
// played
public static void game_makeNewGame( int gamePtr, CurGameInfo gi,
JNIUtils jniu, CommonPrefs cp,
byte[] dict, String dictName ) {
byte[][] dicts, String[] dictNames ) {
game_makeNewGame( gamePtr, gi, (UtilCtxt)null, jniu,
(DrawCtx)null, cp, (TransportProcs)null,
dict, dictName );
dicts, dictNames );
}
public static boolean game_makeFromStream( int gamePtr,
byte[] stream,
JNIUtils jniu,
CurGameInfo gi,
byte[] dict, String dictName,
byte[][] dicts, String[] dictNames,
CommonPrefs cp ) {
return game_makeFromStream( gamePtr, stream, gi, dict, dictName,
return game_makeFromStream( gamePtr, stream, gi, dicts, dictNames,
(UtilCtxt)null, jniu, (DrawCtx)null, cp,
(TransportProcs)null );
}
@ -97,10 +97,10 @@ public class XwJNI {
byte[] stream,
JNIUtils jniu,
CurGameInfo gi,
byte[] dict, String dictName,
byte[][] dicts, String[] dictNames,
UtilCtxt util,
CommonPrefs cp ) {
return game_makeFromStream( gamePtr, stream, gi, dict, dictName,
return game_makeFromStream( gamePtr, stream, gi, dicts, dictNames,
util, jniu, (DrawCtx)null, cp,
(TransportProcs)null );
}