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

133
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
bot.sendMessage(*args, **kwargs)
try:
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
bot.answerInlineQuery(*args, **kwargs)
try:
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,78 +253,88 @@ 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.open = False game = games[-1]
send_async(bot, chat_id, text="Closed the lobby")
return if game.owner.id == user.id:
else: game.open = False
send_async(bot, chat_id, send_async(bot, chat_id, text="Closed the lobby. "
text="Only the game creator can do that", "No more players can join this game.")
reply_to_message_id=update.message.message_id) return
return else:
send_async(bot, chat_id,
text="Only the game creator (%s) can do that"
% game.owner.first_name,
reply_to_message_id=update.message.message_id)
return
def open_game(bot, update): 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.open = True game = games[-1]
send_async(bot, chat_id, text="Opened the lobby")
return if game.owner.id == user.id:
else: game.open = True
send_async(bot, chat_id, send_async(bot, chat_id, text="Opened the lobby. "
text="Only the game creator can do that", "New players may /join the game.")
reply_to_message_id=update.message.message_id) return
return else:
send_async(bot, chat_id,
text="Only the game creator (%s) can do that"
% game.owner.first_name,
reply_to_message_id=update.message.message_id)
return
def skip_player(bot, update): 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
if delta < 120: if delta < 120:
send_async(bot, chat_id, send_async(bot, chat_id,
text="Please wait %d seconds" text="Please wait %d seconds"
% (120 - delta), % (120 - delta),
reply_to_message_id= reply_to_message_id=
update.message.message_id) update.message.message_id)
return return
game.current_player.anti_cheat += 1 game.current_player.anti_cheat += 1
game.turn() game.turn()
send_async(bot, chat_id, send_async(bot, chat_id,
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())