def post(self):
        update_session_time()
        session = get_current_session()
        check_session_status()
        
        if session.is_active():
            if session.has_key('active_user'):
                selected_football_pool_key = Key(self.request.get('football-pool-key'))
                selected_football_pool = CAFootballPool.get(selected_football_pool_key)
                
                selected_first_round_matches = selected_football_pool.first_round_matches.fetch(18)
                first_round_matches = eval(self.request.get('first-round-matches'))
                
                if first_round_matches: #Hay cambios en la primera fase
                    counter = 0
                
                    for match_results in first_round_matches:
                        selected_match = selected_first_round_matches[counter]
                        
                        selected_match.goals_team1 = int(match_results[1])
                        selected_match.goals_team2 = int(match_results[3])
                        selected_match.put()
                
                second_round_matches = eval(self.request.get('second-round-matches'))
                selected_second_round_matches = selected_football_pool.second_round_matches.fetch(8)
                
                for i in range(0, len(selected_second_round_matches)):
                    initials = second_round_matches[i][0].partition('-')
                    initials = initials[2].partition('-')
                    team0_initials = initials[0]
                    initials = initials[2].partition('-')
                    team1_initials = initials[0]
                    
                    team0 = CATeam.all().filter("name =", get_team_whole_name(team0_initials)).fetch(1)[0]
                    team1 = CATeam.all().filter("name =", get_team_whole_name(team1_initials)).fetch(1)[0]
                    
                    teams_list = [team0.key(), team1.key()]
                    
                    selected_match = selected_second_round_matches[i]
                    selected_match.goals_team1 = int(second_round_matches[i][1])
                    selected_match.goals_team2 = int(second_round_matches[i][3])
                    selected_match.teams = teams_list
                    selected_match.put()
                
#                template_values = {
#                    'user': session['active_user'],
#                    'top_scorers': get_top_scorers(),
#                    'top_users': get_top_users_global_ranking(),
#                    'last_jackpot': get_last_jackpot()
#                }
#                        
#                render_template(self, 'home.html', template_values)
                self.redirect('/list/football-pools/view')
        else:
            self.redirect('/')
Ejemplo n.º 2
0
def get_total_points(football_pool):
    original_pool = CAFootballPool.all().filter("privacy =", True).fetch(1)[0]

    original_pool_first_round_matches = original_pool.first_round_matches.fetch(18)
    football_pool_first_round_matches = football_pool.first_round_matches.fetch(18)

    points = 0

    for x in range(0, 18):
        original_match_team1_goals = original_pool_first_round_matches[x].goals_team1
        original_match_team2_goals = original_pool_first_round_matches[x].goals_team2

        match_team1_goals = football_pool_first_round_matches[x].goals_team1
        match_team2_goals = football_pool_first_round_matches[x].goals_team2

        if (original_match_team1_goals == match_team1_goals) and (original_match_team2_goals == match_team2_goals):
            points += 5
        elif (
            (original_match_team1_goals > original_match_team2_goals) and (match_team1_goals > match_team2_goals)
        ) or ((original_match_team1_goals < original_match_team2_goals) and (match_team1_goals < match_team2_goals)):
            points += 3

        if (
            (original_match_team1_goals != -1)
            and (original_match_team2_goals != -1)
            and (
                (original_match_team1_goals == original_match_team2_goals) and (match_team1_goals == match_team2_goals)
            )
        ):
            points += 3

    original_pool_second_round_matches = original_pool.second_round_matches.fetch(8)
    football_pool_second_round_matches = football_pool.second_round_matches.fetch(8)

    # Quarter finals matches
    original_teams = []
    teams = []

    for x in range(0, 4):
        if original_pool_second_round_matches[x].teams:
            original_team1 = CATeam.get(original_pool_second_round_matches[x].teams[0]).name
            original_team2 = CATeam.get(original_pool_second_round_matches[x].teams[1]).name

            team1 = CATeam.get(football_pool_second_round_matches[x].teams[0]).name
            team2 = CATeam.get(football_pool_second_round_matches[x].teams[1]).name

            # Puntos por pegar orden en cuartos de final
            if original_team1 == team1:
                points += 3

            if original_team2 == team2:
                points += 3

            original_teams.append(original_team1)
            original_teams.append(original_team2)

            teams.append(team1)
            teams.append(team2)

            # Puntos por pegar partido completo
            points += add_points_for_match(
                original_team1,
                original_pool_second_round_matches[x].goals_team1,
                original_team2,
                original_pool_second_round_matches[x].goals_team2,
                team1,
                football_pool_second_round_matches[x].goals_team1,
                team2,
                football_pool_second_round_matches[x].goals_team2,
            )

    # Puntos por adivinar equipo
    for team in teams:
        if team in original_teams:
            points += 3

    original_teams = []
    teams = []

    # Semi final matches
    for x in range(4, 6):
        if original_pool_second_round_matches[x].teams:
            original_team1 = CATeam.get(original_pool_second_round_matches[x].teams[0]).name
            original_team2 = CATeam.get(original_pool_second_round_matches[x].teams[1]).name

            team1 = CATeam.get(football_pool_second_round_matches[x].teams[0]).name
            team2 = CATeam.get(football_pool_second_round_matches[x].teams[1]).name

            # Puntos por pegar partido completo
            points += add_points_for_match(
                original_team1,
                original_pool_second_round_matches[x].goals_team1,
                original_team2,
                original_pool_second_round_matches[x].goals_team2,
                team1,
                football_pool_second_round_matches[x].goals_team1,
                team2,
                football_pool_second_round_matches[x].goals_team2,
            )

            original_teams.append(original_team1)
            original_teams.append(original_team2)

            teams.append(team1)
            teams.append(team2)

    # Puntos por adivinar equipo
    for team in teams:
        if team in original_teams:
            points += 3

    original_teams = []
    teams = []

    # Third-fourth match
    if original_pool_second_round_matches[6].teams:
        original_team1 = CATeam.get(original_pool_second_round_matches[6].teams[0]).name
        original_team2 = CATeam.get(original_pool_second_round_matches[6].teams[1]).name

        team1 = CATeam.get(football_pool_second_round_matches[6].teams[0]).name
        team2 = CATeam.get(football_pool_second_round_matches[6].teams[1]).name

        original_teams.append(original_team1)
        original_teams.append(original_team2)

        teams.append(team1)
        teams.append(team2)

        # Puntos por pegar partido completo
        points += add_points_for_match(
            original_team1,
            original_pool_second_round_matches[6].goals_team1,
            original_team2,
            original_pool_second_round_matches[6].goals_team2,
            team1,
            football_pool_second_round_matches[6].goals_team1,
            team2,
            football_pool_second_round_matches[6].goals_team2,
        )

    # Puntos por adivinar equipo
    for team in teams:
        if team in original_teams:
            points += 3

    original_teams = []
    teams = []

    # Final
    if original_pool_second_round_matches[7].teams:
        original_team1 = CATeam.get(original_pool_second_round_matches[7].teams[0]).name
        original_team2 = CATeam.get(original_pool_second_round_matches[7].teams[1]).name

        team1 = CATeam.get(football_pool_second_round_matches[7].teams[0]).name
        team2 = CATeam.get(football_pool_second_round_matches[7].teams[1]).name

        original_teams.append(original_team1)
        original_teams.append(original_team2)

        teams.append(team1)
        teams.append(team2)

        # Puntos por pegar partido completo
        points += add_points_for_match(
            original_team1,
            original_pool_second_round_matches[7].goals_team1,
            original_team2,
            original_pool_second_round_matches[7].goals_team2,
            team1,
            football_pool_second_round_matches[7].goals_team1,
            team2,
            football_pool_second_round_matches[7].goals_team2,
        )

    # Puntos por adivinar equipo
    for team in teams:
        if team in original_teams:
            points += 3

    return points
