|
10 | 10 | from sqlalchemy.exc import IntegrityError
|
11 | 11 |
|
12 | 12 | from bot.minesweeper.game import (get_fake_newgame_data, untouched_cells_count, all_flags_match_bombs,
|
13 |
| - make_text_table, get_real_game_data) |
| 13 | + all_free_cells_are_open, make_text_table, get_real_game_data) |
14 | 14 | from bot.minesweeper.states import ClickMode, CellMask
|
15 | 15 | from bot.keyboards.kb_minefield import make_keyboard_from_minefield
|
16 | 16 | from bot.cbdata import cb_newgame, cb_click, cb_switch_mode, cb_switch_flag, cb_ignore
|
@@ -111,28 +111,27 @@ async def callback_open_square(call: types.CallbackQuery, state: FSMContext,
|
111 | 111 | # This cell contained a number
|
112 | 112 | else:
|
113 | 113 | cells[x][y]["mask"] = CellMask.OPEN
|
114 |
| - # If this is the last cell to open... |
115 |
| - if untouched_cells_count(cells) == 0: |
116 |
| - # ...and all flags stand on bombs |
117 |
| - if all_flags_match_bombs(cells): |
118 |
| - with suppress(MessageNotModified): |
119 |
| - await call.message.edit_text( |
120 |
| - call.message.html_text + f"\n\n{make_text_table(cells)}\n\n<b>You won!</b> 🎉", |
121 |
| - reply_markup=None |
122 |
| - ) |
123 |
| - await log_game(session, fsm_data, call.from_user.id, "win") |
124 |
| - # ...or some flags stand on numbers |
125 |
| - else: |
126 |
| - await state.update_data(game_data=game_data) |
127 |
| - with suppress(MessageNotModified): |
128 |
| - await call.message.edit_reply_markup( |
129 |
| - make_keyboard_from_minefield(cells, game_id, game_data["current_mode"]) |
130 |
| - ) |
131 |
| - await call.answer( |
132 |
| - show_alert=True, |
133 |
| - text="Looks like you've placed more flags than there are bombs on field. Please check them again." |
| 114 | + if all_free_cells_are_open(cells): |
| 115 | + with suppress(MessageNotModified): |
| 116 | + await call.message.edit_text( |
| 117 | + call.message.html_text + f"\n\n{make_text_table(cells)}\n\n<b>You won!</b> 🎉", |
| 118 | + reply_markup=None |
134 | 119 | )
|
135 |
| - return |
| 120 | + await log_game(session, fsm_data, call.from_user.id, "win") |
| 121 | + await call.answer() |
| 122 | + return |
| 123 | + # There are more flags than there should be |
| 124 | + elif untouched_cells_count(cells) == 0 and not all_flags_match_bombs(cells): |
| 125 | + await state.update_data(game_data=game_data) |
| 126 | + with suppress(MessageNotModified): |
| 127 | + await call.message.edit_reply_markup( |
| 128 | + make_keyboard_from_minefield(cells, game_id, game_data["current_mode"]) |
| 129 | + ) |
| 130 | + await call.answer( |
| 131 | + show_alert=True, |
| 132 | + text="Looks like you've placed more flags than there are bombs on field. Please check them again." |
| 133 | + ) |
| 134 | + return |
136 | 135 | # If this is not the last cell to open
|
137 | 136 | else:
|
138 | 137 | await state.update_data(game_data=game_data)
|
|
0 commit comments