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-05-20 02:52:50 +08:00
|
|
|
"""Defines helper functions to build the inline result list"""
|
|
|
|
|
2016-04-29 23:02:50 +08:00
|
|
|
from uuid import uuid4
|
|
|
|
|
|
|
|
from telegram import InlineQueryResultArticle, InputTextMessageContent, \
|
|
|
|
InlineQueryResultCachedSticker as Sticker
|
|
|
|
|
|
|
|
import card as c
|
2016-05-24 21:49:23 +08:00
|
|
|
from utils import display_color, display_color_group, display_name
|
|
|
|
from internationalization import _, __
|
2016-04-29 23:02:50 +08:00
|
|
|
|
|
|
|
|
2016-05-23 01:21:51 +08:00
|
|
|
def add_choose_color(results, game):
|
2016-05-20 02:52:50 +08:00
|
|
|
"""Add choose color options"""
|
2016-04-29 23:02:50 +08:00
|
|
|
for color in c.COLORS:
|
|
|
|
results.append(
|
|
|
|
InlineQueryResultArticle(
|
|
|
|
id=color,
|
2016-05-22 03:41:38 +08:00
|
|
|
title=_("Choose Color"),
|
2016-04-29 23:02:50 +08:00
|
|
|
description=display_color(color),
|
|
|
|
input_message_content=
|
2016-05-23 01:21:51 +08:00
|
|
|
InputTextMessageContent(display_color_group(color, game))
|
2016-04-29 23:02:50 +08:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2016-05-24 21:49:23 +08:00
|
|
|
def add_other_cards(player, results, game):
|
2016-05-20 02:52:50 +08:00
|
|
|
"""Add hand cards when choosing colors"""
|
2016-04-29 23:02:50 +08:00
|
|
|
|
|
|
|
results.append(
|
|
|
|
InlineQueryResultArticle(
|
|
|
|
"hand",
|
2016-05-24 21:49:23 +08:00
|
|
|
title=_("Card (tap for game state):",
|
|
|
|
"Cards (tap for game state):",
|
|
|
|
len(player.cards)),
|
|
|
|
description=', '.join([repr(card) for card in player.cards]),
|
2016-05-22 03:41:38 +08:00
|
|
|
input_message_content=game_info(game)
|
2016-04-29 23:02:50 +08:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def player_list(game):
|
2016-05-20 02:52:50 +08:00
|
|
|
"""Generate list of player strings"""
|
2018-07-17 11:56:16 +08:00
|
|
|
return [_("{name} ({number} card)",
|
|
|
|
"{name} ({number} cards)",
|
2018-07-17 11:45:18 +08:00
|
|
|
len(player.cards))
|
2016-05-22 03:41:38 +08:00
|
|
|
.format(name=player.user.first_name, number=len(player.cards))
|
2016-05-22 00:55:03 +08:00
|
|
|
for player in game.players]
|
|
|
|
|
2016-04-29 23:02:50 +08:00
|
|
|
|
|
|
|
def add_no_game(results):
|
2016-05-20 02:52:50 +08:00
|
|
|
"""Add text result if user is not playing"""
|
2016-04-29 23:02:50 +08:00
|
|
|
results.append(
|
|
|
|
InlineQueryResultArticle(
|
|
|
|
"nogame",
|
2016-05-22 03:41:38 +08:00
|
|
|
title=_("You are not playing"),
|
2016-04-29 23:02:50 +08:00
|
|
|
input_message_content=
|
2016-05-22 03:41:38 +08:00
|
|
|
InputTextMessageContent(_('Not playing right now. Use /new to '
|
|
|
|
'start a game or /join to join the '
|
|
|
|
'current game in this group'))
|
2016-04-29 23:02:50 +08:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def add_not_started(results):
|
2016-05-20 02:52:50 +08:00
|
|
|
"""Add text result if the game has not yet started"""
|
2016-04-29 23:02:50 +08:00
|
|
|
results.append(
|
|
|
|
InlineQueryResultArticle(
|
|
|
|
"nogame",
|
2016-05-22 03:41:38 +08:00
|
|
|
title=_("The game wasn't started yet"),
|
2016-04-29 23:02:50 +08:00
|
|
|
input_message_content=
|
2016-05-22 03:41:38 +08:00
|
|
|
InputTextMessageContent(_('Start the game with /start'))
|
2016-04-29 23:02:50 +08:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2017-11-28 00:59:19 +08:00
|
|
|
def add_mode_classic(results):
|
|
|
|
"""Change mode to classic"""
|
|
|
|
results.append(
|
|
|
|
InlineQueryResultArticle(
|
|
|
|
"mode_classic",
|
|
|
|
title=_("🎻 Classic mode"),
|
|
|
|
input_message_content=
|
|
|
|
InputTextMessageContent(_('Classic 🎻'))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def add_mode_fast(results):
|
|
|
|
"""Change mode to classic"""
|
|
|
|
results.append(
|
|
|
|
InlineQueryResultArticle(
|
|
|
|
"mode_fast",
|
|
|
|
title=_("🚀 Sanic mode"),
|
|
|
|
input_message_content=
|
|
|
|
InputTextMessageContent(_('Gotta go fast! 🚀'))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def add_mode_wild(results):
|
|
|
|
"""Change mode to classic"""
|
|
|
|
results.append(
|
|
|
|
InlineQueryResultArticle(
|
|
|
|
"mode_wild",
|
|
|
|
title=_("🐉 Wild mode"),
|
|
|
|
input_message_content=
|
|
|
|
InputTextMessageContent(_('Into the Wild~ 🐉'))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2018-10-19 00:26:36 +08:00
|
|
|
def add_mode_score(results):
|
|
|
|
"""Change mode to classic"""
|
|
|
|
results.append(
|
|
|
|
InlineQueryResultArticle(
|
|
|
|
"mode_score",
|
|
|
|
title=_("💯 Score mode"),
|
|
|
|
input_message_content=
|
|
|
|
InputTextMessageContent(_('Kaching~ 💯'))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2016-04-29 23:02:50 +08:00
|
|
|
def add_draw(player, results):
|
2016-05-20 02:52:50 +08:00
|
|
|
"""Add option to draw"""
|
2016-05-22 03:41:38 +08:00
|
|
|
n = player.game.draw_counter or 1
|
|
|
|
|
2016-04-29 23:02:50 +08:00
|
|
|
results.append(
|
|
|
|
Sticker(
|
|
|
|
"draw", sticker_file_id=c.STICKERS['option_draw'],
|
|
|
|
input_message_content=
|
2016-05-24 21:49:23 +08:00
|
|
|
InputTextMessageContent(__('Drawing {number} card',
|
|
|
|
'Drawing {number} cards', n,
|
|
|
|
multi=player.game.translate)
|
2016-05-22 03:41:38 +08:00
|
|
|
.format(number=n))
|
2016-04-29 23:02:50 +08:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def add_gameinfo(game, results):
|
2016-05-20 02:52:50 +08:00
|
|
|
"""Add option to show game info"""
|
2016-04-29 23:02:50 +08:00
|
|
|
|
|
|
|
results.append(
|
|
|
|
Sticker(
|
|
|
|
"gameinfo",
|
|
|
|
sticker_file_id=c.STICKERS['option_info'],
|
2016-05-22 03:41:38 +08:00
|
|
|
input_message_content=game_info(game)
|
2016-04-29 23:02:50 +08:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2016-05-22 23:02:27 +08:00
|
|
|
def add_pass(results, game):
|
2016-05-20 02:52:50 +08:00
|
|
|
"""Add option to pass"""
|
2016-04-29 23:02:50 +08:00
|
|
|
results.append(
|
|
|
|
Sticker(
|
|
|
|
"pass", sticker_file_id=c.STICKERS['option_pass'],
|
2016-05-24 21:49:23 +08:00
|
|
|
input_message_content=InputTextMessageContent(
|
|
|
|
__('Pass', multi=game.translate)
|
|
|
|
)
|
2016-04-29 23:02:50 +08:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2016-05-22 23:02:27 +08:00
|
|
|
def add_call_bluff(results, game):
|
2016-05-20 02:52:50 +08:00
|
|
|
"""Add option to call a bluff"""
|
2016-04-29 23:02:50 +08:00
|
|
|
results.append(
|
|
|
|
Sticker(
|
|
|
|
"call_bluff",
|
|
|
|
sticker_file_id=c.STICKERS['option_bluff'],
|
|
|
|
input_message_content=
|
2016-05-22 23:02:27 +08:00
|
|
|
InputTextMessageContent(__("I'm calling your bluff!",
|
2016-05-24 21:49:23 +08:00
|
|
|
multi=game.translate))
|
2016-04-29 23:02:50 +08:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2016-05-20 02:52:50 +08:00
|
|
|
def add_card(game, card, results, can_play):
|
|
|
|
"""Add an option that represents a card"""
|
2016-04-29 23:02:50 +08:00
|
|
|
|
|
|
|
if can_play:
|
|
|
|
results.append(
|
|
|
|
Sticker(str(card), sticker_file_id=c.STICKERS[str(card)])
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
results.append(
|
|
|
|
Sticker(str(uuid4()), sticker_file_id=c.STICKERS_GREY[str(card)],
|
2016-05-22 03:41:38 +08:00
|
|
|
input_message_content=game_info(game))
|
2016-04-29 23:02:50 +08:00
|
|
|
)
|
|
|
|
|
2016-05-22 03:41:38 +08:00
|
|
|
|
|
|
|
def game_info(game):
|
2018-10-19 00:26:36 +08:00
|
|
|
if game.mode == 'score':
|
|
|
|
players = score_list(game)
|
|
|
|
else:
|
|
|
|
players = player_list(game)
|
2018-07-13 23:41:55 +08:00
|
|
|
players_numbered = [
|
|
|
|
'{}. {}'.format(i + 1, p)
|
|
|
|
for i, p in enumerate(players)
|
|
|
|
]
|
2016-05-22 03:41:38 +08:00
|
|
|
return InputTextMessageContent(
|
|
|
|
_("Current player: {name}")
|
|
|
|
.format(name=display_name(game.current_player.user)) +
|
|
|
|
"\n" +
|
|
|
|
_("Last card: {card}").format(card=repr(game.last_card)) +
|
|
|
|
"\n" +
|
2018-07-13 23:41:55 +08:00
|
|
|
_("Player: \n{player_list}",
|
|
|
|
"Players: \n{player_list}",
|
2016-05-24 21:49:23 +08:00
|
|
|
len(players))
|
2018-07-13 23:41:55 +08:00
|
|
|
.format(player_list="\n".join(players_numbered))
|
|
|
|
)
|
2018-10-19 00:26:36 +08:00
|
|
|
|
|
|
|
|
|
|
|
def score_list(game):
|
|
|
|
scores = [game.get_score(player) for player in game.players]
|
|
|
|
plist = [_("{name} ({number} card)",
|
|
|
|
"{name} ({number} cards)",
|
|
|
|
len(player.cards))
|
|
|
|
.format(name=player.user.first_name, number=len(player.cards))
|
|
|
|
for player in game.players]
|
|
|
|
return [s + " " +
|
|
|
|
_("[{point} point]", "[{point} points]", scores[i])
|
|
|
|
.format(point=scores[i])
|
|
|
|
for i, s in enumerate(plist)]
|