2016-02-29 08:53:59 +08:00
|
|
|
import logging
|
|
|
|
|
2016-02-29 06:57:24 +08:00
|
|
|
from game import Game
|
|
|
|
from player import Player
|
|
|
|
|
|
|
|
|
|
|
|
class GameManager(object):
|
2016-03-08 09:50:24 +08:00
|
|
|
""" Manages all running games by using a confusing amount of dicts """
|
2016-02-29 06:57:24 +08:00
|
|
|
|
|
|
|
def __init__(self):
|
2016-04-26 23:53:29 +08:00
|
|
|
self.chatid_games = dict()
|
2016-04-24 08:11:37 +08:00
|
|
|
self.userid_players = dict()
|
|
|
|
self.userid_current = dict()
|
2016-02-29 08:53:59 +08:00
|
|
|
self.logger = logging.getLogger(__name__)
|
2016-02-29 06:57:24 +08:00
|
|
|
|
2016-04-24 08:11:37 +08:00
|
|
|
def new_game(self, chat):
|
2016-03-08 09:50:24 +08:00
|
|
|
"""
|
|
|
|
Generate a game join link with a unique ID and connect the game to the
|
|
|
|
group chat
|
|
|
|
"""
|
2016-04-24 08:11:37 +08:00
|
|
|
chat_id = chat.id
|
2016-02-29 06:57:24 +08:00
|
|
|
|
2016-03-11 16:23:53 +08:00
|
|
|
self.logger.info("Creating new game with id " + str(chat_id))
|
2016-04-24 08:11:37 +08:00
|
|
|
game = Game(chat)
|
2016-04-26 23:53:29 +08:00
|
|
|
|
|
|
|
if chat_id not in self.chatid_games:
|
|
|
|
self.chatid_games[chat_id] = list()
|
|
|
|
|
|
|
|
self.chatid_games[chat_id].append(game)
|
2016-02-29 06:57:24 +08:00
|
|
|
|
2016-03-11 16:23:53 +08:00
|
|
|
def join_game(self, chat_id, user):
|
2016-03-08 09:50:24 +08:00
|
|
|
""" Create a player from the Telegram user and add it to the game """
|
2016-03-11 16:23:53 +08:00
|
|
|
self.logger.info("Joining game with id " + str(chat_id))
|
|
|
|
try:
|
2016-04-26 23:53:29 +08:00
|
|
|
game = self.chatid_games[chat_id][-1]
|
|
|
|
except (KeyError, IndexError):
|
2016-03-11 16:23:53 +08:00
|
|
|
return None
|
2016-04-19 06:42:23 +08:00
|
|
|
|
2016-04-26 23:53:29 +08:00
|
|
|
if user.id not in self.userid_players:
|
|
|
|
self.userid_players[user.id] = list()
|
|
|
|
|
|
|
|
players = self.userid_players[user.id]
|
2016-04-24 08:11:37 +08:00
|
|
|
|
2016-04-26 23:53:29 +08:00
|
|
|
# Don not re-add a player and remove the player from previous games in
|
|
|
|
# this chat
|
|
|
|
for player in players:
|
|
|
|
if player in game.players:
|
|
|
|
return False
|
2016-04-24 08:11:37 +08:00
|
|
|
else:
|
|
|
|
self.leave_game(user, chat_id)
|
|
|
|
|
2016-04-26 23:53:29 +08:00
|
|
|
player = Player(game, user)
|
2016-04-19 06:42:23 +08:00
|
|
|
|
2016-04-26 23:53:29 +08:00
|
|
|
players.append(player)
|
|
|
|
self.userid_current[user.id] = player
|
|
|
|
return True
|
2016-02-29 19:16:12 +08:00
|
|
|
|
2016-04-24 08:11:37 +08:00
|
|
|
def leave_game(self, user, chat_id):
|
2016-03-08 09:50:24 +08:00
|
|
|
""" Remove a player from its current game """
|
2016-03-11 16:23:53 +08:00
|
|
|
try:
|
2016-04-24 08:11:37 +08:00
|
|
|
players = self.userid_players[user.id]
|
2016-04-26 23:53:29 +08:00
|
|
|
games = self.chatid_games[chat_id]
|
|
|
|
|
|
|
|
for player in players:
|
|
|
|
for game in games:
|
|
|
|
if player in game.players:
|
|
|
|
if player is game.current_player:
|
|
|
|
game.turn()
|
|
|
|
|
|
|
|
player.leave()
|
|
|
|
players.remove(player)
|
|
|
|
|
|
|
|
# If this is the selected game, switch to another
|
|
|
|
if self.userid_current[user.id] is player:
|
|
|
|
if len(players):
|
|
|
|
self.userid_current[user.id] = players[0]
|
|
|
|
else:
|
|
|
|
del self.userid_current[user.id]
|
|
|
|
return True
|
2016-04-24 08:11:37 +08:00
|
|
|
else:
|
|
|
|
return False
|
2016-03-11 16:23:53 +08:00
|
|
|
|
|
|
|
except KeyError:
|
|
|
|
return False
|
2016-02-29 19:16:12 +08:00
|
|
|
|
2016-04-24 08:11:37 +08:00
|
|
|
def end_game(self, chat_id, user):
|
2016-03-11 16:23:53 +08:00
|
|
|
"""
|
|
|
|
Generate a game join link with a unique ID and connect the game to the
|
|
|
|
group chat
|
|
|
|
"""
|
2016-02-29 19:16:12 +08:00
|
|
|
|
2016-04-26 23:53:29 +08:00
|
|
|
self.logger.info("Game in chat " + str(chat_id) + " ended")
|
2016-04-24 08:11:37 +08:00
|
|
|
players = self.userid_players[user.id]
|
2016-04-26 23:53:29 +08:00
|
|
|
games = self.chatid_games[chat_id]
|
|
|
|
the_game = None
|
|
|
|
|
|
|
|
# Find the correct game instance to end
|
|
|
|
logging.info(str(players))
|
|
|
|
logging.info(str(games))
|
2016-04-24 08:11:37 +08:00
|
|
|
for player in players:
|
2016-04-26 23:53:29 +08:00
|
|
|
for game in games:
|
|
|
|
if player in game.players:
|
|
|
|
the_game = game
|
|
|
|
break
|
|
|
|
if the_game:
|
2016-04-24 08:11:37 +08:00
|
|
|
break
|
2016-04-26 23:53:29 +08:00
|
|
|
else:
|
|
|
|
return
|
|
|
|
|
|
|
|
for player in the_game.players:
|
|
|
|
self.userid_players[player.user.id].remove(player)
|
|
|
|
if len(self.userid_players[player.user.id]) is 0:
|
|
|
|
del self.userid_players[player.user.id]
|
|
|
|
self.chatid_games[chat_id].remove(the_game)
|
|
|
|
return
|