2016-05-22 20:45:51 +08:00
#!/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/>.
from telegram import ParseMode
from telegram . ext import CommandHandler
2016-05-23 01:21:51 +08:00
from user_setting import UserSetting
2016-05-24 21:49:23 +08:00
from utils import send_async
2016-05-22 20:45:51 +08:00
from shared_vars import dispatcher
2016-05-24 21:49:23 +08:00
from internationalization import _ , user_locale
2016-05-22 20:45:51 +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 "
2016-10-27 20:44:08 +08:00
" 4. Type <code>@unobot</code> into your chat box and hit "
" <b>space</b>, or click the <code>via @unobot</code> text "
2016-05-22 20:45:51 +08:00
" 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, "
2016-07-03 02:37:35 +08:00
" you can use /skip to skip that player. Use /notify_me to "
" receive a private message when a new game is started. \n \n "
2016-05-22 23:02:27 +08:00
" <b>Language</b> and other settings: /settings \n "
2016-05-22 20:45:51 +08:00
" Other commands (only game creator): \n "
" /close - Close lobby \n "
2016-05-22 23:02:27 +08:00
" /open - Open lobby \n "
2017-08-19 05:36:30 +08:00
" /kill - Terminate the game \n "
2017-12-07 16:27:51 +08:00
" /kick - Select a player to kick "
" by replying to him or her \n "
2016-05-22 23:02:27 +08:00
" /enable_translations - Translate relevant texts into all "
" languages spoken in a game \n "
" /disable_translations - Use English for those texts \n \n "
2016-05-22 20:45:51 +08:00
" <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. " )
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-05-24 15:57:48 +08:00
attributions = ( " Attributions: \n "
' Draw icon by '
' <a href= " http://www.faithtoken.com/ " >Faithtoken</a> \n '
' Pass icon by '
' <a href= " http://delapouite.com/ " >Delapouite</a> \n '
" Originals available on http://game-icons.net \n "
" Icons edited by ɳick " )
2016-05-22 20:45:51 +08:00
2017-11-28 00:59:19 +08:00
modes_explanation = ( " This UNO bot has three game modes: Classic, Sanic and Wild. \n \n "
" 🎻 The Classic mode uses the conventional UNO deck and there is no auto skip. \n "
" 🚀 The Sanic mode uses the conventional UNO deck and the bot automatically skips a player if he/she takes too long to play its turn \n "
" 🐉 The Wild mode uses a deck with more special cards, less number variety and no auto skip. \n \n "
" To change the game mode, the GAME CREATOR has to type the bot nickname and a space, just like when playing a card, and all gamemode options should appear. " )
2016-05-22 20:45:51 +08:00
@user_locale
2017-11-28 00:59:19 +08:00
def help_handler ( bot , update ) :
2016-05-22 20:45:51 +08:00
""" Handler for the /help command """
send_async ( bot , update . message . chat_id , text = _ ( help_text ) ,
parse_mode = ParseMode . HTML , disable_web_page_preview = True )
2017-11-28 00:59:19 +08:00
@user_locale
def modes ( bot , update ) :
""" Handler for the /help command """
send_async ( bot , update . message . chat_id , text = _ ( modes_explanation ) ,
parse_mode = ParseMode . HTML , disable_web_page_preview = True )
2016-05-22 20:45:51 +08:00
@user_locale
def source ( bot , update ) :
""" Handler for the /help command """
2016-05-24 15:57:48 +08:00
send_async ( bot , update . message . chat_id , text = _ ( source_text ) + ' \n ' +
_ ( attributions ) ,
2016-05-22 20:45:51 +08:00
parse_mode = ParseMode . HTML , disable_web_page_preview = True )
@user_locale
def news ( bot , update ) :
""" Handler for the /news command """
send_async ( bot , update . message . chat_id ,
text = _ ( " All news here: https://telegram.me/unobotupdates " ) ,
disable_web_page_preview = True )
2016-05-23 01:21:51 +08:00
@user_locale
def stats ( bot , update ) :
user = update . message . from_user
us = UserSetting . get ( id = user . id )
if not us or not us . stats :
send_async ( bot , update . message . chat_id ,
text = _ ( " You did not enable statistics. Use /settings in "
" a private chat with the bot to enable them. " ) )
else :
stats_text = list ( )
2016-05-24 21:49:23 +08:00
n = us . games_played
2016-05-23 01:21:51 +08:00
stats_text . append (
2016-05-24 21:49:23 +08:00
_ ( " {number} game played " ,
" {number} games played " ,
n ) . format ( number = n )
)
n = us . first_places
2016-05-23 01:21:51 +08:00
stats_text . append (
2016-05-25 09:46:25 +08:00
_ ( " {number} first place " ,
2016-05-24 21:49:23 +08:00
" {number} first places " ,
n ) . format ( number = n )
)
n = us . cards_played
2016-05-23 01:21:51 +08:00
stats_text . append (
2016-05-24 21:49:23 +08:00
_ ( " {number} card played " ,
" {number} cards played " ,
n ) . format ( number = n )
)
2016-05-23 01:21:51 +08:00
send_async ( bot , update . message . chat_id ,
text = ' \n ' . join ( stats_text ) )
2016-05-24 21:49:23 +08:00
def register ( ) :
2017-11-28 00:59:19 +08:00
dispatcher . add_handler ( CommandHandler ( ' help ' , help_handler ) )
2016-05-24 21:49:23 +08:00
dispatcher . add_handler ( CommandHandler ( ' source ' , source ) )
dispatcher . add_handler ( CommandHandler ( ' news ' , news ) )
dispatcher . add_handler ( CommandHandler ( ' stats ' , stats ) )
2017-11-28 00:59:19 +08:00
dispatcher . add_handler ( CommandHandler ( ' modes ' , modes ) )