62 lines
2.4 KiB
Python
62 lines
2.4 KiB
Python
import logging
|
|
from typing import Literal
|
|
|
|
import discord
|
|
from discord import app_commands
|
|
from discord.ext import commands
|
|
|
|
from helpers import PD_PLAYERS_ROLE_NAME
|
|
from in_game.game_helpers import PUBLIC_FIELDS_CATEGORY_NAME
|
|
from in_game.gameplay_db import Session, engine, select, Game
|
|
|
|
|
|
def get_games_by_channel(session: Session, channel_id: int):
|
|
return session.exec(select(Game).where(Game.channel_id == channel_id)).all()
|
|
|
|
|
|
class Gameplay(commands.Cog):
|
|
def __init__(self, bot):
|
|
self.bot = bot
|
|
|
|
async def cog_command_error(self, ctx, error):
|
|
await ctx.send(f'{error}\n\nRun !help <command_name> to see the command requirements')
|
|
|
|
async def slash_error(self, ctx, error):
|
|
await ctx.send(f'{error[:1600]}')
|
|
|
|
group_new_game = app_commands.Group(name='new-game', description='Start a new baseball game')
|
|
|
|
@group_new_game.command(name='mlb-campaign', description='Start a new MLB campaign game against an AI')
|
|
@app_commands.rename(
|
|
league='Campaign',
|
|
away_team_abbrev='Away Team Abbrev',
|
|
home_team_abbrev='Home Team Abbrev',
|
|
sp_card_id='SP Card ID',
|
|
num_innings='Number of Innings'
|
|
)
|
|
@app_commands.checks.has_any_role(PD_PLAYERS_ROLE_NAME)
|
|
async def new_game_mlb_campaign_command(
|
|
self, interaction: discord.Interaction,
|
|
league: Literal['Minor League', 'Flashback', 'Major League', 'Hall of Fame'],
|
|
away_team_abbrev: str, home_team_abbrev: str, sp_card_id: int, num_innings: Literal[9, 3] = 9
|
|
):
|
|
await interaction.response.defer()
|
|
|
|
with Session(engine) as session:
|
|
conflict = get_games_by_channel(session, channel_id=interaction.channel_id)
|
|
if len(conflict) > 0:
|
|
await interaction.edit_original_response(
|
|
content=f'Ope. There is already a game going on in this channel. Please wait for it to complete '
|
|
f'before starting a new one.'
|
|
)
|
|
|
|
if interaction.channel.category is None or interaction.channel.category.name != PUBLIC_FIELDS_CATEGORY_NAME:
|
|
await interaction.response.send_message(
|
|
f'Why don\'t you head down to one of the Public Fields that way other humans can help if anything '
|
|
f'pops up?'
|
|
)
|
|
return
|
|
|
|
|
|
async def setup(bot):
|
|
await bot.add_cog(Gameplay(bot)) |