Example #1
0
    def get(self):
        user = self.get_current_user()
        if not user:
            raise HTTPError(403, "Forbidden")
        play_id = self.get_argument('play_id')
        play = self.must_find_play(play_id, user)
        play_points_before = self.get_play_points(user,
                                                  rules_id=play.rules._id)
        points_before = getattr(play_points_before, 'points', 0)
        highscore_position_before = getattr(play_points_before,
                                            'highscore_position', None)
        play_points = PlayPoints.calculate(user, play.rules._id)
        play_points.update_highscore_position()
        #        if user.anonymous:
        #            login_url = self.reverse_url('login')
        #            self.write_json(dict(anonymous=True,
        #                                 login_url=login_url,
        #                                 points=play_points.points))
        #            return

        increment = play_points.points - points_before
        result = dict(
            increment=increment,
            points_before=points_before,
            points=play_points.points,
            highscore_position_before=highscore_position_before,
            highscore_position=play_points.highscore_position,
        )
        if user.anonymous:
            result['login_url'] = self.reverse_url('login')
        if not play.rules.default:
            result['highscore_url'] = self.reverse_url('play_highscore_rules',
                                                       play.rules._id)
        else:
            result['highscore_url'] = self.reverse_url('play_highscore')


#        from pprint import pprint
#        print "USER", user['username']
#        pprint(result)
        self.write_json(result)
Example #2
0
    def get(self):
        user = self.get_current_user()
        if not user:
            raise HTTPError(403, "Forbidden")
        play_id = self.get_argument('play_id')
        play = self.must_find_play(play_id, user)
        play_points_before = self.get_play_points(user, rules_id=play.rules._id)
        points_before = getattr(play_points_before, 'points', 0)
        highscore_position_before = getattr(play_points_before,
                                            'highscore_position', None)
        play_points = PlayPoints.calculate(user, play.rules._id)
        play_points.update_highscore_position()
#        if user.anonymous:
#            login_url = self.reverse_url('login')
#            self.write_json(dict(anonymous=True,
#                                 login_url=login_url,
#                                 points=play_points.points))
#            return

        increment = play_points.points - points_before
        result = dict(
          increment=increment,
          points_before=points_before,
          points=play_points.points,
          highscore_position_before=highscore_position_before,
          highscore_position=play_points.highscore_position,
        )
        if user.anonymous:
            result['login_url'] = self.reverse_url('login')
        if not play.rules.default:
            result['highscore_url'] = self.reverse_url('play_highscore_rules',
                                                       play.rules._id)
        else:
            result['highscore_url'] = self.reverse_url('play_highscore')

#        from pprint import pprint
#        print "USER", user['username']
#        pprint(result)
        self.write_json(result)
