Skip to content

Commit

Permalink
Re #6: Track leader rerolls and don't Loner them
Browse files Browse the repository at this point in the history
Fixes #6
  • Loading branch information
IBBoard committed Jul 26, 2021
1 parent 8d33d16 commit f57c2ae
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 9 deletions.
25 changes: 16 additions & 9 deletions bbreplay/replay.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from .log import parse_log_entries, MatchLogEntry, StupidEntry, DodgeEntry, SkillEntry, ArmourValueRollEntry, \
PickupEntry, TentacledEntry, RerollEntry, TurnOverEntry, BounceLogEntry, FoulAppearanceEntry, LeapEntry, \
ThrowInDirectionLogEntry, CatchEntry, KORecoveryEntry, ThrowEntry, GoingForItEntry, WildAnimalEntry, \
SkillRollEntry, ApothecaryLogEntry
SkillRollEntry, ApothecaryLogEntry, LeaderRerollEntry
from .state import GameState
from .state import StartTurn, EndTurn, WeatherTuple, AbandonMatch, EndMatch # noqa: F401 - these are for export
from .teams import create_team
Expand Down Expand Up @@ -417,12 +417,19 @@ def _process_action_reroll(self, cmds, log_entries, player, board, reroll_skill=
f"but got {type(cmd).__name__}")
elif board.can_reroll(player.team.team_type):
can_reroll = True
if Skills.LONER in player.skills:
log_entry = next(log_entries, None)
if log_entry:
validate_log_entry(log_entry, SkillRollEntry, player.team.team_type, player.number)
actions.append(SkillRoll(player, Skills.LONER, log_entry.result, board))
can_reroll = log_entry.result == ActionResult.SUCCESS
has_leader_reroll = board.has_leader_reroll(player.team.team_type)
if has_leader_reroll:
expected_class = LeaderRerollEntry
reroll_type = 'Leader Reroll'
else:
expected_class = RerollEntry
reroll_type = 'Team Reroll'
if Skills.LONER in player.skills and not has_leader_reroll:
log_entry = next(log_entries, None)
if log_entry:
validate_log_entry(log_entry, SkillRollEntry, player.team.team_type, player.number)
actions.append(SkillRoll(player, Skills.LONER, log_entry.result, board))
can_reroll = log_entry.result == ActionResult.SUCCESS

if can_reroll:
cmd = next(cmds)
Expand All @@ -434,9 +441,9 @@ def _process_action_reroll(self, cmds, log_entries, player, board, reroll_skill=
pass
elif isinstance(cmd, RerollCommand):
log_entry = next(log_entries)
validate_log_entry(log_entry, RerollEntry, player.team.team_type)
validate_log_entry(log_entry, expected_class, player.team.team_type)
board.use_reroll(player.team.team_type)
actions.append(Reroll(cmd.team, 'Team Reroll'))
actions.append(Reroll(cmd.team, reroll_type))
reroll_success = True
elif isinstance(cmd, DiceChoiceCommand):
# Sometimes we only get cmd_type=19 even though most of the time
Expand Down
11 changes: 11 additions & 0 deletions bbreplay/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def __init__(self, home_team, away_team, receiving_team):
self.__last_setup_turn = 0
self.__moves = defaultdict(int)
self.__used_reroll = False
self.__leader_reroll = [False, False]
self.__kicked_off = False

@property
Expand Down Expand Up @@ -102,9 +103,15 @@ def setup_complete(self):
if any(player.is_on_pitch() and Skills.LEADER in player.skills
for player in self.teams[TeamType.HOME.value].get_players()):
self.add_reroll(TeamType.HOME)
self.__leader_reroll[TeamType.HOME.value] = True
else:
self.__leader_reroll[TeamType.HOME.value] = False
if any(player.is_on_pitch() and Skills.LEADER in player.skills
for player in self.teams[TeamType.AWAY.value].get_players()):
self.add_reroll(TeamType.AWAY)
self.__leader_reroll[TeamType.AWAY.value] = True
else:
self.__leader_reroll[TeamType.AWAY.value] = False
self.__last_setup_turn = self.turn
self.__kicked_off = False

Expand Down Expand Up @@ -265,13 +272,17 @@ def use_reroll(self, team):
raise ValueError("Already used a team reroll this turn!")
self.rerolls[team.value] -= 1
self.__used_reroll = True
self.__leader_reroll[team.value] = False

def can_reroll(self, team):
return self.rerolls[team.value] > 0 and not self.__used_reroll

def add_reroll(self, team):
self.rerolls[team.value] += 1

def has_leader_reroll(self, team):
return self.__leader_reroll[team.value]

def has_apothecary(self, team):
return self.apothecaries[team.value] > 0

Expand Down
29 changes: 29 additions & 0 deletions tests/test_replay__reroll.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,32 @@ def test_skill_reroll_not_allowed_with_cancelling_skill(board):
assert event.type == "Team Reroll"

assert new_result == ActionResult.FAILURE


def test_leader_reroll_on_loner(board):
home_team, away_team = board.teams
replay = Replay(home_team, away_team, [], [])
player = home_team.get_player(0)
player.skills.append(Skills.LONER)
home_player_leader = Player(2, "Player2H", 4, 4, 4, 4, 1, 0, 40000, [Skills.LEADER])
home_team.add_player(1, home_player_leader)
board.set_position(Position(1, 1), home_player_leader)
board.setup_complete()
cmds = [
RerollCommand(1, 1, TeamType.HOME, 1, [])
]
log_entries = [
LeaderRerollEntry(TeamType.HOME, 2),
DodgeEntry(TeamType.HOME, 1, "2+", "1", ActionResult.FAILURE.name)
]
cmds_iter = iter_(cmds)
log_entries_iter = iter_(log_entries)
actions, new_result = replay._process_action_reroll(cmds_iter, log_entries_iter, player, board)

assert len(actions) == 1
event = actions[0]
assert isinstance(event, Reroll)
assert event.team == player.team.team_type
assert event.type == "Leader Reroll"

assert new_result is ActionResult.FAILURE

0 comments on commit f57c2ae

Please sign in to comment.