From cd89885cd28e8d021589dd2da7e17057e4b4c6f2 Mon Sep 17 00:00:00 2001 From: Eric House Date: Wed, 23 May 2012 19:58:03 -0700 Subject: [PATCH] add new periodic (one-second) timer, and commandline option that sets what percent of the times that timer fires will result in a move being undone. Will be used to interject random out-of-order undos into games played for testing. (Currently the tests fail when this is enabled; I need to fix that.) --- xwords4/linux/cursesmain.c | 2 +- xwords4/linux/gtkmain.c | 2 ++ xwords4/linux/linuxmain.c | 45 ++++++++++++++++++++++++++++++++++++++ xwords4/linux/linuxmain.h | 6 +++++ xwords4/linux/main.h | 1 + 5 files changed, 55 insertions(+), 1 deletion(-) diff --git a/xwords4/linux/cursesmain.c b/xwords4/linux/cursesmain.c index 6a05525bc..00b303565 100644 --- a/xwords4/linux/cursesmain.c +++ b/xwords4/linux/cursesmain.c @@ -1734,6 +1734,7 @@ cursesmain( XP_Bool isServer, LaunchParams* params ) #ifdef USE_GLIBLOOP cursesListenOnSocket( &g_globals, 0, handle_stdin ); + setOneSecondTimer( &g_globals.cGlobals ); #else cursesListenOnSocket( &g_globals, 0 ); /* stdin */ @@ -1764,7 +1765,6 @@ cursesmain( XP_Bool isServer, LaunchParams* params ) #endif }; - if ( !!params->pipe && !!params->fileName ) { read_pipe_then_close( &g_globals.cGlobals, &procs ); } else if ( !!params->nbs && !!params->fileName ) { diff --git a/xwords4/linux/gtkmain.c b/xwords4/linux/gtkmain.c index 3d32cf1e7..111309259 100644 --- a/xwords4/linux/gtkmain.c +++ b/xwords4/linux/gtkmain.c @@ -2388,6 +2388,8 @@ gtkmain( LaunchParams* params, int argc, char *argv[] ) g_signal_connect( GTK_OBJECT(drawing_area), "button_release_event", G_CALLBACK(button_release_event), &globals ); + setOneSecondTimer( &globals.cGlobals ); + #ifdef KEY_SUPPORT # ifdef KEYBOARD_NAV g_signal_connect( GTK_OBJECT(window), "key_press_event", diff --git a/xwords4/linux/linuxmain.c b/xwords4/linux/linuxmain.c index b823dfd6d..e5fc15a98 100644 --- a/xwords4/linux/linuxmain.c +++ b/xwords4/linux/linuxmain.c @@ -370,6 +370,36 @@ do_nbs_then_close( CommonGlobals* cGlobals, const TransportProcs* procs ) LOG_RETURN_VOID(); } /* do_nbs_then_close */ +#ifdef USE_GLIBLOOP +static gboolean +secondTimerFired( gpointer data ) +{ + CommonGlobals* cGlobals = (CommonGlobals*)data; + + /* Undo */ + XWGame* game = &cGlobals->game; + if ( !!game->server && !!game->board ) { + XP_U16 undoRatio = cGlobals->params->undoRatio; + if ( 0 != undoRatio ) { + if ( (XP_RANDOM() % 100) < undoRatio ) { + XP_LOGF( "%s: calling server_handleUndo", __func__ ); + if ( server_handleUndo( game->server ) ) { + board_draw( game->board ); + } + } + } + } + + return TRUE; +} + +void +setOneSecondTimer( CommonGlobals* cGlobals ) +{ + (void)g_timeout_add_seconds( 1, secondTimerFired, cGlobals ); +} +#endif + typedef enum { CMD_SKIP_GAMEOVER ,CMD_SHOW_OTHERSCORES @@ -436,6 +466,9 @@ typedef enum { ,CMD_SLOWROBOT ,CMD_TRADEPCT #endif +#ifdef USE_GLIBLOOP /* just because hard to implement otherwise */ + ,CMD_UNDOPCT +#endif #if defined PLATFORM_GTK && defined PLATFORM_NCURSES ,CMD_GTK ,CMD_CURSES @@ -521,6 +554,9 @@ static CmdInfoRec CmdInfoRecs[] = { ,{ CMD_SLOWROBOT, true, "slow-robot", "make robot slower to test network" } ,{ CMD_TRADEPCT, true, "trade-pct", "what pct of the time should robot trade" } #endif +#ifdef USE_GLIBLOOP + ,{ CMD_UNDOPCT, true, "undo-pct", "each second, what are the odds of doing an undo" } +#endif #if defined PLATFORM_GTK && defined PLATFORM_NCURSES ,{ CMD_GTK, false, "gtk", "use GTK for display" } ,{ CMD_CURSES, false, "curses", "use curses for display" } @@ -1592,6 +1628,15 @@ main( int argc, char** argv ) break; #endif +#ifdef USE_GLIBLOOP + case CMD_UNDOPCT: + mainParams.undoRatio = atoi( optarg ); + if ( mainParams.undoRatio < 0 || mainParams.undoRatio > 100 ) { + usage(argv[0], "must be 0 <= n <= 100" ); + } + break; +#endif + #if defined PLATFORM_GTK && defined PLATFORM_NCURSES case CMD_GTK: useCurses = XP_FALSE; diff --git a/xwords4/linux/linuxmain.h b/xwords4/linux/linuxmain.h index d9fa97760..a5322a8a4 100644 --- a/xwords4/linux/linuxmain.h +++ b/xwords4/linux/linuxmain.h @@ -81,4 +81,10 @@ void read_pipe_then_close( CommonGlobals* cGlobals, void do_nbs_then_close( CommonGlobals* cGlobals, const TransportProcs* procs ); +#ifdef USE_GLIBLOOP +void setOneSecondTimer( CommonGlobals* cGlobals ); +#else +# define setOneSecondTimer( cGlobals ) +#endif + #endif diff --git a/xwords4/linux/main.h b/xwords4/linux/main.h index 7f8465d62..e9613444b 100644 --- a/xwords4/linux/main.h +++ b/xwords4/linux/main.h @@ -58,6 +58,7 @@ typedef struct LaunchParams { XP_U16 gameSeed; XP_S16 dropNthRcvd; /* negative means use for random calc */ XP_U16 nPacketsRcvd; /* toward dropNthRcvd */ + XP_U16 undoRatio; XP_Bool askNewGame; XP_S16 quitAfter; XP_Bool sleepOnAnchor;