public MazeDataFileModel ParseDataFile(string filePath) { if (!File.Exists(filePath)) { throw new FileNotFoundException("The file could not be found.", filePath); } var mazeDataFileModel = new MazeDataFileModel(); string lineValue; StreamReader file = new StreamReader(filePath); while ((lineValue = file.ReadLine()) != null) { bool isEndOfLine = lineValue.Trim().Equals("-1"); if (!sizeDataIsParsed) { ParseSizeData(lineValue, ref mazeDataFileModel, isEndOfLine); } else if (!mirrorDataIsParsed && sizeDataIsParsed) { ParseMirrorData(lineValue, ref mazeDataFileModel, isEndOfLine); } else if (!lazerDataIsParsed && mirrorDataIsParsed) { ParseLazerGunData(lineValue, ref mazeDataFileModel, isEndOfLine); } } return(mazeDataFileModel); }
private void ParseSizeData(string text, ref MazeDataFileModel mazeDataFileModel, bool isParsed) { sizeDataIsParsed = isParsed; if (!isParsed) { List <string> coordinates = text.Split(",").ToList(); if (coordinates.Any() && coordinates.Count == 2) { mazeDataFileModel.MazeSizeData = new Tuple <int, int>(int.Parse(coordinates[0]), int.Parse(coordinates[1])); } } }
private void ParseLazerGunData(string text, ref MazeDataFileModel mazeDataFileModel, bool isParsed) { lazerDataIsParsed = isParsed; if (!isParsed) { List <string> coordinates = text.Split(",").ToList(); if (coordinates.Any() && coordinates.Count == 2) { int x = int.Parse(coordinates[0]); int y = int.Parse(coordinates[1].Substring(0, 1)); var dir = new string(text.Where(Char.IsLetter).ToArray()); mazeDataFileModel.LazerGunData = new Tuple <int, int, string>(x, y, dir.ToString()); } } }
public void SaveToDatabase(MazeDataFileModel mazeDataFileModel) { var mazeBoardDto = new MazeBoardDto(); var mazeRooms = new List <MazeRoomDto>(); var xCoord = mazeDataFileModel.MazeSizeData.Item1; var yCoord = mazeDataFileModel.MazeSizeData.Item2; for (int x = 0; x < xCoord; x++) { for (int y = 0; y < yCoord; y++) { var mazeRoom = new MazeRoomDto() { XCoordinate = x, YCoordinate = y }; mazeRooms.Add(mazeRoom); } } mazeBoardDto.MazeRooms = mazeRooms; int lazerGunX = mazeDataFileModel.LazerGunData.Item1; int lazerGunY = mazeDataFileModel.LazerGunData.Item2; string lazerGunOrientation = mazeDataFileModel.LazerGunData.Item3; foreach (var room in mazeRooms) { foreach (var mirrorData in mazeDataFileModel.MirrorData) { var x = mirrorData.Item1; var y = mirrorData.Item2; var types = mirrorData.Item3; if (room.XCoordinate == x && room.YCoordinate == y) { var mirror = new MirrorDto(); if (!string.IsNullOrEmpty(types)) { var length = types.Length; switch (length) { case 1: SetMirrorLeanDirection(ref mirror, types); break; case 2: SetMirrorLeanDirection(ref mirror, types); SetMirrorReflection(ref mirror, types); break; } } room.Mirror = mirror; } } if (room.XCoordinate == lazerGunX && room.YCoordinate == lazerGunY) { room.LazerGun = new LazerGunDto() { XCoordinate = lazerGunX, YCoordinate = lazerGunY }; if (lazerGunOrientation == "H") { room.LazerGun.LazerOrientation = Common.LazerOrientations.Horizontal; } if (lazerGunOrientation == "V") { room.LazerGun.LazerOrientation = Common.LazerOrientations.Vertical; } } } base.UnitOfWork.MazeBoards.Insert(base.Mapper.Map <MazeBoard>(mazeBoardDto)); }