Exemplo n.º 1
0
        /// <summary>
        /// Parse the receiving line info
        /// </summary>
        /// <param name="iGameId">                  ID of the game being listened to</param>
        /// <param name="lines">                    List of lines to parse</param>
        /// <param name="queueLine">                Queue where to register parsed lines</param>
        /// <param name="strTerminationComment">    Termination comment if any</param>
        /// <param name="strError">                 Error if any, null if none</param>
        /// <returns>
        /// Termination code
        /// </returns>
        static public TerminationE ParseStyle12Lines(int iGameId, List <string> lines, Queue <Style12MoveLine> queueLine, out string strTerminationComment, out string strError)
        {
            TerminationE    eRetVal = TerminationE.None;
            int             iFoundGameId;
            Style12MoveLine line;

            strError = null;
            strTerminationComment = "";
            foreach (string strLine in lines)
            {
                line = ParseLine(strLine, out iFoundGameId, out eRetVal, out strTerminationComment, out strError);
                if (iFoundGameId == iGameId)
                {
                    if (line != null)
                    {
                        queueLine.Enqueue(line);
                    }
                    else if (eRetVal != TerminationE.None)
                    {
                        break;
                    }
                }
            }
            return(eRetVal);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Set the termination code and the error if any
        /// </summary>
        /// <param name="eTermination">             Termination code</param>
        /// <param name="strTerminationComment">    Termination comment</param>
        /// <param name="strError">                 Error</param>
        public virtual void SetTermination(TerminationE eTermination, string strTerminationComment, string strError)
        {
            string strMsg = null;

            if (m_timerMoveTimeout != null)
            {
                m_timerMoveTimeout.Change(System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);
            }
            Termination = eTermination;
            switch (eTermination)
            {
            case TerminationE.None:
                throw new ArgumentException("Cannot terminate with none");

            case TerminationE.WhiteWin:
                strMsg = "White Win";
                break;

            case TerminationE.BlackWin:
                strMsg = "Black Win";
                break;

            case TerminationE.Draw:
                strMsg = "Draw";
                break;

            case TerminationE.Terminated:
                strMsg = "Game finished";
                break;

            case TerminationE.TerminatedWithErr:
                strMsg = strError ?? strError;
                strTerminationComment = "";
                TerminationError      = strError;
                break;

            default:
                break;
            }
            if (!String.IsNullOrEmpty(strTerminationComment))
            {
                strMsg += " - " + strTerminationComment;
            }
            if (m_actionGameFinished != null)
            {
                m_actionGameFinished(this, eTermination, strMsg);
            }
            else
            {
                if (eTermination != TerminationE.TerminatedWithErr)
                {
                    ShowMessage(strMsg);
                }
                else
                {
                    ShowError(strMsg);
                }
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// Set the termination code and the error if any
 /// </summary>
 /// <param name="eTermination">             Termination code</param>
 /// <param name="strTerminationComment">    Termination comment</param>
 /// <param name="strError">                 Error</param>
 public override void SetTermination(TerminationE eTermination, string strTerminationComment, string strError)
 {
     base.SetTermination(eTermination, strTerminationComment, strError);
     if (EventWaitHandle != null)
     {
         EventWaitHandle.Set();
     }
 }
Exemplo n.º 4
0
        /// <summary>
        /// Check if an move termination as been issued
        /// </summary>
        /// <param name="strLine">                  Line to parse</param>
        /// <param name="iGameId">                  Game id</param>
        /// <param name="strTerminationComment">    Termination comment if any</param>
        /// <param name="strError">                 Error if any</param>
        /// <returns></returns>
        static public TerminationE IsMoveTermination(string strLine, out int iGameId, out string strTerminationComment, out string strError)
        {
            TerminationE eRetVal = TerminationE.None;
            int          iStartIndex;
            int          iEndIndex;

            string[] arrParts;
            //{Game 378 (OlegM vs. Chessnull) Chessnull forfeits on time} 1-0
            strTerminationComment = "";
            iGameId  = 0;
            strError = null;
            strLine  = strLine.Trim();
            if (strLine.StartsWith("{Game "))
            {
                arrParts = strLine.Split(' ');
                if (Int32.TryParse(arrParts[1], out iGameId))
                {
                    switch (arrParts[arrParts.Length - 1])
                    {
                    case "1/2-1/2":
                        eRetVal = TerminationE.Draw;
                        break;

                    case "1-0":
                        eRetVal = TerminationE.WhiteWin;
                        break;

                    case "0-1":
                        eRetVal = TerminationE.BlackWin;
                        break;

                    case "*":
                        eRetVal = TerminationE.Terminated;
                        break;

                    default:
                        eRetVal  = TerminationE.TerminatedWithErr;
                        strError = "Unknown termination character '" + arrParts[arrParts.Length - 1] + "'";
                        break;
                    }
                    if (eRetVal != TerminationE.TerminatedWithErr)
                    {
                        iStartIndex = strLine.IndexOf(") ");
                        iEndIndex   = strLine.IndexOf("}");
                        if (iStartIndex != -1 && iEndIndex != -1)
                        {
                            strTerminationComment = strLine.Substring(iStartIndex + 2, iEndIndex - iStartIndex - 2);
                        }
                    }
                }
            }
            else if (strLine.StartsWith("Removing game ") && Int32.TryParse((arrParts = strLine.Split(' '))[2], out iGameId))
            {
                eRetVal = TerminationE.Terminated;
            }
            return(eRetVal);
        }
Exemplo n.º 5
0
 /// <summary>
 /// Terminate a game
 /// </summary>
 /// <param name="gameIntf">                 Game interface</param>
 /// <param name="eTermination">             Termination code</param>
 /// <param name="strTerminationComment">    Termination comment</param>
 /// <param name="strError">                 Error if any</param>
 public void TerminateGame(GameIntf gameIntf, TerminationE eTermination, string strTerminationComment, string strError)
 {
     lock (m_dictGameIntf) {
         if (RemoveGameIntfInt(gameIntf.Game.GameId))
         {
             gameIntf.SetTermination(eTermination, strTerminationComment, strError);
         }
     }
 }
Exemplo n.º 6
0
        /// <summary>
        /// Parse a line
        /// </summary>
        /// <param name="strLine">                  Line to parse</param>
        /// <param name="iGameId">                  Game ID</param>
        /// <param name="eTermination">             Termination code if error or if game has ended</param>
        /// <param name="strTerminationComment">    Termination comment if any</param>
        /// <param name="strError">                 Returned error if any. null if no error detected</param>
        /// <returns>
        /// Line or null if not a style12 line or error
        /// </returns>
        static public Style12MoveLine ParseLine(string strLine, out int iGameId, out TerminationE eTermination, out string strTerminationComment, out string strError)
        {
            Style12MoveLine lineRetVal;

            string[] arrParts;
            string   strFENLine;

            ChessBoard.PieceE ePiece;
            int iLine;
            int iPos;

            int[] arrIntVal;

            eTermination = IsMoveTermination(strLine, out iGameId, out strTerminationComment, out strError);
            if (eTermination != TerminationE.None)
            {
                lineRetVal = null;
            }
            else
            {
                arrParts = GetLineParts(strLine);
                if (arrParts == null)
                {
                    lineRetVal = null;
                }
                else
                {
                    lineRetVal = new Style12MoveLine();
                    iPos       = 63;
                    iLine      = 0;
                    arrIntVal  = new int[11];
                    while (iLine < 8 && strError == null)
                    {
                        strFENLine = arrParts[iLine + 1];
                        if (strFENLine.Length != 8)
                        {
                            strError = "Illegal board definition - bad FEN line size";
                        }
                        else
                        {
                            foreach (char chr in strFENLine)
                            {
                                if (DecodePiece(chr, out ePiece))
                                {
                                    lineRetVal.Board[iPos--] = ePiece;
                                }
                                else
                                {
                                    strError = "Illegal board definition - Unknown piece specification '" + chr + "'";
                                    break;
                                }
                            }
                        }
                        iLine++;
                    }
                    if (strError == null)
                    {
                        switch (arrParts[9])
                        {
                        case "B":
                            lineRetVal.NextMovePlayer = ChessBoard.PlayerE.Black;
                            break;

                        case "W":
                            lineRetVal.NextMovePlayer = ChessBoard.PlayerE.White;
                            break;

                        default:
                            strError = "Next move player not 'B' or 'W'";
                            break;
                        }
                        if (strError == null)
                        {
                            if (!lineRetVal.SetBoardStateMask(arrParts[11], ChessBoard.BoardStateMaskE.WRCastling) ||
                                !lineRetVal.SetBoardStateMask(arrParts[12], ChessBoard.BoardStateMaskE.WLCastling) ||
                                !lineRetVal.SetBoardStateMask(arrParts[13], ChessBoard.BoardStateMaskE.BRCastling) ||
                                !lineRetVal.SetBoardStateMask(arrParts[14], ChessBoard.BoardStateMaskE.BLCastling) ||
                                !Int32.TryParse(arrParts[15], out arrIntVal[0]) ||
                                !Int32.TryParse(arrParts[16], out arrIntVal[1]) ||
                                !Int32.TryParse(arrParts[19], out arrIntVal[2]) ||
                                !Int32.TryParse(arrParts[20], out arrIntVal[3]) ||
                                !Int32.TryParse(arrParts[21], out arrIntVal[4]) ||
                                !Int32.TryParse(arrParts[22], out arrIntVal[5]) ||
                                !Int32.TryParse(arrParts[23], out arrIntVal[6]) ||
                                !Int32.TryParse(arrParts[24], out arrIntVal[7]) ||
                                !Int32.TryParse(arrParts[25], out arrIntVal[8]) ||
                                !Int32.TryParse(arrParts[26], out arrIntVal[9]) ||
                                !Int32.TryParse(arrParts[30], out arrIntVal[10]))
                            {
                                strError = "Illegal value in field.";
                            }
                            else if (arrIntVal[2] < -3 ||
                                     arrIntVal[2] > 2 ||
                                     arrIntVal[3] < 0 ||
                                     arrIntVal[9] < 0 ||
                                     arrIntVal[10] < 0 ||
                                     arrIntVal[10] > 1)
                            {
                                strError = "Field value out of range.";
                            }
                            else
                            {
                                lineRetVal.WhitePlayerName       = arrParts[17];
                                lineRetVal.BlackPlayerName       = arrParts[18];
                                lineRetVal.IrreversibleMoveCount = arrIntVal[0];
                                lineRetVal.GameId             = arrIntVal[1];
                                lineRetVal.RelationWithGame   = (RelationWithGameE)arrIntVal[2];
                                lineRetVal.InitialTime        = arrIntVal[3];
                                lineRetVal.IncrementTime      = arrIntVal[4];
                                lineRetVal.WhiteMaterial      = arrIntVal[5];
                                lineRetVal.BlackMaterial      = arrIntVal[6];
                                lineRetVal.WhiteRemainingTime = arrIntVal[7];
                                lineRetVal.BlackRemainingTime = arrIntVal[8];
                                lineRetVal.MoveNumber         = arrIntVal[9];
                                lineRetVal.LastMoveVerbose    = arrParts[27];
                                lineRetVal.LastMoveSpan       = FICSGame.ParseTime(arrParts[28].Replace("(", "").Replace(")", ""));
                                lineRetVal.LastMoveSAN        = arrParts[29];
                                lineRetVal.IsFlipped          = (arrIntVal[9] == 1);
                                iGameId = lineRetVal.GameId;
                            }
                        }
                        if (strError == null)
                        {
                            if (arrParts.Length >= 33 &&
                                Int32.TryParse(arrParts[31], out arrIntVal[0]) &&
                                Int32.TryParse(arrParts[32], out arrIntVal[1]))
                            {
                                lineRetVal.IsClockTicking = (arrIntVal[0] == 1);
                                lineRetVal.LagInMS        = arrIntVal[1];
                            }
                            else
                            {
                                lineRetVal.IsClockTicking = true;
                                lineRetVal.LagInMS        = 0;
                            }
                        }
                    }
                    if (strError != null)
                    {
                        lineRetVal   = null;
                        eTermination = TerminationE.TerminatedWithErr;
                    }
                }
            }
            return(lineRetVal);
        }