I have a code with a tg bot and a database, an approximate database model:
<code>Class User:
lucky: Mapped[int] = mapped_column(Integer)
chip: Mapped[int] = mapped_column(Integer)
cash: Mapped[int] = mapped_column(Integer)
</code>
<code>Class User:
lucky: Mapped[int] = mapped_column(Integer)
chip: Mapped[int] = mapped_column(Integer)
cash: Mapped[int] = mapped_column(Integer)
</code>
Class User:
lucky: Mapped[int] = mapped_column(Integer)
chip: Mapped[int] = mapped_column(Integer)
cash: Mapped[int] = mapped_column(Integer)
And the bot has to perform the function of exchanging coins at a rate (these fields in the class are the number of coins). And in the bot comes a string of coins to be exchanged and received, and I have to do a bunch of checks on their name to perform interactions with the model, that is, exchanging ‘lucky’ coins for ‘cash’ I have to check it first:
<code>async def trade(callback: types.CallbackQuery, state: FSMContext):
data = await state.get_data() # here's a dictionary with these coins
if data['get_coin'] == 'lucky':
if data['send_coin'] == 'cash':
User.lucky -= 10
User.cash += 1
if data['send_coin' == 'chip':
User.lucky -= 5
User.chip += 1
# and so it is with every coin
</code>
<code>async def trade(callback: types.CallbackQuery, state: FSMContext):
data = await state.get_data() # here's a dictionary with these coins
if data['get_coin'] == 'lucky':
if data['send_coin'] == 'cash':
User.lucky -= 10
User.cash += 1
if data['send_coin' == 'chip':
User.lucky -= 5
User.chip += 1
# and so it is with every coin
</code>
async def trade(callback: types.CallbackQuery, state: FSMContext):
data = await state.get_data() # here's a dictionary with these coins
if data['get_coin'] == 'lucky':
if data['send_coin'] == 'cash':
User.lucky -= 10
User.cash += 1
if data['send_coin' == 'chip':
User.lucky -= 5
User.chip += 1
# and so it is with every coin
New contributor
Akamakaka is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.