roguelike_sql_game_generator / game_generator.py
Chezlui
First version
ff23759
raw
history blame
8.26 kB
class GameGenerator:
def __init__(self):
pass
@staticmethod
def generate_game(callback=None):
print("Generating a new game...")
# Class to generate a game by using Crew.ai agents
import os
from dotenv import load_dotenv, find_dotenv
load_dotenv(find_dotenv())
os.environ["OPENAI_MODEL_NAME"] = (
"gpt-4-turbo" # we need a larger context window for this project
)
from crewai_tools import FileReadTool
file_read_tool = FileReadTool()
from crewai import Agent
ideator = Agent(
role="RogueLike SQLite Game Ideator",
goal="Ideate a fantasy RogueLike game that uses SQLite as a core mechanic",
backstory="""
You are a creative game designer with a passion for fantasy games and a deep understanding of SQLite.
Your goal is to come up with a unique and engaging RogueLike game concept that uses SQLite as a core mechanic
and fantasy elements to create a compelling experience for players.
""",
verbose=True,
)
architect = Agent(
role="SQLite Game Architect",
goal="Design the database architecture of the RogueLike SQLite game",
backstory="""
You are an experienced database architect with a deep understanding of SQLite and database design principles.
Your goal is to design the database architecture of the RogueLike game, including the tables, relationships, and types of data
that will be stored in the database to support the game mechanics and features.
""",
verbose=True,
)
populator = Agent(
role="Database Populator",
goal="Populate the database with sample data for the RogueLike SQLite game",
backstory="""
You are a data expert with experience populating databases with sample data for testing and development purposes.
Your goal is to populate the database of the RogueLike game with sample data that will be used to play the game
and ensure that the database is working as intended.
""",
# Fakr as a tool to generate fake data?
verbose=True,
)
challenger = Agent(
role="SQL Game Challenger",
goal="Create challenging quests and puzzles for the RogueLike SQL game",
backstory="""
You are a game designer with a knack for creating challenging quests and puzzles that test the skills of players.
Your goal is to design quests and puzzles for the RogueLike game that will challenge players to use their SQLite knowledge
and problem-solving skills to progress through the game and complete the objectives.
""",
verbose=True,
)
from pydantic import BaseModel
class Challenge(BaseModel):
name: str
description: str
storytelling: str
sql_query: str
def __str__(self):
return f"{self.name}\n\n{self.description}\n\n{self.storytelling}\n\n{self.sql_query}"
class Challenges(BaseModel):
challenges: list[Challenge]
from crewai import Task
print("Creating tasks...")
ideate_task = Task(
description="""
Ideate a fantasy RogueLike game that uses SQLite as a core mechanic.
Think about the setting, characters, gameplay mechanics, and how SQLite will be integrated into the game.
""",
expected_output="""
A detailed concept for a RogueLike fantasy game that uses SQLite as a core mechanic.
""",
output_file="output/idea_description.md",
agent=ideator
)
scaffold_schemma = Task(
description="""
Design the database architecture of the RogueLike SQL game considering the game mechanics and features ideated in the previous task.
Define the tables, relationships, and types of data that will be stored in the database.
Just include SQLite code in the output file, no markdown or code block formatting.
""",
expected_output="""The database schema for the RogueLike SQLite game in SQLite format.""",
output_file="output/database_schema.sql",
agent=architect,
tools=[file_read_tool],
context=[ideate_task] # This task depends on the output of the ideate_task
)
populate_database = Task(
description="""
Populate the database with data for the RogueLike SQLite game by using the schema designed in the previous task.
Generate at least 10 rows of data for each table in the database to be able to play the game.
Generate the output in SQLite format to be executed in a database management system.
Don't use any makdown or code block formatting in the output.
Pay attention to the content generated, if you include any apostrophes or special characters, escape them properly, using double quotes for the strings.
""",
expected_output="""A file with a sequence of SQLite commands to populate the database with data.""",
output_file="output/populated_database.sql",
agent=populator,
tools=[file_read_tool],
context=[scaffold_schemma] # This task depends on the output of the scaffold_schemma
)
create_challenges = Task(
description="""
Create a list of 5 challenges and puzzles for the RogueLike SQLite game that will test the skills of players.
Design quests and puzzles that require players to use their SQLite knowledge and problem-solving skills to progress through the game.
Design the challenges in a progressive manner, starting from easy to hard.
""",
expected_output="""A set of challenging quests and puzzles for the RogueLike SQLite game.""",
output_file="output/challenges.md",
output_json=Challenges,
agent=challenger,
tools=[file_read_tool],
context=[populate_database, scaffold_schemma, ideate_task]
# This task depends on the output of the populate_database
)
from crewai import Crew
crew = Crew(
agents=[ideator, architect, populator, challenger],
tasks=[ideate_task, scaffold_schemma, populate_database, create_challenges],
verbose=True,
)
print("Kicking off the crew...")
#result = crew.kickoff()
# Open Markdown file & print on the screen
ddbb_path = "output/game_database.db"
# Delete the database file if it already exists
if os.path.exists(ddbb_path):
os.remove(ddbb_path)
print("Deleted the existing database file.")
import sqlite3
# Define the path to your SQL file
sql_file_schema_path = "output/database_schema.sql"
sql_file_path = "output/populated_database.sql"
# Define the path to your SQLite database
# Connect to the SQLite database
conn = sqlite3.connect(ddbb_path)
cursor = conn.cursor()
# Read and execute the SQL file
with open(sql_file_schema_path, 'r') as sql_file:
sql_script = sql_file.read()
with open(sql_file_path, 'r') as sql_file:
sql_script += sql_file.read()
# Since the SQL script may contain multiple commands, split and execute them individually
sql_commands = sql_script.split(';') # This assumes each command ends with a semicolon
for command in sql_commands:
try:
if command.strip(): # Ensure it's not an empty command
cursor.execute(command)
except Exception as e:
print(f"An error occurred: {e}")
# Commit changes and close the connection
conn.commit()
conn.close()
print("Database has been populated successfully.")