implement blackjack prediction into client logging, privatize Blackjack data variables, letter conversion function

This commit is contained in:
Xevion
2021-01-24 19:13:21 -06:00
parent bc204fe13d
commit 58884a43ff
2 changed files with 29 additions and 33 deletions

View File

@@ -123,20 +123,20 @@ def generate_table_structure(filename: str, column_keys: List[str], row_keys: Li
class Blackjack(object): class Blackjack(object):
# The row and column headers for the hair, soft and pair baseline tables. # The row and column headers for the hair, soft and pair baseline tables.
hard_column = ['2', '3', '4', '5', '6', '7', '8', '9', 'T', 'A'] __hard_column = ['2', '3', '4', '5', '6', '7', '8', '9', 'T', 'A']
hard_row = ['20', '19', '18', '17', '16', '15', '14', '13', '12', '11', '10', '9', '8', '7', '6', '5'] __hard_row = ['20', '19', '18', '17', '16', '15', '14', '13', '12', '11', '10', '9', '8', '7', '6', '5']
soft_column = ['2', '3', '4', '5', '6', '7', '8', '9', 'T', 'A'] __soft_column = ['2', '3', '4', '5', '6', '7', '8', '9', 'T', 'A']
soft_row = ['A-9', 'A-8', 'A-7', 'A-6', 'A-5', 'A-4', 'A-3', 'A-2'] __soft_row = ['A-9', 'A-8', 'A-7', 'A-6', 'A-5', 'A-4', 'A-3', 'A-2']
pair_column = ['2', '3', '4', '5', '6', '7', '8', '9', 'T', 'A'] __pair_column = ['2', '3', '4', '5', '6', '7', '8', '9', 'T', 'A']
pair_row = ['A-A', 'T-T', '9-9', '8-8', '7-7', '6-6', '5-5', '4-4', '3-3', '2-2'] __pair_row = ['A-A', 'T-T', '9-9', '8-8', '7-7', '6-6', '5-5', '4-4', '3-3', '2-2']
hard_data = generate_table_structure('baseline_hard.dat', hard_column, hard_row) __letter_meanings = {'H': 'hit', 'S': 'stand', 'D': 'double down', 'P': 'split'}
soft_data = generate_table_structure('baseline_soft.dat', soft_column, soft_row)
pair_data = generate_table_structure('baseline_pairs.dat', pair_column, pair_row)
HARD = 0 __hard_data = generate_table_structure('baseline_hard.dat', __hard_column, __hard_row)
SOFT = 1 __soft_data = generate_table_structure('baseline_soft.dat', __soft_column, __soft_row)
PAIR = 2 __pair_data = generate_table_structure('baseline_pairs.dat', __pair_column, __pair_row)
HARD, SOFT, PAIR = 0, 1, 2
@staticmethod @staticmethod
def choose(options: constants.PlayOptions, cards: List[Card], dealer: Card) -> str: def choose(options: constants.PlayOptions, cards: List[Card], dealer: Card) -> str:
@@ -172,7 +172,12 @@ class Blackjack(object):
logger.warning('No tables were accessed to make a choice. Defaulting to stand.') 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]): @classmethod
def convert_letter(cls, letter: str) -> str:
"""Simple class method for returning the player response to the bot based on the letter given."""
return Blackjack.__letter_meanings[letter]
def options_convert(self, choice: str, options: constants.PlayOptions) -> str:
"""Converts the choice to the best possible choice based on the options given by the bot.""" """Converts the choice to the best possible choice based on the options given by the bot."""
new_choice = None new_choice = None
@@ -208,6 +213,6 @@ class Blackjack(object):
:param table: :param table:
:return: :return:
""" """
if table == cls.HARD: return cls.hard_data[key] if table == cls.HARD: return cls.__hard_data[key]
if table == cls.SOFT: return cls.soft_data[key] if table == cls.SOFT: return cls.__soft_data[key]
if table == cls.PAIR: return cls.pair_data[key] if table == cls.PAIR: return cls.__pair_data[key]

View File

@@ -1,15 +1,12 @@
import asyncio
import ctypes import ctypes
import logging import logging
import re from typing import Optional
from datetime import datetime
from typing import Optional, Tuple
import discord import discord
from discord.ext.tasks import loop from discord.ext.tasks import loop
from bot import constants, parsers, timings, helpers from bot import constants, parsers, timings, helpers
from bot.blackjack import Card from bot.blackjack import Card, Blackjack
from bot.constants import PlayOptions from bot.constants import PlayOptions
logger = logging.getLogger(__file__) logger = logging.getLogger(__file__)
@@ -31,7 +28,7 @@ class UnbelievaClient(discord.Client):
'$crime': timings.Cooldown(20 * 60 + 2), '$crime': timings.Cooldown(20 * 60 + 2),
'$dep all': timings.Cooldown(30 * 60) '$dep all': timings.Cooldown(30 * 60)
} }
self.command_cooldown = timings.Cooldown(5) self.command_cooldown = timings.Cooldown(6.5)
self.money = 0 self.money = 0
self.last_deposit = -1 self.last_deposit = -1
@@ -73,9 +70,11 @@ class UnbelievaClient(discord.Client):
# Handling for blackjack # Handling for blackjack
if embed.description.startswith('Type `hit` to draw another card'): if embed.description.startswith('Type `hit` to draw another card'):
options = self.parse_options(embed.description) options = self.parse_options(embed.description)
my_cards = Card.parse_cards(embed.fields[0]) my_cards = Card.parse_cards(embed.fields[0])[1]
dealer_cards = Card.parse_cards(embed.fields[1]) dealer_card = Card.parse_cards(embed.fields[1])[1][0]
print(options, my_cards, dealer_cards)
choice = Blackjack.choose(options, my_cards, dealer_card)
logger.info(f'Predicted best choice for Blackjack: {choice}')
def parse_options(self, options_str: str) -> PlayOptions: def parse_options(self, options_str: str) -> PlayOptions:
""" """
@@ -86,14 +85,6 @@ class UnbelievaClient(discord.Client):
# noinspection PyProtectedMember # noinspection PyProtectedMember
return PlayOptions._make(options) return PlayOptions._make(options)
def handle_blackjack(self):
embed = self.current_blackjack.embeds[0]
options = self.parse_options(embed.description)
my_cards = self.parse_cards(embed.fields[0])
dealer_cards = self.parse_cards(embed.fields[1])
print(options, my_cards, dealer_cards)
pass
@loop(seconds=1) @loop(seconds=1)
async def check_task_available(self): async def check_task_available(self):
"""Loop to run tasks as soon as they are available.""" """Loop to run tasks as soon as they are available."""