fix utf-8 encoding issues in logging FileHandler, round sleep time logs, add hard table logic, fix switched row/col key in generate

This commit is contained in:
Xevion
2021-01-24 19:12:00 -06:00
parent 9bebdafd7c
commit bc204fe13d
3 changed files with 30 additions and 22 deletions
+28 -20
View File
@@ -116,7 +116,7 @@ def generate_table_structure(filename: str, column_keys: List[str], row_keys: Li
# Iterate along the column keys and build the dictionary # Iterate along the column keys and build the dictionary
for x, col_key in enumerate(column_keys): for x, col_key in enumerate(column_keys):
for y, row_key in enumerate(row_keys): for y, row_key in enumerate(row_keys):
data[(col_key, row_key)] = raw_data[y][x] data[(row_key, col_key)] = raw_data[y][x]
return data return data
@@ -143,22 +143,33 @@ class Blackjack(object):
"""With all information presented, calculates the final decision.""" """With all information presented, calculates the final decision."""
choice: str = 'S' # Default is to stand choice: str = 'S' # Default is to stand
usedDefault = True
# Pair checking first # Pair checking first
if len(cards) == 2: if len(cards) == 2 and cards[0] == cards[1]:
if cards[0] == cards[1]: symbol = {cards[0].table}
symbol = {cards[0].table} logger.debug(f'Pair of {cards[0]} found.')
logger.debug(f'Pair of {cards[0]} found.') choice = Blackjack.access(Blackjack.PAIR, (f'{symbol}-{symbol}', dealer.table))
choice = Blackjack.access(Blackjack.PAIR, (f'{symbol}-{symbol}', dealer.table)) usedDefault = False
elif any(card.isAce() for card in cards):
if any(card.isAce() for card in cards):
sum_value = sum(card.value for card in cards if not card.isAce()) sum_value = sum(card.value for card in cards if not card.isAce())
if 2 < sum_value < 9: if 2 <= sum_value <= 9:
choice = Blackjack.access(Blackjack.SOFT, (f'A-{sum_value}', dealer.table)) choice = Blackjack.access(Blackjack.SOFT, (f'A-{sum_value}', dealer.table))
usedDefault = False
else: else:
logger.error( cards = ", ".join(card.symbol.upper() for card in cards)
f'Sum of cards was a Soft {sum_value} ({", ".join(card.symbol.upper() for card in cards)})') logger.error(f'Sum of cards was a Soft {sum_value} ({cards})')
else:
sum_value = sum(card.value for card in cards)
if 5 <= sum_value <= 20:
choice = Blackjack.access(Blackjack.HARD, (str(sum_value), dealer.table))
usedDefault = False
else:
cards = ", ".join(card.symbol.upper() for card in cards)
logger.error(f'Sum of cards was a Soft {sum_value} ({cards})')
if usedDefault:
logger.warning('No tables were accessed to make a choice. Defaulting to stand.')
return Blackjack.convert_letter(choice) return Blackjack.convert_letter(choice)
def options_convert(self, choice: str, options: Tuple[bool, bool, bool, bool]): def options_convert(self, choice: str, options: Tuple[bool, bool, bool, bool]):
@@ -166,28 +177,25 @@ class Blackjack(object):
new_choice = None new_choice = None
if choice == 'P': if choice == 'P':
if options.split: pass if not options.split:
else:
logger.warning(f'Poor options available for splitting. ({options})') logger.warning(f'Poor options available for splitting. ({options})')
new_choice = 'S' new_choice = 'S'
elif choice == 'D': elif choice == 'D':
if options.double: pass if not options.double:
else:
logger.warning(f'Poor options available for doubling. ({options})') logger.warning(f'Poor options available for doubling. ({options})')
new_choice = 'H' new_choice = 'H'
elif choice == 'H': elif choice == 'H':
if options.hit: pass if not options.hit:
else:
logger.error(f'Hit option preferred but not possible? ({options})') logger.error(f'Hit option preferred but not possible? ({options})')
new_choice = 'S' new_choice = 'S'
elif choice == 'S': elif choice == 'S':
if options.stand: pass if not options.stand:
else:
logger.error(f'Stand option preferred but not possible? ({options})') logger.error(f'Stand option preferred but not possible? ({options})')
new_choice = 'H' new_choice = 'H'
if new_choice is not None: if new_choice is not None:
logger.info(f'Option verification yielded a different method than originally selected: {choice} -> {new_choice}') logger.info(
f'Option verification yielded a different method than originally selected: {choice} -> {new_choice}')
return new_choice return new_choice
return choice return choice
+1 -1
View File
@@ -46,7 +46,7 @@ class Cooldown(object):
async def sleep(self) -> None: async def sleep(self) -> None:
if self.ready: if self.ready:
return return
logger.debug(f'Sleeping for {self.time_left} before sending a command.') logger.debug(f'Sleeping for {round(self.time_left, 2)}s before sending a command.')
await asyncio.sleep(self.time_left) await asyncio.sleep(self.time_left)
@property @property
+1 -1
View File
@@ -17,7 +17,7 @@ if __name__ == "__main__":
# noinspection PyArgumentList # noinspection PyArgumentList
logging.basicConfig(format='[%(asctime)s] [%(levelname)s] [%(funcName)s] %(message)s', logging.basicConfig(format='[%(asctime)s] [%(levelname)s] [%(funcName)s] %(message)s',
handlers=[ handlers=[
logging.FileHandler(f"bot-{parsed.channel}.log"), logging.FileHandler(f"bot-{parsed.channel}.log", encoding='utf-8'),
logging.StreamHandler() logging.StreamHandler()
]) ])
logger.setLevel(constants.LOGGING_LEVEL) logger.setLevel(constants.LOGGING_LEVEL)