public static SaveOfLiveGame FetchSaveOfLiveGame(Guid idOfSave) { SaveOfLiveGame result = new SaveOfLiveGame(); using (SqlConnection cn = DbConnectionFactory()) { var command = cn.CreateCommand(); command.CommandText = $@"Select SavedCells.X AS X, SavedCells.Y AS Y, SavedCells.Alive AS Alive, Saves.[Name] AS NameOfSafe, Saves.DateOfCreation AS DateOfCreation, Saves.GameAreaSizeX AS GameAreaSizeX, Saves.GameAreaSizeY AS GameAreaSizeY, Saves.CellSize AS CellSize, Saves.UseToroid AS UseToroid From [dbo].[SavedCells] AS SavedCells Left Join [dbo].[Saves] As Saves ON SavedCells.[Save] = Saves.[Id] Where SavedCells.[Save] = '{idOfSave}';"; command.CommandType = CommandType.Text; command.Connection = cn; bool startOfReading = true; using (var dr = command.ExecuteReader()) { while (dr.Read()) { if (startOfReading) { result.Id = idOfSave; result.GameAreaSizeX = (int)dr["GameAreaSizeX"]; result.GameAreaSizeY = (int)dr["GameAreaSizeY"]; result.CellSize = (int)dr["CellSize"]; result.NameOfSave = (string)dr["NameOfSafe"]; result.UseToroid = (bool)dr["UseToroid"]; startOfReading = false; } result.Cells.Add(new Cell((int)dr["X"], (int)dr["Y"]) { Alive = (bool)dr["Alive"] }); //var val1 = (string)dr["FieldName"]; } } } return(result); }
public void LoadGame(Guid idOfSave) { Cells.Clear(); SaveOfLiveGame save = WorkWithSaves.FetchSaveOfLiveGame(idOfSave); if (save.GameAreaSizeX != GameModelAreaSizeX || save.GameAreaSizeY != GameModelAreaSizeY || save.CellSize != CellSize) { throw new Exception("Не удается загрузить сохранение, т.к. у него другой размер поля"); } Cells = save.Cells; UseToroid = save.UseToroid; }
public static bool CheckThatSaveIsUnique(List <Cell> cells, int areaSizeX, int areaSizeY, int cellSize, bool useToroid, ref string nameOfSameSave) { int boolConverter = useToroid ? 1 : 0; List <Guid> result = new List <Guid>(); List <Cell> aliveCells = cells.Where(cell => cell.Alive).ToList(); if (aliveCells.Count < 3) { return(false); } using (SqlConnection cn = DbConnectionFactory()) { var command = cn.CreateCommand(); command.CommandText = $@"Select [Saves].Id As IdOfSave From (Select * From [dbo].Saves As S Where S.GameAreaSizeX = {areaSizeX} And S.GameAreaSizeY = {areaSizeY} And S.CellSize = {cellSize} And S.UseToroid = {boolConverter}) As Saves Cross Apply (Select Count(SavedCells.Id) AS NumberOfMatches From [dbo].SavedCells As SavedCells Where SavedCells.[Save] = [Saves].Id And SavedCells.Alive = 1 And ((SavedCells.X = {aliveCells[0].PositionX} And SavedCells.Y = {aliveCells[0].PositionY}) Or (SavedCells.X = {aliveCells[1].PositionX} And SavedCells.Y = {aliveCells[1].PositionY}) Or (SavedCells.X = {aliveCells[2].PositionX} And SavedCells.Y = {aliveCells[2].PositionY})) ) As SavedCells Where SavedCells.NumberOfMatches = 3"; command.CommandType = CommandType.Text; command.Connection = cn; using (var dr = command.ExecuteReader()) { while (dr.Read()) { result.Add((Guid)dr["IdOfSave"]); //var val1 = (string)dr["FieldName"]; } } } if (result.Count == 0) { return(true); } else { bool isUnique = true; string nameOfSameSaveTemp = String.Empty; Parallel.ForEach(result, guidOfSave => { if (!isUnique) { return; } SaveOfLiveGame saveObject = FetchSaveOfLiveGame(guidOfSave); if (Cell.CompareListsOfCells(cells, saveObject.Cells, new CellsComparerByPositionAndState())) { lock (lockObj) { isUnique = false; nameOfSameSaveTemp = saveObject.NameOfSave; } } }); nameOfSameSave = nameOfSameSaveTemp; return(isUnique); } }