ea881cf520
* Changes for possible bug * New sanic mode wip * More changes for fast mode, WIP * Fast mode is playable (code is ugly tho) * Fixed skip error * Fixed fast mode error * Bug fixing * Possible fix for the /leave bug before the game starts * Update README to include Codacy badge * Fixing error prone code * Removing code smells * Removing more code smells * How long can this go on? (More smells according to Codacy) * Compile locale fixed for Linux. Small es_ES fix. * Major refactoring * Wild mode finished. Changed emojis for text in log. * Removing test prints, back to emojis * Code cleaning and fix for player time in fast mode * Changing help to not override builtin function * Decreased bot.py's complexity * Default gamemode is now Fast. Added a bot configuration file * back to random * Moved logger to shared_vars * Added MIN_FAST_TURN_TIME to config and fixed 'skipped 4 times' message * Pull review changes * More review changes * Removing codacy badge linked to my account for pull request * Fixed first special card issue, logger back to how it was (with just one logging init) * Renamed gameplay config file to gameplay_config.py.
128 lines
No EOL
3.5 KiB
Python
128 lines
No EOL
3.5 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
#
|
|
# 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/>.
|
|
|
|
|
|
import logging
|
|
|
|
from telegram.ext.dispatcher import run_async
|
|
|
|
from internationalization import _, __
|
|
from mwt import MWT
|
|
from shared_vars import gm
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
TIMEOUT = 2.5
|
|
|
|
|
|
def list_subtract(list1, list2):
|
|
""" Helper function to subtract two lists and return the sorted result """
|
|
list1 = list1.copy()
|
|
|
|
for x in list2:
|
|
list1.remove(x)
|
|
|
|
return list(sorted(list1))
|
|
|
|
|
|
def display_name(user):
|
|
""" Get the current players name including their username, if possible """
|
|
user_name = user.first_name
|
|
if user.username:
|
|
user_name += ' (@' + user.username + ')'
|
|
return user_name
|
|
|
|
|
|
def display_color(color):
|
|
""" Convert a color code to actual color name """
|
|
if color == "r":
|
|
return _("{emoji} Red").format(emoji='❤️')
|
|
if color == "b":
|
|
return _("{emoji} Blue").format(emoji='💙')
|
|
if color == "g":
|
|
return _("{emoji} Green").format(emoji='💚')
|
|
if color == "y":
|
|
return _("{emoji} Yellow").format(emoji='💛')
|
|
|
|
|
|
def display_color_group(color, game):
|
|
""" Convert a color code to actual color name """
|
|
if color == "r":
|
|
return __("{emoji} Red", game.translate).format(
|
|
emoji='❤️')
|
|
if color == "b":
|
|
return __("{emoji} Blue", game.translate).format(
|
|
emoji='💙')
|
|
if color == "g":
|
|
return __("{emoji} Green", game.translate).format(
|
|
emoji='💚')
|
|
if color == "y":
|
|
return __("{emoji} Yellow", game.translate).format(
|
|
emoji='💛')
|
|
|
|
|
|
def error(bot, update, error):
|
|
"""Simple error handler"""
|
|
logger.exception(error)
|
|
|
|
|
|
@run_async
|
|
def send_async(bot, *args, **kwargs):
|
|
"""Send a message asynchronously"""
|
|
if 'timeout' not in kwargs:
|
|
kwargs['timeout'] = TIMEOUT
|
|
|
|
try:
|
|
bot.sendMessage(*args, **kwargs)
|
|
except Exception as e:
|
|
error(None, None, e)
|
|
|
|
|
|
@run_async
|
|
def answer_async(bot, *args, **kwargs):
|
|
"""Answer an inline query asynchronously"""
|
|
if 'timeout' not in kwargs:
|
|
kwargs['timeout'] = TIMEOUT
|
|
|
|
try:
|
|
bot.answerInlineQuery(*args, **kwargs)
|
|
except Exception as e:
|
|
error(None, None, e)
|
|
|
|
|
|
def game_is_running(game):
|
|
return game in gm.chatid_games.get(game.chat.id, list())
|
|
|
|
|
|
def user_is_creator(user, game):
|
|
return user.id in game.owner
|
|
|
|
|
|
def user_is_admin(user, bot, chat):
|
|
return user.id in get_admin_ids(bot, chat.id)
|
|
|
|
|
|
def user_is_creator_or_admin(user, game, bot, chat):
|
|
return user_is_creator(user, game) or user_is_admin(user, bot, chat)
|
|
|
|
|
|
@MWT(timeout=60*60)
|
|
def get_admin_ids(bot, chat_id):
|
|
"""Returns a list of admin IDs for a given chat. Results are cached for 1 hour."""
|
|
return [admin.user.id for admin in bot.get_chat_administrators(chat_id)] |