174 lines
5.3 KiB
Python
174 lines
5.3 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/>.
|
||
|
||
|
||
"""Defines helper functions to build the inline result list"""
|
||
|
||
from uuid import uuid4
|
||
|
||
from telegram import InlineQueryResultArticle, InputTextMessageContent, \
|
||
InlineQueryResultCachedSticker as Sticker
|
||
|
||
import card as c
|
||
from utils import display_color, display_color_group, display_name
|
||
from internationalization import _, __
|
||
|
||
|
||
def add_choose_color(results, game):
|
||
"""Add choose color options"""
|
||
for color in c.COLORS:
|
||
results.append(
|
||
InlineQueryResultArticle(
|
||
id=color,
|
||
title=_("選擇顏色"),
|
||
description=display_color(color),
|
||
input_message_content=
|
||
InputTextMessageContent(display_color_group(color, game))
|
||
)
|
||
)
|
||
|
||
|
||
def add_other_cards(player, results, game):
|
||
"""Add hand cards when choosing colors"""
|
||
|
||
results.append(
|
||
InlineQueryResultArticle(
|
||
"hand",
|
||
title=_("你的手牌(點一下以獲得遊戲狀態):",
|
||
"你的手牌(點一下以獲得遊戲狀態):",
|
||
len(player.cards)),
|
||
description=', '.join([repr(card) for card in player.cards]),
|
||
input_message_content=game_info(game)
|
||
)
|
||
)
|
||
|
||
|
||
def player_list(game):
|
||
"""Generate list of player strings"""
|
||
return [_("{name}(有 {number} 張牌)",
|
||
"{name}(有 {number} 張牌)",
|
||
len(player.cards))
|
||
.format(name=player.user.first_name, number=len(player.cards))
|
||
for player in game.players]
|
||
|
||
|
||
def add_no_game(results):
|
||
"""Add text result if user is not playing"""
|
||
results.append(
|
||
InlineQueryResultArticle(
|
||
"nogame",
|
||
title=_("你沒有在遊戲中"),
|
||
input_message_content=
|
||
InputTextMessageContent(_('你目前沒有在遊玩,使用 /new@unobot 開始新的遊戲或使用 /join@unobot 加入目前的遊戲'))
|
||
)
|
||
)
|
||
|
||
|
||
def add_not_started(results):
|
||
"""Add text result if the game has not yet started"""
|
||
results.append(
|
||
InlineQueryResultArticle(
|
||
"nogame",
|
||
title=_("遊戲尚未開始"),
|
||
input_message_content=
|
||
InputTextMessageContent(_('使用 /start 開始遊戲'))
|
||
)
|
||
)
|
||
|
||
|
||
def add_draw(player, results):
|
||
"""Add option to draw"""
|
||
n = player.game.draw_counter or 1
|
||
|
||
results.append(
|
||
Sticker(
|
||
"draw", sticker_file_id=c.STICKERS['option_draw'],
|
||
input_message_content=
|
||
InputTextMessageContent(__('抽 {number} 張牌',
|
||
'抽 {number} 張牌', n,
|
||
multi=player.game.translate)
|
||
.format(number=n))
|
||
)
|
||
)
|
||
|
||
|
||
def add_gameinfo(game, results):
|
||
"""Add option to show game info"""
|
||
|
||
results.append(
|
||
Sticker(
|
||
"gameinfo",
|
||
sticker_file_id=c.STICKERS['option_info'],
|
||
input_message_content=game_info(game)
|
||
)
|
||
)
|
||
|
||
|
||
def add_pass(results, game):
|
||
"""Add option to pass"""
|
||
results.append(
|
||
Sticker(
|
||
"pass", sticker_file_id=c.STICKERS['option_pass'],
|
||
input_message_content=InputTextMessageContent(
|
||
__('Pass!下一位', multi=game.translate)
|
||
)
|
||
)
|
||
)
|
||
|
||
|
||
def add_call_bluff(results, game):
|
||
"""Add option to call a bluff"""
|
||
results.append(
|
||
Sticker(
|
||
"call_bluff",
|
||
sticker_file_id=c.STICKERS['option_bluff'],
|
||
input_message_content=
|
||
InputTextMessageContent(__("你作弊,一定還有其他牌可以出!",
|
||
multi=game.translate))
|
||
)
|
||
)
|
||
|
||
|
||
def add_card(game, card, results, can_play):
|
||
"""Add an option that represents a card"""
|
||
|
||
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)],
|
||
input_message_content=game_info(game))
|
||
)
|
||
|
||
|
||
def game_info(game):
|
||
players = player_list(game)
|
||
return InputTextMessageContent(
|
||
_("目前玩家:{name}")
|
||
.format(name=display_name(game.current_player.user)) +
|
||
"\n" +
|
||
_("最後的牌:{card}").format(card=repr(game.last_card)) +
|
||
"\n" +
|
||
_("玩家列表:{player_list}",
|
||
"玩家列表:{player_list}",
|
||
len(players))
|
||
.format(player_list=" -> ".join(players))
|
||
) |