Ejemplo n.º 1
0
def task_matches():

    logging.info("Performing match maintainance...")

    matches = list(Match.query().fetch())

    for match in matches:

        # Delete all matches whose last move is older than the specified timeout
        if match.last_move_date:
            expired = (datetime.now() -
                       match.last_move_date) > timedelta(seconds=match.timeout)
        else:
            expired = False

        if expired:

            white = User.get_by_id(match.white_id)
            black = User.get_by_id(match.black_id)

            if match.is_user_turn(white):
                looser = white
                winner = black
            else:
                winner = white
                looser = black

            white.send_message(
                constants.ERROR_TIMEOUT.format(
                    looser.username,
                    humanfriendly.format_timespan(match.timeout)))
            black.send_message(
                constants.ERROR_TIMEOUT.format(
                    looser.username,
                    humanfriendly.format_timespan(match.timeout)))

            winner.end_game(GameResult.WIN)
            looser.end_game(GameResult.LOSE)

            # Delete match
            logging.info("Deleting match %s. Last activity: %s",
                         match.key.id(), str(match.last_move_date))
            match.key.delete()
Ejemplo n.º 2
0
def webhook_handler():
    if flask.request.method == "POST":

        # Retrieve the message in JSON and then transform it to Telegram object
        update = telegram.Update.de_json(flask.request.get_json(force=True),
                                         bot)

        if update.message:
            # Regular message
            text = update.message.text
            user_id = update.message.from_user.id
            chat_id = update.message.chat_id
            username = update.message.from_user.username
            message_id = None
        elif update.callback_query:
            # Callback query
            text = update.callback_query.data
            user_id = update.callback_query.from_user.id
            chat_id = update.callback_query.message.chat_id
            username = update.callback_query.from_user.username
            message_id = update.callback_query.message.message_id
        else:
            logging.error("Received unknown update!")
            return constants.RESPONSE_OK

        # User must have username
        if not username:
            bot.sendMessage(chat_id, constants.ERROR_NO_USERNAME)
            return constants.RESPONSE_OK

        # Retrieve/Create user
        user = User.get_by_id(user_id)
        if not user:
            # New user
            logging.info("User %s not found! Creating new user...", user_id)
            user = User(id=user_id, chat_id=chat_id, username=username)
            user.put()
        else:
            # Existing user
            user.last_activity_date = datetime.now()
            if username != user.username:
                logging.debug("User %s has changed username from %s to %s",
                              user_id, user.username, username)
                user.username = username
            user.put()

        commands.handle_input(user, text, message_id)

        return constants.RESPONSE_OK
Ejemplo n.º 3
0
 def get_user(self):
     user_cookie = self.read_secure_cookie()
     if user_cookie:
         user_cookie = user_cookie.split('|')
         return User.get_by_id(int(user_cookie[0]))
     return None
Ejemplo n.º 4
0
 def get_user(self):
     user_cookie = self.read_secure_cookie()
     if user_cookie:
         user_cookie = user_cookie.split('|')
         return User.get_by_id(int(user_cookie[0]))
     return None