Выдает ошибку:
line 95, in
success_rate = cur.execute('SELECT success_rate FROM users WHERE id = ?', (user_id)).fetchone()[0]
NameError: name 'user_id' is not defined
Код:
con = sqlite3.connect('basketball_database.db')
cur = con.cursor()
'''cur.execute('CREATE TABLE IF NOT EXISTS upgrades (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, price INTEGER, effect INTEGER)')
cur.execute('CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT, coins INTEGER, success_rate REAL)')
cur.execute('CREATE TABLE IF NOT EXISTS shots (id INTEGER PRIMARY KEY AUTOINCREMENT, user_id INTEGER, success INTEGER)')
cur.execute('INSERT INTO upgrades(name, price, effect) VALUES (?, ?, ?)', ('+1% к успеху броска', 10, 0.01))
cur.execute('INSERT INTO upgrades(name, price, effect) VALUES (?, ?, ?)', ('+5% к успеху броска', 50, 0.05))
cur.execute('INSERT INTO upgrades(name, price, effect) VALUES (?, ?, ?)', ('+10% к успеху броска', 100, 0.1))'''
con.commit()
users_coins = {}
def get_upgraded_success_rate(user_id):
global cur
return success_rate
success_rate = cur.execute('SELECT success_rate FROM users WHERE id = ?', (user_id)).fetchone()[0]
upgrades = cur.execute('SELECT effect FROM upgrades WHERE id IN (SELECT upgrade_id FROM user_upgrades WHERE user_id = ?'), (user_id,)
for effect, in upgrades:
success_rate += effect
def process_shot(user_id):
global cur, con
return True
return False
success_rate = get_upgraded_success_rate(user_id)
success = random.random() < success_rate
cur.execute('INSERT INTO shots(user_id, success) VALUES (?, ?)', (user_id, int(success)))
if success:
cur.execute('UPDATE users SET coins = coins + 1 WHERE id = ?', (user_id,))
cur.execute('UPDATE users SET success_rate = success_rate + 0.001 WHERE id = ?', (user_id,))
con.commit()
else:
cur.execute('UPDATE users SET success_rate = success_rate - 0.001 WHERE id = ?', (user_id,))
con.commit()
def has_upgrade(user_id, upgrade_name):
global cur
return bool(res.fetchone()[0])
res = cur.execute('SELECT EXISTS(SELECT 1 FROM user_upgrades JOIN upgrades ON user_upgrades.upgrade_id = upgrades.id WHERE user_upgrades.user_id = ? AND upgrades.name = ?)', (user_id, upgrade_name))
def add_upgrade(user_id, upgrade_name):
global cur, con
upgrade_id = cur.execute('SELECT id FROM upgrades WHERE name = ?', (upgrade_name,)).fetchone()[0]
cur.execute('INSERT INTO user_upgrades(user_id, upgrade_id) VALUES (?, ?)', (user_id, upgrade_id))
con.commit()
def send_message_with_buttons(peer_id, message, buttons):
keyboard = vk_api.keyboard.VkKeyboard(inline=True)
for label, color, payload in buttons:
keyboard.add_button(label, color=color, payload=payload)
vk.messages.send(peer_id=peer_id, message=message, random_id=0, keyboard=keyboard.get_keyboard())
def process_command(command, user_id, peer_id):
global users_coins
if command == '/помощь':
send_message(peer_id, 'Доступные команды:\n'
'/помощь - помощь\n'
'/кинуть - бросить мяч\n'
'/улучшения - список доступных улучшений')
elif command == 'play':
success = process_shot(user_id)
if success:
send_message(peer_id, 'Вы попали в корзину и получаете 1 монету')
else:
send_message(peer_id, 'Вы промахнулись и не получаете монет')
elif command == 'upgrades':
upgrades = cur.execute(
'SELECT name, price FROM upgrades WHERE NOT EXISTS(SELECT 1 FROM user_upgrades WHERE user_id = ? AND upgrade_id = upgrades.id)',
(user_id,))
buttons = [(f'{name} ({price} монет)', vk_api.keyboard.VkKeyboardColor.PRIMARY, f'upgrade:{name}') for
name, price in upgrades]
send_message_with_buttons(peer_id, 'Доступные улучшения:', buttons)
elif command.startswith('upgrade:'):
upgrade_name = command.replace('upgrade:', '')
if has_upgrade(user_id, upgrade_name):
send_message(peer_id, 'У вас уже есть это улучшение')
else:
price = cur.execute('SELECT price FROM upgrades WHERE name = ?', (upgrade_name,)).fetchone()[0]
coins = cur.execute('SELECT coins FROM users WHERE id = ?', (user_id,)).fetchone()[0]
if coins < price:
send_message(peer_id, 'Недостаточно монет для покупки')
else:
cur.execute('UPDATE users SET coins = coins - ? WHERE id = ?', (price, user_id))
add_upgrade(user_id, upgrade_name)
send_message(peer_id, f'Вы успешно купили улучшение "{upgrade_name}"')
for event in longpoll.listen():
if event.type == VkEventType.MESSAGE_NEW and event.to_me:
handle_message(event)
command = event.text.lower()
user_id = event.user_id
peer_id = event.peer_id
if user_id != event.from_id:
if command.startswith('/'):
process_command(command[10-0, user_id, peer_id])