E
E
EvgenyCherny2020-07-31 18:07:19
Python
EvgenyCherny, 2020-07-31 18:07:19

Why is the database cleared?

If len_players == 3 works, then base.yml is cleared and nothing stupidly works

elif call.data == "start_game":
                with open('base.yml', 'r') as f:
                    text = f.read()
                text = text.split('\n')
                del text[-1]
                for string in text:
                    if str(call.message.chat.id) == string.split(':')[0]:
                        account = string.split(':')
                        game_number = account[3]
                        with open('games_hdice.yml', 'r') as f:
                            text = f.read()
                        text = text.split('\n')
                        players_list = []
                        dice_list = []
                        for string in text:
                            if str(game_number) == string.split(':')[0]:
                                account = string.split(':')
                                len_game = len(account)
                                game_number = account[0]
                                rate = account[1]
                                chatid = account[2]
                                print(game_number)
                                i = 3
                                while i < len_game:
                                    players_list.append(str(account[i]))
                                    i += 1
                                len_players = len(players_list)
                                if len_players == 1:
                                    bot.send_message(call.message.chat.id, "Игра не может быть запущена, требуется минимум 2 игрока")
                                elif len_players == 3:
                                    dice_p1 = random.randint(1,12)
                                    dice_p2 = random.randint(1,12)
                                    dice_p3 = random.randint(1,12)
                                    i = 0
                                    while i < len(players_list):
                                        userGamesAdd(players_list[i], 1)
                                        i + 1
                                    if dice_p1 > dice_p2 and dice_p1 > dice_p3:
                                        bot.send_message(players_list[0], "Поздравляем! Вы выиграли " + (int(rate) * 2) + "$", reply_markup=keyboard_menu)
                                        userBalanceAdd(players_list[0], int(rate) * 2)
                                        userBalanceTake(players_list[1], rate)
                                        userBalanceTake(players_list[2], rate)
                                        userRivalSet(players_list[0], "null")
                                        userRivalSet(players_list[1], "null")
                                        userRivalSet(players_list[2], "null")
                                        hdiceGameDelete(game_number)
                                        bot.send_message(players_list[1], "К сожалению, Вы проиграли", reply_markup=keyboard_menu)
                                        bot.send_message(players_list[2], "К сожалению, Вы проиграли", reply_markup=keyboard_menu)
                                        bot.send_message(chatid, "Результат игры «Хай-Дайс»\n\[email protected]" + getNick(players_list[0]) + " выпало: " + str(dice_p1) + "\[email protected]" + getNick(players_list[1]) + " выпало: " + str(dice_p2) + "\[email protected]" + getNick(players_list[2]) + " выпало: " + str(dice_p3) + "\n\nДепозит: " + str(int(len_players) * int(rate)) + "$", reply_markup=None)
                                        bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text="Игра завершена", reply_markup=None)
                                    if dice_p2 > dice_p1 and dice_p2 > dice_p3:
                                        bot.send_message(players_list[1], "Поздравляем! Вы выиграли " + (int(rate) * 2) + "$", reply_markup=keyboard_menu)
                                        userBalanceAdd(players_list[1], int(rate) * 2)
                                        userBalanceTake(players_list[0], rate)
                                        userBalanceTake(players_list[2], rate)
                                        userRivalSet(players_list[0], "null")
                                        userRivalSet(players_list[1], "null")
                                        userRivalSet(players_list[2], "null")
                                        hdiceGameDelete(game_number)
                                        bot.send_message(players_list[0], "К сожалению, Вы проиграли", reply_markup=keyboard_menu)
                                        bot.send_message(players_list[2], "К сожалению, Вы проиграли", reply_markup=keyboard_menu)
                                        bot.send_message(chatid, "Результат игры «Хай-Дайс»\n\[email protected]" + getNick(players_list[0]) + " выпало: " + str(dice_p1) + "\[email protected]" + getNick(players_list[1]) + " выпало: " + str(dice_p2) + "\[email protected]" + getNick(players_list[2]) + " выпало: " + str(dice_p3) + "\n\nДепозит: " + str(int(len_players) * int(rate)) + "$", reply_markup=None)
                                        bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text="Игра завершена", reply_markup=None)
                                    if dice_p1 == dice_p2 and dice_p1 == dice_p3 and dice_p2 == dice_p3:
                                        bot.send_message(players_list[0], "Игра закончилась в ничью", reply_markup=keyboard_menu)
                                        userRivalSet(players_list[0], "null")
                                        userRivalSet(players_list[1], "null")
                                        userRivalSet(players_list[2], "null")
                                        hdiceGameDelete(game_number)
                                        bot.send_message(players_list[1], "Игра закончилась в ничью", reply_markup=keyboard_menu)
                                        bot.send_message(players_list[2], "Игра закончилась в ничью", reply_markup=keyboard_menu)
                                        bot.send_message(chatid, "Результат игры «Хай-Дайс»\n\[email protected]" + getNick(players_list[0]) + " выпало: " + str(dice_p1) + "\[email protected]" + getNick(players_list[1]) + " выпало: " + str(dice_p2) + "\[email protected]" + getNick(players_list[2]) + " выпало: " + str(dice_p3) + "\n\nДепозит: " + str(int(len_players) * int(rate)) + "$", reply_markup=None)
                                        bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text="Игра завершена", reply_markup=None)
                                    else:
                                        bot.send_message(players_list[2], "Поздравляем! Вы выиграли " + (int(rate) * 2) + "$", reply_markup=keyboard_menu)
                                        userBalanceAdd(players_list[2], int(rate) * 2)
                                        userBalanceTake(players_list[0], rate)
                                        userBalanceTake(players_list[1], rate)
                                        userRivalSet(players_list[0], "null")
                                        userRivalSet(players_list[1], "null")
                                        userRivalSet(players_list[2], "null")
                                        hdiceGameDelete(game_number)
                                        bot.send_message(players_list[0], "К сожалению, Вы проиграли", reply_markup=keyboard_menu)
                                        bot.send_message(players_list[1], "К сожалению, Вы проиграли", reply_markup=keyboard_menu)
                                        bot.send_message(chatid, "Результат игры «Хай-Дайс»\n\[email protected]" + getNick(players_list[0]) + " выпало: " + str(dice_p1) + "\[email protected]" + getNick(players_list[1]) + " выпало: " + str(dice_p2) + "\[email protected]" + getNick(players_list[2]) + " выпало: " + str(dice_p3) + "\n\nДепозит: " + str(int(len_players) * int(rate)) + "$", reply_markup=None)
                                        bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text="Игра завершена", reply_markup=None)
                                break

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Z
zexer, 2020-07-31
@EvgenyCherny

while i < len(players_list):
    userGamesAdd(players_list[i], 1)
    i + 1

Weird piece of code, you write i +1 but don't update the past value of i

S
soremix, 2020-07-31
@SoreMix

Here the loop is infinite, a unit was added to i, but the value of i was not overwritten

i = 0
while i < len(players_list):
    userGamesAdd(players_list[i], 1)
    i + 1

In general, why do we need such structures, if you can do it simply
for player in players_list:
    userGamesAdd(player, 1)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question