def get_total_week_points(account_id, week_num): league_id = account_key(account_id).get().league active_teams = lineup_key(Choice_key(account_key(account_id), league_id), week_num).get().active_teams total_points = 0 for team in active_teams: total_points += get_team_schedule(team)[week_num - 1]['points'] # -1 for conversion to 0 based index return total_points
def get(self): user = users.get_current_user() #Current user's id, used to identify their data user_id = user.user_id() logout_url = users.create_logout_url('/') account = globals.get_or_create_account(user) league_id = account.league find_Choice_key = choice_key(account_key(user_id), str(league_id)) post_Choice_key = find_Choice_key new_team = self.request.get('team') selection_error = is_valid_team(new_team, post_Choice_key.parent().get().league) #Form data into entity and submit post_Choice = Choice.get_or_insert(post_Choice_key.id(), parent=post_Choice_key.parent()) if selection_error == "Good": if len( choice_key(account_key(user_id), league_id).get(). current_team_roster) < maximum_roster_size: post_Choice.current_team_roster.append(int(new_team)) str(post_Choice.put()) else: selection_error = "Your roster is full" #Send them back to the previous page self.redirect( str( self.request.referer.split('?', 1)[0] + '?updated=' + selection_error))
def post(self): user = users.get_current_user() #Current user's id, used to identify their data user_id = user.user_id() logout_url = users.create_logout_url('/') account = globals.get_or_create_account(user) league_id = account.league find_Choice_key = choice_key(account_key(user_id), str(league_id)) post_Choice_key = find_Choice_key new_team = self.request.get('team') selection_error = is_valid_team(new_team, post_Choice_key.parent().get().league) #Form data into entity and submit post_Choice = Choice.get_or_insert(post_Choice_key.id(), parent=post_Choice_key.parent()) if selection_error == "Good": if len(choice_key(account_key(user_id), league_id).get().current_team_roster) < maximum_roster_size: post_Choice.current_team_roster.append(int(new_team)) str(post_Choice.put()) else: selection_error = "You have reached the maximum capacity for teams on your roster" # close_draft(post_Choice_key.parent().get().league) #Display the homepage self.redirect('/draft/pickUp/?updated=' + selection_error)
def get(self): user = users.get_current_user() #Current user's id, used to identify their data user_id = user.user_id() logout_url = users.create_logout_url('/') account = globals.get_or_create_account(user) league_id = account.league find_Choice_key = choice_key(account_key(user_id), str(league_id)) post_Choice_key = find_Choice_key new_team = self.request.get('team') selection_error = is_valid_team(new_team, post_Choice_key.parent().get().league) #Form data into entity and submit post_Choice = Choice.get_or_insert(post_Choice_key.id(), parent=post_Choice_key.parent()) if selection_error == "Good": if len(choice_key(account_key(user_id), league_id).get().current_team_roster) < maximum_roster_size: post_Choice.current_team_roster.append(int(new_team)) str(post_Choice.put()) else: selection_error = "Your roster is full" #Send them back to the previous page self.redirect(str(self.request.referer.split('?', 1)[0] + '?updated=' + selection_error))
def get(self): user = users.get_current_user() account = globals.get_or_create_account(user) match_key_id = account.active_match match = ndb.Key(urlsafe=match_key_id).get() pair = match.key.parent().get() match.active = False if match.won != True: match.won = False match.put() pair.current_match_number += 1 pair.put() account.active_match = globals.no_match_active_id account.put() if pair.key.id() == account.key.id(): partner = account_key(pair.key.parent().id()).get() else: partner = account_key(pair.key.id()).get() partner.active_match = globals.no_match_active_id partner.put() self.redirect('/')
def post(self): user = users.get_current_user() #Current user's id, used to identify their data user_id = user.user_id() logout_url = users.create_logout_url('/') account = globals.get_or_create_account(user) league_id = account.league find_Choice_key = choice_key(account_key(user_id), str(league_id)) post_Choice_key = find_Choice_key new_team = self.request.get('team') selection_error = is_valid_team(new_team, post_Choice_key.parent().get().league) #Form data into entity and submit post_Choice = Choice.get_or_insert(post_Choice_key.id(), parent=post_Choice_key.parent()) if selection_error == "Good": if len( choice_key(account_key(user_id), league_id).get(). current_team_roster) < maximum_roster_size: post_Choice.current_team_roster.append(int(new_team)) str(post_Choice.put()) else: selection_error = "You have reached the maximum capacity for teams on your roster" # close_draft(post_Choice_key.parent().get().league) #Display the homepage self.redirect('/draft/pickUp/?updated=' + selection_error)
def get_total_week_points(account_id, week_num): league_id = account_key(account_id).get().league active_teams = lineup_key(choice_key(account_key(account_id), league_id), week_num).get().active_teams total_points = 0 for team in active_teams: total_points += get_team_schedule(team)[week_num - 1][ 'points'] # -1 for conversion to 0 based index return total_points
def get(self): # Checks for active Google account session user = users.get_current_user() logout_url = users.create_logout_url('/') account = globals.get_or_create_account(user) league_id = account.league league_query = League.query() league_list = league_query.fetch() league_output = [] for league in league_list: number_of_players = len(Account.query().filter( Account.league == league.key.id()).fetch()) commissioner = "None" if account_key(league.key.id()).get(): commissioner = account_key(league.key.id()).get().nickname league_output.append({ 'name': league.name, 'id': league.key.id(), 'size': number_of_players, 'commissioner': commissioner, 'join_url': '/leagueManagement/joinLeague/' + league.key.id() }) if league_id != '0': if league_key(league_id).get().draft_current_position == 0: league_name = league_key(league_id).get().name else: league_name = globals.draft_started_sentinel else: league_name = "" #Send html data to browser template_values = { 'user': user.nickname(), 'logout_url': logout_url, 'league_list': league_output, 'league_name': league_name } template = JINJA_ENVIRONMENT.get_template('templates/league_list.html') self.response.write(template.render(template_values))
def get_current_roster(user_id): """ Return the list of teams currently on the roster :parameter user_id: The id of the user to gather list from :type str or int :return: An array containing, for each team in the roster, a dictionary: - number: The team number(int) - name: The team name (string) - detail_url: A link to the team's individual page (string) - total_points: The number of points(our system) this team scored in total. (int) - disabled: Is 'True' if team is locked because of good performance (string(bool)) """ account = account_key(user_id).get() choice = choice_key(account.key, account.league).get() roster = choice.current_team_roster current_roster = [] for number in roster: team = {} team['number'] = number team['name'] = root_team_key(str(number)).get().name team['detail_url'] = '/allianceManagement/teamDetail/%s' % number team['total_points'] = get_points_to_date(int(number)) if str(number) in get_top_teams(globals.number_of_locked_teams): team['disabled'] = 'True' current_roster.append(team) return sorted(current_roster, key=itemgetter('total_points'), reverse=True)
def get_current_roster(user_id): """ Return the list of teams currently on the roster :parameter user_id: The id of the user to gather list from :type str or int :return: An array containing, for each team in the roster, a dictionary: - number: The team number(int) - name: The team name (string) - detail_url: A link to the team's individual page (string) - total_points: The number of points(our system) this team scored in total. (int) - disabled: Is 'True' if team is locked because of good performance (string(bool)) """ account = account_key(user_id).get() choice = choice_key(account.key, account.league).get() roster = choice.current_team_roster current_roster = [] for number in roster: team = {} team['number'] = number team['name'] = root_team_key(str(number)).get().name team['detail_url'] = '/allianceManagement/teamDetail/%s' % number team['total_points'] = get_points_to_date(int(number)) if str(number) in get_top_teams(globals.number_of_locked_teams): team['disabled'] = 'True' current_roster.append(team) return sorted(current_roster, key=itemgetter('total_points'), reverse=True)
def get_bench_points(account_id, week_num): league_id = account_key(account_id).get().league active_teams = lineup_key(choice_key(account_key(account_id), league_id), week_num).get().active_teams roster = choice_key(account_key(account_id), league_id).get().current_team_roster bench_teams = roster for team in active_teams: bench_teams.remove(team) total_points = 0 for team in bench_teams: total_points += get_team_schedule(team)[week_num - 1][ 'points'] # -1 for conversion to 0 based index return total_points
def remove_from_league(user_id): """ Remove a certain user_id from their league, only if the draft hasn't already started """ #Current user's id, used to identify their data account = Account.get_or_insert(user_id) if league_key(account.league).get() and league_key( account.league).get().draft_current_position == 0: #Remove user's choices and lineup for the league choice = choice_key(account_key(user_id), account.league).get() if choice: lineup_query = Lineup.query(ancestor=choice.key).fetch() for lineup in lineup_query: lineup.key.delete() choice.key.delete() #If this is the last person in the league, or this is the commissioner, delete it after they leave players = Account.query().filter( Account.league == account.league).fetch() if len(players) == 1 or account.league == account.key.id(): past_league = account.league #Remove User's association with league account.league = '0' account.put() delete_league(past_league) #Remove User's association with league account.league = '0' account.put()
def get(self, week_number): """ Update the active teams for the user and redirects them to /viewAlliance/ for the week number Expects a post parameter: 'action' to be one of the following: - bench: Takes a team off the active lineup - putin: Adds a team to the lineup - drop: Drops a team from the user's roster Expects a post parameter: 'team_number' to be the number of the team to perform this action on :parameter week_number: Taken from the url, in string form """ #The choice_key of the request action = self.request.get('action') team_number = self.request.get('team_number') user = users.get_current_user() #Current user's id, used to identify their data user_id = user.user_id() account = globals.get_or_create_account(user) league_id = account.league choice = choice_key(account_key(user_id), league_id).get() roster = [] for team in choice.current_team_roster: roster.append(int(team)) #Only allow changes to the lineup if the week is editable if is_week_editable(week_number): error = False active_lineup = lineup_key(choice_key(account.key, league_id), week_number).get() if action == "bench": active_lineup.active_teams.remove(int(team_number)) elif action == "putin": if len(active_lineup.active_teams) < maximum_active_teams: if int(team_number) in roster: active_lineup.active_teams.append(int(team_number)) else: error = True else: error = True globals.display_error_page( self, self.request.referer, error_messages.maximum_active_teams_reached) elif action == "drop": if not str(team_number) in get_top_teams( globals.number_of_locked_teams): choice = choice_key(account.key, league_id).get() choice.current_team_roster.remove(int(team_number)) if int(team_number) in active_lineup.active_teams: active_lineup.active_teams.remove(int(team_number)) choice.put() active_lineup.put() if not error: self.redirect(self.request.referer)
def get_taken_teams(league_id): """Returns a list of taken teams based on a league and event id""" taken_teams = [] league_player_query = Account.query(Account.league == league_id) league_players = league_player_query.fetch() for player in league_players: choice = Choice_key(account_key(player.key.id()), league_id).get() if choice: taken_teams.append(str(choice.drafted_team)) return taken_teams
def get(self): user_id = users.get_current_user().user_id() if league_key(account_key( user_id).get().league).get().draft_current_position == 0: remove_from_league(user_id) self.redirect('/') else: globals.display_error_page( self, self.request.referer, error_messages.league_already_started_leaving)
def get(self, week_number): """ Update the active teams for the user and redirects them to /viewAlliance/ for the week number Expects a post parameter: 'action' to be one of the following: - bench: Takes a team off the active lineup - putin: Adds a team to the lineup - drop: Drops a team from the user's roster Expects a post parameter: 'team_number' to be the number of the team to perform this action on :parameter week_number: Taken from the url, in string form """ #The choice_key of the request action = self.request.get('action') team_number = self.request.get('team_number') user = users.get_current_user() #Current user's id, used to identify their data user_id = user.user_id() account = globals.get_or_create_account(user) league_id = account.league choice = choice_key(account_key(user_id), league_id).get() roster = [] for team in choice.current_team_roster: roster.append(int(team)) #Only allow changes to the lineup if the week is editable if is_week_editable(week_number): error = False active_lineup = lineup_key(choice_key(account.key, league_id), week_number).get() if action == "bench": active_lineup.active_teams.remove(int(team_number)) elif action == "putin": if len(active_lineup.active_teams) < maximum_active_teams: if int(team_number) in roster: active_lineup.active_teams.append(int(team_number)) else: error = True else: error = True globals.display_error_page(self, self.request.referer, error_messages.maximum_active_teams_reached) elif action == "drop": if not str(team_number) in get_top_teams(globals.number_of_locked_teams): choice = choice_key(account.key, league_id).get() choice.current_team_roster.remove(int(team_number)) if int(team_number) in active_lineup.active_teams: active_lineup.active_teams.remove(int(team_number)) choice.put() active_lineup.put() if not error: self.redirect(self.request.referer)
def get(self): # Checks for active Google account session user = users.get_current_user() #Current user's id, used to identify their data user_id = user.user_id() logout_url = users.create_logout_url('/') account = globals.get_or_create_account(user) league_id = account.league league_query = League.query() league_list = league_query.fetch() league_output = [] for league in league_list: number_of_players = len(Account.query().filter(Account.league == league.key.id()).fetch()) commissioner = "None" if account_key(league.key.id()).get(): commissioner = account_key(league.key.id()).get().nickname league_output.append({'name': league.name, 'id': league.key.id(), 'size': number_of_players, 'commissioner': commissioner, 'join_url': '/leagueManagement/joinLeague/' + league.key.id() }) if league_id != '0': league_name = league_key(league_id).get().name else: league_name = "" #Send html data to browser template_values = { 'user': user.nickname(), 'logout_url': logout_url, 'league_list': league_output, 'league_name': league_name } template = JINJA_ENVIRONMENT.get_template('templates/league_list.html') self.response.write(template.render(template_values))
def get_readable_user_schedule(user_id): """Return the schedule of a single person in readable format""" player = account_key(user_id).get() schedule = player.schedule #Convert to nicknames for i, opponent in enumerate(schedule): if opponent != globals.schedule_bye_week: schedule[i] = opponent else: schedule[i] = "Bye" return schedule
def get_player_record(player_id): """Accesses the data store to return a player's record""" account = account_key(player_id).get() record_WLT = [0, 0, 0, 0] for week in range(0, globals.number_of_official_weeks): week_result = account.record[week] if week_result == globals.record_win: record_WLT[0] += 1 elif week_result == globals.record_loss: record_WLT[1] += 1 elif week_result == globals.record_tie: record_WLT[2] += 1 elif week_result == globals.record_bye: record_WLT[3] += 1 return record_WLT
def get_readable_schedule(league_id): league_player_query = Account.query(Account.league == league_id) league_players = league_player_query.fetch() master_schedule = [] for player in league_players: schedule = player.schedule #Convert to nicknames for i, opponent in enumerate(schedule): schedule[i] = account_key(opponent).get().nickname master_schedule.append({'name': player.nickname, 'schedule': schedule}) return master_schedule
def get_player_record(player_id): """Access the data store to return a player's record""" account = account_key(player_id).get() record_WLT = [0, 0, 0, 0] for week in range(0, globals.number_of_official_weeks): week_result = account.record[week] if week_result == globals.record_win: record_WLT[0] += 1 elif week_result == globals.record_loss: record_WLT[1] += 1 elif week_result == globals.record_tie: record_WLT[2] += 1 elif week_result == globals.record_bye: record_WLT[3] += 1 return record_WLT
def get_team_lists(user_id, week_number): account = account_key(user_id).get() choice = Choice_key(account.key, account.league).get() roster = choice.current_team_roster active_lineup = lineup_key(Choice_key(account.key, account.league), week_number).get().active_teams current_lineup = [] for number in active_lineup: team = {} team['number'] = number team['detail_url'] = '/allianceManagement/teamDetail/%s' % number team['schedule'] = get_team_schedule(number) if is_week_editable(week_number): team['total_points'] = get_points_to_date(int(number)) else: team['total_points'] = 0 event_key = get_team_schedule(int(number))[int(week_number) - 1]['event_key']#-1 to convert to 0-based index if event_key: #Check if the team is competing that week team['total_points'] = get_team_points_at_event(int(number), event_key) current_lineup.append(team) bench_numbers = [] for team in roster: #Just trust me on this one, don't mess with this bench_numbers.append(team) for number in active_lineup: if number in bench_numbers: bench_numbers.remove(number) current_bench = [] for number in bench_numbers: if is_week_editable(week_number): total_points = get_points_to_date(int(number)) else: total_points = 0 event_key = get_team_schedule(int(number))[int(week_number) - 1]['event_key']#-1 to convert to 0-based index if event_key: #Check if the team is competing that week total_points = get_team_points_at_event(int(number), event_key) current_bench.append({'number':number, 'total_points': total_points}) logging.info(current_bench) return [current_lineup, current_bench]
def get_readable_schedule(league_id): """Return the league master schedule for a league id in a readable format""" league_player_query = Account.query(Account.league == league_id) league_players = league_player_query.fetch() master_schedule = [] for player in league_players: schedule = player.schedule #Convert to nicknames for i, opponent in enumerate(schedule): if opponent != globals.schedule_bye_week: schedule[i] = account_key(opponent).get().nickname else: schedule[i] = "Bye" master_schedule.append({'name': player.nickname, 'schedule': schedule}) return master_schedule
def get(self, league_id): """ Adds a user to the specified league :param league_id: Collected from url, the league to join """ user_id = users.get_current_user().user_id() current_league = league_key(account_key(user_id).get().league).get() if not current_league or current_league.draft_current_position == 0: if league_key(league_id).get().draft_current_position == 0: remove_from_league(user_id) #Remove from old league add_to_league(user_id, league_id) #Add to new one self.redirect('/') else: globals.display_error_page( self, self.request.referer, error_messages.league_already_started) else: globals.display_error_page( self, self.request.referer, error_messages.league_already_started_leaving)
def remove_from_league(user_id): #Current user's id, used to identify their data account = Account.get_or_insert(user_id) #Remove user's choices and lineup for the league choice = Choice_key(account_key(user_id), account.league).get() if choice: lineup_query = Lineup.query(ancestor=choice.key).fetch() for lineup in lineup_query: lineup.key.delete() choice.key.delete() #If this is the last person in the league, delete it after they leave players = Account.query().filter(Account.league == account.league).fetch() if len(players) == 1: past_league = account.league #Remove User's association with league account.league = '0' account.put() delete_league(past_league) #Remove User's association with league account.league = '0' account.put()
def generate_schedule(league_id): """ Generate and distribute a schedule based on a round robin system where team members play each other fairly :parameter league_id: The id of the league to generate the schedule for :type: string Generate a random schedule in which all members play each other at least once before playing each other again Take this schedule and use to assign an individual schedule to each player """ player_ids_list = [] league_player_query = Account.query(Account.league == league_id) league_players = league_player_query.fetch() for player in league_players: player_ids_list.append(player.key.id()) shuffle(player_ids_list) if len(player_ids_list) % 2 == 1: # Check if the number of players is odd # Add 0 to represent bye week, added to beginning to solve last player issue player_ids_list.insert(0, globals.schedule_bye_week) number_of_players = len(player_ids_list) if number_of_players > 2: #Now that there's an even number of teams, use a round robin system to assign teams to weeks for player_num, player_id in enumerate(player_ids_list, start=1): if player_id != '0' and player_num != number_of_players: player = account_key(player_id).get() schedule = [] for week in range(1, number_of_players): opponent = (week + (number_of_players - 1) - player_num) % (number_of_players - 1) if opponent == player_num - 1: opponent = number_of_players - 1 schedule.append(player_ids_list[opponent]) #Confirm schedule length is appropriate schedule = make_schedule_fit(schedule) player.schedule = schedule player.put() #For the last player, whose schedule doesn't follow the same pattern last_player = account_key(player_ids_list[number_of_players - 1]).get() last_player_schedule = [] for week in range(1, number_of_players): #This equation comes from a mathematica document. Truss me about it - 2014 season last_player_schedule.append(player_ids_list[int( .25 * ((-1)**week) * ((number_of_players - 1) + (number_of_players - 3) * ((-1)**week) + 2 * ((-1)**week) * week))]) last_player_schedule = make_schedule_fit(last_player_schedule) last_player.schedule = last_player_schedule last_player.put() elif number_of_players == 2: #If there's only two players, they just constantly play each other player1 = league_players[0] schedule1 = [] player2 = league_players[1] schedule2 = [] for week in range(1, globals.number_of_round_robin_weeks + 1): schedule1.append(player_ids_list[0]) schedule2.append(player_ids_list[1]) player1.schedule = schedule1 player2.schedule = schedule2 player1.put() player2.put()
def get_opponent_name(user_id, week_number): return account_key(get_opponent(user_id, week_number)).get().nickname
def get_opponent(user_id, week_number): """Return the account key of the opponent of a certain user_id on a specific week_number""" return account_key(user_id).get().schedule[ int(week_number) - 1] # -1 for conversion to 0 based
def get_team_lists(user_id, week_number): """ Return the bench and active team lists for a particular user on a particular week :parameter user_id: The id of the user to gather lists from :type str or int :parameter week_number: The week to gather data on :type str or int :return: An array of two arrays(lineup, bench) containing, for each team in the lineup, a dictionary: - number: The name of the event (int) - detail_url: A link to the team's individual page (string) - schedule: An array containing scheduling data (schedule) (See get_team_schedule()) - total_points: The number of points(our system) this team scored in total. (int) If week_number is editable, this is actually the points scored this week, not total - disabled: Is 'True' if team is locked because of good performance (string(bool)) For each team in the bench list, the dictionary contains the following: - number: The name of the event (int) - total_points: The number of points(our system) this team scored in total. (int) If week_number is editable, this is actually the points scored this week, not total - disabled: Is 'True' if team is locked because of good performance (string(bool)) """ account = account_key(user_id).get() choice = choice_key(account.key, account.league).get() roster = choice.current_team_roster active_lineup = lineup_key(choice_key(account.key, account.league), week_number).get().active_teams current_lineup = [] for number in active_lineup: team = {} team['number'] = number team['detail_url'] = '/allianceManagement/teamDetail/%s' % number team['schedule'] = get_team_schedule(number) if is_week_editable(week_number): team['total_points'] = get_points_to_date(int(number)) else: team['total_points'] = 0 event_key = get_team_schedule( int(number))[int(week_number) - 1]['event_key'] # -1 convert to 0-based index if event_key: # Check if the team is competing that week team['total_points'] = get_team_points_at_event( int(number), event_key) if str(number) in get_top_teams(globals.number_of_locked_teams): team['disabled'] = 'True' current_lineup.append(team) bench_numbers = [] for team in roster: # Just trust me on this one, don't mess with this bench_numbers.append(team) for number in active_lineup: if number in bench_numbers: bench_numbers.remove(number) current_bench = [] for number in bench_numbers: if is_week_editable(week_number): total_points = get_points_to_date(int(number)) else: total_points = 0 event_key = get_team_schedule( int(number))[int(week_number) - 1]['event_key'] # -1 convert to 0-based index if event_key: # Check if the team is competing that week total_points = get_team_points_at_event(int(number), event_key) disabled = '' if str(number) in get_top_teams(globals.number_of_locked_teams): disabled = 'True' current_bench.append({ 'number': number, 'total_points': total_points, 'disabled': disabled }) return [current_lineup, current_bench]
def get_opponent_name(user_id, week_number): """Return the name of the opponent of certain user_id on a specific week_number""" return account_key(get_opponent(user_id, week_number)).get().nickname
def get_opponent(user_id, week_number): return account_key(user_id).get().schedule[int(week_number) - 1] # -1 for conversion to 0 based
def generate_schedule(league_id): """ Generate and distribute a schedule based on a round robin system where team members play each other fairly :parameter league_id: The id of the league to generate the schedule for :type: string Generate a random schedule in which all members play each other at least once before playing each other again Take this schedule and use to assign an individual schedule to each player """ player_ids_list = [] league_player_query = Account.query(Account.league == league_id) league_players = league_player_query.fetch() for player in league_players: player_ids_list.append(player.key.id()) shuffle(player_ids_list) if len(player_ids_list) % 2 == 1: # Check if the number of players is odd # Add 0 to represent bye week, added to beginning to solve last player issue player_ids_list.insert(0, globals.schedule_bye_week) number_of_players = len(player_ids_list) if number_of_players > 2: #Now that there's an even number of teams, use a round robin system to assign teams to weeks for player_num, player_id in enumerate(player_ids_list, start=1): if player_id != '0' and player_num != number_of_players: player = account_key(player_id).get() schedule = [] for week in range(1, number_of_players): opponent = (week + (number_of_players - 1) - player_num) % (number_of_players - 1) if opponent == player_num - 1: opponent = number_of_players - 1 schedule.append(player_ids_list[opponent]) #Confirm schedule length is appropriate schedule = make_schedule_fit(schedule) player.schedule = schedule player.put() #For the last player, whose schedule doesn't follow the same pattern last_player = account_key(player_ids_list[number_of_players - 1]).get() last_player_schedule = [] for week in range(1, number_of_players): #This equation comes from a mathematica document. Truss me about it - 2014 season last_player_schedule.append(player_ids_list[int(.25 * ((-1) ** week) * ((number_of_players - 1) + (number_of_players - 3) * ((-1) ** week) + 2 * ((-1) ** week) * week))]) last_player_schedule = make_schedule_fit(last_player_schedule) last_player.schedule = last_player_schedule last_player.put() elif number_of_players == 2: #If there's only two players, they just constantly play each other player1 = league_players[0] schedule1 = [] player2 = league_players[1] schedule2 = [] for week in range(1, globals.number_of_round_robin_weeks + 1): schedule1.append(player_ids_list[0]) schedule2.append(player_ids_list[1]) player1.schedule = schedule1 player2.schedule = schedule2 player1.put() player2.put()
def get(self): # Checks for active Google account session user = users.get_current_user() #Check if user is logged in if user is None: #Send html data to browser template_values = {'logged_out': users.create_login_url('/')} template = JINJA_ENVIRONMENT.get_template('templates/index.html') self.response.write(template.render(template_values)) else: logout_url = users.create_logout_url('/') account = globals.get_or_create_account(user) match_key_id = account.active_match user_wordlist = None partner_wordlist = None match_won = None winning_word = None partner_name = None users_turn = None if match_key_id == globals.no_match_active_id: match_active = False else: match_active = True match = ndb.Key(urlsafe=match_key_id).get() match_won = match.won if match.key.parent().parent().id() == account.key.id(): user_wordlist = match.user_1_list partner_wordlist = match.user_2_list partner_name = account_key(match.key.parent().id()).get().nickname else: partner_wordlist = match.user_1_list user_wordlist = match.user_2_list partner_name = account_key(match.key.parent().parent().id()).get().nickname if len(user_wordlist) != len(partner_wordlist): if len(user_wordlist) > len(partner_wordlist): partner_wordlist.append("Waiting...") users_turn = False else: users_turn = True partner_wordlist[-1] = "*******" user_wordlist.append("-") else: users_turn = True if match_won: winning_word = user_wordlist[-1] user_wordlist = reversed(user_wordlist) partner_wordlist = reversed(partner_wordlist) update_text = self.request.get('updated') #Send html data to browser template_values = {'user': user.nickname(), 'users_turn': users_turn, 'logout_url': logout_url, 'match_key': match_key_id, 'match_active': match_active, 'match_won': match_won, 'update_text': update_text, 'user_wordlist': user_wordlist, 'partner_wordlist': partner_wordlist, 'partner_name': partner_name, 'winning_word': winning_word, } template = JINJA_ENVIRONMENT.get_template('templates/index.html') self.response.write(template.render(template_values))
def get(self): # Checks for active Google account session user = users.get_current_user() #Force user login if user is None: self.redirect(users.create_login_url(self.request.uri)) else: #Current user's id, used to identify their data user_id = user.user_id() logout_url = users.create_logout_url('/') account = globals.get_or_create_account(user) league_id = account.league #Get user's choices for the current league find_choice_key = choice_key(account_key(user_id), str(league_id)) found_choice = find_choice_key.get() #Display update text for the status of the last choice update update_text = self.request.get('updated') if self.request.get('updated') == "Good": update_text = "Team added successfully" #Display the user's current roster user_roster = [] if found_choice: user_roster = found_choice.current_team_roster #Get list of players in the league and their choices league_table = [{'player_team': 'Roster', 'player_name': 'Player'}] league_player_query = Account.query(Account.league == league_id) league_players = league_player_query.fetch() #league_player_query.order(Account.nickname).fetch() for player in league_players: choice = choice_key(account_key(player.key.id()), league_id).get() if choice: league_table.append( {'player_team': str(choice.current_team_roster), 'player_name': player.nickname}) else: league_table.append({'player_team': 'None', 'player_name': player.nickname}) if league_id != '0': if league_key(league_id).get().draft_current_position == 0: league_name = league_key(league_id).get().name else: league_name = globals.draft_started_sentinel else: league_name = "" #Send html data to browser template_values = { 'user': user.nickname(), 'logout_url': logout_url, 'update_text': update_text, 'league_table': league_table, 'league_name': league_name, 'roster': user_roster, 'default_team': self.request.get('team'), } template = JINJA_ENVIRONMENT.get_template('templates/pick_up_main.html') self.response.write(template.render(template_values))
def get_team_lists(user_id, week_number): """ Return the bench and active team lists for a particular user on a particular week :parameter user_id: The id of the user to gather lists from :type str or int :parameter week_number: The week to gather data on :type str or int :return: An array of two arrays(lineup, bench) containing, for each team in the lineup, a dictionary: - number: The name of the event (int) - detail_url: A link to the team's individual page (string) - schedule: An array containing scheduling data (schedule) (See get_team_schedule()) - total_points: The number of points(our system) this team scored in total. (int) If week_number is editable, this is actually the points scored this week, not total - disabled: Is 'True' if team is locked because of good performance (string(bool)) For each team in the bench list, the dictionary contains the following: - number: The name of the event (int) - total_points: The number of points(our system) this team scored in total. (int) If week_number is editable, this is actually the points scored this week, not total - disabled: Is 'True' if team is locked because of good performance (string(bool)) """ account = account_key(user_id).get() choice = choice_key(account.key, account.league).get() roster = choice.current_team_roster active_lineup = lineup_key(choice_key(account.key, account.league), week_number).get().active_teams current_lineup = [] for number in active_lineup: team = {} team['number'] = number team['detail_url'] = '/allianceManagement/teamDetail/%s' % number team['schedule'] = get_team_schedule(number) if is_week_editable(week_number): team['total_points'] = get_points_to_date(int(number)) else: team['total_points'] = 0 event_key = get_team_schedule(int(number))[int(week_number) - 1]['event_key'] # -1 convert to 0-based index if event_key: # Check if the team is competing that week team['total_points'] = get_team_points_at_event(int(number), event_key) if str(number) in get_top_teams(globals.number_of_locked_teams): team['disabled'] = 'True' current_lineup.append(team) bench_numbers = [] for team in roster: # Just trust me on this one, don't mess with this bench_numbers.append(team) for number in active_lineup: if number in bench_numbers: bench_numbers.remove(number) current_bench = [] for number in bench_numbers: if is_week_editable(week_number): total_points = get_points_to_date(int(number)) else: total_points = 0 event_key = get_team_schedule(int(number))[int(week_number) - 1]['event_key'] # -1 convert to 0-based index if event_key: # Check if the team is competing that week total_points = get_team_points_at_event(int(number), event_key) disabled = '' if str(number) in get_top_teams(globals.number_of_locked_teams): disabled = 'True' current_bench.append({'number':number, 'total_points': total_points, 'disabled': disabled}) return [current_lineup, current_bench]
def get(self): # Checks for active Google account session user = users.get_current_user() #Force user login if user is None: self.redirect(users.create_login_url(self.request.uri)) else: #Current user's id, used to identify their data user_id = user.user_id() logout_url = users.create_logout_url('/') account = globals.get_or_create_account(user) league_id = account.league #Get user's choices for the current league find_choice_key = choice_key(account_key(user_id), str(league_id)) found_choice = find_choice_key.get() #Display update text for the status of the last choice update update_text = self.request.get('updated') if self.request.get('updated') == "Good": update_text = "Team added successfully" #Display the user's current roster user_roster = [] if found_choice: user_roster = found_choice.current_team_roster #Get list of players in the league and their choices league_table = [{'player_team': 'Roster', 'player_name': 'Player'}] league_player_query = Account.query(Account.league == league_id) league_players = league_player_query.fetch( ) #league_player_query.order(Account.nickname).fetch() for player in league_players: choice = choice_key(account_key(player.key.id()), league_id).get() if choice: league_table.append({ 'player_team': str(choice.current_team_roster), 'player_name': player.nickname }) else: league_table.append({ 'player_team': 'None', 'player_name': player.nickname }) if league_id != '0': if league_key(league_id).get().draft_current_position == 0: league_name = league_key(league_id).get().name else: league_name = globals.draft_started_sentinel else: league_name = "" #Send html data to browser template_values = { 'user': user.nickname(), 'logout_url': logout_url, 'update_text': update_text, 'league_table': league_table, 'league_name': league_name, 'roster': user_roster, 'default_team': self.request.get('team'), } template = JINJA_ENVIRONMENT.get_template( 'templates/pick_up_main.html') self.response.write(template.render(template_values))