Ejemplo n.º 1
0
    def accept_challenge(self, challenge_id):
        challenge = self.db.query(Challenge).filter_by(
            id=challenge_id, challengee=self.user).one()

        if challenge.expire_at < datetime.utcnow():
            raise ChallengeExpiredError()

        self.db.delete(challenge)

        if challenge.owner_is_black:
            black, white = challenge.owner, challenge.challengee
        else:
            black, white = challenge.challengee, challenge.owner

        game = self._create_game(
            challenge.is_ranked, challenge.is_correspondence, black, white,
            challenge.handicap, challenge.komi, challenge.board_size,
            challenge.timing_system, challenge.is_correspondence,
            challenge.maintime, challenge.overtime, challenge.overtime_count,
            challenge.is_private)

        self.db.commit()

        self._publish_game_started(game)
        self._publish_challenges(challenge)

        if game.is_correspondence:
            CorrespondenceService(self.db,
                                  self.socket).notify_challenge_started(game)
Ejemplo n.º 2
0
def test_correspondence_settings(db, socket, mails):
    game = GameFactory(is_correspondence=True,
                       black_user__correspondence_emails=False,
                       black_user__is_online=False,
                       white_user__correspondence_emails=True,
                       white_user__is_online=False)

    svc = CorrespondenceService(db, socket)
    svc.notify_automatch_started(game)

    assert len(mails) == 1
    assert mails[0]['to'] == game.white_user.email
Ejemplo n.º 3
0
    def automatch(self, preset, max_hc):
        start, end = rating_range(self.user.rating, max_hc)
        game = None

        with transaction(self.db):
            self.db.query(Automatch).filter_by(user=self.user).delete()

            query = self.db.query(Automatch).with_for_update()

            # We want to avoid matching against a user if there is already a game running between these two.
            sub = self.db.query(Game).filter(
                Game.is_demo.is_(False), Game.stage == 'playing',
                ((Game.black_user_id == self.user.id) &
                 (Game.white_user_id == Automatch.user_id)) |
                ((Game.black_user_id == Automatch.user_id) &
                 (Game.white_user_id == self.user.id)))
            query = query.filter(~sub.exists())

            query = query.filter(Automatch.min_rating <= self.user.rating,
                                 Automatch.max_rating >= self.user.rating,
                                 Automatch.user_rating >= start,
                                 Automatch.user_rating <= end,
                                 Automatch.preset == preset)
            query = query.order_by(Automatch.created_at)
            other = query.first()

            if other:
                game = self._create_automatch_game(self.user, other.user,
                                                   preset)
                self.db.delete(other)
            else:
                item = Automatch(preset=preset,
                                 user=self.user,
                                 user_rating=self.user.rating,
                                 min_rating=start,
                                 max_rating=end)
                self.db.add(item)

        if game:
            self._publish_game_started(game)
            self._publish_automatch(game.black_user, False)
            self._publish_automatch(game.white_user, False)

            if game.is_correspondence:
                CorrespondenceService(
                    self.db, self.socket).notify_automatch_started(game)
        else:
            self._publish_automatch(self.user, True)
Ejemplo n.º 4
0
    def _finish_game(self, game):
        if game.is_demo or game.stage != 'finished':
            return

        if game.board.moves_played <= game.board.size:
            game.result = 'aborted'

        if game.is_ranked:
            RatingService(self.db).update_ratings(game)

        self.socket.publish('game_finished', game.to_frontend())
        self._publish_game_data(game)

        UserService(self.db, self.socket, game.black_user).publish_status()
        UserService(self.db, self.socket, game.white_user).publish_status()

        if game.is_correspondence:
            CorrespondenceService(self.db, self.socket).notify_game_finished(game)
Ejemplo n.º 5
0
    def move(self, game_id, move):
        with self._game_for_update(game_id) as game:
            if game.is_demo:
                self._game_move_demo(game, move)
            else:
                self._game_move(game, move)

                if game.stage == 'finished':
                    self._finish_game(game)

            game.apply_board_change()

            self.db.commit()

            if game.is_demo or game.stage != 'finished':
                self._publish_game_update(game)

            if game.is_correspondence and game.stage != 'finished':
                CorrespondenceService(self.db, self.socket).notify_move_played(game, self.user)