/// <summary> /// Start to observe a game /// </summary> /// <param name="game"> Game to observe</param> /// <param name="chessBoardControl"> Chess board control to associate with the game</param> /// <param name="iTimeOut"> Command timeout in second</param> /// <param name="iMoveTimeOut"> Command timeout in second</param> /// <param name="actionGameFinished"> Action to call when game is finished or null if none</param> /// <param name="strError"> Error if any</param> /// <returns> /// true if succeed, false if game is already defined /// </returns> public bool ObserveGame(FICSGame game, SrcChess2.ChessBoardControl chessBoardControl, int iTimeOut, int?iMoveTimeOut, Action <GameIntf, TerminationE, string> actionGameFinished, out string strError) { bool bRetVal; GameIntf gameIntf; Action <GameIntf> actionMoveTimeOut; if (chessBoardControl == null) { throw new ArgumentNullException(); } if (iMoveTimeOut == 0) { actionMoveTimeOut = null; } else { actionMoveTimeOut = GetTimeOutAction(); } gameIntf = new GameIntf(game, chessBoardControl, iMoveTimeOut, actionMoveTimeOut, actionGameFinished); #if DEBUG iTimeOut = 3600; #endif bRetVal = ObserveGame(gameIntf, iTimeOut, out strError); return(bRetVal); }
/// <summary> /// Start a background game /// </summary> /// <param name="conn"> Connection with FICS server</param> /// <param name="chessBoardCtl">Chess board control use to display the games</param> public static void StartBackgroundGame(FICSConnection conn, SrcChess2.ChessBoardControl chessBoardCtl) { Action action; action = () => { BackgroundGame(conn, chessBoardCtl); }; Task.Factory.StartNew(action); }
/// <summary> /// Ctor /// </summary> /// <param name="game"> FICS Game</param> /// <param name="chessBoardCtl"> Chess board control</param> /// <param name="iMoveTimeout"> Move timeout in second</param> /// <param name="actionMoveTimeOut"> Action to call if move timeout</param> /// <param name="actionGameFinished"> Action to do when game is finished</param> public GameIntf(FICSGame game, SrcChess2.ChessBoardControl chessBoardCtl, int?iMoveTimeout, Action <GameIntf> actionMoveTimeOut, Action <GameIntf, TerminationE, string> actionGameFinished) { Game = game; ChessBoardCtl = chessBoardCtl; BoardCreated = false; m_chessBoard = new SrcChess2.ChessBoard(); m_parser = new SrcChess2.PgnParser(m_chessBoard); m_queueMove = new Queue <Style12MoveLine>(16); m_spanTotalWhiteTime = TimeSpan.Zero; m_spanTotalBlackTime = TimeSpan.Zero; m_spanOriginalMaxTime = (game.PlayerTimeInMin == 0) ? (TimeSpan?)null : TimeSpan.FromMinutes(game.PlayerTimeInMin); m_listInitialMoves = new List <SrcChess2.MoveExt>(128); m_actionGameFinished = actionGameFinished; Termination = TerminationE.None; m_iMoveTimeOut = iMoveTimeout.HasValue ? iMoveTimeout.Value : int.MaxValue; m_pgnGame = new SrcChess2.PgnGame(true /*bAttrList*/, true /*bMoveList*/); m_pgnGame.attrs.Add("Event", "FICS Game " + game.GameId.ToString()); m_pgnGame.attrs.Add("Site", "FICS Server"); m_pgnGame.attrs.Add("White", game.WhitePlayer); m_pgnGame.attrs.Add("Black", game.BlackPlayer); if (game.PlayerTimeInMin != 0 && chessBoardCtl != null) { chessBoardCtl.GameTimer.MaxWhitePlayTime = TimeSpan.FromMinutes(game.PlayerTimeInMin); chessBoardCtl.GameTimer.MaxBlackPlayTime = TimeSpan.FromMinutes(game.PlayerTimeInMin); chessBoardCtl.GameTimer.MoveIncInSec = game.IncTimeInSec; } if (iMoveTimeout != 0 && actionMoveTimeOut != null) { m_timerMoveTimeout = new System.Threading.Timer(TimerCallback, actionMoveTimeOut, System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite); } }
/// <summary> /// Find a game using its attached chess board control /// </summary> /// <param name="chessBoardControl"> Chess board control</param> /// <returns> /// Game or null if not found /// </returns> public GameIntf FindGameIntf(SrcChess2.ChessBoardControl chessBoardControl) { GameIntf gameIntfRetVal = null; lock (m_dictGameIntf) { gameIntfRetVal = m_dictGameIntf.Values.FirstOrDefault(x => x.ChessBoardCtl == chessBoardControl); } return(gameIntfRetVal); }
/// <summary> /// Ctor /// </summary> /// <param name="game"> Game</param> /// <param name="chessBoardControl"> Chess board control if any</param> /// <param name="streamLog"> Stream where to send the log information</param> /// <param name="eventWaitHandle"> Use to inform background tester the game is terminated</param> /// <param name="iMoveTimeOut"> Move timeout in second</param> /// <param name="actionMoveTimeOut"> Action to call if move timeout</param> public GameIntfTest(FICSGame game, SrcChess2.ChessBoardControl chessBoardControl, System.IO.StreamWriter streamLog, EventWaitHandle eventWaitHandle, int iMoveTimeOut, Action <GameIntf> actionMoveTimeOut) : base(game, chessBoardControl, iMoveTimeOut, actionMoveTimeOut, null /*actionGameTerminated*/) { m_streamLog = streamLog; EventWaitHandle = eventWaitHandle; }
/// <summary> /// Ctor /// </summary> /// <param name="ctlMain"> Main chess board control</param> /// <param name="strHostname"> Host name</param> /// <param name="iPort"> Port number</param> /// <param name="bDebugTrace"> true to send trace to the debugging output</param> public FICSConnection(SrcChess2.ChessBoardControl ctlMain, string strHostname, int iPort, bool bDebugTrace) { m_state = new AutomatonState(); m_dictVariables = new Dictionary <string, string>(512, StringComparer.OrdinalIgnoreCase); m_setChangedSettings = new HashSet <string>(StringComparer.OrdinalIgnoreCase); m_ctlMain = ctlMain; m_connection = new TelnetConnection(bDebugTrace); m_connection.NewTextReceived += m_connection_NewTextReceived; m_connection.Connect(strHostname, iPort); }
/// <summary> /// Ctor /// </summary> /// <param name="connectionSetting"> Connection setting</param> /// <param name="ctlMain"> Main chessboard control</param> public frmConnectToFICS(SrcChess2.ChessBoardControl ctlMain, FICSConnectionSetting connectionSetting) { InitializeComponent(); m_ctlMain = ctlMain; ConnectionSetting = connectionSetting; HostName = connectionSetting.HostName; PortNumber = connectionSetting.HostPort; UserName = connectionSetting.UserName; Password = ""; IsAnonymous = string.Compare(connectionSetting.UserName, "guest", true) == 0; }
/// <summary> /// Terminate the game observation for the specified chess board control /// </summary> /// <param name="chessBoardControl"> Chess board control</param> /// <returns> /// true if found, false if not /// </returns> public bool TerminateObservation(SrcChess2.ChessBoardControl chessBoardControl) { bool bRetVal; GameIntf gameIntf; gameIntf = m_state.FindGameIntf(chessBoardControl); if (gameIntf == null) { bRetVal = false; } else { bRetVal = true; m_state.TerminateGame(gameIntf, TerminationE.TerminatedWithErr, "", "Stop by user"); } return(bRetVal); }
/// <summary> /// Start a background game /// </summary> /// <param name="conn"> Connection to FICS server</param> /// <param name="chessBoardCtl">Chess board control</param> private static void BackgroundGame(FICSConnection conn, SrcChess2.ChessBoardControl chessBoardCtl) { string strError; List <FICSGame> games; FICSGame game; EventWaitHandle eventWaitHandle; GameIntfTest gameIntf; bool bGameFound; int iLastGameId = -1; eventWaitHandle = new EventWaitHandle(false, EventResetMode.AutoReset); bGameFound = false; System.IO.FileStream stream = System.IO.File.Open("c:\\tmp\\chesslog.txt", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write, System.IO.FileShare.Read); using (stream) { stream.Seek(0, System.IO.SeekOrigin.End); System.IO.StreamWriter writer = new System.IO.StreamWriter(stream, System.Text.Encoding.UTF8); using (writer) { writer.WriteLine(); writer.WriteLine("Starting new session at " + DateTime.Now.ToString("HH:mm:ss")); writer.WriteLine("-----------------------------------------"); writer.WriteLine(); do { games = conn.GetGameList(true, 10); game = games.FirstOrDefault(x => (x.GameId != iLastGameId && !x.IsPrivate && x.GameType == FICSGame.GameTypeE.Lightning || x.GameType == FICSGame.GameTypeE.Blitz) && x.PlayerTimeInMin < 3 && x.IncTimeInSec < 5); if (game != null) { bGameFound = true; iLastGameId = game.GameId; LogWrite(writer, DateTime.Now.ToString("HH:mm:ss") + ": " + "Found game: " + game.ToString()); writer.Flush(); gameIntf = new GameIntfTest(game, chessBoardCtl, writer, eventWaitHandle, 30 /*iMoveTimeOut*/, conn.GetTimeOutAction()); eventWaitHandle.Reset(); if (conn.ObserveGame(gameIntf, 10, out strError)) { eventWaitHandle.WaitOne(); lock (writer) { writer.WriteLine("PGN Game"); writer.WriteLine("----------------------"); writer.WriteLine(gameIntf.GetPGNGame()); writer.WriteLine("----------------------"); } if (gameIntf.Termination == TerminationE.TerminatedWithErr) { lock (writer) { LogWrite(writer, DateTime.Now.ToString("HH:mm:ss") + ": " + "Game " + gameIntf.Game.GameId.ToString() + " terminated with error - " + gameIntf.TerminationError); } } else { lock (writer) { LogWrite(writer, DateTime.Now.ToString("HH:mm:ss") + ": " + "Game finished - " + gameIntf.Termination.ToString()); } } lock (writer) { writer.Flush(); } bGameFound = false; } else { lock (writer) { LogWrite(writer, "Games failed to start - " + strError ?? "???"); writer.Flush(); } Thread.Sleep(5000); } } else { lock (writer) { LogWrite(writer, "No games found - trying again in 5 sec."); writer.Flush(); } System.Threading.Thread.Sleep(5000); } } while (!bGameFound); writer.WriteLine("Session end at " + DateTime.Now.ToString("HH:mm:ss")); } } }
/// <summary> /// Ctor /// </summary> /// <param name="ctlMain"> Main chess board control</param> /// <param name="connectionSetting"> Connection setting</param> public FICSConnection(SrcChess2.ChessBoardControl ctlMain, FICSConnectionSetting connectionSetting) : this(ctlMain, connectionSetting.HostName, connectionSetting.HostPort, false /*bDebugTrace*/) { ConnectionSetting = connectionSetting; }