Ejemplo n.º 3
0
def get_team_whole_name(team_initials):
    teams = CATeam.all().fetch(12)

    for team in teams:
        if team.name[:3] == team_initials:
            return team.name
    def post(self):
        update_session_time()
        session = get_current_session()
        check_session_status()
        
        if session.is_active():
            if session.has_key('active_user'):
                football_pool_name = self.request.get('football-pool-name')
                active_user = session['active_user']
                
                active_user_football_pool = CAFootballPool(user=active_user, name=football_pool_name, privacy=False)
                active_user_football_pool.put()
                
                original_pool = CAFootballPool.all().filter("privacy =", True).fetch(1)[0]
                first_round_matches = eval(self.request.get('first-round-matches'))
                original_first_round_matches = original_pool.first_round_matches.fetch(18)
                counter = 0
                
                for match_results in first_round_matches:
                    initials = match_results[0].partition('-')
                    team0_initials = initials[0]
                    initials = initials[2].partition('-')
                    team1_initials = initials[0]
                    
                    team0 = CATeam.all().filter("name =", get_team_whole_name(team0_initials)).fetch(1)[0]
                    team1 = CATeam.all().filter("name =", get_team_whole_name(team1_initials)).fetch(1)[0]
                    
                    teams_list = [team0.key(), team1.key()]
                    
                    original_match = original_first_round_matches[counter]
                    active_user_match = CAMatch(date=original_match.date, goals_team1=int(match_results[1]), goals_team2=int(match_results[3]), teams=teams_list, football_pool=active_user_football_pool)
                    active_user_match.put()
                    counter += 1
                
                second_round_matches = eval(self.request.get('second-round-matches'))
                original_second_round_matches = original_pool.second_round_matches.fetch(8)
                
                for i in range(0, len(original_second_round_matches)):
                    initials = second_round_matches[i][0].partition('-')
                    initials = initials[2].partition('-')
                    team0_initials = initials[0]
                    initials = initials[2].partition('-')
                    team1_initials = initials[0]
                    
                    team0 = CATeam.all().filter("name =", get_team_whole_name(team0_initials)).fetch(1)[0]
                    team1 = CATeam.all().filter("name =", get_team_whole_name(team1_initials)).fetch(1)[0]
                    #print team0.name + ' ' + second_round_matches[i][1] + ' ' + second_round_matches[i][3] + ' ' + team1.name
                    
                    teams_list = [team0.key(), team1.key()]
                    
                    original_match = original_second_round_matches[i]
                    active_user_match = CAMatch(date=original_match.date, goals_team1=int(second_round_matches[i][1]), goals_team2=int(second_round_matches[i][3]), teams=teams_list, football_pool=active_user_football_pool)
                    active_user_match.put()
                
#                template_values = {
#                    'session_status': True,
#                    'user': session['active_user'],
#                    'top_scorers': get_top_scorers(),
#                    'top_users': get_top_users_global_ranking(),
#                    'last_jackpot': get_last_jackpot()
#                }
#                        
#                render_template(self, 'home.html', template_values)
                self.redirect('/list/football-pools/view')
        else:
            self.redirect('/')