2016-05-08 20:37:25 +08:00
|
|
|
#!/usr/bin/env python3
|
2016-05-20 05:15:46 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-05-08 20:37:25 +08:00
|
|
|
#
|
|
|
|
# Telegram bot to play UNO in group chats
|
|
|
|
# Copyright (c) 2016 Jannes Höke <uno@jhoeke.de>
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-02-29 08:53:59 +08:00
|
|
|
import logging
|
2016-03-09 18:31:38 +08:00
|
|
|
from datetime import datetime
|
2016-02-29 06:57:24 +08:00
|
|
|
|
2017-11-28 00:59:19 +08:00
|
|
|
from telegram import ParseMode, InlineKeyboardMarkup, \
|
2016-04-24 08:11:37 +08:00
|
|
|
InlineKeyboardButton
|
2016-05-22 20:45:51 +08:00
|
|
|
from telegram.ext import InlineQueryHandler, ChosenInlineResultHandler, \
|
|
|
|
CommandHandler, MessageHandler, Filters, CallbackQueryHandler
|
2016-04-24 04:13:15 +08:00
|
|
|
from telegram.ext.dispatcher import run_async
|
2016-02-29 06:57:24 +08:00
|
|
|
|
2016-05-20 02:52:50 +08:00
|
|
|
import card as c
|
2017-11-28 00:59:19 +08:00
|
|
|
import settings
|
|
|
|
import simple_commands
|
|
|
|
from actions import do_skip, do_play_card, do_draw, do_call_bluff, start_player_countdown
|
2017-12-07 16:27:51 +08:00
|
|
|
from config import WAITING_TIME, DEFAULT_GAMEMODE, MIN_PLAYERS
|
2016-05-20 02:52:50 +08:00
|
|
|
from errors import (NoGameInChatError, LobbyClosedError, AlreadyJoinedError,
|
|
|
|
NotEnoughPlayersError, DeckEmptyError)
|
2016-05-24 21:49:23 +08:00
|
|
|
from internationalization import _, __, user_locale, game_locales
|
2017-11-28 00:59:19 +08:00
|
|
|
from results import (add_call_bluff, add_choose_color, add_draw, add_gameinfo,
|
|
|
|
add_no_game, add_not_started, add_other_cards, add_pass,
|
|
|
|
add_card, add_mode_classic, add_mode_fast, add_mode_wild)
|
2017-12-07 16:27:51 +08:00
|
|
|
from shared_vars import gm, updater, dispatcher
|
2017-11-28 00:59:19 +08:00
|
|
|
from simple_commands import help_handler
|
|
|
|
from start_bot import start_bot
|
|
|
|
from utils import display_name
|
|
|
|
from utils import send_async, answer_async, error, TIMEOUT, user_is_creator_or_admin, user_is_creator, game_is_running
|
2016-05-20 02:52:50 +08:00
|
|
|
|
2016-02-29 06:57:24 +08:00
|
|
|
|
2016-02-29 08:53:59 +08:00
|
|
|
logging.basicConfig(
|
2016-02-29 19:16:12 +08:00
|
|
|
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
2017-11-28 00:59:19 +08:00
|
|
|
level=logging.INFO
|
|
|
|
)
|
2016-02-29 08:53:59 +08:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2016-07-02 23:53:34 +08:00
|
|
|
@user_locale
|
2016-07-03 02:37:35 +08:00
|
|
|
def notify_me(bot, update):
|
|
|
|
"""Handler for /notify_me command, pm people for next game"""
|
2016-07-02 23:53:34 +08:00
|
|
|
chat_id = update.message.chat_id
|
|
|
|
if update.message.chat.type == 'private':
|
2016-07-03 02:37:35 +08:00
|
|
|
send_async(bot,
|
|
|
|
chat_id,
|
|
|
|
text=_("Send this command in a group to be notified "
|
|
|
|
"when a new game is started there."))
|
2016-07-02 23:53:34 +08:00
|
|
|
else:
|
|
|
|
try:
|
2016-07-05 06:38:02 +08:00
|
|
|
gm.remind_dict[chat_id].add(update.message.from_user.id)
|
2016-07-02 23:53:34 +08:00
|
|
|
except KeyError:
|
2016-07-05 06:38:02 +08:00
|
|
|
gm.remind_dict[chat_id] = {update.message.from_user.id}
|
2016-07-02 23:53:34 +08:00
|
|
|
|
2016-03-08 06:50:39 +08:00
|
|
|
|
2016-05-22 03:41:38 +08:00
|
|
|
@user_locale
|
2016-02-29 06:57:24 +08:00
|
|
|
def new_game(bot, update):
|
2016-05-20 02:52:50 +08:00
|
|
|
"""Handler for the /new command"""
|
2016-02-29 06:57:24 +08:00
|
|
|
chat_id = update.message.chat_id
|
2016-05-20 02:52:50 +08:00
|
|
|
|
2016-03-09 18:31:38 +08:00
|
|
|
if update.message.chat.type == 'private':
|
2017-11-28 00:59:19 +08:00
|
|
|
help_handler(bot, update)
|
2016-05-20 02:52:50 +08:00
|
|
|
|
2016-03-09 18:31:38 +08:00
|
|
|
else:
|
2016-07-03 02:37:35 +08:00
|
|
|
|
|
|
|
if update.message.chat_id in gm.remind_dict:
|
|
|
|
for user in gm.remind_dict[update.message.chat_id]:
|
|
|
|
send_async(bot,
|
|
|
|
user,
|
2016-07-03 03:34:06 +08:00
|
|
|
text=_("A new game has been started in {title}").format(
|
|
|
|
title=update.message.chat.title))
|
2016-07-03 02:37:35 +08:00
|
|
|
|
|
|
|
del gm.remind_dict[update.message.chat_id]
|
|
|
|
|
2016-05-02 00:23:59 +08:00
|
|
|
game = gm.new_game(update.message.chat)
|
2017-08-19 05:36:30 +08:00
|
|
|
game.starter = update.message.from_user
|
|
|
|
game.owner.append(update.message.from_user.id)
|
2017-11-28 00:59:19 +08:00
|
|
|
game.mode = DEFAULT_GAMEMODE
|
2016-04-27 21:47:11 +08:00
|
|
|
send_async(bot, chat_id,
|
2016-05-22 03:41:38 +08:00
|
|
|
text=_("Created a new game! Join the game with /join "
|
|
|
|
"and start the game with /start"))
|
2016-05-20 02:52:50 +08:00
|
|
|
|
2016-03-09 18:31:38 +08:00
|
|
|
|
2017-08-19 05:36:30 +08:00
|
|
|
@user_locale
|
|
|
|
def kill_game(bot, update):
|
|
|
|
"""Handler for the /kill command"""
|
|
|
|
chat = update.message.chat
|
|
|
|
user = update.message.from_user
|
|
|
|
games = gm.chatid_games.get(chat.id)
|
|
|
|
|
|
|
|
if update.message.chat.type == 'private':
|
2017-11-28 00:59:19 +08:00
|
|
|
help_handler(bot, update)
|
2017-08-19 05:36:30 +08:00
|
|
|
return
|
|
|
|
|
|
|
|
if not games:
|
|
|
|
send_async(bot, chat.id,
|
|
|
|
text=_("There is no running game in this chat."))
|
|
|
|
return
|
|
|
|
|
|
|
|
game = games[-1]
|
|
|
|
|
2017-11-28 00:59:19 +08:00
|
|
|
if user_is_creator_or_admin(user, game, bot, chat):
|
2017-08-19 05:36:30 +08:00
|
|
|
|
|
|
|
try:
|
|
|
|
gm.end_game(chat, user)
|
|
|
|
send_async(bot, chat.id, text=__("Game ended!", multi=game.translate))
|
|
|
|
|
|
|
|
except NoGameInChatError:
|
|
|
|
send_async(bot, chat.id,
|
|
|
|
text=_("The game is not started yet. "
|
|
|
|
"Join the game with /join and start the game with /start"),
|
|
|
|
reply_to_message_id=update.message.message_id)
|
|
|
|
|
|
|
|
else:
|
|
|
|
send_async(bot, chat.id,
|
|
|
|
text=_("Only the game creator ({name}) and admin can do that.")
|
|
|
|
.format(name=game.starter.first_name),
|
|
|
|
reply_to_message_id=update.message.message_id)
|
2016-03-09 18:31:38 +08:00
|
|
|
|
2016-05-22 03:41:38 +08:00
|
|
|
@user_locale
|
2016-03-09 18:31:38 +08:00
|
|
|
def join_game(bot, update):
|
2016-05-20 02:52:50 +08:00
|
|
|
"""Handler for the /join command"""
|
|
|
|
chat = update.message.chat
|
|
|
|
|
2016-03-09 18:31:38 +08:00
|
|
|
if update.message.chat.type == 'private':
|
2017-11-28 00:59:19 +08:00
|
|
|
help_handler(bot, update)
|
2016-05-20 02:52:50 +08:00
|
|
|
return
|
2016-04-26 23:53:29 +08:00
|
|
|
|
2016-05-20 02:52:50 +08:00
|
|
|
try:
|
|
|
|
gm.join_game(update.message.from_user, chat)
|
|
|
|
|
|
|
|
except LobbyClosedError:
|
2016-05-22 03:41:38 +08:00
|
|
|
send_async(bot, chat.id, text=_("The lobby is closed"))
|
2016-05-20 02:52:50 +08:00
|
|
|
|
|
|
|
except NoGameInChatError:
|
|
|
|
send_async(bot, chat.id,
|
2016-05-22 03:41:38 +08:00
|
|
|
text=_("No game is running at the moment. "
|
|
|
|
"Create a new game with /new"),
|
2016-05-20 02:52:50 +08:00
|
|
|
reply_to_message_id=update.message.message_id)
|
|
|
|
|
|
|
|
except AlreadyJoinedError:
|
|
|
|
send_async(bot, chat.id,
|
2016-05-22 03:41:38 +08:00
|
|
|
text=_("You already joined the game. Start the game "
|
|
|
|
"with /start"),
|
2016-05-20 02:52:50 +08:00
|
|
|
reply_to_message_id=update.message.message_id)
|
2016-05-21 00:34:27 +08:00
|
|
|
|
|
|
|
except DeckEmptyError:
|
|
|
|
send_async(bot, chat.id,
|
2016-05-22 09:13:05 +08:00
|
|
|
text=_("There are not enough cards left in the deck for "
|
|
|
|
"new players to join."),
|
2016-05-21 00:34:27 +08:00
|
|
|
reply_to_message_id=update.message.message_id)
|
|
|
|
|
2016-05-20 02:52:50 +08:00
|
|
|
else:
|
|
|
|
send_async(bot, chat.id,
|
2016-05-22 03:41:38 +08:00
|
|
|
text=_("Joined the game"),
|
2016-05-20 02:52:50 +08:00
|
|
|
reply_to_message_id=update.message.message_id)
|
2016-02-29 06:57:24 +08:00
|
|
|
|
|
|
|
|
2016-05-22 03:41:38 +08:00
|
|
|
@user_locale
|
2016-03-08 06:50:39 +08:00
|
|
|
def leave_game(bot, update):
|
2016-05-20 02:52:50 +08:00
|
|
|
"""Handler for the /leave command"""
|
|
|
|
chat = update.message.chat
|
2016-04-24 08:11:37 +08:00
|
|
|
user = update.message.from_user
|
2016-05-20 02:52:50 +08:00
|
|
|
|
|
|
|
player = gm.player_for_user_in_chat(user, chat)
|
|
|
|
|
|
|
|
if player is None:
|
2016-05-22 03:41:38 +08:00
|
|
|
send_async(bot, chat.id, text=_("You are not playing in a game in "
|
|
|
|
"this group."),
|
2016-04-27 21:47:11 +08:00
|
|
|
reply_to_message_id=update.message.message_id)
|
2016-04-26 23:53:29 +08:00
|
|
|
return
|
|
|
|
|
2016-05-20 02:52:50 +08:00
|
|
|
game = player.game
|
2016-03-08 06:50:39 +08:00
|
|
|
user = update.message.from_user
|
2016-03-08 09:50:24 +08:00
|
|
|
|
2016-05-20 02:52:50 +08:00
|
|
|
try:
|
|
|
|
gm.leave_game(user, chat)
|
|
|
|
|
|
|
|
except NoGameInChatError:
|
2016-05-22 03:41:38 +08:00
|
|
|
send_async(bot, chat.id, text=_("You are not playing in a game in "
|
|
|
|
"this group."),
|
2016-05-20 02:52:50 +08:00
|
|
|
reply_to_message_id=update.message.message_id)
|
|
|
|
|
|
|
|
except NotEnoughPlayersError:
|
|
|
|
gm.end_game(chat, user)
|
2016-05-24 21:49:23 +08:00
|
|
|
send_async(bot, chat.id, text=__("Game ended!", multi=game.translate))
|
2016-05-20 02:52:50 +08:00
|
|
|
|
2016-03-08 06:50:39 +08:00
|
|
|
else:
|
2017-11-28 00:59:19 +08:00
|
|
|
if game.started:
|
|
|
|
send_async(bot, chat.id,
|
|
|
|
text=__("Okay. Next Player: {name}",
|
|
|
|
multi=game.translate).format(
|
|
|
|
name=display_name(game.current_player.user)),
|
|
|
|
reply_to_message_id=update.message.message_id)
|
|
|
|
else:
|
|
|
|
send_async(bot, chat.id,
|
|
|
|
text=__("{name} left the game before it started.",
|
|
|
|
multi=game.translate).format(
|
|
|
|
name=display_name(user)),
|
|
|
|
reply_to_message_id=update.message.message_id)
|
2016-04-24 08:11:37 +08:00
|
|
|
|
|
|
|
|
2017-12-07 16:27:51 +08:00
|
|
|
@user_locale
|
|
|
|
def kick_player(bot, update):
|
|
|
|
"""Handler for the /kick command"""
|
|
|
|
|
|
|
|
if update.message.chat.type == 'private':
|
|
|
|
help_handler(bot, update)
|
|
|
|
return
|
|
|
|
|
|
|
|
chat = update.message.chat
|
|
|
|
user = update.message.from_user
|
|
|
|
|
|
|
|
try:
|
|
|
|
game = gm.chatid_games[chat.id][-1]
|
|
|
|
|
|
|
|
except (KeyError, IndexError):
|
|
|
|
send_async(bot, chat.id,
|
|
|
|
text=_("No game is running at the moment. "
|
|
|
|
"Create a new game with /new"),
|
|
|
|
reply_to_message_id=update.message.message_id)
|
|
|
|
return
|
|
|
|
|
|
|
|
if not game.started:
|
|
|
|
send_async(bot, chat.id,
|
|
|
|
text=_("The game is not started yet. "
|
|
|
|
"Join the game with /join and start the game with /start"),
|
|
|
|
reply_to_message_id=update.message.message_id)
|
|
|
|
return
|
|
|
|
|
|
|
|
if user_is_creator_or_admin(user, game, bot, chat):
|
|
|
|
|
|
|
|
if update.message.reply_to_message:
|
|
|
|
kicked = update.message.reply_to_message.from_user
|
2018-12-27 21:49:29 +08:00
|
|
|
if kicked.id == bot.id:
|
|
|
|
kicked = game.current_player.user
|
2017-12-07 16:27:51 +08:00
|
|
|
|
|
|
|
try:
|
|
|
|
gm.leave_game(kicked, chat)
|
|
|
|
|
|
|
|
except NoGameInChatError:
|
|
|
|
send_async(bot, chat.id, text=_("Player {name} is not found in the current game.".format(name=display_name(kicked))),
|
|
|
|
reply_to_message_id=update.message.message_id)
|
|
|
|
return
|
|
|
|
|
|
|
|
except NotEnoughPlayersError:
|
|
|
|
gm.end_game(chat, user)
|
|
|
|
send_async(bot, chat.id,
|
|
|
|
text=_("{0} was kicked by {1}".format(display_name(kicked), display_name(user))))
|
|
|
|
send_async(bot, chat.id, text=__("Game ended!", multi=game.translate))
|
|
|
|
return
|
|
|
|
|
|
|
|
send_async(bot, chat.id,
|
|
|
|
text=_("{0} was kicked by {1}".format(display_name(kicked), display_name(user))))
|
|
|
|
|
|
|
|
else:
|
|
|
|
send_async(bot, chat.id,
|
2018-12-27 21:49:29 +08:00
|
|
|
text=_("Please reply to the person you want to kick and type /kick again. "
|
|
|
|
"Note that replying to the bot causes the current player to be kicked."),
|
2017-12-07 16:27:51 +08:00
|
|
|
reply_to_message_id=update.message.message_id)
|
|
|
|
return
|
|
|
|
|
|
|
|
send_async(bot, chat.id,
|
|
|
|
text=__("Okay. Next Player: {name}",
|
|
|
|
multi=game.translate).format(
|
|
|
|
name=display_name(game.current_player.user)),
|
|
|
|
reply_to_message_id=update.message.message_id)
|
|
|
|
|
|
|
|
else:
|
|
|
|
send_async(bot, chat.id,
|
|
|
|
text=_("Only the game creator ({name}) and admin can do that.")
|
|
|
|
.format(name=game.starter.first_name),
|
|
|
|
reply_to_message_id=update.message.message_id)
|
|
|
|
|
|
|
|
|
2016-04-24 08:11:37 +08:00
|
|
|
def select_game(bot, update):
|
2016-05-20 02:52:50 +08:00
|
|
|
"""Handler for callback queries to select the current game"""
|
2016-04-24 08:11:37 +08:00
|
|
|
|
|
|
|
chat_id = int(update.callback_query.data)
|
|
|
|
user_id = update.callback_query.from_user.id
|
|
|
|
players = gm.userid_players[user_id]
|
|
|
|
for player in players:
|
|
|
|
if player.game.chat.id == chat_id:
|
|
|
|
gm.userid_current[user_id] = player
|
|
|
|
break
|
|
|
|
else:
|
2016-06-02 21:03:33 +08:00
|
|
|
send_async(bot,
|
|
|
|
update.callback_query.message.chat_id,
|
|
|
|
text=_("Game not found."))
|
2016-04-24 08:11:37 +08:00
|
|
|
return
|
|
|
|
|
2016-06-02 21:03:33 +08:00
|
|
|
@run_async
|
|
|
|
def selected(bot):
|
|
|
|
back = [[InlineKeyboardButton(text=_("Back to last group"),
|
|
|
|
switch_inline_query='')]]
|
|
|
|
bot.answerCallbackQuery(update.callback_query.id,
|
|
|
|
text=_("Please switch to the group you selected!"),
|
|
|
|
show_alert=False,
|
|
|
|
timeout=TIMEOUT)
|
2016-04-24 08:11:37 +08:00
|
|
|
|
2016-06-02 21:03:33 +08:00
|
|
|
bot.editMessageText(chat_id=update.callback_query.message.chat_id,
|
|
|
|
message_id=update.callback_query.message.message_id,
|
|
|
|
text=_("Selected group: {group}\n"
|
|
|
|
"<b>Make sure that you switch to the correct "
|
|
|
|
"group!</b>").format(
|
|
|
|
group=gm.userid_current[user_id].game.chat.title),
|
|
|
|
reply_markup=InlineKeyboardMarkup(back),
|
|
|
|
parse_mode=ParseMode.HTML,
|
2016-05-20 02:52:50 +08:00
|
|
|
timeout=TIMEOUT)
|
|
|
|
|
2016-06-04 18:44:21 +08:00
|
|
|
selected(bot)
|
2016-03-08 06:50:39 +08:00
|
|
|
|
|
|
|
|
2016-05-22 09:13:05 +08:00
|
|
|
@game_locales
|
2016-04-19 08:22:42 +08:00
|
|
|
def status_update(bot, update):
|
2016-05-20 02:52:50 +08:00
|
|
|
"""Remove player from game if user leaves the group"""
|
|
|
|
chat = update.message.chat
|
2016-04-19 08:22:42 +08:00
|
|
|
|
|
|
|
if update.message.left_chat_member:
|
2016-05-22 23:02:27 +08:00
|
|
|
user = update.message.left_chat_member
|
2016-04-19 08:22:42 +08:00
|
|
|
|
2016-05-20 02:52:50 +08:00
|
|
|
try:
|
2016-05-27 17:31:06 +08:00
|
|
|
gm.leave_game(user, chat)
|
2016-06-02 21:03:33 +08:00
|
|
|
game = gm.player_for_user_in_chat(user, chat).game
|
2016-05-22 23:02:27 +08:00
|
|
|
|
2016-05-20 02:52:50 +08:00
|
|
|
except NoGameInChatError:
|
|
|
|
pass
|
|
|
|
except NotEnoughPlayersError:
|
|
|
|
gm.end_game(chat, user)
|
2016-05-24 21:49:23 +08:00
|
|
|
send_async(bot, chat.id, text=__("Game ended!",
|
|
|
|
multi=game.translate))
|
2016-05-20 02:52:50 +08:00
|
|
|
else:
|
2016-05-22 23:02:27 +08:00
|
|
|
send_async(bot, chat.id, text=__("Removing {name} from the game",
|
2016-05-24 21:49:23 +08:00
|
|
|
multi=game.translate)
|
2016-05-22 03:41:38 +08:00
|
|
|
.format(name=display_name(user)))
|
2016-04-19 08:22:42 +08:00
|
|
|
|
|
|
|
|
2016-05-22 09:13:05 +08:00
|
|
|
@game_locales
|
2016-05-22 03:41:38 +08:00
|
|
|
@user_locale
|
2017-11-28 00:59:19 +08:00
|
|
|
def start_game(bot, update, args, job_queue):
|
2016-05-20 02:52:50 +08:00
|
|
|
"""Handler for the /start command"""
|
2016-03-11 16:23:53 +08:00
|
|
|
|
|
|
|
if update.message.chat.type != 'private':
|
2016-05-20 02:52:50 +08:00
|
|
|
chat = update.message.chat
|
|
|
|
|
2016-04-26 23:53:29 +08:00
|
|
|
try:
|
2016-05-20 02:52:50 +08:00
|
|
|
game = gm.chatid_games[chat.id][-1]
|
2016-04-26 23:53:29 +08:00
|
|
|
except (KeyError, IndexError):
|
2016-05-22 03:41:38 +08:00
|
|
|
send_async(bot, chat.id,
|
|
|
|
text=_("There is no game running in this chat. Create "
|
|
|
|
"a new one with /new"))
|
2016-04-26 23:53:29 +08:00
|
|
|
return
|
2016-03-11 16:23:53 +08:00
|
|
|
|
2016-05-20 02:52:50 +08:00
|
|
|
if game.started:
|
2016-05-22 03:41:38 +08:00
|
|
|
send_async(bot, chat.id, text=_("The game has already started"))
|
2016-05-20 02:52:50 +08:00
|
|
|
|
2017-11-28 00:59:19 +08:00
|
|
|
elif len(game.players) < MIN_PLAYERS:
|
2016-05-22 03:41:38 +08:00
|
|
|
send_async(bot, chat.id,
|
2017-11-28 00:59:19 +08:00
|
|
|
text=__("At least {minplayers} players must /join the game "
|
|
|
|
"before you can start it").format(minplayers=MIN_PLAYERS))
|
2016-05-20 02:52:50 +08:00
|
|
|
|
2016-03-11 16:23:53 +08:00
|
|
|
else:
|
2017-11-28 00:59:19 +08:00
|
|
|
# Starting a game
|
|
|
|
game.start()
|
|
|
|
|
|
|
|
for player in game.players:
|
|
|
|
player.draw_first_hand()
|
2016-05-20 02:52:50 +08:00
|
|
|
|
2016-05-22 23:02:27 +08:00
|
|
|
first_message = (
|
|
|
|
__("First player: {name}\n"
|
|
|
|
"Use /close to stop people from joining the game.\n"
|
|
|
|
"Enable multi-translations with /enable_translations",
|
2016-05-24 21:49:23 +08:00
|
|
|
multi=game.translate)
|
2016-05-22 23:02:27 +08:00
|
|
|
.format(name=display_name(game.current_player.user)))
|
|
|
|
|
2016-05-20 02:52:50 +08:00
|
|
|
@run_async
|
|
|
|
def send_first():
|
|
|
|
"""Send the first card and player"""
|
|
|
|
|
|
|
|
bot.sendSticker(chat.id,
|
|
|
|
sticker=c.STICKERS[str(game.last_card)],
|
|
|
|
timeout=TIMEOUT)
|
|
|
|
|
|
|
|
bot.sendMessage(chat.id,
|
2016-05-22 23:02:27 +08:00
|
|
|
text=first_message,
|
2016-05-20 02:52:50 +08:00
|
|
|
timeout=TIMEOUT)
|
|
|
|
|
|
|
|
send_first()
|
2017-11-28 00:59:19 +08:00
|
|
|
start_player_countdown(bot, game, job_queue)
|
2016-05-20 02:52:50 +08:00
|
|
|
|
2016-04-24 08:11:37 +08:00
|
|
|
elif len(args) and args[0] == 'select':
|
|
|
|
players = gm.userid_players[update.message.from_user.id]
|
|
|
|
|
|
|
|
groups = list()
|
|
|
|
for player in players:
|
2016-05-20 02:52:50 +08:00
|
|
|
title = player.game.chat.title
|
|
|
|
|
|
|
|
if player is gm.userid_current[update.message.from_user.id]:
|
|
|
|
title = '- %s -' % player.game.chat.title
|
|
|
|
|
|
|
|
groups.append(
|
|
|
|
[InlineKeyboardButton(text=title,
|
|
|
|
callback_data=str(player.game.chat.id))]
|
|
|
|
)
|
|
|
|
|
2016-04-24 08:11:37 +08:00
|
|
|
send_async(bot, update.message.chat_id,
|
2016-05-22 03:41:38 +08:00
|
|
|
text=_('Please select the group you want to play in.'),
|
2016-04-24 08:11:37 +08:00
|
|
|
reply_markup=InlineKeyboardMarkup(groups))
|
2016-05-20 02:52:50 +08:00
|
|
|
|
2016-02-29 06:57:24 +08:00
|
|
|
else:
|
2017-11-28 00:59:19 +08:00
|
|
|
help_handler(bot, update)
|
2016-02-29 06:57:24 +08:00
|
|
|
|
|
|
|
|
2016-05-22 03:41:38 +08:00
|
|
|
@user_locale
|
2016-04-26 23:53:29 +08:00
|
|
|
def close_game(bot, update):
|
2016-05-20 02:52:50 +08:00
|
|
|
"""Handler for the /close command"""
|
|
|
|
chat = update.message.chat
|
2016-04-26 23:53:29 +08:00
|
|
|
user = update.message.from_user
|
2016-05-20 02:52:50 +08:00
|
|
|
games = gm.chatid_games.get(chat.id)
|
2016-04-26 23:53:29 +08:00
|
|
|
|
2016-05-02 00:23:59 +08:00
|
|
|
if not games:
|
2016-05-22 03:41:38 +08:00
|
|
|
send_async(bot, chat.id,
|
|
|
|
text=_("There is no running game in this chat."))
|
2016-05-02 00:23:59 +08:00
|
|
|
return
|
|
|
|
|
|
|
|
game = games[-1]
|
|
|
|
|
2017-08-19 05:36:30 +08:00
|
|
|
if user.id in game.owner:
|
2016-05-02 00:23:59 +08:00
|
|
|
game.open = False
|
2016-05-22 03:41:38 +08:00
|
|
|
send_async(bot, chat.id, text=_("Closed the lobby. "
|
|
|
|
"No more players can join this game."))
|
2016-05-02 00:23:59 +08:00
|
|
|
return
|
2016-05-20 02:52:50 +08:00
|
|
|
|
2016-05-02 00:23:59 +08:00
|
|
|
else:
|
2016-05-20 02:52:50 +08:00
|
|
|
send_async(bot, chat.id,
|
2017-08-19 05:36:30 +08:00
|
|
|
text=_("Only the game creator ({name}) and admin can do that.")
|
|
|
|
.format(name=game.starter.first_name),
|
2016-05-02 00:23:59 +08:00
|
|
|
reply_to_message_id=update.message.message_id)
|
|
|
|
return
|
2016-04-26 23:53:29 +08:00
|
|
|
|
|
|
|
|
2016-05-22 03:41:38 +08:00
|
|
|
@user_locale
|
2016-04-26 23:53:29 +08:00
|
|
|
def open_game(bot, update):
|
2016-05-20 02:52:50 +08:00
|
|
|
"""Handler for the /open command"""
|
|
|
|
chat = update.message.chat
|
2016-04-26 23:53:29 +08:00
|
|
|
user = update.message.from_user
|
2016-05-20 02:52:50 +08:00
|
|
|
games = gm.chatid_games.get(chat.id)
|
2016-04-26 23:53:29 +08:00
|
|
|
|
2016-05-02 00:23:59 +08:00
|
|
|
if not games:
|
2016-05-22 03:41:38 +08:00
|
|
|
send_async(bot, chat.id,
|
|
|
|
text=_("There is no running game in this chat."))
|
2016-05-02 00:23:59 +08:00
|
|
|
return
|
|
|
|
|
|
|
|
game = games[-1]
|
|
|
|
|
2017-08-19 05:36:30 +08:00
|
|
|
if user.id in game.owner:
|
2016-05-02 00:23:59 +08:00
|
|
|
game.open = True
|
2016-05-22 03:41:38 +08:00
|
|
|
send_async(bot, chat.id, text=_("Opened the lobby. "
|
|
|
|
"New players may /join the game."))
|
2016-05-02 00:23:59 +08:00
|
|
|
return
|
|
|
|
else:
|
2016-05-20 02:52:50 +08:00
|
|
|
send_async(bot, chat.id,
|
2017-08-19 05:36:30 +08:00
|
|
|
text=_("Only the game creator ({name}) and admin can do that.")
|
|
|
|
.format(name=game.starter.first_name),
|
2016-05-02 00:23:59 +08:00
|
|
|
reply_to_message_id=update.message.message_id)
|
|
|
|
return
|
2016-04-26 23:53:29 +08:00
|
|
|
|
|
|
|
|
2016-05-22 23:02:27 +08:00
|
|
|
@user_locale
|
|
|
|
def enable_translations(bot, update):
|
|
|
|
"""Handler for the /enable_translations command"""
|
|
|
|
chat = update.message.chat
|
|
|
|
user = update.message.from_user
|
|
|
|
games = gm.chatid_games.get(chat.id)
|
|
|
|
|
|
|
|
if not games:
|
|
|
|
send_async(bot, chat.id,
|
|
|
|
text=_("There is no running game in this chat."))
|
|
|
|
return
|
|
|
|
|
|
|
|
game = games[-1]
|
|
|
|
|
2017-08-19 05:36:30 +08:00
|
|
|
if user.id in game.owner:
|
2016-05-22 23:02:27 +08:00
|
|
|
game.translate = True
|
|
|
|
send_async(bot, chat.id, text=_("Enabled multi-translations. "
|
|
|
|
"Disable with /disable_translations"))
|
|
|
|
return
|
|
|
|
|
|
|
|
else:
|
|
|
|
send_async(bot, chat.id,
|
2017-08-19 05:36:30 +08:00
|
|
|
text=_("Only the game creator ({name}) and admin can do that.")
|
|
|
|
.format(name=game.starter.first_name),
|
2016-05-22 23:02:27 +08:00
|
|
|
reply_to_message_id=update.message.message_id)
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
@user_locale
|
|
|
|
def disable_translations(bot, update):
|
|
|
|
"""Handler for the /disable_translations command"""
|
|
|
|
chat = update.message.chat
|
|
|
|
user = update.message.from_user
|
|
|
|
games = gm.chatid_games.get(chat.id)
|
|
|
|
|
|
|
|
if not games:
|
|
|
|
send_async(bot, chat.id,
|
|
|
|
text=_("There is no running game in this chat."))
|
|
|
|
return
|
|
|
|
|
|
|
|
game = games[-1]
|
|
|
|
|
2017-08-19 05:36:30 +08:00
|
|
|
if user.id in game.owner:
|
2016-05-22 23:02:27 +08:00
|
|
|
game.translate = False
|
|
|
|
send_async(bot, chat.id, text=_("Disabled multi-translations. "
|
|
|
|
"Enable them again with "
|
|
|
|
"/enable_translations"))
|
|
|
|
return
|
|
|
|
|
|
|
|
else:
|
|
|
|
send_async(bot, chat.id,
|
2017-08-19 05:36:30 +08:00
|
|
|
text=_("Only the game creator ({name}) and admin can do that.")
|
|
|
|
.format(name=game.starter.first_name),
|
2016-05-22 23:02:27 +08:00
|
|
|
reply_to_message_id=update.message.message_id)
|
|
|
|
return
|
|
|
|
|
|
|
|
|
2016-05-22 09:13:05 +08:00
|
|
|
@game_locales
|
2016-05-22 03:41:38 +08:00
|
|
|
@user_locale
|
2016-04-26 23:53:29 +08:00
|
|
|
def skip_player(bot, update):
|
2016-05-20 02:52:50 +08:00
|
|
|
"""Handler for the /skip command"""
|
|
|
|
chat = update.message.chat
|
2016-04-26 23:53:29 +08:00
|
|
|
user = update.message.from_user
|
2016-05-02 00:23:59 +08:00
|
|
|
|
2016-05-20 02:52:50 +08:00
|
|
|
player = gm.player_for_user_in_chat(user, chat)
|
|
|
|
if not player:
|
2016-05-22 03:41:38 +08:00
|
|
|
send_async(bot, chat.id,
|
|
|
|
text=_("You are not playing in a game in this chat."))
|
2016-05-02 00:23:59 +08:00
|
|
|
return
|
|
|
|
|
2016-05-20 02:52:50 +08:00
|
|
|
game = player.game
|
|
|
|
skipped_player = game.current_player
|
2016-04-26 23:53:29 +08:00
|
|
|
|
2016-05-20 02:52:50 +08:00
|
|
|
started = skipped_player.turn_started
|
|
|
|
now = datetime.now()
|
|
|
|
delta = (now - started).seconds
|
2016-05-08 23:03:29 +08:00
|
|
|
|
2017-11-28 00:59:19 +08:00
|
|
|
# You can't skip if the current player still has time left
|
|
|
|
# You can skip yourself even if you have time left (you'll still draw)
|
|
|
|
if delta < skipped_player.waiting_time and player != skipped_player:
|
2016-05-24 21:49:23 +08:00
|
|
|
n = skipped_player.waiting_time - delta
|
2016-05-20 02:52:50 +08:00
|
|
|
send_async(bot, chat.id,
|
2016-05-24 21:49:23 +08:00
|
|
|
text=_("Please wait {time} second",
|
|
|
|
"Please wait {time} seconds",
|
|
|
|
n)
|
|
|
|
.format(time=n),
|
2016-05-20 02:52:50 +08:00
|
|
|
reply_to_message_id=update.message.message_id)
|
|
|
|
else:
|
2017-11-28 00:59:19 +08:00
|
|
|
do_skip(bot, player)
|
2016-05-02 00:23:59 +08:00
|
|
|
|
2016-05-22 23:02:27 +08:00
|
|
|
|
2016-05-22 09:13:05 +08:00
|
|
|
@game_locales
|
2016-05-22 03:41:38 +08:00
|
|
|
@user_locale
|
2016-03-01 08:25:26 +08:00
|
|
|
def reply_to_query(bot, update):
|
2016-05-20 02:52:50 +08:00
|
|
|
"""
|
|
|
|
Handler for inline queries.
|
|
|
|
Builds the result list for inline queries and answers to the client.
|
|
|
|
"""
|
2016-03-01 08:25:26 +08:00
|
|
|
results = list()
|
2016-04-24 08:11:37 +08:00
|
|
|
switch = None
|
2016-03-01 08:25:26 +08:00
|
|
|
|
2016-03-09 18:31:38 +08:00
|
|
|
try:
|
2017-11-28 00:59:19 +08:00
|
|
|
user = update.inline_query.from_user
|
|
|
|
user_id = user.id
|
2016-04-24 08:11:37 +08:00
|
|
|
players = gm.userid_players[user_id]
|
|
|
|
player = gm.userid_current[user_id]
|
|
|
|
game = player.game
|
2016-03-09 18:31:38 +08:00
|
|
|
except KeyError:
|
|
|
|
add_no_game(results)
|
|
|
|
else:
|
2017-11-28 00:59:19 +08:00
|
|
|
|
|
|
|
# The game has not started.
|
|
|
|
# The creator may change the game mode, other users just get a "game has not started" message.
|
2016-03-11 16:23:53 +08:00
|
|
|
if not game.started:
|
2017-11-28 00:59:19 +08:00
|
|
|
if user_is_creator(user, game):
|
|
|
|
add_mode_classic(results)
|
|
|
|
add_mode_fast(results)
|
|
|
|
add_mode_wild(results)
|
|
|
|
else:
|
|
|
|
add_not_started(results)
|
|
|
|
|
2016-05-20 02:52:50 +08:00
|
|
|
|
2016-03-11 16:23:53 +08:00
|
|
|
elif user_id == game.current_player.user.id:
|
2016-03-09 18:31:38 +08:00
|
|
|
if game.choosing_color:
|
2016-05-23 01:21:51 +08:00
|
|
|
add_choose_color(results, game)
|
2016-05-24 21:49:23 +08:00
|
|
|
add_other_cards(player, results, game)
|
2016-03-09 18:31:38 +08:00
|
|
|
else:
|
|
|
|
if not player.drew:
|
2016-04-20 06:55:56 +08:00
|
|
|
add_draw(player, results)
|
2016-03-01 08:38:23 +08:00
|
|
|
|
2016-03-09 18:31:38 +08:00
|
|
|
else:
|
2016-05-22 23:02:27 +08:00
|
|
|
add_pass(results, game)
|
2016-03-01 08:25:26 +08:00
|
|
|
|
2016-03-09 18:31:38 +08:00
|
|
|
if game.last_card.special == c.DRAW_FOUR and game.draw_counter:
|
2016-05-22 23:02:27 +08:00
|
|
|
add_call_bluff(results, game)
|
2016-03-01 08:25:26 +08:00
|
|
|
|
2016-04-20 06:55:56 +08:00
|
|
|
playable = player.playable_cards()
|
2016-05-20 02:52:50 +08:00
|
|
|
added_ids = list() # Duplicates are not allowed
|
2016-04-20 06:55:56 +08:00
|
|
|
|
|
|
|
for card in sorted(player.cards):
|
2016-05-20 02:52:50 +08:00
|
|
|
add_card(game, card, results,
|
|
|
|
can_play=(card in playable and
|
2016-04-20 07:32:02 +08:00
|
|
|
str(card) not in added_ids))
|
|
|
|
added_ids.append(str(card))
|
2016-04-20 06:55:56 +08:00
|
|
|
|
2016-05-27 20:03:12 +08:00
|
|
|
add_gameinfo(game, results)
|
|
|
|
|
2016-04-20 06:55:56 +08:00
|
|
|
elif user_id != game.current_player.user.id or not game.started:
|
|
|
|
for card in sorted(player.cards):
|
2016-05-20 02:52:50 +08:00
|
|
|
add_card(game, card, results, can_play=False)
|
|
|
|
|
2016-04-20 06:55:56 +08:00
|
|
|
else:
|
|
|
|
add_gameinfo(game, results)
|
2016-03-01 08:25:26 +08:00
|
|
|
|
2016-04-19 07:26:38 +08:00
|
|
|
for result in results:
|
|
|
|
result.id += ':%d' % player.anti_cheat
|
|
|
|
|
2016-04-24 08:11:37 +08:00
|
|
|
if players and game and len(players) > 1:
|
2016-05-22 03:41:38 +08:00
|
|
|
switch = _('Current game: {game}').format(game=game.chat.title)
|
2016-04-24 08:11:37 +08:00
|
|
|
|
|
|
|
answer_async(bot, update.inline_query.id, results, cache_time=0,
|
|
|
|
switch_pm_text=switch, switch_pm_parameter='select')
|
2016-03-01 08:25:26 +08:00
|
|
|
|
|
|
|
|
2016-05-22 09:13:05 +08:00
|
|
|
@game_locales
|
2016-05-22 03:41:38 +08:00
|
|
|
@user_locale
|
2017-11-28 00:59:19 +08:00
|
|
|
def process_result(bot, update, job_queue):
|
2016-05-20 02:52:50 +08:00
|
|
|
"""
|
|
|
|
Handler for chosen inline results.
|
|
|
|
Checks the players actions and acts accordingly.
|
|
|
|
"""
|
2016-04-19 06:45:27 +08:00
|
|
|
try:
|
|
|
|
user = update.chosen_inline_result.from_user
|
2016-04-24 08:11:37 +08:00
|
|
|
player = gm.userid_current[user.id]
|
|
|
|
game = player.game
|
2016-04-19 06:45:27 +08:00
|
|
|
result_id = update.chosen_inline_result.result_id
|
2016-05-20 02:52:50 +08:00
|
|
|
chat = game.chat
|
2016-06-02 21:03:33 +08:00
|
|
|
except (KeyError, AttributeError):
|
2016-04-19 06:45:27 +08:00
|
|
|
return
|
|
|
|
|
2016-03-08 09:50:24 +08:00
|
|
|
logger.debug("Selected result: " + result_id)
|
2016-03-01 08:25:26 +08:00
|
|
|
|
2016-05-08 23:46:41 +08:00
|
|
|
result_id, anti_cheat = result_id.split(':')
|
|
|
|
last_anti_cheat = player.anti_cheat
|
|
|
|
player.anti_cheat += 1
|
|
|
|
|
2016-03-09 18:31:38 +08:00
|
|
|
if result_id in ('hand', 'gameinfo', 'nogame'):
|
2016-03-01 08:25:26 +08:00
|
|
|
return
|
2017-11-28 00:59:19 +08:00
|
|
|
elif result_id.startswith('mode_'):
|
|
|
|
# First 5 characters are 'mode_', the rest is the gamemode.
|
|
|
|
mode = result_id[5:]
|
|
|
|
game.set_mode(mode)
|
|
|
|
logger.info("Gamemode changed to {mode}".format(mode = mode))
|
|
|
|
send_async(bot, chat.id, text=__("Gamemode changed to {mode}".format(mode = mode)))
|
|
|
|
return
|
2016-04-20 06:55:56 +08:00
|
|
|
elif len(result_id) == 36: # UUID result
|
|
|
|
return
|
2016-04-19 07:26:38 +08:00
|
|
|
elif int(anti_cheat) != last_anti_cheat:
|
2016-05-20 02:52:50 +08:00
|
|
|
send_async(bot, chat.id,
|
2016-05-24 21:49:23 +08:00
|
|
|
text=__("Cheat attempt by {name}", multi=game.translate)
|
2016-05-22 03:41:38 +08:00
|
|
|
.format(name=display_name(player.user)))
|
2016-04-19 07:26:38 +08:00
|
|
|
return
|
2016-03-01 08:56:33 +08:00
|
|
|
elif result_id == 'call_bluff':
|
2016-05-20 02:52:50 +08:00
|
|
|
reset_waiting_time(bot, player)
|
|
|
|
do_call_bluff(bot, player)
|
2016-03-01 08:25:26 +08:00
|
|
|
elif result_id == 'draw':
|
2016-05-20 02:52:50 +08:00
|
|
|
reset_waiting_time(bot, player)
|
2016-05-22 00:56:58 +08:00
|
|
|
do_draw(bot, player)
|
2016-03-01 08:25:26 +08:00
|
|
|
elif result_id == 'pass':
|
|
|
|
game.turn()
|
|
|
|
elif result_id in c.COLORS:
|
|
|
|
game.choose_color(result_id)
|
2016-02-29 06:57:24 +08:00
|
|
|
else:
|
2016-05-20 02:52:50 +08:00
|
|
|
reset_waiting_time(bot, player)
|
|
|
|
do_play_card(bot, player, result_id)
|
2016-03-01 08:25:26 +08:00
|
|
|
|
2017-11-28 00:59:19 +08:00
|
|
|
if game_is_running(game):
|
2016-05-22 03:41:38 +08:00
|
|
|
send_async(bot, chat.id,
|
2016-05-24 21:49:23 +08:00
|
|
|
text=__("Next player: {name}", multi=game.translate)
|
2016-05-22 03:41:38 +08:00
|
|
|
.format(name=display_name(game.current_player.user)))
|
2017-11-28 00:59:19 +08:00
|
|
|
start_player_countdown(bot, game, job_queue)
|
2016-02-29 08:53:59 +08:00
|
|
|
|
|
|
|
|
2016-05-20 02:52:50 +08:00
|
|
|
def reset_waiting_time(bot, player):
|
|
|
|
"""Resets waiting time for a player and sends a notice to the group"""
|
|
|
|
chat = player.game.chat
|
|
|
|
|
2017-11-28 00:59:19 +08:00
|
|
|
if player.waiting_time < WAITING_TIME:
|
|
|
|
player.waiting_time = WAITING_TIME
|
2016-05-22 03:41:38 +08:00
|
|
|
send_async(bot, chat.id,
|
2017-11-28 00:59:19 +08:00
|
|
|
text=__("Waiting time for {name} has been reset to {time} "
|
2016-05-24 21:49:23 +08:00
|
|
|
"seconds", multi=player.game.translate)
|
2017-11-28 00:59:19 +08:00
|
|
|
.format(name=display_name(player.user), time=WAITING_TIME))
|
2016-02-29 08:53:59 +08:00
|
|
|
|
2016-02-29 07:00:32 +08:00
|
|
|
|
2016-03-08 09:50:24 +08:00
|
|
|
# Add all handlers to the dispatcher and run the bot
|
2016-05-22 20:45:51 +08:00
|
|
|
dispatcher.add_handler(InlineQueryHandler(reply_to_query))
|
2017-11-28 00:59:19 +08:00
|
|
|
dispatcher.add_handler(ChosenInlineResultHandler(process_result, pass_job_queue=True))
|
2016-05-22 20:45:51 +08:00
|
|
|
dispatcher.add_handler(CallbackQueryHandler(select_game))
|
2017-11-28 00:59:19 +08:00
|
|
|
dispatcher.add_handler(CommandHandler('start', start_game, pass_args=True, pass_job_queue=True))
|
2016-05-22 20:45:51 +08:00
|
|
|
dispatcher.add_handler(CommandHandler('new', new_game))
|
2017-08-19 05:36:30 +08:00
|
|
|
dispatcher.add_handler(CommandHandler('kill', kill_game))
|
2016-05-22 20:45:51 +08:00
|
|
|
dispatcher.add_handler(CommandHandler('join', join_game))
|
|
|
|
dispatcher.add_handler(CommandHandler('leave', leave_game))
|
2017-12-07 16:27:51 +08:00
|
|
|
dispatcher.add_handler(CommandHandler('kick', kick_player))
|
2016-05-22 20:45:51 +08:00
|
|
|
dispatcher.add_handler(CommandHandler('open', open_game))
|
|
|
|
dispatcher.add_handler(CommandHandler('close', close_game))
|
2016-05-22 23:02:27 +08:00
|
|
|
dispatcher.add_handler(CommandHandler('enable_translations',
|
|
|
|
enable_translations))
|
|
|
|
dispatcher.add_handler(CommandHandler('disable_translations',
|
|
|
|
disable_translations))
|
2016-05-22 20:45:51 +08:00
|
|
|
dispatcher.add_handler(CommandHandler('skip', skip_player))
|
2016-07-05 06:40:36 +08:00
|
|
|
dispatcher.add_handler(CommandHandler('notify_me', notify_me))
|
2016-05-24 21:49:23 +08:00
|
|
|
simple_commands.register()
|
|
|
|
settings.register()
|
2017-08-19 05:36:30 +08:00
|
|
|
dispatcher.add_handler(MessageHandler(Filters.status_update, status_update))
|
2016-05-22 20:45:51 +08:00
|
|
|
dispatcher.add_error_handler(error)
|
|
|
|
|
|
|
|
start_bot(updater)
|
|
|
|
updater.idle()
|