File size: 3,591 Bytes
2cdc05c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# 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: "))