use actual game creator, make skip available to every player

This commit is contained in:
Jannes Höke 2016-05-01 18:23:59 +02:00
parent 76b869fce7
commit 0cf7b70454
3 changed files with 77 additions and 58 deletions

73
bot.py
View file

@ -18,7 +18,7 @@ from utils import *
logging.basicConfig( logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO) level=logging.DEBUG)
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
gm = GameManager() gm = GameManager()
@ -61,14 +61,22 @@ help_text = "Follow these steps:\n\n" \
def send_async(bot, *args, **kwargs): def send_async(bot, *args, **kwargs):
if 'timeout' not in kwargs: if 'timeout' not in kwargs:
kwargs['timeout'] = 2.5 kwargs['timeout'] = 2.5
try:
bot.sendMessage(*args, **kwargs) bot.sendMessage(*args, **kwargs)
except Exception as e:
error(None, None, e)
@run_async @run_async
def answer_async(bot, *args, **kwargs): def answer_async(bot, *args, **kwargs):
if 'timeout' not in kwargs: if 'timeout' not in kwargs:
kwargs['timeout'] = 2.5 kwargs['timeout'] = 2.5
try:
bot.answerInlineQuery(*args, **kwargs) bot.answerInlineQuery(*args, **kwargs)
except Exception as e:
error(None, None, e)
def error(bot, update, error): def error(bot, update, error):
@ -82,7 +90,8 @@ def new_game(bot, update):
if update.message.chat.type == 'private': if update.message.chat.type == 'private':
help(bot, update) help(bot, update)
else: 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, send_async(bot, chat_id,
text="Created a new game! Join the game with /join " text="Created a new game! Join the game with /join "
"and start the game with /start") "and start the game with /start")
@ -175,7 +184,7 @@ def select_game(bot, update):
show_alert=False) show_alert=False)
bot.editMessageText(chat_id=update.callback_query.message.chat_id, bot.editMessageText(chat_id=update.callback_query.message.chat_id,
message_id=update.callback_query.message.message_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 " "<b>Make sure that you switch to the correct "
"group!</b>" "group!</b>"
% gm.userid_current[user_id].game.chat.title, % gm.userid_current[user_id].game.chat.title,
@ -244,19 +253,23 @@ def close_game(bot, update):
""" Handler for the /close command """ """ Handler for the /close command """
chat_id = update.message.chat_id chat_id = update.message.chat_id
user = update.message.from_user user = update.message.from_user
games = gm.chatid_games[chat_id] games = gm.chatid_games.get(chat_id)
players = gm.userid_players[user.id]
for game in games: if not games:
for player in players: send_async(bot, chat_id, text="There is no running game")
if player in game.players: return
if player is game.owner:
game = games[-1]
if game.owner.id == user.id:
game.open = False 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 return
else: else:
send_async(bot, chat_id, 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) reply_to_message_id=update.message.message_id)
return return
@ -265,19 +278,23 @@ def open_game(bot, update):
""" Handler for the /open command """ """ Handler for the /open command """
chat_id = update.message.chat_id chat_id = update.message.chat_id
user = update.message.from_user user = update.message.from_user
games = gm.chatid_games[chat_id] games = gm.chatid_games.get(chat_id)
players = gm.userid_players[user.id]
for game in games: if not games:
for player in players: send_async(bot, chat_id, text="There is no running game")
if player in game.players: return
if player is game.owner:
game = games[-1]
if game.owner.id == user.id:
game.open = True 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 return
else: else:
send_async(bot, chat_id, 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) reply_to_message_id=update.message.message_id)
return return
@ -286,13 +303,20 @@ def skip_player(bot, update):
""" Handler for the /skip command """ """ Handler for the /skip command """
chat_id = update.message.chat_id chat_id = update.message.chat_id
user = update.message.from_user user = update.message.from_user
games = gm.chatid_games[chat_id] games = gm.chatid_games.get(chat_id)
players = gm.userid_players[user.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 game in games:
for player in players: for player in players:
if player in game.players: if player in game.players:
if player is game.owner:
started = game.current_player.turn_started started = game.current_player.turn_started
now = datetime.now() now = datetime.now()
delta = (now - started).seconds delta = (now - started).seconds
@ -311,11 +335,6 @@ def skip_player(bot, update):
text="Next player: %s" text="Next player: %s"
% display_name(game.current_player.user)) % display_name(game.current_player.user))
return 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): def help(bot, update):

View file

@ -27,6 +27,7 @@ class GameManager(object):
self.chatid_games[chat_id] = list() self.chatid_games[chat_id] = list()
self.chatid_games[chat_id].append(game) self.chatid_games[chat_id].append(game)
return game
def join_game(self, chat_id, user): def join_game(self, chat_id, user):
""" Create a player from the Telegram user and add it to the game """ """ Create a player from the Telegram user and add it to the game """

View file

@ -28,7 +28,6 @@ class Player(object):
self._next = self self._next = self
self._prev = self self._prev = self
game.current_player = self game.current_player = self
game.owner = self
for i in range(7): for i in range(7):
self.cards.append(self.game.deck.draw()) self.cards.append(self.game.deck.draw())