Multiplayer / game_logic.py
ILLERRAPS's picture
Upload folder using huggingface_hub
2cdc05c verified
# Initialize Groq client
client = Groq()
# Function to read the prompt from a file
def read_prompt(game_name):
file_path = os.path.join('prompts', f'{game_name}_prompt.txt')
if not os.path.exists(file_path):
raise FileNotFoundError(f"{file_path} does not exist.")
with open(file_path, 'r') as file:
return file.read().strip()
# Function to create the players table
def create_players_table():
conn = sqlite3.connect('game.db')
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS players (
username TEXT PRIMARY KEY,
location TEXT
)
''')
conn.commit()
conn.close()
# Function to get player's location from the database
def get_player_location(username):
conn = sqlite3.connect('game.db')
cursor = conn.cursor()
cursor.execute('SELECT location FROM players WHERE username = ?', (username,))
location = cursor.fetchone()
conn.close()
return location[0] if location else None
# Function to update player's location in the database
def update_player_location(username, new_location):
conn = sqlite3.connect('game.db')
cursor = conn.cursor()
cursor.execute('INSERT OR REPLACE INTO players (username, location) VALUES (?, ?)', (username, new_location))
conn.commit()
conn.close()
# Function to handle sending messages to the Gemini model
def handle_message(message):
return client.chat.completions.create(
model="llama3-8b-8192",
messages=[{"role": "user", "content": message}],
temperature=0.2,
max_tokens=250
).choices[0].message.content
# Game logic
def process_command(username, game_name, command):
create_players_table() # Ensure the players table exists
location = get_player_location(username)
if not location:
return "Player not found."
prompt = read_prompt(game_name)
full_command = f"{prompt} {command}"
response = handle_message(full_command)
# Update player location if needed
new_location = command.strip().lower() # Assuming command contains the new location
update_player_location(username, new_location)
return response
# Example usage
username = "player1"
game_name = "game1"
command = "go north"
response = process_command(username, game_name, command)
print(response)
# Function to check if a user exists in the database
def user_exists(username):
conn = sqlite3.connect('game.db')
cursor = conn.cursor()
cursor.execute('SELECT * FROM players WHERE username = ?', (username,))
user = cursor.fetchone()
conn.close()
return user is not None
# Function to log in the user
def login(username):
if user_exists(username):
print(f"Welcome back, {username}!")
# Set session variable or authentication token
else:
print("User does not exist. Please create a new account.")
# Function to create a new account
def create_account(username, password):
if not user_exists(username):
conn = sqlite3.connect('game.db')
cursor = conn.cursor()
cursor.execute('INSERT INTO players (username, password) VALUES (?, ?)', (username, password))
conn.commit()
conn.close()
print(f"Account created successfully for {username}!")
# Set session variable or authentication token
else:
print("Username already exists. Please choose another username.")
# Example usage
username = input("Enter your username: ")
if user_exists(username):
login(username)
else:
create_account(username, input("Enter your password: "))