Exemplo n.º 1
0
        public void WhenDiceRolled_Should_ReturnNumberBw1And6()
        {
            var sut    = new DiceService();
            var result = sut.RollDice();

            result.Should().BeInRange(1, 6);
        }
Exemplo n.º 2
0
        public void RollWhiteDice(Random rnd, Dice dice, DB db, DbContextTransaction transaction)
        {
            try
            {
                DiceService _diceService = new DiceService(db);

                while (true && !_rollWhiteDiceThreadAborted && !_rollWhiteDiceThreadStopped)
                {
                    RollResult _rollResult = _diceService.RollDice(rnd, dice);
                    if (Mut.WaitOne())
                    {
                        try
                        {
                            if (!_transactionDisposed)
                            {
                                db.RollResults.Add(_rollResult);
                                db.SaveChanges();
                                if (_rollResult.RollValue == 1)
                                {
                                    _whiteAcesCounter++;
                                }
                                _whiteThrowsCounter++;
                                if (!lbl_white_dice_ace_counter.IsDisposed)
                                {
                                    lbl_white_dice_ace_counter.BeginInvoke((MethodInvoker)(() =>
                                    {
                                        lbl_white_dice_ace_counter.Text = _whiteAcesCounter.ToString();
                                    }));
                                }
                                if (!lbl_white_dice_throws_counter.IsDisposed)
                                {
                                    lbl_white_dice_throws_counter.BeginInvoke((MethodInvoker)(() =>
                                    {
                                        lbl_white_dice_throws_counter.Text = _whiteThrowsCounter.ToString();
                                    }));
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                        }
                        finally
                        {
                            Mut.ReleaseMutex();
                        }
                    }
                    Debug.WriteLine(_rollResult.ToString());
                }
                if (_rollWhiteDiceThreadStopped)
                {
                    if (Mut.WaitOne())
                    {
                        try
                        {
                            if (!_transactionDisposed)
                            {
                                transaction.Commit();
                                transaction.Dispose();
                                _transactionDisposed = true;
                            }
                        }
                        catch (Exception ex)
                        {
                        }
                        finally
                        {
                            Mut.ReleaseMutex();
                        }
                    }
                }
                if (_rollWhiteDiceThreadAborted)
                {
                    if (Mut.WaitOne())
                    {
                        try
                        {
                            if (!_transactionDisposed)
                            {
                                transaction.Rollback();
                                transaction.Dispose();
                                _transactionDisposed = true;
                                ResetCounters(db);
                            }
                        }
                        catch (Exception ex)
                        {
                        }
                        finally
                        {
                            Mut.ReleaseMutex();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }