Beispiel #1
0
        /// <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"));
                }
            }
        }