Skip to content

Commit

Permalink
Merge pull request #7 from adamjmcgrath/feature/1-average-score-front…
Browse files Browse the repository at this point in the history
…-end

Feature/1 average score front end
  • Loading branch information
adamjmcgrath committed Jun 14, 2015
2 parents 03a9a26 + 11faf8e commit f3f46bb
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 12 deletions.
24 changes: 20 additions & 4 deletions scripts/fix_average_score.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,16 @@
def auth_func():
return os.environ['USER_EMAIL'], getpass.getpass('Password:')


def fix_user(u):
uqs = [uq for uq in models.UserQuestion.query(
def get_uqs(u):
return [uq for uq in models.UserQuestion.query(
models.UserQuestion.user==u.key,
models.UserQuestion.complete==True,
ndb.OR(models.UserQuestion.score==0, models.UserQuestion.score==None))]

def fix_user(u):
uqs = get_uqs(u)
lus = models.LeagueUser.query(models.LeagueUser.user==u.key)

correction = 0

for uq in uqs:
Expand All @@ -47,9 +50,22 @@ def fix_user(u):

u.active_questions_answered = u.questions_answered - correction
print '%s: old: %d, new: %d' % (u.username, u.questions_answered, u.active_questions_answered)
if correction > 1:
if u.active_questions_answered > 0:
u.put()

for lu in lus:
l_correction = 0
league = lu.league.get()
for uq in uqs:
if (uq.created and
uq.created.date() > league.created and
(uq.created - uq.question.get().posed) > datetime.timedelta(days=7)):
l_correction += 1
lu.active_questions_answered = lu.questions_answered - l_correction
print ' league - %s: old: %d, new: %d' % (u.username, lu.questions_answered, lu.active_questions_answered)
if lu.active_questions_answered > 0:
lu.put()


def main():
# Use 'localhost:8080' for dev server.
Expand Down
7 changes: 5 additions & 2 deletions src/api/question.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,16 @@ def update_users_season_score(user_season, score, num_guesses):
user_season.questions_answered += 1


def update_users_league_scores(user_key, score, num_guesses):
def update_users_league_scores(user_key, score, num_guesses, is_current):
to_put = []
user = user_key.get()
for league_key in user.leagues:
user_league = models.LeagueUser.from_league_user(league_key, user_key)
user_league.score += score
user_league.clues += num_guesses - 1
user_league.questions_answered += 1
if is_current:
user_league.active_questions_answered += 1
to_put.append(user_league)
ndb.put_multi(to_put)

Expand Down Expand Up @@ -130,7 +132,8 @@ def get_or_post(self, question_id):
deferred.defer(update_users_league_scores,
user.key,
score,
num_guesses)
num_guesses,
question.is_current)

# Update the user season.
if question.season and not user.is_anonymous:
Expand Down
7 changes: 4 additions & 3 deletions src/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ def to_leaderboard_json(user):
'user_pic': user.pic_url(size=30),
'score': user.overall_score,
'clues': user.overall_clues,
'answered': user.questions_answered,
'answered': user.active_questions_answered
}


Expand Down Expand Up @@ -380,7 +380,7 @@ def to_leaderboard_json(user_season):
'user_pic': user.pic_url(size=30),
'score': user_season.score,
'clues': user_season.clues,
'answered': user_season.questions_answered,
'answered': user_season.questions_answered
}


Expand Down Expand Up @@ -456,6 +456,7 @@ class LeagueUser(ndb.Model):
score = ndb.IntegerProperty(default=0)
clues = ndb.IntegerProperty(default=0)
questions_answered = ndb.IntegerProperty(default=0)
active_questions_answered = ndb.IntegerProperty(default=0)

@classmethod
def from_league_user(cls, league_key, user_key):
Expand All @@ -479,5 +480,5 @@ def to_leaderboard_json(league_user):
'user_pic': user.pic_url(size=30),
'score': league_user.score,
'clues': league_user.clues,
'answered': league_user.questions_answered,
'answered': league_user.active_questions_answered
}
1 change: 1 addition & 0 deletions src/tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ def league_user(id=None,
league=None,
score=None,
clues=None,
active_questions_answered=None,
questions_answered=None):
return models.LeagueUser(**locals())

Expand Down
6 changes: 4 additions & 2 deletions src/tests/test_api_leaderboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ def setUp(self):
username='user-%d' % i,
overall_score=i,
overall_clues=i,
questions_answered=i))
questions_answered=i,
active_questions_answered=i))
to_put.append(helpers.user_question(
user_is_admin=False,
complete=True,
Expand All @@ -55,7 +56,8 @@ def setUp(self):
user=user_key,
score=i,
clues=i,
questions_answered=i))
questions_answered=i,
active_questions_answered=i))
to_put.append(helpers.user_season(
score=i,
clues=i,
Expand Down
2 changes: 1 addition & 1 deletion src/tests/test_api_question.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ def testUpdateUsersLeagueScore(self):
clues=5,
questions_answered=1)
league_user.put()
question_api.update_users_league_scores(user_key, 5, 5)
question_api.update_users_league_scores(user_key, 5, 5, False)
self.assertEqual(league_user.score, 10)
self.assertEqual(league_user.clues, 9)
self.assertEqual(league_user.questions_answered, 2)
Expand Down
1 change: 1 addition & 0 deletions src/tests/test_model_league_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def testToLeaderboardJson(self):
league_user.score = 1
league_user.clues = 2
league_user.questions_answered = 3
league_user.active_questions_answered = 3
league_user_json = models.LeagueUser.to_leaderboard_json(league_user)
self.assertEqual(league_user_json['user_name'], 'foo')
self.assertEqual(league_user_json['score'], 1)
Expand Down

0 comments on commit f3f46bb

Please sign in to comment.