Compare commits
5 Commits
38b549e10c
...
65192ab2c2
Author | SHA1 | Date | |
---|---|---|---|
65192ab2c2 | |||
67a018f897 | |||
04e1556d2d | |||
ddbb884a43 | |||
b43ee8a39c |
44
GhostBallBot/database/models.py
Normal file
44
GhostBallBot/database/models.py
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
# Copyright 2022 - c0de <c0de@c0de.dev>
|
||||||
|
# Licensed under the MIT License (https://opensource.org/licenses/MIT)
|
||||||
|
|
||||||
|
import os
|
||||||
|
import datetime
|
||||||
|
|
||||||
|
from peewee import *
|
||||||
|
|
||||||
|
# User can provide path to database, or it will be put next to models.py
|
||||||
|
DATABASE = os.environ.get('database_path', os.getcwd() + '/ghostball.db')
|
||||||
|
database = SqliteDatabase(DATABASE)
|
||||||
|
|
||||||
|
class BaseModel(Model):
|
||||||
|
"""All of our models will inherit this class
|
||||||
|
and use the same database connection"""
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
database = database
|
||||||
|
|
||||||
|
class GameModel(BaseModel):
|
||||||
|
|
||||||
|
game_id = UUIDField(primary_key=True)
|
||||||
|
server_id = UUIDField() # Unsure if this is actually a uuid
|
||||||
|
|
||||||
|
pitch_value = IntegerField(null=True)
|
||||||
|
date_created = DateTimeField(default=datetime.datetime.now)
|
||||||
|
|
||||||
|
class GuessModel(BaseModel):
|
||||||
|
|
||||||
|
player_id = IntegerField(primary_key=True)
|
||||||
|
game_id = ForeignKeyField(Game, backref="guesses")
|
||||||
|
|
||||||
|
player_name = CharField()
|
||||||
|
guess = IntegerField()
|
||||||
|
difference = IntegerField(null=True)
|
||||||
|
|
||||||
|
# TODO: Add unique constraint for player_id and game_id
|
||||||
|
# ie: one guess per player allowed per game
|
||||||
|
|
||||||
|
|
||||||
|
def create_models():
|
||||||
|
with database:
|
||||||
|
database.create_tables([Play, Guess])
|
41
GhostBallBot/discord_client/client.py
Normal file
41
GhostBallBot/discord_client/client.py
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
# Copyright 2022 - c0de <c0de@c0de.dev>
|
||||||
|
# Licensed under the MIT License (https://opensource.org/licenses/MIT)
|
||||||
|
|
||||||
|
import sys
|
||||||
|
|
||||||
|
import discord
|
||||||
|
|
||||||
|
# Import game functions
|
||||||
|
sys.path.append('..')
|
||||||
|
import game
|
||||||
|
|
||||||
|
class GhostBallClient(discord.Client):
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
super(GhostBallClient, self).__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
self.game = game.Game()
|
||||||
|
self.game.discord = self
|
||||||
|
|
||||||
|
async def on_ready(self):
|
||||||
|
print("Logged on as", self.user)
|
||||||
|
|
||||||
|
async def on_message(self, message):
|
||||||
|
# Don't respond to ourself
|
||||||
|
if message.author == self.user:
|
||||||
|
return
|
||||||
|
|
||||||
|
# Bot health check
|
||||||
|
if message.content == "ping":
|
||||||
|
await message.channel.send("pong")
|
||||||
|
|
||||||
|
# Game commands
|
||||||
|
if message.content.startswith('!'):
|
||||||
|
firstword = message.content[1:].split()[0]
|
||||||
|
|
||||||
|
# Determine if the first word is a command, and run it
|
||||||
|
for command, function in self.game.commands:
|
||||||
|
if firstword == command:
|
||||||
|
self.game.message = message
|
||||||
|
await function()
|
108
GhostBallBot/game.py
Normal file
108
GhostBallBot/game.py
Normal file
@ -0,0 +1,108 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
# Copyright 2022 - c0de <c0de@c0de.dev>
|
||||||
|
# Licensed under the MIT License (https://opensource.org/licenses/MIT)
|
||||||
|
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
from .database.models import database, GameModel, GuessModel
|
||||||
|
|
||||||
|
class Game:
|
||||||
|
"""
|
||||||
|
The game state class
|
||||||
|
|
||||||
|
This represents a game that exists in a channel
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
# Only one game should run at at time
|
||||||
|
self.is_running = False
|
||||||
|
|
||||||
|
self.commands = {
|
||||||
|
'ghostball': self.start,
|
||||||
|
'resolve': self.stop,
|
||||||
|
'guess': self.guess,
|
||||||
|
'points': self.points,
|
||||||
|
'help': self.help,
|
||||||
|
}
|
||||||
|
|
||||||
|
self.game = None
|
||||||
|
|
||||||
|
# Discord message
|
||||||
|
self.message = None
|
||||||
|
|
||||||
|
# Discord client instance
|
||||||
|
self.discord = None
|
||||||
|
|
||||||
|
async def start(self):
|
||||||
|
if self.is_running:
|
||||||
|
return await self.message.channel.send("A game is already running")
|
||||||
|
|
||||||
|
database.connect()
|
||||||
|
self.is_running = True
|
||||||
|
|
||||||
|
# game.pitch_value is unknown at the start of the game
|
||||||
|
self.game = GameModel.create(
|
||||||
|
game_id = uuid.uuid4(),
|
||||||
|
server_id = self.message.guild.id
|
||||||
|
)
|
||||||
|
|
||||||
|
database.close()
|
||||||
|
|
||||||
|
await self.message.send("@flappy ball, pitch is in! Send me your guesses with !guess <number>")
|
||||||
|
|
||||||
|
def __stopArgs__(self):
|
||||||
|
pieces = self.message.content.split()
|
||||||
|
|
||||||
|
if len(pieces) == 2:
|
||||||
|
return pieces[1], False, None, None
|
||||||
|
elif len(pieces) == 4:
|
||||||
|
return pieces[1], True, pieces[2], pieces[3]
|
||||||
|
|
||||||
|
return None, False, None, None
|
||||||
|
|
||||||
|
async def stop(self):
|
||||||
|
if not self.is_running:
|
||||||
|
return await self.message.channel.send("There is no game running to resolve")
|
||||||
|
|
||||||
|
# Determine arguments
|
||||||
|
pitch_value, has_batter, batter_id, batter_guess = self.__stopArgs__()
|
||||||
|
if not pitch_value:
|
||||||
|
return await self.message.channel.send(f"Invalid command <@{ str(self.message.author.id) }>!")
|
||||||
|
|
||||||
|
database.connect()
|
||||||
|
|
||||||
|
if has_batter:
|
||||||
|
player_id = batter_id[3:]
|
||||||
|
GuessModel.create(
|
||||||
|
game_id = self.game.game_id,
|
||||||
|
player_id = player_id,
|
||||||
|
player_name = self.discord.get_user(int(player_id).name),
|
||||||
|
guess = int(batter_guess)
|
||||||
|
)
|
||||||
|
|
||||||
|
# Save the pitch value
|
||||||
|
self.game.update({'pitch_value': pitch_value})
|
||||||
|
|
||||||
|
# TODO: Determine differences
|
||||||
|
|
||||||
|
# stop and discard game
|
||||||
|
self.is_running = False
|
||||||
|
self.game = None
|
||||||
|
database.close()
|
||||||
|
|
||||||
|
async def guess(self):
|
||||||
|
database.connect()
|
||||||
|
|
||||||
|
database.close()
|
||||||
|
|
||||||
|
async def points(self):
|
||||||
|
database.connect()
|
||||||
|
|
||||||
|
database.close()
|
||||||
|
|
||||||
|
async def help(self):
|
||||||
|
# TODO: Add help message
|
||||||
|
help_message = "help"
|
||||||
|
|
||||||
|
recipient = await self.discord.fetch_user(self.message.author.id)
|
||||||
|
await recipient.send(help_message)
|
19
GhostBallBot/main.py
Normal file
19
GhostBallBot/main.py
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
# Copyright 2022 - c0de <c0de@c0de.dev>
|
||||||
|
# Licensed under the MIT License (https://opensource.org/licenses/MIT)
|
||||||
|
|
||||||
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from .discord_client import client
|
||||||
|
from .database.models import DATABASE, database, create_models
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
client = GhostBallClient()
|
||||||
|
client.run(os.environ.get('discord_token'))
|
||||||
|
|
||||||
|
# Set up the database if we haven't already
|
||||||
|
if not os.path.exists(DATABASE):
|
||||||
|
database.connect()
|
||||||
|
create_models()
|
||||||
|
database.close()
|
4
GhostBallBot/run.sh
Executable file
4
GhostBallBot/run.sh
Executable file
@ -0,0 +1,4 @@
|
|||||||
|
export discord_token=''
|
||||||
|
export database_path='/tmp/ghostball.db'
|
||||||
|
|
||||||
|
python3 main.py
|
Loading…
Reference in New Issue
Block a user