forked from c0de/BaseballBot
Bot now explains who won each play
This commit is contained in:
parent
143fd45149
commit
caa0819d91
@ -82,3 +82,22 @@ class GuessDAO():
|
|||||||
.order_by(Guess.difference)\
|
.order_by(Guess.difference)\
|
||||||
.limit(num_to_fetch)
|
.limit(num_to_fetch)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def get_closest_on_play(self, play):
|
||||||
|
session = DatabaseSession.session
|
||||||
|
|
||||||
|
# TODO: Make this a MAX query for ties
|
||||||
|
converted_guesses = self.__convert_all__(
|
||||||
|
session
|
||||||
|
.query(Guess)
|
||||||
|
.filter(Guess.play_id == play)
|
||||||
|
.order_by(Guess.difference)
|
||||||
|
.limit(1)
|
||||||
|
)
|
||||||
|
|
||||||
|
if len(converted_guesses) > 1:
|
||||||
|
raise AssertionError("More than one best guess! Can't continue!")
|
||||||
|
elif len(converted_guesses) == 0:
|
||||||
|
return None
|
||||||
|
else:
|
||||||
|
return converted_guesses[0]
|
@ -5,7 +5,7 @@ import uuid
|
|||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
from src.main.configs import Configs
|
from src.main.configs import Configs
|
||||||
from src.main.database_module.guess_dao import GuessDAO, GUESSED_NUMBER, MEMBER_ID, MEMBER_NAME
|
from src.main.database_module.guess_dao import GuessDAO, GUESSED_NUMBER, MEMBER_ID, MEMBER_NAME, DIFFERENCE
|
||||||
from src.main.database_module.play_dao import PlayDAO, PLAY_ID, CREATION_DATE
|
from src.main.database_module.play_dao import PlayDAO, PLAY_ID, CREATION_DATE
|
||||||
from src.main.db_session import DatabaseSession
|
from src.main.db_session import DatabaseSession
|
||||||
from src.main.discord_module.leaderboard_config import LeaderboardConfig
|
from src.main.discord_module.leaderboard_config import LeaderboardConfig
|
||||||
@ -14,6 +14,7 @@ play_dao = None
|
|||||||
guess_dao = None
|
guess_dao = None
|
||||||
bot = discord.Client()
|
bot = discord.Client()
|
||||||
|
|
||||||
|
|
||||||
@bot.event
|
@bot.event
|
||||||
async def on_ready():
|
async def on_ready():
|
||||||
print('Logged in as')
|
print('Logged in as')
|
||||||
@ -21,6 +22,7 @@ async def on_ready():
|
|||||||
print(bot.user.id)
|
print(bot.user.id)
|
||||||
print('------')
|
print('------')
|
||||||
|
|
||||||
|
|
||||||
@bot.event
|
@bot.event
|
||||||
async def on_message(message):
|
async def on_message(message):
|
||||||
if message.author == bot.user:
|
if message.author == bot.user:
|
||||||
@ -35,16 +37,16 @@ async def on_message(message):
|
|||||||
if play_dao.is_active_play():
|
if play_dao.is_active_play():
|
||||||
await message.channel.send("There's already an active play. Could you close that one first, please?")
|
await message.channel.send("There's already an active play. Could you close that one first, please?")
|
||||||
else:
|
else:
|
||||||
play_object = {PLAY_ID : uuid.uuid4(), CREATION_DATE : datetime.datetime.now()}
|
play_object = {PLAY_ID: uuid.uuid4(), CREATION_DATE: datetime.datetime.now()}
|
||||||
play_dao.insert(play_object)
|
play_dao.insert(play_object)
|
||||||
|
|
||||||
await message.channel.send( "@flappyball, pitch is in! Send me your guesses with a !guess command.")
|
await message.channel.send("@flappyball, pitch is in! Send me your guesses with a !guess command.")
|
||||||
|
|
||||||
if content.startswith("!guess"):
|
if content.startswith("!guess"):
|
||||||
guess_value = __parse_guess__(content)
|
guess_value = __parse_guess__(content)
|
||||||
|
|
||||||
if not play_dao.is_active_play():
|
if not play_dao.is_active_play():
|
||||||
await message.channel.send( "Hey, there's no active play! Start one up first with !ghostball.")
|
await message.channel.send("Hey, there's no active play! Start one up first with !ghostball.")
|
||||||
else:
|
else:
|
||||||
play = play_dao.get_active_play()
|
play = play_dao.get_active_play()
|
||||||
guess_object = {PLAY_ID: play['play_id'],
|
guess_object = {PLAY_ID: play['play_id'],
|
||||||
@ -60,19 +62,22 @@ async def on_message(message):
|
|||||||
# try:
|
# try:
|
||||||
pitch_value = __parse_resolve_play__(content)
|
pitch_value = __parse_resolve_play__(content)
|
||||||
if pitch_value is None:
|
if pitch_value is None:
|
||||||
await message.channel.send( "Hey " + "<@" + str(message.author.id) + ">, I'm not sure what you meant. "
|
await message.channel.send("Hey " + "<@" + str(message.author.id) + ">, I'm not sure what you meant. "
|
||||||
"You need real, numeric, values for this command to work. "
|
"You need real, numeric, values for this command to work. "
|
||||||
"Use !resolve <pitch number> <swing_number> and try again.")
|
"Use !resolve <pitch number> and try again.")
|
||||||
|
|
||||||
# Check if we have an active play
|
# Check if we have an active play
|
||||||
if not play_dao.is_active_play():
|
if not play_dao.is_active_play():
|
||||||
await message.channel.send( "You confused me. There's no active play so I have nothing to close!")
|
await message.channel.send("You confused me. There's no active play so I have nothing to close!")
|
||||||
else:
|
else:
|
||||||
play = play_dao.resolve_play(pitch_value)
|
play = play_dao.resolve_play(pitch_value)
|
||||||
guess_dao.set_differences(pitch_value, play['play_id'])
|
guess_dao.set_differences(pitch_value, play['play_id'])
|
||||||
|
closest_guess = guess_dao.get_closest_on_play(play['play_id'])
|
||||||
|
|
||||||
await message.channel.send(
|
await message.channel.send(
|
||||||
"You got it boss. Closed this play, no further guesses allowed!")
|
"Closed this play! " + "<@" + str(closest_guess[MEMBER_ID]) +
|
||||||
|
"> was the closest with a guess of " + closest_guess[GUESSED_NUMBER] +
|
||||||
|
" resulting in a difference of " + closest_guess[DIFFERENCE] + ".")
|
||||||
|
|
||||||
# Likely due to too few parameters but could be any number of things
|
# Likely due to too few parameters but could be any number of things
|
||||||
# except :
|
# except :
|
||||||
@ -90,19 +95,24 @@ async def on_message(message):
|
|||||||
for i, value in enumerate(values):
|
for i, value in enumerate(values):
|
||||||
string_to_send += str(i + 1) + ': ' + value['member_name'] + ', ' + value['difference'] + '\n'
|
string_to_send += str(i + 1) + ': ' + value['member_name'] + ', ' + value['difference'] + '\n'
|
||||||
|
|
||||||
await message.channel.send( string_to_send)
|
await message.channel.send(string_to_send)
|
||||||
|
|
||||||
elif leaderboard_config.should_sort_by_best_average():
|
elif leaderboard_config.should_sort_by_best_average():
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
await message.channel.send( "I don't understand that leaderboard command, sorry! I know it's a little confusing, so send me"
|
await message.channel.send(
|
||||||
|
"I don't understand that leaderboard command, sorry! I know it's a little confusing, so send me"
|
||||||
" a !help message if you want the full rundown for how to make this work!")
|
" a !help message if you want the full rundown for how to make this work!")
|
||||||
|
|
||||||
|
if content.startswith("!points"):
|
||||||
|
pass #TODO
|
||||||
|
|
||||||
if content.startswith('!help'):
|
if content.startswith('!help'):
|
||||||
help_message = __get_help_message__()
|
help_message = __get_help_message__()
|
||||||
recipient = await bot.fetch_user(message.author.id)
|
recipient = await bot.fetch_user(message.author.id)
|
||||||
await recipient.send(help_message)
|
await recipient.send(help_message)
|
||||||
|
|
||||||
|
|
||||||
def __get_help_message__():
|
def __get_help_message__():
|
||||||
# Start message with person who asked for help
|
# Start message with person who asked for help
|
||||||
help_message = "Hey! I can be instructed to do any number of things! Use the following commands: \n" \
|
help_message = "Hey! I can be instructed to do any number of things! Use the following commands: \n" \
|
||||||
@ -120,6 +130,7 @@ def __get_help_message__():
|
|||||||
def __parse_leaderboard_message__(message_content):
|
def __parse_leaderboard_message__(message_content):
|
||||||
return LeaderboardConfig(message_content)
|
return LeaderboardConfig(message_content)
|
||||||
|
|
||||||
|
|
||||||
def __parse_guess__(message_content):
|
def __parse_guess__(message_content):
|
||||||
pieces = message_content.split(' ')
|
pieces = message_content.split(' ')
|
||||||
try:
|
try:
|
||||||
@ -127,6 +138,7 @@ def __parse_guess__(message_content):
|
|||||||
except TypeError:
|
except TypeError:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def __parse_resolve_play__(message_content):
|
def __parse_resolve_play__(message_content):
|
||||||
pieces = message_content.split(' ')
|
pieces = message_content.split(' ')
|
||||||
try:
|
try:
|
||||||
@ -134,6 +146,7 @@ def __parse_resolve_play__(message_content):
|
|||||||
except TypeError:
|
except TypeError:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
args = sys.argv
|
args = sys.argv
|
||||||
token = args[1]
|
token = args[1]
|
||||||
|
Loading…
Reference in New Issue
Block a user