Skip to content

Commit

Permalink
[u] implementation in telegram
Browse files Browse the repository at this point in the history
  • Loading branch information
grisha765 committed Oct 21, 2024
1 parent 7265ced commit 5073bee
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions core/tg.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from casino.emoji import set_emoji_command, get_emoji_command, del_emoji_command
from core.trans import get_translation
from chesse.invite import chess_start, join_chess_black, remove_expired_chess_session
from tictactoe.invite import ttt_start, remove_expired_ttt_session, join_ttt_o, update_board_size_buttons
from tictactoe.invite import ttt_start, remove_expired_ttt_session, join_ttt_o, update_buttons
from chesse.game import move_chess
from tictactoe.game import move_ttt
from config import logging_config
Expand Down Expand Up @@ -90,6 +90,7 @@ async def handle_ttt_start(client, message):
"message_id": None,
"chat_id": message.chat.id,
"board_size": 3,
"game_mode": 0,
"lang": message.from_user.language_code
}
selected_squares[session_id] = None
Expand All @@ -98,6 +99,7 @@ async def handle_ttt_start(client, message):
cleanup_task = asyncio.create_task(remove_expired_ttt_session(session_id, sessions, selected_squares, available_session_ids, client))
session_cleanup_tasks[session_id] = cleanup_task
logging.debug(f"Session {session_id}: Add cleanup Task {session_cleanup_tasks[session_id]}.")
logging.debug(f"All cleanup tasks: {session_cleanup_tasks}")

@app.on_callback_query(filters.regex(r"^board_size_(\d+)_(\d+)$"))
async def handle_board_size_selection(client, callback_query):
Expand All @@ -110,20 +112,36 @@ async def handle_board_size_selection(client, callback_query):
if sessions[session_id].get("board_size") == size:
await callback_query.answer(f"{get_translation(sessions[session_id]["lang"], "size_already_selected")} {size}x{size}.")
return

if size == 3:
sessions[session_id]["game_mode"] = 0
logging.debug(f"Session {session_id}: selected board size {size}, mod is selected {sessions[session_id]["game_mode"]}")

sessions[session_id]["board_size"] = size

await update_board_size_buttons(client, session_id, sessions[session_id], callback_query.message, size, get_translation)
await callback_query.answer(f"{get_translation(sessions[session_id]["lang"], "select_size")} {size}x{size}.")
await update_buttons(client, session_id, sessions[session_id], callback_query.message, size, sessions[session_id]["game_mode"], get_translation)
await callback_query.answer(f"{get_translation(sessions[session_id]["lang"], "select_size")}: {size}x{size}.")

@app.on_callback_query(filters.regex(r"^game_mode_(\d+)_(\d+)$"))
async def handle_game_mode_selection(client, callback_query):
mode, session_id = map(int, callback_query.data.split('_')[2:])
if sessions[session_id]["x"]["id"] != callback_query.from_user.id:
await callback_query.answer(get_translation(sessions[session_id]["lang"], "unavailable"))
return

if sessions[session_id].get("game_mode") == mode:
await callback_query.answer(get_translation(sessions[session_id]["lang"], "mode_already_selected"))
return

sessions[session_id]["game_mode"] = mode

await update_buttons(client, session_id, sessions[session_id], callback_query.message, sessions[session_id]["board_size"], mode, get_translation)
await callback_query.answer(f"{get_translation(sessions[session_id]["lang"], "select_mode")}: {get_translation(sessions[session_id]["lang"], f"mode_{mode}").lower()}")

@app.on_callback_query(filters.regex(r"join_o_(\d+)"))
async def handle_ttt_join(client, callback_query):
session_id = int(callback_query.data.split('_')[-1])
if session_id in session_cleanup_tasks:
session_cleanup_tasks[session_id].cancel()
logging.debug(f"Session {session_id}: Del cleanup Task {session_cleanup_tasks[session_id]}.")
del session_cleanup_tasks[session_id]
await join_ttt_o(session_id, sessions, client, callback_query, get_translation)
await join_ttt_o(session_id, sessions, client, callback_query, get_translation, session_cleanup_tasks)

@app.on_callback_query(filters.regex(r"^(\d+)_(\d+)$"))
async def handle_ttt_move(client, callback_query):
Expand Down

0 comments on commit 5073bee

Please sign in to comment.