def score_users(): # get fixture with status "updated_result" logger.info("Getting fixture with status `updated_result`") fixture_list = list(Fixture.status_index.query("updated_result", limit=1)) if len(fixture_list) > 0: f = fixture_list[0] logger.info( "Calculating users score for the fixture result `{}`".format( f.fixture_id)) logger.info( "Getting predictions for users with fixture_id `{}` in curr_prediction" .format(f.fixture_id)) # get user records where current prediction fixture id is the same as the fixture that ended users_list = list( User.scan(User.curr_prediction.fixture == f.fixture_id)) if len(users_list) > 0: user_list_to_save = [] scoreline_dict = {} scorerpoints_dict = {} highest_user = None for u in users_list: u.curr_prediction.points = 0 # just to be sure logger.info("Calculating score for user : `{}`".format( u.user_id)) points = 0 ur = u.curr_prediction.result if is_correct_result(ur, f.result): points += 2 logger.info("Correct result - points = {}".format(points)) if is_correct_scoreline(ur, f.result): points += 3 logger.info( "Correct scoreline - points = {}".format(points)) correct_scorers = list() actual_scorers = list(f.result.scorers) logger.info("Correct scorer = {}, pred_scorer = {}".format( actual_scorers, ur.scorers)) for s in ur.scorers: if s in actual_scorers: correct_scorers.append(s) actual_scorers.remove(s) scoreline_dict[u.user_id] = 1 scorerpoints_dict[u.user_id] = len(correct_scorers) points = points + len(correct_scorers) logger.info( "Adding scorer points, total = {}".format(points)) # update points u = add_points(u, points, f.league) user_list_to_save.append(u) user_list_to_save = sorted(user_list_to_save, key=lambda u: u.curr_prediction.points, reverse=True) for u in user_list_to_save: print(u.user_id, u.curr_prediction.points) second_list_to_save = [] highest_user = None while user_list_to_save: u = user_list_to_save.pop(0) if not highest_user: highest_user = u else: if u.curr_prediction.points > highest_user.curr_prediction.points: highest_user = u elif u.curr_prediction.points == highest_user.curr_prediction.points: logger.info("{} vs {}".format(u.user_id, highest_user.user_id)) if win_tie(u, highest_user, scoreline_dict, scorerpoints_dict, f): logger.info("{} won".format(u.user_id)) highest_user = add_points(highest_user, 3, f.league) second_list_to_save.append(highest_user) highest_user = u else: logger.info( "Lost to highest user with same points, adding +3" ) u = add_points(u, 3, f.league) second_list_to_save.append(u) else: # current user less than highest_user, so break second_list_to_save.append(u) break highest_user = add_points(highest_user, 5, f.league) second_list_to_save.append(highest_user) for u in user_list_to_save: u.save() for u in second_list_to_save: u.save() return f
def get_users_from_db(): users_list = sorted(User.scan(), key=lambda u: u.total_points, reverse=True) return users_list