2016-05-08 20:37:25 +08:00
|
|
|
#!/usr/bin/env python3
|
2016-05-20 05:15:46 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-05-08 20:37:25 +08:00
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
|
|
|
|
|
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
|
2018-04-11 12:18:24 +08:00
|
|
|
from user_setting import UserSetting
|
2016-05-20 02:52:50 +08:00
|
|
|
from errors import (AlreadyJoinedError, LobbyClosedError, NoGameInChatError,
|
|
|
|
NotEnoughPlayersError)
|
2016-02-29 06:57:24 +08:00
|
|
|
|
|
|
|
|
|
|
|
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-07-03 02:37:35 +08:00
|
|
|
self.remind_dict = 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
|
|
|
"""
|
2016-05-08 23:46:14 +08:00
|
|
|
Create a new game in this chat
|
2016-03-08 09:50:24 +08:00
|
|
|
"""
|
2016-04-24 08:11:37 +08:00
|
|
|
chat_id = chat.id
|
2016-02-29 06:57:24 +08:00
|
|
|
|
2016-05-20 02:52:50 +08:00
|
|
|
self.logger.debug("Creating new game in chat " + 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()
|
|
|
|
|
2016-06-02 21:51:07 +08:00
|
|
|
# remove old games
|
2016-06-04 18:43:06 +08:00
|
|
|
for g in list(self.chatid_games[chat_id]):
|
|
|
|
if not g.players:
|
|
|
|
self.chatid_games[chat_id].remove(g)
|
2016-06-02 21:51:07 +08:00
|
|
|
|
2016-04-26 23:53:29 +08:00
|
|
|
self.chatid_games[chat_id].append(game)
|
2016-05-02 00:23:59 +08:00
|
|
|
return game
|
2016-02-29 06:57:24 +08:00
|
|
|
|
2016-05-20 02:52:50 +08:00
|
|
|
def join_game(self, user, chat):
|
2016-03-08 09:50:24 +08:00
|
|
|
""" Create a player from the Telegram user and add it to the game """
|
2016-05-20 02:52:50 +08:00
|
|
|
self.logger.info("Joining game with id " + str(chat.id))
|
|
|
|
|
2016-03-11 16:23:53 +08:00
|
|
|
try:
|
2018-04-11 12:16:26 +08:00
|
|
|
us = UserSetting.get(id=user.id)
|
2018-10-14 21:52:35 +08:00
|
|
|
if not us:
|
|
|
|
us = UserSetting(id=user.id)
|
|
|
|
|
2018-04-11 12:20:31 +08:00
|
|
|
us.games_played += 1
|
2016-05-20 02:52:50 +08:00
|
|
|
game = self.chatid_games[chat.id][-1]
|
2016-04-26 23:53:29 +08:00
|
|
|
except (KeyError, IndexError):
|
2016-05-20 02:52:50 +08:00
|
|
|
raise NoGameInChatError()
|
|
|
|
|
|
|
|
if not game.open:
|
|
|
|
raise LobbyClosedError()
|
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
|
2016-05-20 02:52:50 +08:00
|
|
|
# this chat, if he is in one of them
|
2016-04-26 23:53:29 +08:00
|
|
|
for player in players:
|
|
|
|
if player in game.players:
|
2016-05-20 02:52:50 +08:00
|
|
|
raise AlreadyJoinedError()
|
2016-04-24 08:11:37 +08:00
|
|
|
|
2017-11-28 00:59:19 +08:00
|
|
|
try:
|
|
|
|
self.leave_game(user, chat)
|
|
|
|
except NoGameInChatError:
|
|
|
|
pass
|
|
|
|
except NotEnoughPlayersError:
|
|
|
|
self.end_game(chat, user)
|
|
|
|
|
|
|
|
if user.id not in self.userid_players:
|
|
|
|
self.userid_players[user.id] = list()
|
2016-05-23 19:14:42 +08:00
|
|
|
|
2017-11-28 00:59:19 +08:00
|
|
|
players = self.userid_players[user.id]
|
2016-05-23 19:14:42 +08:00
|
|
|
|
2016-04-26 23:53:29 +08:00
|
|
|
player = Player(game, user)
|
2017-11-28 00:59:19 +08:00
|
|
|
if game.started:
|
|
|
|
player.draw_first_hand()
|
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
|
2016-02-29 19:16:12 +08:00
|
|
|
|
2016-05-20 02:52:50 +08:00
|
|
|
def leave_game(self, user, chat):
|
2016-03-08 09:50:24 +08:00
|
|
|
""" Remove a player from its current game """
|
2016-03-11 16:23:53 +08:00
|
|
|
|
2016-05-20 02:52:50 +08:00
|
|
|
player = self.player_for_user_in_chat(user, chat)
|
|
|
|
players = self.userid_players.get(user.id, list())
|
|
|
|
|
|
|
|
if not player:
|
2016-06-03 13:58:05 +08:00
|
|
|
games = self.chatid_games[chat.id]
|
|
|
|
for g in games:
|
2016-06-03 14:00:13 +08:00
|
|
|
for p in g.players:
|
2016-06-03 13:58:05 +08:00
|
|
|
if p.user.id == user.id:
|
|
|
|
if p is g.current_player:
|
|
|
|
g.turn()
|
|
|
|
|
|
|
|
p.leave()
|
|
|
|
return
|
2017-11-28 00:59:19 +08:00
|
|
|
|
|
|
|
raise NoGameInChatError
|
2016-05-20 02:52:50 +08:00
|
|
|
|
|
|
|
game = player.game
|
2016-02-29 19:16:12 +08:00
|
|
|
|
2016-05-20 02:52:50 +08:00
|
|
|
if len(game.players) < 3:
|
|
|
|
raise NotEnoughPlayersError()
|
|
|
|
|
|
|
|
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.get(user.id, None) is player:
|
|
|
|
if players:
|
|
|
|
self.userid_current[user.id] = players[0]
|
|
|
|
else:
|
|
|
|
del self.userid_current[user.id]
|
|
|
|
del self.userid_players[user.id]
|
|
|
|
|
|
|
|
def end_game(self, chat, user):
|
2016-03-11 16:23:53 +08:00
|
|
|
"""
|
2016-05-08 23:46:14 +08:00
|
|
|
End a game
|
2016-03-11 16:23:53 +08:00
|
|
|
"""
|
2016-02-29 19:16:12 +08:00
|
|
|
|
2016-05-20 02:52:50 +08:00
|
|
|
self.logger.info("Game in chat " + str(chat.id) + " ended")
|
2016-04-26 23:53:29 +08:00
|
|
|
|
|
|
|
# Find the correct game instance to end
|
2016-05-20 02:52:50 +08:00
|
|
|
player = self.player_for_user_in_chat(user, chat)
|
|
|
|
|
|
|
|
if not player:
|
|
|
|
raise NoGameInChatError
|
|
|
|
|
|
|
|
game = player.game
|
2016-04-26 23:53:29 +08:00
|
|
|
|
2016-05-20 02:52:50 +08:00
|
|
|
# Clear game
|
|
|
|
for player_in_game in game.players:
|
2016-05-23 07:53:23 +08:00
|
|
|
this_users_players = \
|
|
|
|
self.userid_players.get(player_in_game.user.id, list())
|
|
|
|
|
|
|
|
try:
|
|
|
|
this_users_players.remove(player_in_game)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
2016-05-20 02:52:50 +08:00
|
|
|
|
|
|
|
if this_users_players:
|
2016-06-02 21:36:40 +08:00
|
|
|
try:
|
|
|
|
self.userid_current[player_in_game.user.id] = this_users_players[0]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2016-05-20 02:52:50 +08:00
|
|
|
else:
|
2016-06-02 21:36:40 +08:00
|
|
|
try:
|
|
|
|
del self.userid_players[player_in_game.user.id]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
|
|
|
del self.userid_current[player_in_game.user.id]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2016-05-08 23:46:14 +08:00
|
|
|
|
2016-05-20 02:52:50 +08:00
|
|
|
self.chatid_games[chat.id].remove(game)
|
2016-05-20 03:28:04 +08:00
|
|
|
if not self.chatid_games[chat.id]:
|
|
|
|
del self.chatid_games[chat.id]
|
2016-05-20 02:52:50 +08:00
|
|
|
|
|
|
|
def player_for_user_in_chat(self, user, chat):
|
|
|
|
players = self.userid_players.get(user.id, list())
|
|
|
|
for player in players:
|
|
|
|
if player.game.chat.id == chat.id:
|
|
|
|
return player
|
2017-11-28 00:59:19 +08:00
|
|
|
return None
|