Example #3
0
    def test_anonymous_to_existing_logged_in_after_play(self):
        peterbe = self.db.User()
        peterbe.username = u'peterbe'
        peterbe.save()

        play_points = self.db.PlayPoints()
        play_points.user = peterbe
        play_points.rules = self.default_rules_id
        play_points.points = 10
        play_points.wins = 1
        play_points.draws = 1
        play_points.losses = 1
        play_points.highscore_position = 2
        play_points.save()

        url = self.reverse_url('start_play')
        response = self.client.get(url)
        self.assertEqual(response.code, 302)
        user = self.db.User.one({'anonymous': True})
        self.assertTrue(user.anonymous)

        bob = self.db.User()
        bob.username = u'bob'
        bob.save()

        play = self.db.Play()
        play.users = [user, bob]
        play.rules = self.default_rules_id
        play.no_players = 2
        play.no_questions = 2
        play.started = datetime.datetime.now()
        play.save()

        q1 = self._create_question()
        q2 = self._create_question()

        # 1 point
        pq1a = self.db.PlayedQuestion()
        pq1a.play = play
        pq1a.question = q1
        pq1a.user = user
        pq1a.answer = u'yes'
        pq1a.alternatives = True
        pq1a.right = True
        pq1a.save()

        # 0 points
        pq1b = self.db.PlayedQuestion()
        pq1b.play = play
        pq1b.question = q1
        pq1b.user = bob
        pq1b.answer = u'wrong'
        pq1b.alternatives = True
        pq1b.right = False
        pq1b.save()

        # 0 points
        pq2a = self.db.PlayedQuestion()
        pq2a.play = play
        pq2a.question = q2
        pq2a.user = user
        pq2a.answer = u'wrong'
        pq2a.right = False
        pq2a.save()

        # 3 points
        pq2b = self.db.PlayedQuestion()
        pq2b.play = play
        pq2b.question = q2
        pq2b.user = bob
        pq2b.answer = u'yes'
        pq2b.right = True
        pq2b.save()

        play.winner = bob
        play.finished = datetime.datetime.now()
        play.save()

        from apps.play.models import PlayPoints

        play_points_user = PlayPoints.calculate(user, self.default_rules_id)
        self.assertEqual(play_points_user.draws, 0)
        self.assertEqual(play_points_user.wins, 0)
        self.assertEqual(play_points_user.losses, 1)
        self.assertEqual(play_points_user.points, 1)

        play_points_bob = PlayPoints.calculate(bob, self.default_rules_id)
        self.assertEqual(play_points_bob.draws, 0)
        self.assertEqual(play_points_bob.wins, 1)
        self.assertEqual(play_points_bob.losses, 0)
        self.assertEqual(play_points_bob.points, 3)

        # lastly, both people send each other a message
        msg1 = self.db.PlayMessage()
        msg1.play = play
        msg1['from'] = user
        msg1.to = bob
        msg1.message = u'Well done'
        msg1.save()

        msg2 = self.db.PlayMessage()
        msg2.play = play
        msg2['from'] = bob
        msg2.to = user
        msg2.message = u'Thanks'
        msg2.save()

        url = self.reverse_url('login')
        response = self.client.get(url)
        self.assertEqual(response.code, 200)
        self.assertTrue('1 Kwissle point' in response.body)

        self.assertEqual(self.db.PlayPoints.find().count(), 3)

        from apps.main.handlers import TwitterAuthHandler
        TwitterAuthHandler.get_authenticated_user = \
          twitter_get_authenticated_user
        TwitterAuthHandler.authenticate_redirect = \
          twitter_authenticate_redirect
        url = self.reverse_url('auth_twitter')
        response = self.client.get(url)
        self.assertEqual(response.code, 302)
        self.assertTrue('twitter.com' in response.headers['location'])

        response = self.client.get(url, {'oauth_token':'xxx'})
        self.assertEqual(response.code, 302)

        peterbe = self.db.User.one({'username': '******'})
        self.assertTrue(peterbe)

        self.assertEqual(self.db.User.find().count(), 2)
        self.assertEqual(self.db.PlayedQuestion.find({'user.$id': peterbe._id}).count(), 2)
        self.assertEqual(self.db.PlayPoints.find({'user.$id': peterbe._id}).count(), 1)
        self.assertEqual(self.db.Play.find({'users.$id': peterbe._id}).count(), 1)
        self.assertEqual(self.db.PlayMessage.find({'from.$id': peterbe._id}).count(), 1)
        self.assertEqual(self.db.PlayMessage.find({'to.$id': peterbe._id}).count(), 1)

        play_points = self.db.PlayPoints.one({'user.$id': peterbe._id})
        self.assertEqual(play_points.points, 11)
        self.assertEqual(play_points.wins, 1)
        self.assertEqual(play_points.draws, 1)
        self.assertEqual(play_points.losses, 1 + 1)
        self.assertEqual(play_points.highscore_position, 1)

        self.assertEqual(self.db.PlayPoints.find().count(), 2)
