diff --git a/gameloop.py b/gameloop.py index ecf34cf..76d8c21 100644 --- a/gameloop.py +++ b/gameloop.py @@ -5,7 +5,6 @@ from collections import namedtuple import cardcast_api # TODO: rando -# TODO: $0 support # TODO: collapse several _ in a row into one # TODO: keep track of where cards come from purge from hand when deck remove @@ -520,11 +519,45 @@ def game(send, notice, get_event): notice(nick, cards) def combine_cards(call, responses): - combined = [sanitize(call[0])] + def handle_call_part(call_part): + nonlocal responses + + r = [] + after_dollar = False + for char in call_part: + if after_dollar and ord('0') <= ord(char) <= ord('9'): + # Handle $0 .. $9 + # Hopefully we won't run into more backreferences + # in one card + index = int(char) + + if 0 <= index < len(responses): + r.append(responses[index]) + else: + # Not valid backreference, copy verbatim + r.append('$' + char) + + after_dollar = False + + elif after_dollar: + # Wasn't a backreference, copy verbatim + r.append('$' + char) + + after_dollar = False + + elif char == '$': + after_dollar = True + + else: + r.append(char) + + return sanitize(''.join(r)) + + combined = [handle_call_part(call[0])] for i in range(len(call) - 1): combined.append('[' + sanitize(responses[i]) + ']') - combined.append(sanitize(call[i + 1])) + combined.append(handle_call_part(call[i + 1])) return ''.join(combined)