コード例 #1
0
        private void DoWork(object state)
        {
            _logger.LogInformation("Timed Background Service is working.");

            using (var scope = Services.CreateScope())
            {
                var scopedServices = scope.ServiceProvider;
                var db             = scopedServices.GetRequiredService <WordHintDbContext>();

                try
                {
                    var model = CrossBoardCreator.GetCrossWordModelFromUrl("http-random");
                    var board = model.ToCrossBoard();

                    // add in database
                    var newTemplate = new CrossWord.Scraper.MySQLDbService.Models.CrosswordTemplate()
                    {
                        Rows = model.Size.Rows,
                        Cols = model.Size.Cols,
                        Grid = model.Grid
                    };

                    db.CrosswordTemplates.Add(newTemplate);
                    db.SaveChangesAsync();
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex,
                                     "An error occurred writing to the " +
                                     $"database. Error: {ex.Message}");
                }
            }
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: tonley/CrossWord
        CrossGenerator CreateGenerator(string file, string dictFile, CommandStore commands)
        {
            DateTime startTime = DateTime.Now;
            var      cb        = CrossBoardCreator.CreateFromFile(file);
            var      dict      = new Dictionary(dictFile, cb.MaxWordLength);

            cb.Preprocess(dict);
            var gen = new CrossGenerator(dict, cb);

            gen.Watcher += GeneratorWatcher;
            return(gen);
        }
コード例 #3
0
 public void FileBoardCreatorTest()
 {
     using (var memoryStream = new MemoryStream())
     {
         var w = new StreamWriter(memoryStream);
         w.WriteLine("--   ");
         w.WriteLine("-    ");
         w.WriteLine("     ");
         w.WriteLine("    -");
         w.WriteLine("   --");
         w.Flush();
         memoryStream.Position = 0;
         var board = CrossBoardCreator.CreateFromStream(memoryStream);
         Assert.True(board != null);
     }
 }
コード例 #4
0
        public IActionResult GenerateTemplates()
        {
            Queue.QueueBackgroundWorkItem(async token =>
            {
                var guid = Guid.NewGuid().ToString();
                logger.LogInformation(
                    $"Queued Background Task {guid} added to the queue.");

                using (var scope = serviceScopeFactory.CreateScope())
                {
                    var scopedServices = scope.ServiceProvider;
                    var db             = scopedServices.GetRequiredService <WordHintDbContext>();

                    try
                    {
                        var model = CrossBoardCreator.GetCrossWordModelFromUrl("http-random");
                        var board = model.ToCrossBoard();

                        // add in database
                        var newTemplate = new CrosswordTemplate()
                        {
                            Rows = model.Size.Rows,
                            Cols = model.Size.Cols,
                            Grid = model.Grid
                        };

                        db.CrosswordTemplates.Add(newTemplate);
                        await db.SaveChangesAsync();
                    }
                    catch (Exception ex)
                    {
                        logger.LogError(ex,
                                        "An error occurred writing to the " +
                                        $"database. Error: {ex.Message}");
                    }
                }

                logger.LogInformation(
                    $"Queued Background Task {guid} is complete.");
            });

            return(Ok("Generate crosssword template added to the queue"));
        }
コード例 #5
0
        private CrossBoard GetCrossboard()
        {
            ICrossBoard board = null;
            // var template = GetRandomCrosswordTemplateFromDb();
            CrosswordTemplate template = null;

            if (template != null)
            {
                board = new CrossBoard();

                int cols = (int)template.Cols;
                int rows = (int)template.Rows;

                board.SetBoardSize(cols, rows);

                int n = 0;
                for (int row = 0; row < rows; row++)
                {
                    for (int col = 0; col < cols; col++)
                    {
                        var val = template.Grid[n];
                        if (val == ".")
                        {
                            board.AddStartWord(col, row);
                        }

                        n += 1;
                    }
                }

                // debug the generated template
                // using (StreamWriter writer = new StreamWriter("template.txt"))
                // {
                //     board.WriteTemplateTo(writer);
                // }
            }
            else
            {
                var model = CrossBoardCreator.GetCrossWordModelFromUrl("http-random");
                board = model.ToCrossBoard();

                // add in database
                var newTemplate = new CrosswordTemplate()
                {
                    Rows = model.Size.Rows,
                    Cols = model.Size.Cols,
                    Grid = model.Grid
                };

                db.CrosswordTemplates.Add(newTemplate);
                db.SaveChanges();
            }

            var gen = new CrossGenerator(dictionary, board);

            board.Preprocess(dictionary);

            var generated = gen.Generate().FirstOrDefault() as CrossBoard;

            return(generated);
        }