public void StartGame()
        {
            drawService.ProjectionClear();
            drawService.PointsHistoryBoxClear();

            detectionService.PrepareCamsAndTryCapture();
            detectionService.RunDetection();

            var selectedGameType = Converter.NewGameControlsToGameType(mainWindow.NewGameControls);
            var legs             = Converter.ComboBoxSelectedContentToInt(mainWindow.NewGameLegsComboBox);
            var sets             = Converter.ComboBoxSelectedContentToInt(mainWindow.NewGameSetsComboBox);
            var legPoints        = Converter.ComboBoxSelectedContentToString(mainWindow.NewGamePointsComboBox);

            GameType = selectedGameType;

            var selectedPlayer1 = mainWindow.NewGamePlayer1ComboBox.SelectedItem as Player;
            var selectedPlayer2 = mainWindow.NewGamePlayer2ComboBox.SelectedItem as Player;

            var players = new List <Player>();

            if (selectedPlayer1 != null)
            {
                players.Add(selectedPlayer1);
            }

            if (selectedPlayer2 != null)
            {
                players.Add(selectedPlayer2);
            }

            Game = new Game(selectedGameType);

            dbService.GameSaveNew(Game, players);

            switch (selectedGameType)
            {
            case GameType.FreeThrowsSingle:
                switch (legPoints)
                {
                case "Free":
                    GameProcessor = new FreeThrowsSingleFreePointsProcessor(Game, players, dbService, scoreBoardService);
                    scoreBoardService.OpenScoreBoard(selectedGameType, players, "Free throws");
                    break;

                case "301":
                    GameProcessor = new FreeThrowsSingleWriteOffPointsProcessor(Game, players, dbService, scoreBoardService, 301);
                    scoreBoardService.OpenScoreBoard(selectedGameType, players, "Write off 301", 301);
                    break;

                case "501":
                    GameProcessor = new FreeThrowsSingleWriteOffPointsProcessor(Game, players, dbService, scoreBoardService, 501);
                    scoreBoardService.OpenScoreBoard(selectedGameType, players, "Write off 501", 501);
                    break;

                case "701":
                    GameProcessor = new FreeThrowsSingleWriteOffPointsProcessor(Game, players, dbService, scoreBoardService, 701);
                    scoreBoardService.OpenScoreBoard(selectedGameType, players, "Write off 701", 701);
                    break;

                case "1001":
                    GameProcessor = new FreeThrowsSingleWriteOffPointsProcessor(Game, players, dbService, scoreBoardService, 1001);
                    scoreBoardService.OpenScoreBoard(selectedGameType, players, "Write off 1001", 1001);
                    break;
                }

                break;

            case GameType.FreeThrowsDouble:
                switch (legPoints)
                {
                case "Free":
                    GameProcessor = new FreeThrowsDoubleFreePointsProcessor(Game, players, dbService, scoreBoardService);
                    scoreBoardService.OpenScoreBoard(selectedGameType, players, "Free throws");
                    break;

                case "301":
                    GameProcessor = new FreeThrowsDoubleWriteOffPointsProcessor(Game, players, dbService, scoreBoardService, 301);
                    scoreBoardService.OpenScoreBoard(selectedGameType, players, "Write off 301", 301);
                    break;

                case "501":
                    GameProcessor = new FreeThrowsDoubleWriteOffPointsProcessor(Game, players, dbService, scoreBoardService, 501);
                    scoreBoardService.OpenScoreBoard(selectedGameType, players, "Write off 501", 501);
                    break;

                case "701":
                    GameProcessor = new FreeThrowsDoubleWriteOffPointsProcessor(Game, players, dbService, scoreBoardService, 701);
                    scoreBoardService.OpenScoreBoard(selectedGameType, players, "Write off 701", 701);
                    break;

                case "1001":
                    GameProcessor = new FreeThrowsDoubleWriteOffPointsProcessor(Game, players, dbService, scoreBoardService, 1001);
                    scoreBoardService.OpenScoreBoard(selectedGameType, players, "Write off 1001", 1001);
                    break;
                }

                break;

            case GameType.Classic:
                switch (legPoints)
                {
                case "301":
                    GameProcessor = new ClassicDoubleProcessor(Game, players, dbService, scoreBoardService, 301, legs, sets);
                    scoreBoardService.OpenScoreBoard(selectedGameType, players, $"First to {sets}", 301);
                    break;

                case "501":
                    GameProcessor = new ClassicDoubleProcessor(Game, players, dbService, scoreBoardService, 501, legs, sets);
                    scoreBoardService.OpenScoreBoard(selectedGameType, players, $"First to {sets}", 501);
                    break;

                case "701":
                    GameProcessor = new ClassicDoubleProcessor(Game, players, dbService, scoreBoardService, 701, legs, sets);
                    scoreBoardService.OpenScoreBoard(selectedGameType, players, $"First to {sets}", 701);
                    break;

                case "1001":
                    GameProcessor = new ClassicDoubleProcessor(Game, players, dbService, scoreBoardService, 1001, legs, sets);
                    scoreBoardService.OpenScoreBoard(selectedGameType, players, $"First to {sets}", 1001);
                    break;
                }

                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            Task.Run(() =>
            {
                IsGameRun = true;
                detectionService.OnThrowDetected += OnAnotherThrow;
                detectionService.OnStatusChanged += OnDetectionServiceStatusChanged;
                GameProcessor.OnMatchEnd         += OnMatchEnd;
                while (IsGameRun)
                {
                }

                detectionService.OnThrowDetected -= OnAnotherThrow;
                detectionService.OnStatusChanged -= OnDetectionServiceStatusChanged;
                GameProcessor.OnMatchEnd         -= OnMatchEnd;
            });
        }
Example #2
0
        private void StartDetection()
        {
            extractionSleepTime   = configService.Read <double>("ExtractionSleepTime");
            thresholdSleepTime    = configService.Read <double>("ThresholdSleepTime");
            moveDetectedSleepTime = configService.Read <double>("MoveDetectedSleepTime");
            var runtimeCapturing = mainWindowView.RuntimeCapturingCheckBox.IsChecked.Value;

            drawService.ProjectionClear();
            cts         = new CancellationTokenSource();
            cancelToken = cts.Token;

            cams = new List <CamWindow>();
            if (mainWindowView.Cam1CheckBox.IsChecked.Value)
            {
                cams.Add(new CamWindow(1));
            }

            if (mainWindowView.Cam2CheckBox.IsChecked.Value)
            {
                cams.Add(new CamWindow(2));
            }

            if (mainWindowView.Cam3CheckBox.IsChecked.Value)
            {
                cams.Add(new CamWindow(3));
            }

            if (mainWindowView.Cam4CheckBox.IsChecked.Value)
            {
                cams.Add(new CamWindow(4));
            }

            logger.Info($"Detection for {cams.Count} cams start");

            Task.Run(() =>
            {
                Thread.CurrentThread.Name = $"Recognition_workerThread";

                ClearAllCamsImageBoxes();

                while (!cancelToken.IsCancellationRequested)
                {
                    foreach (var cam in cams)
                    {
                        logger.Debug($"Cam_{cam.camNumber} detection start");

                        var response = cam.DetectMove();

                        if (response == ResponseType.Move)
                        {
                            Thread.Sleep(TimeSpan.FromSeconds(moveDetectedSleepTime));
                            response = cam.DetectThrow();

                            if (response == ResponseType.Trow)
                            {
                                cam.FindAndProcessDartContour();

                                FindThrowOnRemainingCams(cam);

                                logger.Debug($"Cam_{cam.camNumber} detection end with response type '{ResponseType.Trow}'. Cycle break");
                                break;
                            }

                            if (response == ResponseType.Extraction)
                            {
                                Thread.Sleep(TimeSpan.FromSeconds(extractionSleepTime));

                                drawService.ProjectionClear();
                                ClearAllCamsImageBoxes();

                                logger.Debug($"Cam_{cam.camNumber} detection end with response type '{ResponseType.Extraction}'. Cycle break");
                                break;
                            }
                        }

                        if (!runtimeCapturing)
                        {
                            Thread.Sleep(TimeSpan.FromSeconds(thresholdSleepTime));
                        }

                        logger.Debug($"Cam_{cam.camNumber} detection end with response type '{ResponseType.Nothing}'");
                    }
                }

                logger.Info($"Detection for {cams.Count} cams end. Cancellation requested");

                foreach (var cam in cams)
                {
                    cam.Dispatcher.Invoke(() => cam.Close());
                }
            });
        }
Example #3
0
        public void StartGame()
        {
            drawService.ProjectionClear();
            drawService.PointsHistoryBoxClear();

            detectionService.PrepareAndTryCapture();
            detectionService.RunDetection();

            var selectedGameTypeUi = Converter.NewGameControlsToGameTypeUi(mainWindow.NewGameControls);
            var selectedGameType   = Converter.NewGameControlsToGameTypeGameService(mainWindow.NewGameControls);

            GameType = selectedGameType;

            var selectedPlayer1 = mainWindow.NewGamePlayer1ComboBox.SelectedItem as Player;
            var selectedPlayer2 = mainWindow.NewGamePlayer2ComboBox.SelectedItem as Player;

            var players = new List <Player>();

            if (selectedPlayer1 != null)
            {
                players.Add(selectedPlayer1);
            }

            if (selectedPlayer2 != null)
            {
                players.Add(selectedPlayer2);
            }

            Game = new Game(selectedGameType);

            dbService.GameSaveNew(Game, players);

            switch (selectedGameType)
            {
            case GameType.FreeThrowsSingleFreePoints:
                GameProcessor = new FreeThrowsSingleFreePointsProcessor(Game, players, dbService, scoreBoardService);
                scoreBoardService.OpenScoreBoard(selectedGameTypeUi, players, "Free throws");
                break;

            case GameType.FreeThrowsSingle301Points:
                GameProcessor = new FreeThrowsSingleWriteOffPointsProcessor(Game, players, dbService, scoreBoardService, 301);
                scoreBoardService.OpenScoreBoard(selectedGameTypeUi, players, "Write off 301", 301);
                break;

            case GameType.FreeThrowsSingle501Points:
                GameProcessor = new FreeThrowsSingleWriteOffPointsProcessor(Game, players, dbService, scoreBoardService, 501);
                scoreBoardService.OpenScoreBoard(selectedGameTypeUi, players, "Write off 501", 501);
                break;

            case GameType.FreeThrowsSingle701Points:
                GameProcessor = new FreeThrowsSingleWriteOffPointsProcessor(Game, players, dbService, scoreBoardService, 701);
                scoreBoardService.OpenScoreBoard(selectedGameTypeUi, players, "Write off 701", 701);
                break;

            case GameType.FreeThrowsSingle1001Points:
                GameProcessor = new FreeThrowsSingleWriteOffPointsProcessor(Game, players, dbService, scoreBoardService, 1001);
                scoreBoardService.OpenScoreBoard(selectedGameTypeUi, players, "Write off 1001", 1001);
                break;

            case GameType.FreeThrowsDoubleFreePoints:
                GameProcessor = new FreeThrowsDoubleFreePointsProcessor(Game, players, dbService, scoreBoardService);
                scoreBoardService.OpenScoreBoard(selectedGameTypeUi, players, "Free throws");
                break;

            case GameType.FreeThrowsDouble301Points:
                GameProcessor = new FreeThrowsDoubleWriteOffPointsProcessor(Game, players, dbService, scoreBoardService, 301);
                scoreBoardService.OpenScoreBoard(selectedGameTypeUi, players, "Write off 301", 301);
                break;

            case GameType.FreeThrowsDouble501Points:
                GameProcessor = new FreeThrowsDoubleWriteOffPointsProcessor(Game, players, dbService, scoreBoardService, 501);
                scoreBoardService.OpenScoreBoard(selectedGameTypeUi, players, "Write off 501", 501);
                break;

            case GameType.FreeThrowsDouble701Points:
                GameProcessor = new FreeThrowsDoubleWriteOffPointsProcessor(Game, players, dbService, scoreBoardService, 701);
                scoreBoardService.OpenScoreBoard(selectedGameTypeUi, players, "Write off 701", 701);
                break;

            case GameType.FreeThrowsDouble1001Points:
                GameProcessor = new FreeThrowsDoubleWriteOffPointsProcessor(Game, players, dbService, scoreBoardService, 1001);
                scoreBoardService.OpenScoreBoard(selectedGameTypeUi, players, "Write off 1001", 1001);
                break;

            case GameType.Classic301Points:
                break;

            case GameType.Classic501Points:
                break;

            case GameType.Classic701Points:
                break;

            case GameType.Classic1001Points:
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            Task.Run(() =>
            {
                IsGameRun = true;
                detectionService.OnThrowDetected += OnAnotherThrow;
                detectionService.OnStatusChanged += OnDetectionServiceStatusChanged;
                while (IsGameRun)
                {
                }

                detectionService.OnThrowDetected -= OnAnotherThrow;
                detectionService.OnStatusChanged -= OnDetectionServiceStatusChanged;
            });
        }