2016-05-08 20:37:25 +08:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
from random import randint
|
2016-02-29 06:57:24 +08:00
|
|
|
|
2016-04-29 23:02:50 +08:00
|
|
|
from telegram import ParseMode, Message, Chat, InlineKeyboardMarkup, \
|
2016-04-24 08:11:37 +08:00
|
|
|
InlineKeyboardButton
|
2016-04-19 06:45:27 +08:00
|
|
|
from telegram.ext import Updater, InlineQueryHandler, \
|
2016-04-29 17:12:56 +08:00
|
|
|
ChosenInlineResultHandler, CommandHandler, MessageHandler, Filters, \
|
2016-04-24 08:11:37 +08:00
|
|
|
CallbackQueryHandler
|
2016-04-24 04:13:15 +08:00
|
|
|
from telegram.ext.dispatcher import run_async
|
2016-03-09 18:31:38 +08:00
|
|
|
from telegram.utils.botan import Botan
|
2016-02-29 06:57:24 +08:00
|
|
|
|
|
|
|
from game_manager import GameManager
|
2016-03-09 18:31:38 +08:00
|
|
|
from credentials import TOKEN, BOTAN_TOKEN
|
2016-03-08 06:50:39 +08:00
|
|
|
from start_bot import start_bot
|
2016-04-29 23:02:50 +08:00
|
|
|
from results import *
|
|
|
|
from utils import *
|
2016-05-16 22:55:40 +08:00
|
|
|
from player import Player # for ai players
|
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',
|
2016-05-16 22:55:40 +08:00
|
|
|
level=logging.WARNING)
|
2016-02-29 08:53:59 +08:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2016-05-08 20:37:25 +08:00
|
|
|
help_text = ("Follow these steps:\n\n"
|
|
|
|
"1. Add this bot to a group\n"
|
|
|
|
"2. In the group, start a new game with /new or join an already"
|
|
|
|
" running game with /join\n"
|
|
|
|
"3. After at least two players have joined, start the game with"
|
|
|
|
" /start\n"
|
|
|
|
"4. Type <code>@mau_mau_bot</code> into your chat box and hit "
|
|
|
|
"<b>space</b>, or click the <code>via @mau_mau_bot</code> text "
|
|
|
|
"next to messages. You will see your cards (some greyed out), "
|
|
|
|
"any extra options like drawing, and a <b>?</b> to see the "
|
|
|
|
"current game state. The <b>greyed out cards</b> are those you "
|
|
|
|
"<b>can not play</b> at the moment. Tap an option to execute "
|
|
|
|
"the selected action.\n"
|
|
|
|
"Players can join the game at any time. To leave a game, "
|
|
|
|
"use /leave. If a player takes more than 90 seconds to play, "
|
|
|
|
"you can use /skip to skip that player.\n\n"
|
|
|
|
"Other commands (only game creator):\n"
|
|
|
|
"/close - Close lobby\n"
|
|
|
|
"/open - Open lobby\n\n"
|
|
|
|
"<b>Experimental:</b> Play in multiple groups at the same time. "
|
|
|
|
"Press the <code>Current game: ...</code> button and select the "
|
|
|
|
"group you want to play a card in.\n"
|
|
|
|
"If you enjoy this bot, "
|
|
|
|
"<a href=\"https://telegram.me/storebot?start=mau_mau_bot\">"
|
|
|
|
"rate me</a>, join the "
|
|
|
|
"<a href=\"https://telegram.me/unobotupdates\">update channel</a>"
|
|
|
|
" and buy an UNO card game.\n")
|
|
|
|
|
|
|
|
source_text = ("This bot is Free Software and licensed under the AGPL. "
|
|
|
|
"The code is available here: \n"
|
|
|
|
"https://github.com/jh0ker/mau_mau_bot")
|
2016-03-08 09:03:14 +08:00
|
|
|
|
2016-05-16 22:55:40 +08:00
|
|
|
ai_iterations = 100
|
2016-02-29 06:57:24 +08:00
|
|
|
|
2016-04-24 04:13:15 +08:00
|
|
|
@run_async
|
|
|
|
def send_async(bot, *args, **kwargs):
|
2016-04-26 23:53:29 +08:00
|
|
|
if 'timeout' not in kwargs:
|
|
|
|
kwargs['timeout'] = 2.5
|
2016-05-02 00:23:59 +08:00
|
|
|
|
|
|
|
try:
|
|
|
|
bot.sendMessage(*args, **kwargs)
|
|
|
|
except Exception as e:
|
|
|
|
error(None, None, e)
|
2016-04-24 04:13:15 +08:00
|
|
|
|
|
|
|
|
2016-05-16 22:55:40 +08:00
|
|
|
@run_async
|
|
|
|
def sticker_async(bot, *args, **kwargs):
|
|
|
|
if 'timeout' not in kwargs:
|
|
|
|
kwargs['timeout'] = 2.5
|
|
|
|
|
|
|
|
try:
|
|
|
|
bot.sendSticker(*args, **kwargs)
|
|
|
|
except Exception as e:
|
|
|
|
error(None, None, e)
|
|
|
|
|
|
|
|
|
2016-04-24 04:13:15 +08:00
|
|
|
@run_async
|
|
|
|
def answer_async(bot, *args, **kwargs):
|
2016-04-30 18:20:36 +08:00
|
|
|
if 'timeout' not in kwargs:
|
|
|
|
kwargs['timeout'] = 2.5
|
2016-05-02 00:23:59 +08:00
|
|
|
|
|
|
|
try:
|
|
|
|
bot.answerInlineQuery(*args, **kwargs)
|
|
|
|
except Exception as e:
|
|
|
|
error(None, None, e)
|
2016-04-24 04:13:15 +08:00
|
|
|
|
|
|
|
|
2016-03-08 06:50:39 +08:00
|
|
|
def error(bot, update, error):
|
2016-03-08 09:50:24 +08:00
|
|
|
""" Simple error handler """
|
2016-03-08 06:50:39 +08:00
|
|
|
logger.exception(error)
|
|
|
|
|
|
|
|
|
2016-02-29 06:57:24 +08:00
|
|
|
def new_game(bot, update):
|
2016-03-08 09:50:24 +08:00
|
|
|
""" Handler for the /new command """
|
2016-02-29 06:57:24 +08:00
|
|
|
chat_id = update.message.chat_id
|
2016-03-09 18:31:38 +08:00
|
|
|
if update.message.chat.type == 'private':
|
|
|
|
help(bot, update)
|
|
|
|
else:
|
2016-05-02 00:23:59 +08:00
|
|
|
game = gm.new_game(update.message.chat)
|
|
|
|
game.owner = update.message.from_user
|
2016-04-27 21:47:11 +08:00
|
|
|
send_async(bot, chat_id,
|
|
|
|
text="Created a new game! Join the game with /join "
|
|
|
|
"and start the game with /start")
|
2016-03-09 18:31:38 +08:00
|
|
|
if botan:
|
|
|
|
botan.track(update.message, 'New games')
|
|
|
|
|
|
|
|
|
|
|
|
def join_game(bot, update):
|
|
|
|
""" Handler for the /join command """
|
|
|
|
chat_id = update.message.chat_id
|
|
|
|
if update.message.chat.type == 'private':
|
|
|
|
help(bot, update)
|
|
|
|
else:
|
2016-04-26 23:53:29 +08:00
|
|
|
try:
|
|
|
|
game = gm.chatid_games[chat_id][-1]
|
|
|
|
if not game.open:
|
|
|
|
send_async(bot, chat_id, text="The lobby is closed")
|
2016-04-27 21:47:11 +08:00
|
|
|
return
|
2016-04-26 23:53:29 +08:00
|
|
|
except (KeyError, IndexError):
|
|
|
|
pass
|
|
|
|
|
2016-03-11 16:23:53 +08:00
|
|
|
joined = gm.join_game(chat_id, update.message.from_user)
|
|
|
|
if joined:
|
2016-04-24 04:13:15 +08:00
|
|
|
send_async(bot, chat_id,
|
|
|
|
text="Joined the game",
|
|
|
|
reply_to_message_id=update.message.message_id)
|
2016-03-11 16:23:53 +08:00
|
|
|
elif joined is None:
|
2016-04-24 04:13:15 +08:00
|
|
|
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)
|
2016-03-11 16:23:53 +08:00
|
|
|
else:
|
2016-04-24 04:13:15 +08:00
|
|
|
send_async(bot, chat_id,
|
|
|
|
text="You already joined the game. Start the game "
|
|
|
|
"with /start",
|
|
|
|
reply_to_message_id=update.message.message_id)
|
2016-02-29 06:57:24 +08:00
|
|
|
|
|
|
|
|
2016-05-16 22:55:40 +08:00
|
|
|
def add_ai(bot, update):
|
|
|
|
""" Handler for the /add_ai command """
|
|
|
|
chat_id = update.message.chat_id
|
|
|
|
if update.message.chat.type == 'private':
|
|
|
|
help(bot, update)
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
game = gm.chatid_games[chat_id][-1]
|
|
|
|
if not game.open:
|
|
|
|
send_async(bot, chat_id, text="The lobby is closed")
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
Player(game, None, ai=True)
|
|
|
|
send_async(bot, chat_id,
|
|
|
|
text="Added computer player",
|
|
|
|
reply_to_message_id=update.message.message_id)
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
2016-03-08 06:50:39 +08:00
|
|
|
def leave_game(bot, update):
|
2016-03-08 09:50:24 +08:00
|
|
|
""" Handler for the /leave command """
|
2016-03-08 06:50:39 +08:00
|
|
|
chat_id = update.message.chat_id
|
2016-04-24 08:11:37 +08:00
|
|
|
user = update.message.from_user
|
2016-04-26 23:53:29 +08:00
|
|
|
players = gm.userid_players.get(user.id, list())
|
2016-04-24 08:11:37 +08:00
|
|
|
for player in players:
|
|
|
|
if player.game.chat.id == chat_id:
|
|
|
|
game = player.game
|
|
|
|
break
|
|
|
|
else:
|
2016-04-26 23:53:29 +08:00
|
|
|
send_async(bot, chat_id, text="You are not playing in a game in "
|
2016-04-27 21:47:11 +08:00
|
|
|
"this group.",
|
|
|
|
reply_to_message_id=update.message.message_id)
|
2016-04-26 23:53:29 +08:00
|
|
|
return
|
|
|
|
|
2016-03-08 06:50:39 +08:00
|
|
|
user = update.message.from_user
|
2016-03-08 09:50:24 +08:00
|
|
|
|
2016-04-26 23:53:29 +08:00
|
|
|
if len(game.players) < 3:
|
2016-04-24 08:11:37 +08:00
|
|
|
gm.end_game(chat_id, user)
|
2016-04-24 04:30:21 +08:00
|
|
|
send_async(bot, chat_id, text="Game ended!")
|
2016-03-08 06:50:39 +08:00
|
|
|
else:
|
2016-04-24 08:11:37 +08:00
|
|
|
if gm.leave_game(user, chat_id):
|
2016-04-30 17:43:26 +08:00
|
|
|
send_async(bot, chat_id,
|
|
|
|
text="Okay. Next Player: " +
|
|
|
|
display_name(game.current_player.user),
|
2016-04-27 21:47:11 +08:00
|
|
|
reply_to_message_id=update.message.message_id)
|
2016-05-16 22:55:40 +08:00
|
|
|
ai_turn(bot, game)
|
2016-04-24 08:11:37 +08:00
|
|
|
else:
|
|
|
|
send_async(bot, chat_id, text="You are not playing in a game in "
|
2016-04-27 21:47:11 +08:00
|
|
|
"this group.",
|
|
|
|
reply_to_message_id=update.message.message_id)
|
2016-04-24 08:11:37 +08:00
|
|
|
|
|
|
|
|
|
|
|
def select_game(bot, update):
|
|
|
|
|
|
|
|
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:
|
|
|
|
send_async(bot, update.callback_query.message.chat_id,
|
|
|
|
text="Game not found :(")
|
|
|
|
return
|
|
|
|
|
2016-04-26 23:53:29 +08:00
|
|
|
back = [[InlineKeyboardButton(text='Back to last group',
|
|
|
|
switch_inline_query='')]]
|
2016-04-24 08:11:37 +08:00
|
|
|
|
|
|
|
bot.answerCallbackQuery(update.callback_query.id,
|
2016-04-26 23:53:29 +08:00
|
|
|
text="Please switch to the group you selected!",
|
2016-05-08 20:37:25 +08:00
|
|
|
show_alert=False,
|
|
|
|
timeout=2.5)
|
2016-04-26 23:53:29 +08:00
|
|
|
bot.editMessageText(chat_id=update.callback_query.message.chat_id,
|
|
|
|
message_id=update.callback_query.message.message_id,
|
2016-05-02 00:23:59 +08:00
|
|
|
text="Selected group: %s\n"
|
2016-04-26 23:53:29 +08:00
|
|
|
"<b>Make sure that you switch to the correct "
|
|
|
|
"group!</b>"
|
|
|
|
% gm.userid_current[user_id].game.chat.title,
|
|
|
|
reply_markup=InlineKeyboardMarkup(back),
|
2016-05-08 20:37:25 +08:00
|
|
|
parse_mode=ParseMode.HTML,
|
|
|
|
timeout=2.5)
|
2016-03-08 06:50:39 +08:00
|
|
|
|
|
|
|
|
2016-04-19 08:22:42 +08:00
|
|
|
def status_update(bot, update):
|
|
|
|
""" Remove player from game if user leaves the group """
|
|
|
|
|
|
|
|
if update.message.left_chat_member:
|
|
|
|
try:
|
|
|
|
chat_id = update.message.chat_id
|
|
|
|
user = update.message.left_chat_member
|
|
|
|
except KeyError:
|
|
|
|
return
|
|
|
|
|
2016-04-26 23:53:29 +08:00
|
|
|
if gm.leave_game(user, chat_id):
|
2016-04-24 04:13:15 +08:00
|
|
|
send_async(bot, chat_id, text="Removing %s from the game"
|
2016-04-19 08:22:42 +08:00
|
|
|
% display_name(user))
|
|
|
|
|
|
|
|
|
2016-04-24 08:11:37 +08:00
|
|
|
def start_game(bot, update, args):
|
2016-03-08 09:50:24 +08:00
|
|
|
""" Handler for the /start command """
|
2016-03-11 16:23:53 +08:00
|
|
|
|
|
|
|
if update.message.chat.type != 'private':
|
|
|
|
# Show the first card
|
|
|
|
chat_id = update.message.chat_id
|
2016-04-26 23:53:29 +08:00
|
|
|
try:
|
|
|
|
game = gm.chatid_games[chat_id][-1]
|
|
|
|
except (KeyError, IndexError):
|
|
|
|
send_async(bot, chat_id, text="There is no game running in this "
|
|
|
|
"chat. Create a new one with /new")
|
|
|
|
return
|
2016-03-11 16:23:53 +08:00
|
|
|
|
2016-04-20 06:55:56 +08:00
|
|
|
if game.current_player is None or \
|
|
|
|
game.current_player is game.current_player.next:
|
2016-04-24 04:13:15 +08:00
|
|
|
send_async(bot, chat_id, text="At least two players must /join "
|
2016-03-11 16:23:53 +08:00
|
|
|
"the game before you can start it")
|
|
|
|
elif game.started:
|
2016-04-24 04:13:15 +08:00
|
|
|
send_async(bot, chat_id, text="The game has already started")
|
2016-03-11 16:23:53 +08:00
|
|
|
else:
|
2016-02-29 19:16:12 +08:00
|
|
|
game.play_card(game.last_card)
|
2016-03-11 16:23:53 +08:00
|
|
|
game.started = True
|
2016-04-20 06:55:56 +08:00
|
|
|
bot.sendSticker(chat_id,
|
2016-05-08 20:37:25 +08:00
|
|
|
sticker=c.STICKERS[str(game.last_card)],
|
|
|
|
timeout=2.5)
|
2016-04-24 04:13:15 +08:00
|
|
|
send_async(bot, chat_id,
|
2016-05-09 00:52:10 +08:00
|
|
|
text="First player: %s\n"
|
|
|
|
"Use /close to stop people from joining the game."
|
|
|
|
% display_name(game.current_player.user))
|
2016-05-16 22:55:40 +08:00
|
|
|
ai_turn(bot, game)
|
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:
|
|
|
|
groups.append([InlineKeyboardButton(text=player.game.chat.title,
|
|
|
|
callback_data=
|
|
|
|
str(player.game.chat.id))])
|
|
|
|
send_async(bot, update.message.chat_id,
|
2016-04-26 23:53:29 +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-02-29 06:57:24 +08:00
|
|
|
else:
|
2016-03-11 16:23:53 +08:00
|
|
|
help(bot, update)
|
2016-02-29 06:57:24 +08:00
|
|
|
|
|
|
|
|
2016-04-26 23:53:29 +08:00
|
|
|
def close_game(bot, update):
|
|
|
|
""" Handler for the /close command """
|
|
|
|
chat_id = update.message.chat_id
|
|
|
|
user = update.message.from_user
|
2016-05-02 00:23:59 +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:
|
|
|
|
send_async(bot, chat_id, text="There is no running game")
|
|
|
|
return
|
|
|
|
|
|
|
|
game = games[-1]
|
|
|
|
|
|
|
|
if game.owner.id == user.id:
|
|
|
|
game.open = False
|
|
|
|
send_async(bot, chat_id, text="Closed the lobby. "
|
|
|
|
"No more players can join this game.")
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
send_async(bot, chat_id,
|
|
|
|
text="Only the game creator (%s) can do that"
|
|
|
|
% game.owner.first_name,
|
|
|
|
reply_to_message_id=update.message.message_id)
|
|
|
|
return
|
2016-04-26 23:53:29 +08:00
|
|
|
|
|
|
|
|
|
|
|
def open_game(bot, update):
|
|
|
|
""" Handler for the /open command """
|
|
|
|
chat_id = update.message.chat_id
|
|
|
|
user = update.message.from_user
|
2016-05-02 00:23:59 +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:
|
|
|
|
send_async(bot, chat_id, text="There is no running game")
|
|
|
|
return
|
|
|
|
|
|
|
|
game = games[-1]
|
|
|
|
|
|
|
|
if game.owner.id == user.id:
|
|
|
|
game.open = True
|
|
|
|
send_async(bot, chat_id, text="Opened the lobby. "
|
|
|
|
"New players may /join the game.")
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
send_async(bot, chat_id,
|
|
|
|
text="Only the game creator (%s) can do that"
|
|
|
|
% game.owner.first_name,
|
|
|
|
reply_to_message_id=update.message.message_id)
|
|
|
|
return
|
2016-04-26 23:53:29 +08:00
|
|
|
|
|
|
|
|
|
|
|
def skip_player(bot, update):
|
|
|
|
""" Handler for the /skip command """
|
|
|
|
chat_id = update.message.chat_id
|
|
|
|
user = update.message.from_user
|
2016-05-02 00:23:59 +08:00
|
|
|
games = gm.chatid_games.get(chat_id)
|
|
|
|
players = gm.userid_players.get(user.id)
|
|
|
|
|
|
|
|
if not games:
|
|
|
|
send_async(bot, chat_id, text="There is no running game")
|
|
|
|
return
|
|
|
|
|
|
|
|
if not players:
|
|
|
|
send_async(bot, chat_id, text="You are not playing")
|
|
|
|
return
|
2016-04-26 23:53:29 +08:00
|
|
|
|
|
|
|
for game in games:
|
|
|
|
for player in players:
|
|
|
|
if player in game.players:
|
2016-05-02 00:23:59 +08:00
|
|
|
started = game.current_player.turn_started
|
|
|
|
now = datetime.now()
|
|
|
|
delta = (now - started).seconds
|
|
|
|
|
2016-05-08 23:17:51 +08:00
|
|
|
if delta < game.current_player.waiting_time:
|
2016-04-26 23:53:29 +08:00
|
|
|
send_async(bot, chat_id,
|
2016-05-02 00:23:59 +08:00
|
|
|
text="Please wait %d seconds"
|
2016-05-08 23:17:51 +08:00
|
|
|
% (game.current_player.waiting_time -
|
|
|
|
delta),
|
2016-05-02 00:23:59 +08:00
|
|
|
reply_to_message_id=
|
|
|
|
update.message.message_id)
|
2016-04-30 19:26:50 +08:00
|
|
|
return
|
2016-04-26 23:53:29 +08:00
|
|
|
|
2016-05-08 23:03:29 +08:00
|
|
|
elif game.current_player.waiting_time > 0:
|
|
|
|
game.current_player.anti_cheat += 1
|
|
|
|
game.current_player.waiting_time -= 30
|
|
|
|
game.current_player.cards.append(game.deck.draw())
|
|
|
|
send_async(bot, chat_id,
|
|
|
|
text="Waiting time to skip this player has "
|
|
|
|
"been reduced to %d seconds.\n"
|
|
|
|
"Next player: %s"
|
|
|
|
% (game.current_player.waiting_time,
|
2016-05-08 23:46:41 +08:00
|
|
|
display_name(
|
2016-05-08 23:52:06 +08:00
|
|
|
game.current_player.next.user)))
|
2016-05-08 23:13:01 +08:00
|
|
|
game.turn()
|
2016-05-16 22:55:40 +08:00
|
|
|
ai_turn(bot, game)
|
2016-05-08 23:03:29 +08:00
|
|
|
return
|
|
|
|
|
|
|
|
elif len(game.players) > 2:
|
|
|
|
send_async(bot, chat_id,
|
|
|
|
text="%s was skipped four times in a row "
|
|
|
|
"and has been removed from the game.\n"
|
|
|
|
"Next player: %s"
|
|
|
|
% (display_name(game.current_player.user),
|
|
|
|
display_name(
|
|
|
|
game.current_player.next.user)))
|
|
|
|
|
|
|
|
gm.leave_game(game.current_player.user, chat_id)
|
2016-05-16 22:55:40 +08:00
|
|
|
ai_turn(bot, game)
|
2016-05-08 23:03:29 +08:00
|
|
|
return
|
|
|
|
else:
|
|
|
|
send_async(bot, chat_id,
|
|
|
|
text="%s was skipped four times in a row "
|
|
|
|
"and has been removed from the game.\n"
|
|
|
|
"The game ended."
|
|
|
|
% display_name(game.current_player.user))
|
|
|
|
|
|
|
|
gm.end_game(chat_id, game.current_player.user)
|
|
|
|
return
|
2016-05-02 00:23:59 +08:00
|
|
|
|
2016-04-26 23:53:29 +08:00
|
|
|
|
2016-03-08 09:03:14 +08:00
|
|
|
def help(bot, update):
|
2016-03-08 09:50:24 +08:00
|
|
|
""" Handler for the /help command """
|
2016-04-24 04:13:15 +08:00
|
|
|
send_async(bot, update.message.chat_id, text=help_text,
|
|
|
|
parse_mode=ParseMode.HTML, disable_web_page_preview=True)
|
2016-03-08 09:03:14 +08:00
|
|
|
|
|
|
|
|
2016-05-08 20:37:25 +08:00
|
|
|
def source(bot, update):
|
|
|
|
""" Handler for the /help command """
|
|
|
|
send_async(bot, update.message.chat_id, text=source_text,
|
|
|
|
parse_mode=ParseMode.HTML, disable_web_page_preview=True)
|
|
|
|
|
|
|
|
|
2016-03-11 16:23:53 +08:00
|
|
|
def news(bot, update):
|
|
|
|
""" Handler for the /news command """
|
2016-04-24 04:13:15 +08:00
|
|
|
send_async(bot, update.message.chat_id,
|
|
|
|
text="All news here: https://telegram.me/unobotupdates",
|
|
|
|
disable_web_page_preview=True)
|
2016-03-11 16:23:53 +08:00
|
|
|
|
|
|
|
|
2016-03-01 08:25:26 +08:00
|
|
|
def reply_to_query(bot, update):
|
2016-03-08 09:50:24 +08:00
|
|
|
""" Builds the result list for inline queries and answers to the client """
|
2016-03-01 08:25:26 +08:00
|
|
|
results = list()
|
|
|
|
playable = 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:
|
|
|
|
user_id = update.inline_query.from_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:
|
2016-03-11 16:23:53 +08:00
|
|
|
if not game.started:
|
|
|
|
add_not_started(results)
|
|
|
|
elif user_id == game.current_player.user.id:
|
2016-03-09 18:31:38 +08:00
|
|
|
if game.choosing_color:
|
|
|
|
add_choose_color(results)
|
|
|
|
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:
|
|
|
|
add_pass(results)
|
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:
|
|
|
|
add_call_bluff(results)
|
2016-03-01 08:25:26 +08:00
|
|
|
|
2016-04-20 06:55:56 +08:00
|
|
|
playable = player.playable_cards()
|
2016-04-20 07:32:02 +08:00
|
|
|
added_ids = list()
|
2016-04-20 06:55:56 +08:00
|
|
|
|
|
|
|
for card in sorted(player.cards):
|
|
|
|
add_play_card(game, card, results,
|
2016-04-20 07:32:02 +08:00
|
|
|
can_play=(card in playable and
|
|
|
|
str(card) not in added_ids))
|
|
|
|
added_ids.append(str(card))
|
2016-04-20 06:55:56 +08:00
|
|
|
|
|
|
|
if False or game.choosing_color:
|
|
|
|
add_other_cards(playable, player, results, game)
|
|
|
|
elif user_id != game.current_player.user.id or not game.started:
|
|
|
|
for card in sorted(player.cards):
|
|
|
|
add_play_card(game, card, results, can_play=False)
|
|
|
|
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:
|
|
|
|
switch = 'Current game: %s' % game.chat.title
|
|
|
|
|
|
|
|
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-03-08 06:50:39 +08:00
|
|
|
def process_result(bot, update):
|
2016-03-08 09:50:24 +08:00
|
|
|
""" Check the players actions and act 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-04-26 23:53:29 +08:00
|
|
|
chat_id = game.chat.id
|
2016-04-19 06:45:27 +08:00
|
|
|
except KeyError:
|
|
|
|
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
|
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-04-24 04:13:15 +08:00
|
|
|
send_async(bot, chat_id,
|
|
|
|
text="Cheat attempt by %s" % 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-09 00:38:31 +08:00
|
|
|
reset_waiting_time(bot, chat_id, player)
|
2016-03-08 06:50:39 +08:00
|
|
|
do_call_bluff(bot, chat_id, game, player)
|
2016-03-01 08:25:26 +08:00
|
|
|
elif result_id == 'draw':
|
2016-05-09 00:38:31 +08:00
|
|
|
reset_waiting_time(bot, chat_id, player)
|
2016-03-08 06:50:39 +08:00
|
|
|
do_draw(game, 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-09 00:38:31 +08:00
|
|
|
reset_waiting_time(bot, chat_id, player)
|
2016-03-08 06:50:39 +08:00
|
|
|
do_play_card(bot, chat_id, game, player, result_id, user)
|
2016-03-01 08:25:26 +08:00
|
|
|
|
2016-04-26 23:53:29 +08:00
|
|
|
if game in gm.chatid_games.get(chat_id, list()):
|
2016-04-24 04:13:15 +08:00
|
|
|
send_async(bot, chat_id, text="Next player: " +
|
2016-04-19 07:26:38 +08:00
|
|
|
display_name(game.current_player.user))
|
2016-05-16 22:55:40 +08:00
|
|
|
ai_turn(bot, game)
|
2016-02-29 08:53:59 +08:00
|
|
|
|
|
|
|
|
2016-05-09 00:38:31 +08:00
|
|
|
def reset_waiting_time(bot, chat_id, player):
|
|
|
|
if player.waiting_time < 90:
|
|
|
|
player.waiting_time = 90
|
|
|
|
send_async(bot, chat_id, text="Waiting time for %s has been reset to "
|
|
|
|
"90 seconds" % display_name(player.user))
|
|
|
|
|
|
|
|
|
2016-03-08 06:50:39 +08:00
|
|
|
def do_play_card(bot, chat_id, game, player, result_id, user):
|
|
|
|
card = c.from_str(result_id)
|
|
|
|
game.play_card(card)
|
|
|
|
player.cards.remove(card)
|
|
|
|
if game.choosing_color:
|
2016-04-24 04:13:15 +08:00
|
|
|
send_async(bot, chat_id, text="Please choose a color")
|
2016-03-08 06:50:39 +08:00
|
|
|
if len(player.cards) == 1:
|
2016-04-26 23:53:29 +08:00
|
|
|
send_async(bot, chat_id, text="UNO!")
|
2016-03-08 06:50:39 +08:00
|
|
|
if len(player.cards) == 0:
|
2016-04-26 23:53:29 +08:00
|
|
|
send_async(bot, chat_id, text="%s won!" % user.first_name)
|
2016-04-30 19:26:50 +08:00
|
|
|
if len(game.players) < 3:
|
2016-04-24 04:13:15 +08:00
|
|
|
send_async(bot, chat_id, text="Game ended!")
|
2016-04-24 08:11:37 +08:00
|
|
|
gm.end_game(chat_id, user)
|
2016-04-30 19:26:50 +08:00
|
|
|
else:
|
|
|
|
gm.leave_game(user, chat_id)
|
2016-03-08 06:50:39 +08:00
|
|
|
|
2016-03-09 18:31:38 +08:00
|
|
|
if botan:
|
|
|
|
botan.track(Message(randint(1, 1000000000), user, datetime.now(),
|
|
|
|
Chat(chat_id, 'group')),
|
|
|
|
'Played cards')
|
|
|
|
|
2016-03-08 06:50:39 +08:00
|
|
|
|
|
|
|
def do_draw(game, player):
|
|
|
|
draw_counter_before = game.draw_counter
|
|
|
|
for n in range(game.draw_counter or 1):
|
|
|
|
player.cards.append(game.deck.draw())
|
|
|
|
game.draw_counter = 0
|
|
|
|
player.drew = True
|
2016-03-08 19:54:16 +08:00
|
|
|
if (game.last_card.value == c.DRAW_TWO or
|
|
|
|
game.last_card.special == c.DRAW_FOUR) and \
|
|
|
|
draw_counter_before > 0:
|
2016-03-08 06:50:39 +08:00
|
|
|
game.turn()
|
|
|
|
|
|
|
|
|
|
|
|
def do_call_bluff(bot, chat_id, game, player):
|
2016-05-16 22:55:40 +08:00
|
|
|
if player.prev.ai:
|
|
|
|
send_async(bot, chat_id, text="Computer doesn't know bluffing yet")
|
|
|
|
return
|
|
|
|
elif player.prev.bluffing:
|
2016-04-24 04:13:15 +08:00
|
|
|
send_async(bot, chat_id, text="Bluff called! Giving %d cards to %s"
|
2016-03-08 06:50:39 +08:00
|
|
|
% (game.draw_counter,
|
|
|
|
player.prev.user.first_name))
|
|
|
|
for i in range(game.draw_counter):
|
|
|
|
player.prev.cards.append(game.deck.draw())
|
|
|
|
else:
|
2016-04-24 04:13:15 +08:00
|
|
|
send_async(bot, chat_id, text="%s didn't bluff! Giving %d cards to %s"
|
2016-03-08 06:50:39 +08:00
|
|
|
% (player.prev.user.first_name,
|
|
|
|
game.draw_counter + 2,
|
|
|
|
player.user.first_name))
|
|
|
|
for i in range(game.draw_counter + 2):
|
|
|
|
player.cards.append(game.deck.draw())
|
|
|
|
game.draw_counter = 0
|
|
|
|
game.turn()
|
2016-05-16 22:55:40 +08:00
|
|
|
ai_turn(bot, game)
|
2016-02-29 08:53:59 +08:00
|
|
|
|
2016-02-29 07:00:32 +08:00
|
|
|
|
2016-05-16 22:55:40 +08:00
|
|
|
def ai_turn(bot, game):
|
|
|
|
player = game.current_player
|
|
|
|
while player.ai:
|
|
|
|
reply = ''
|
|
|
|
|
|
|
|
from ISMCTS import UNOState, ISMCTS
|
|
|
|
chat_id = game.chat.id
|
|
|
|
state = UNOState(game)
|
|
|
|
move = ISMCTS(state, itermax=ai_iterations, verbose=False)
|
|
|
|
if move == 'draw':
|
|
|
|
reply += 'Drawing\n'
|
|
|
|
else:
|
|
|
|
sticker_async(bot, chat_id,
|
|
|
|
sticker=c.STICKERS[str(move)])
|
|
|
|
if move.special:
|
|
|
|
reply += "Choosing color: %s\n" % display_color(move.color)
|
|
|
|
|
|
|
|
state.DoMove(move)
|
|
|
|
if len(player.cards) == 1:
|
|
|
|
reply += "UNO!\n"
|
|
|
|
if len(player.cards) == 0:
|
|
|
|
reply += "%s won!\n" % player.user.first_name
|
|
|
|
if len(game.players) < 3:
|
|
|
|
reply += "Game ended!"
|
|
|
|
gm.end_game(chat_id, player.next.user)
|
|
|
|
else:
|
|
|
|
player.leave()
|
|
|
|
|
|
|
|
player = game.current_player
|
|
|
|
if game in gm.chatid_games.get(chat_id, list()):
|
|
|
|
reply += "Next player: " + display_name(player.user)
|
|
|
|
|
|
|
|
send_async(bot, chat_id, text=reply)
|
|
|
|
|
|
|
|
|
|
|
|
def set_ai_iterations(bot, update, args):
|
|
|
|
global ai_iterations
|
|
|
|
ai_iterations = int(args[0])
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
gm = GameManager()
|
|
|
|
u = Updater(token=TOKEN, workers=32)
|
|
|
|
dp = u.dispatcher
|
|
|
|
|
|
|
|
botan = False
|
|
|
|
if BOTAN_TOKEN:
|
|
|
|
botan = Botan(BOTAN_TOKEN)
|
|
|
|
|
|
|
|
# Add all handlers to the dispatcher and run the bot
|
|
|
|
dp.addHandler(InlineQueryHandler(reply_to_query))
|
|
|
|
dp.addHandler(ChosenInlineResultHandler(process_result))
|
|
|
|
dp.addHandler(CallbackQueryHandler(select_game))
|
|
|
|
dp.addHandler(CommandHandler('start', start_game, pass_args=True))
|
|
|
|
dp.addHandler(CommandHandler('new', new_game))
|
|
|
|
dp.addHandler(CommandHandler('join', join_game))
|
|
|
|
dp.addHandler(CommandHandler('add_ai', add_ai))
|
|
|
|
dp.addHandler(CommandHandler('set_ai', set_ai_iterations, pass_args=True))
|
|
|
|
dp.addHandler(CommandHandler('leave', leave_game))
|
|
|
|
dp.addHandler(CommandHandler('open', open_game))
|
|
|
|
dp.addHandler(CommandHandler('close', close_game))
|
|
|
|
dp.addHandler(CommandHandler('skip', skip_player))
|
|
|
|
dp.addHandler(CommandHandler('help', help))
|
|
|
|
dp.addHandler(CommandHandler('source', source))
|
|
|
|
dp.addHandler(CommandHandler('news', news))
|
|
|
|
dp.addHandler(MessageHandler([Filters.status_update], status_update))
|
|
|
|
dp.addErrorHandler(error)
|
|
|
|
|
|
|
|
start_bot(u)
|
|
|
|
u.idle()
|