def test_average_floor(self): dices = [Dice(4), Dice(9), Dice(0), Dice(4)] dices_modifier = DicesModifierType.AVERAGE_FLOOR result = dices_modifier.modify_dices(dices) self.assertEqual(result, [4])
def test_average_ceil(self): dices = [Dice(4), Dice(9), Dice(0), Dice(4)] dices_modifier = DicesModifierType.AVERAGE_CEIL result = dices_modifier.modify_dices(dices) self.assertEqual(result, [5])
def test_sum(self): dices = [Dice(4), Dice(9), Dice(0), Dice(4)] dices_modifier = DicesModifierType.SUM result = dices_modifier.modify_dices(dices) self.assertEqual(result, [17])
def test_max(self): dices = [Dice(4), Dice(9), Dice(0), Dice(4)] dices_modifier = DicesModifierType.MAX result = dices_modifier.modify_dices(dices) self.assertEqual(result, [9])
def test_sorted(self): dices = [Dice(4), Dice(9), Dice(0), Dice(4)] dices_modifier = DicesModifierType.SORTED result = dices_modifier.modify_dices(dices) self.assertEqual([0, 4, 4, 9], result)
def test_none(self): dices = [Dice(4), Dice(9), Dice(0), Dice(5)] dices_filter = DicesFilterType.NONE result = dices_filter.filter_dices(dices, None) self.assertEqual(result, dices)
def test_lower_than(self): dices = [Dice(4), Dice(9), Dice(0), Dice(5)] dices_filter = DicesFilterType.LOWER_THAN result = dices_filter.filter_dices(dices, None) self.assertEqual(result, dices)
def test_lower_than_limit(self): dices = [Dice(4), Dice(9), Dice(0), Dice(5)] dices_filter = DicesFilterType.LOWER_THAN result = dices_filter.filter_dices(dices, 5) self.assertEqual([4, 0], result)
def test_upper_than_limit(self): dices = [Dice(4), Dice(9), Dice(0), Dice(5)] dices_filter = DicesFilterType.UPPER_THAN result = dices_filter.filter_dices(dices, 4) self.assertEqual([9, 5], result)
def test_equal_to_value(self): dices = [Dice(4), Dice(9), Dice(0), Dice(4)] dices_filter = DicesFilterType.EQUAL result = dices_filter.filter_dices(dices, 4) self.assertEqual([4, 4], result)
def test_equal(self): dices = [Dice(4), Dice(9), Dice(0), Dice(4)] dices_filter = DicesFilterType.EQUAL result = dices_filter.filter_dices(dices, None) self.assertEqual(result, dices)
def test_none(self): dices = [Dice(4), Dice(9), Dice(0), Dice(5)] dices_modifier = DicesModifierType.NONE result = dices_modifier.modify_dices(dices) self.assertEqual(result, dices)
def test_reverse_sorted(self): dices = [Dice(4), Dice(9), Dice(0), Dice(4)] dices_modifier = DicesModifierType.REVERSE_SORTED result = dices_modifier.modify_dices(dices) self.assertEqual([9, 4, 4, 0], result)
def test_value(self): value: int = 20 dice = Dice(value) self.assertEqual(dice.value, value) self.assertEqual(int(dice), value) self.assertEqual(str(dice), str(value))
def __max_dice(ones_dice: OnesDice, main_tens_dice: TensDice, extra_dices: [TensDice]) -> Dice: tens_dices: [TensDice] = [main_tens_dice] + extra_dices if ones_dice == 0 and any(it == 0 for it in tens_dices): return Dice(0) else: return max(tens_dices)
def test_roll(self, randrange_mock): (result, min_value, max_value, step) = (7, 5, 10, 2) randrange_mock.return_value = result dice = Dice.roll(min_value, max_value, step) randrange_mock.assert_called_with(min_value, max_value + 1, step) self.assertEqual(dice.value, result)
def modify_dices(self, dices: [Dice]) -> [Dice]: if self == self.MIN: result = [min(dices)] elif self == self.MAX: result = [max(dices)] elif self == self.SORTED: result = sorted(dices) elif self == self.REVERSE_SORTED: result = sorted(dices, reverse=True) elif self == self.SUM: result = [sum(dices)] elif self == self.AVERAGE_FLOOR: value = self.__average(dices) result = [Dice(floor(value))] elif self == self.AVERAGE_CEIL: value = self.__average(dices) result = [Dice(ceil(value))] elif self == self.NONE: result = dices else: raise Exception('Unsupported DicesModifier') return result
def __resolve_dices(cls, dices_pattern: str) -> [Dice]: matches = cls.__DICE_TYPE_PATTERN.match(dices_pattern) if not matches: raise DiceException(f'Unsupported dice type: {dices_pattern}') groups: [str] = matches.groups() dice_amount: int = int(groups[0]) if (groups[0] and groups[1]) else 1 dice_range: int = int(groups[1]) if groups[1] else int(groups[0]) if dice_amount < 1: raise DiceException( f'Dice amount must be positive, but is: {dice_amount}') return [Dice.roll(1, dice_range) for _ in range(0, dice_amount)]
def __create_result(self, result_dice: (TensDice, OnesDice), all_dices: ([TensDice], [OnesDice]), threshold: int): (result_tens_dice, result_ones_dice) = result_dice result_dice = result_tens_dice + result_ones_dice result_dice = Dice(100) if result_dice == 0 else result_dice result_type = self.__skill_result_type(result_dice, threshold) description = self.__describe_roll(result_dice, result_tens_dice, result_ones_dice, all_dices) return SkillCheckResult(value=result_dice, type=result_type, descriptions=[description], user=self.__user, basic_arguments=self.__arguments)
def roll(self) -> OvercomeTroubleResult: dices = [ Dice.roll(1, 6) for _ in range(0, self.__arguments.dice_amount) ] successes = self.__filter_successes(dices) successes_amount = len(successes) result_type = self.__check_result_type( successes_amount, self.__arguments.success_requirement) description = self.__describe_roll(successes_amount, dices) return OvercomeTroubleResult(user=self.__user, descriptions=[description], type=result_type, success_amount=successes_amount, dices=dices, basic_arguments=self.__arguments)
def __roll_dice() -> Dice: return Dice.roll(1, 6)
def test_operators(self): self.assertTrue(Dice(10) == 10) self.assertTrue(Dice(1) != Dice(10)) self.assertFalse(Dice(10) < Dice(1)) self.assertTrue(Dice(10) <= Dice(10)) self.assertFalse(Dice(10) > Dice(100)) self.assertTrue(Dice(10) >= Dice(1)) self.assertTrue(Dice(10) + Dice(5) == Dice(15)) self.assertTrue(Dice(10) - Dice(2) == Dice(8))