/// <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> /// Called when connection has succeed or failed /// </summary> /// <param name="bSucceed"> true if succeed</param> /// <param name="conn"> Connection if any</param> /// <param name="strError"> Error if any</param> private void ConnectionDone(bool bSucceed, FICSConnection conn, string strError) { ProgressBar.Stop(); ProgressBar.Visibility = Visibility.Hidden; if (bSucceed) { m_conn = conn; MessageBox.Show("Connected to FICS Server"); DialogResult = true; } else { MessageBox.Show(strError); butOk.IsEnabled = true; butCancel.IsEnabled = true; } }
/// <summary> /// Ctor /// </summary> /// <param name="conn"> FICS Connection</param> /// <param name="searchCriteria"> Search criteria</param> public frmFindBlitzGame(FICSConnection conn, SearchCriteria searchCriteria) : this() { m_conn = conn; m_searchCriteria = searchCriteria; PlayerName = searchCriteria.PlayerName; BlitzGame = searchCriteria.BlitzGame; LightningGame = searchCriteria.LightningGame; UntimedGame = searchCriteria.UntimedGame; StandardGame = searchCriteria.StandardGame; IsRated = searchCriteria.IsRated; MinRating = searchCriteria.MinRating; MinTimePerPlayer = searchCriteria.MinTimePerPlayer; MaxTimePerPlayer = searchCriteria.MaxTimePerPlayer; MinIncTimePerMove = searchCriteria.MinIncTimePerMove; MaxIncTimePerMove = searchCriteria.MaxIncTimePerMove; MaxMoveDone = searchCriteria.MaxMoveDone; MoveTimeout = searchCriteria.MoveTimeOut; m_searchCriteria = searchCriteria; }
/// <summary> /// Try to connect to the server /// </summary> /// <param name="strHostName"> Host name</param> /// <param name="iPortNumber"> Port number</param> /// <param name="strUserName"> User name</param> /// <param name="strPassword"> Password</param> private void InitializeConnection(string strHostName, int iPortNumber, string strUserName, string strPassword) { FICSConnection conn; string strError; ConnectionSetting.HostName = strHostName; ConnectionSetting.HostPort = iPortNumber; ConnectionSetting.Anonymous = String.Compare(strUserName, "guest", true) == 0; ConnectionSetting.UserName = strUserName; conn = new FICSConnection(m_ctlMain, ConnectionSetting); if (!conn.Login(strPassword, 10, out strError)) { conn.Dispose(); Dispatcher.Invoke((Action)(() => { ConnectionDone(false /*bSucceed*/, null /*conn*/, strError); })); } else { Dispatcher.Invoke((Action)(() => { ConnectionDone(true /*bSucceed*/, conn, null /*strError*/); })); } }
/// <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")); } } }