def main(*args):
    tornado.options.parse_command_line()
    from apps.main.models import User, connection
    from apps.play.models import PlayPoints
    import apps.rules.models
    db = connection.gkc
    if options.all:
        max_users = 999999
    else:
        max_users = options.users

    recent_users = []
    if options.random:
        while len(recent_users) < max_users:
            play = db.Play.find_random()
            if play.finished:
                for user in play.users:
                    if user.anonymous:
                        continue
                    if user not in recent_users:
                        recent_users.append(user)

    else:
        finished = (db.Play.find({'finished': {'$ne':None}})
                    .sort('finished', -1))
        _broken = False
        for play in finished:
            if _broken:
                break
            for user in play.users:
                if user.anonymous:
                    continue
                if user not in recent_users:
                    recent_users.append(user)
                if len(recent_users) >= max_users:
                    _broken = True
                    break
    computer = (db.User.collection
          .one({'username': settings.COMPUTER_USERNAME}))

    default_rules_id = db.Rules.one({'default': True})._id

    _rule_names = {}
    def get_rule_name(rule_id):
        if rule_id not in _rule_names:
            r = db.Rules.collection.one({'_id': rule_id})
            _rule_names[rule_id] = r['name']
        return _rule_names[rule_id]

    for user in recent_users:
        if computer and user._id == computer['_id']:
            continue
        played_rules_ids = [default_rules_id]
        for p in db.Play.collection.find({'users.$id': user['_id'],
                                               'rules': {'$ne': default_rules_id}}):
            p_rules_id = p['rules']  # nb. not using the 'rules' proper in Play
            if p_rules_id not in played_rules_ids:
                played_rules_ids.append(p_rules_id)

        assert len(played_rules_ids) == len(set(played_rules_ids))
        for rule_id in played_rules_ids:
            play_points = PlayPoints.calculate(user, rule_id)
            if options.verbose and not options.all:
                print user.username.ljust(20),
                print get_rule_name(rule_id).ljust(50),
                print play_points.points

    if options.verbose:
        print "Recalculated the points of", len(recent_users), "players"
Example #5
0
def main(*args):
    tornado.options.parse_command_line()
    from apps.main.models import User, connection
    from apps.play.models import PlayPoints
    import apps.rules.models
    db = connection.gkc
    if options.all:
        max_users = 999999
    else:
        max_users = options.users

    recent_users = []
    if options.random:
        while len(recent_users) < max_users:
            play = db.Play.find_random()
            if play.finished:
                for user in play.users:
                    if user.anonymous:
                        continue
                    if user not in recent_users:
                        recent_users.append(user)

    else:
        finished = (db.Play.find({
            'finished': {
                '$ne': None
            }
        }).sort('finished', -1))
        _broken = False
        for play in finished:
            if _broken:
                break
            for user in play.users:
                if user.anonymous:
                    continue
                if user not in recent_users:
                    recent_users.append(user)
                if len(recent_users) >= max_users:
                    _broken = True
                    break
    computer = (db.User.collection.one(
        {'username': settings.COMPUTER_USERNAME}))

    default_rules_id = db.Rules.one({'default': True})._id

    _rule_names = {}

    def get_rule_name(rule_id):
        if rule_id not in _rule_names:
            r = db.Rules.collection.one({'_id': rule_id})
            _rule_names[rule_id] = r['name']
        return _rule_names[rule_id]

    for user in recent_users:
        if computer and user._id == computer['_id']:
            continue
        played_rules_ids = [default_rules_id]
        for p in db.Play.collection.find({
                'users.$id': user['_id'],
                'rules': {
                    '$ne': default_rules_id
                }
        }):
            p_rules_id = p['rules']  # nb. not using the 'rules' proper in Play
            if p_rules_id not in played_rules_ids:
                played_rules_ids.append(p_rules_id)

        assert len(played_rules_ids) == len(set(played_rules_ids))
        for rule_id in played_rules_ids:
            play_points = PlayPoints.calculate(user, rule_id)
            if options.verbose and not options.all:
                print user.username.ljust(20),
                print get_rule_name(rule_id).ljust(50),
                print play_points.points

    if options.verbose:
        print "Recalculated the points of", len(recent_users), "players"