Answer the question
In order to leave comments, you need to log in
How is it better to make the function of adding +30 to the database, so that with 2 users the code does not hang and does not wait for the end of the cycle?
Greetings, having a problem that I have been studying for a week and not understanding, I am writing here, I am writing a bot toy just to understand programming ... And the problem is that I just can’t understand how the mine function that adds +30 to make it autonomous from the code, because there is the problem is that when 2 users register it, the code simply gets hung up on the execution of 1 function and scores on others, although when 1 user then everything works fine
import config
import sqlite3
import random
import time
import peewee
import multiprocessing
from select import select
from aiogram import Bot, types
from aiogram.dispatcher import Dispatcher
from aiogram.utils import executor
import asyncio,time
import threading
from multiprocessing import Pool
from concurrent.futures import ThreadPoolExecutor
from time import sleep
from threading import Thread
from peewee import *
db = SqliteDatabase('database1.db')
class User(Model):
user_id = TextField()
balance = IntegerField(default=6000)
s9 = IntegerField(default=0)
s10 = IntegerField(default=0)
s11 = IntegerField(default=0)
class Meta:
database = db
db_table = 'users'
User.create_table()
def does_it_exist(model, instance):
exits = model.select().where(model.user_id == instance)
if not bool(exits):
return True
else:
return False
def reg_DB(uid):
if not does_it_exist(User, uid):
user = User.get(User.user_id == uid)
user.save()
else:
print('I')
usersDB = User(user_id = uid)
usersDB.save()
bot = Bot(config.TOKEN)
dp = Dispatcher(bot)
import threading
@dp.message_handler(commands=['start'])
async def start(message):
await reg_DB(message.from_user.id)
await bot.send_message(message.chat.id, 'Привет, это чат игра про криптовалюту! Попробуй!')
await bot.send_message(message.chat.id, 'Напишите мне что хотите выбрать: меню, Магазин, Баланс, /mine')
@dp.message_handler(commands=['/mine'])
async def mine(message):
reg_DB(message.from_user.id)
user = User.get(User.user_id == message.from_user.id)
balance = User.get(User.user_id == message.from_user.id).balance
s9 = User.get(User.user_id == message.from_user.id).s9
s10 = User.get(User.user_id == message.from_user.id).s10
await bot.send_message(message.chat.id, 'GleenCoin Welcome')
if s9 > 0:
await bot.send_message(message.chat.id, 'Start.mining S9 "True" ')
await bot.send_message(message.chat.id, 'Wallet:')
await bot.send_message(message.chat.id, user.user_id)
while (True):
user.balance += user.s9 * 30
time.sleep(30)
user.save()
else:
await bot.send_message(message.chat.id, 'Error, s9 not founded')
if s10 > 0:
await bot.send_message(message.chat.id, 'Start.mining S10 "True" ')
await bot.send_message(message.chat.id, 'Wallet:')
await bot.send_message(message.chat.id, user.user_id)
while (True):
user.balance += user.s10 * 60
time.sleep(30)
user.save()
else:
await bot.send_message(message.chat.id, 'Error, s 10 not founded')
@dp.message_handler(func=lambda message: message.text == "магазин")
async def shop(message):
await reg_DB(message.from_user.id)
await bot.send_message(message.chat.id, 'Добро пожаловать в магазин! :) ')
await bot.send_message(message.chat.id, 'Чтобы купить напишите мне название ПРИМЕР: "S9"')
await bot.send_message(message.chat.id, 'В наличии "S9 (30 рублей в 40 секунд) Цена: 3000')
await bot.send_message(message.chat.id, 'В наличии "S10 (60 рублей в 40 секунд) Цена: 6.000')
await bot.send_message(message.chat.id, 'В наличии "S11 (100 рублей в 40 секунду) Цена: 10.000')
@dp.message_handler(func=lambda message: True)
async def message_variant_2(message):
reg_DB(message.from_user.id)
user = User.get(User.user_id == message.from_user.id)
balance = User.get(User.user_id == message.from_user.id).balance
s9 = User.get(User.user_id == message.from_user.id).s9
s10 = User.get(User.user_id == message.from_user.id).s10
if 'S9' in message.text:
if user.balance >= 3000:
user.balance -= 3000
user.s9 += 1
await bot.send_message(message.chat.id,'Поздравляю с покупкой!')
await bot.send_message(message.chat.id, 'Количество асиков S9')
await bot.send_message(message.chat.id, 'Чтобы майнить /mine')
await bot.send_message(message.chat.id, user.s9)
await bot.send_message(message.chat.id, user.balance)
await print(message.from_user.id, 'Кол-во', user.s9, user.balance)
user.save()
else:
await bot.send_message(message.chat.id,'Недостаточно средств!')
if 'S10' in message.text:
if user.balance >= 6000:
user.balance -= 6000
user.s10 += 1
await bot.send_message(message.chat.id, 'Поздравляю с покупкой!')
await bot.send_message(message.chat.id, 'Количество асиков S10')
await bot.send_message(message.chat.id, user.s10)
await bot.send_message(message.chat.id, 'Чтобы майнить /mine')
user.save()
else:
await bot.send_message(message.chat.id,'Недостаточно средств!')
if 'Баланс' in message.text:
user = User.get(User.user_id == message.from_user.id)
balance = User.get(User.user_id == message.from_user.id).balance
s9 = User.get(User.user_id == message.from_user.id).s9
s10 = User.get(User.user_id == message.from_user.id).s10
await bot.send_message(message.chat.id, 'Ваш баланс: ' )
await bot.send_message(message.chat.id, user.balance)
await bot.send_message(message.chat.id, 'Количество Асиков S9: ')
await bot.send_message(message.chat.id, user.s9)
await bot.send_message(message.chat.id, 'Количество Асиков S10: ')
await bot.send_message(message.chat.id, user.s10)
else:
await bot.send_message(message.chat.id,'Не знаю ответа ;) ')
if __name__ == '__main__':
executor.start_polling(dp)
print('bot_enable')
bot.polling(none_stop=True)
Answer the question
In order to leave comments, you need to log in
Learn how asynchronous programming works.
In a nutshell: whenever you await something there, the execution of an asynchronous function (like an event handler) is suspended and another one is passed.
But the code in the interval between await calls (well, or the beginning-end of the function) is executed continuously, and occupies the control flow. While this code is being executed, the rest of the bot is standing still!
Now look what you have:
while (True):
user.balance += user.s9 * 30
time.sleep(30)
user.save()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question