Ejemplo n.º 1
0
        public void LoadChilds(Move m, Dictionary <string, DataRow> bookMovesCollection)
        {
            CheckAndAddMove(m, bookMovesCollection, false);

            string tempParentId = "";
            bool   isInitialFen = FenParser.IsInitialFen(m.Fen, true);

            if (m.Flags.IsRootMove && isInitialFen)
            {
                // if rootMove and initial fen, then CurrentMove's parent is "0"
                tempParentId = "0";
            }
            else
            {
                #region Find Book's CurrentMove's Parent
                StringBuilder filter = new StringBuilder();
                if (!m.Flags.IsRootMove)
                {
                    filter.Append(BookMove.ColumnMoveNumber + " = " + m.MoveNo);
                    filter.Append(" and " + BookMove.ColumnWhiteMove + " = " + m.White);
                    filter.Append(" and " + BookMove.ColumnFrom + " = '" + m.From + "'");
                    filter.Append(" and " + BookMove.ColumnTo + " = '" + m.To + "'");
                    filter.Append(" and ");
                }
                filter.Append(BookMove.ColumnFen + " like '" + ChessLibrary.FenParser.GetOnlyFen(m.Fen) + "%'");

                DataRow[] rows = this.DataTable.Select(filter.ToString());

                if (rows.Length > 0)
                {
                    tempParentId = rows[0][BookMove.ColumnId].ToString();
                }
                #endregion
            }

            if (string.IsNullOrEmpty(tempParentId))
            {
                #region if "tempParentId" not found, set defaultView to not display moves.
                DataTable.DefaultView.RowFilter = BookMove.ColumnParentId + " = " + -2;

                if (!AllowMoveAdding)
                {
                    CheckAndAddMove(m, bookMovesCollection, true);
                }

                rowIndex = -1;
                #endregion
            }
            else
            {
                if (m.Flags.IsRootMove && isInitialFen)
                {
                    #region If RootMove and initial fen, then set move to display according to white or black turn
                    string filter = "";

                    if (m.White == 1)
                    {
                        filter += BookMove.ColumnParentId + " = " + tempParentId;
                    }
                    else
                    {
                        filter += BookMove.ColumnMoveNumber + " = 1";
                    }

                    filter += " and " + BookMove.ColumnWhiteMove + " = " + m.White;

                    DataTable.DefaultView.RowFilter = filter;
                    #endregion
                }
                else
                {
                    // if parent found(and not rootMove), then load its childs
                    DataTable.DefaultView.RowFilter = BookMove.ColumnParentId + " = " + tempParentId;
                }

                // set CurrentParentMoveId to current parent founded.
                CurrentParentMoveId = BaseItem.ToInt32(tempParentId);

                #region Set rowIndex
                if (DataTable.DefaultView.Count > 0)
                {
                    rowIndex = 0;

                    if (m != null)
                    {
                        for (int i = 0; i < DataTable.DefaultView.Count; i++)
                        {
                            if (m.To == DataTable.DefaultView[i][BookMove.ColumnTo].ToString())
                            {
                                rowIndex = i;
                            }
                        }
                    }
                }
                else
                {
                    rowIndex = -1;
                }
                #endregion
            }

            this.DataTable.DefaultView.Sort = BookMove.ColumnWinCount + " Desc";
        }