use actual game creator, make skip available to every player
This commit is contained in:
parent
76b869fce7
commit
0cf7b70454
3 changed files with 77 additions and 58 deletions
73
bot.py
73
bot.py
|
@ -18,7 +18,7 @@ from utils import *
|
|||
|
||||
logging.basicConfig(
|
||||
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
||||
level=logging.INFO)
|
||||
level=logging.DEBUG)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
gm = GameManager()
|
||||
|
@ -61,14 +61,22 @@ help_text = "Follow these steps:\n\n" \
|
|||
def send_async(bot, *args, **kwargs):
|
||||
if 'timeout' not in kwargs:
|
||||
kwargs['timeout'] = 2.5
|
||||
|
||||
try:
|
||||
bot.sendMessage(*args, **kwargs)
|
||||
except Exception as e:
|
||||
error(None, None, e)
|
||||
|
||||
|
||||
@run_async
|
||||
def answer_async(bot, *args, **kwargs):
|
||||
if 'timeout' not in kwargs:
|
||||
kwargs['timeout'] = 2.5
|
||||
|
||||
try:
|
||||
bot.answerInlineQuery(*args, **kwargs)
|
||||
except Exception as e:
|
||||
error(None, None, e)
|
||||
|
||||
|
||||
def error(bot, update, error):
|
||||
|
@ -82,7 +90,8 @@ def new_game(bot, update):
|
|||
if update.message.chat.type == 'private':
|
||||
help(bot, update)
|
||||
else:
|
||||
gm.new_game(update.message.chat)
|
||||
game = gm.new_game(update.message.chat)
|
||||
game.owner = update.message.from_user
|
||||
send_async(bot, chat_id,
|
||||
text="Created a new game! Join the game with /join "
|
||||
"and start the game with /start")
|
||||
|
@ -175,7 +184,7 @@ def select_game(bot, update):
|
|||
show_alert=False)
|
||||
bot.editMessageText(chat_id=update.callback_query.message.chat_id,
|
||||
message_id=update.callback_query.message.message_id,
|
||||
text="Selected game: %s\n"
|
||||
text="Selected group: %s\n"
|
||||
"<b>Make sure that you switch to the correct "
|
||||
"group!</b>"
|
||||
% gm.userid_current[user_id].game.chat.title,
|
||||
|
@ -244,19 +253,23 @@ def close_game(bot, update):
|
|||
""" Handler for the /close command """
|
||||
chat_id = update.message.chat_id
|
||||
user = update.message.from_user
|
||||
games = gm.chatid_games[chat_id]
|
||||
players = gm.userid_players[user.id]
|
||||
games = gm.chatid_games.get(chat_id)
|
||||
|
||||
for game in games:
|
||||
for player in players:
|
||||
if player in game.players:
|
||||
if player is game.owner:
|
||||
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")
|
||||
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 can do that",
|
||||
text="Only the game creator (%s) can do that"
|
||||
% game.owner.first_name,
|
||||
reply_to_message_id=update.message.message_id)
|
||||
return
|
||||
|
||||
|
@ -265,19 +278,23 @@ def open_game(bot, update):
|
|||
""" Handler for the /open command """
|
||||
chat_id = update.message.chat_id
|
||||
user = update.message.from_user
|
||||
games = gm.chatid_games[chat_id]
|
||||
players = gm.userid_players[user.id]
|
||||
games = gm.chatid_games.get(chat_id)
|
||||
|
||||
for game in games:
|
||||
for player in players:
|
||||
if player in game.players:
|
||||
if player is game.owner:
|
||||
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")
|
||||
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 can do that",
|
||||
text="Only the game creator (%s) can do that"
|
||||
% game.owner.first_name,
|
||||
reply_to_message_id=update.message.message_id)
|
||||
return
|
||||
|
||||
|
@ -286,13 +303,20 @@ def skip_player(bot, update):
|
|||
""" Handler for the /skip command """
|
||||
chat_id = update.message.chat_id
|
||||
user = update.message.from_user
|
||||
games = gm.chatid_games[chat_id]
|
||||
players = gm.userid_players[user.id]
|
||||
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
|
||||
|
||||
for game in games:
|
||||
for player in players:
|
||||
if player in game.players:
|
||||
if player is game.owner:
|
||||
started = game.current_player.turn_started
|
||||
now = datetime.now()
|
||||
delta = (now - started).seconds
|
||||
|
@ -311,11 +335,6 @@ def skip_player(bot, update):
|
|||
text="Next player: %s"
|
||||
% display_name(game.current_player.user))
|
||||
return
|
||||
else:
|
||||
send_async(bot, chat_id,
|
||||
text="Only the game creator can do that",
|
||||
reply_to_message_id=update.message.message_id)
|
||||
return
|
||||
|
||||
|
||||
def help(bot, update):
|
||||
|
|
|
@ -27,6 +27,7 @@ class GameManager(object):
|
|||
self.chatid_games[chat_id] = list()
|
||||
|
||||
self.chatid_games[chat_id].append(game)
|
||||
return game
|
||||
|
||||
def join_game(self, chat_id, user):
|
||||
""" Create a player from the Telegram user and add it to the game """
|
||||
|
|
|
@ -28,7 +28,6 @@ class Player(object):
|
|||
self._next = self
|
||||
self._prev = self
|
||||
game.current_player = self
|
||||
game.owner = self
|
||||
|
||||
for i in range(7):
|
||||
self.cards.append(self.game.deck.draw())
|
||||
|
|
Loading…
Reference in a new issue