def generate_league(league_name, players): player_ids = [p.id for p in players] if not player_ids: player_ids.append(95) league = League.create_league( league_name, random.choice(player_ids), player_ids) loaded_league = League.load_opponents(league.id) if loaded_league is not None: print("League ({0}) created successfully.".format(loaded_league.id)) return loaded_league
played_ids = [opponent_ids[n] for n in range(0, number_of_players)] results = _prepare_result(played_ids, played) return _create_game(league_id, creator_id, results, sport_id) def generate_competitive_game(league_id, opponent_ids): won = "won" lost = "lost" number_of_winners = random.randint(1, 4) number_of_losers = random.randint(1, 4) (creator_id, opponent_ids, sport_id) = _prepare_game_creation(opponent_ids) won_ids = [opponent_ids[n] for n in range(0, number_of_winners)] lost_ids = [opponent_ids[n] for n in range( number_of_winners, number_of_winners + number_of_losers)] won_results = _prepare_result(won_ids, won) lost_results = _prepare_result(lost_ids, lost) results = dict(won_results.items() + lost_results.items()) return _create_game(league_id, creator_id, results, sport_id) if __name__ == "__main__": league_id = int(sys.argv[1]) print "Creating schedule for League {0}.".format(league_id) league = League.load_opponents(league_id) schedule = generate_schedule(league)
def load(self): """ Populate context, aggregations, objects, and opponents. """ # TODO: we should be able to do all this in one or two queries. given # player id, traverse to a league. from there get games and opponents # for those games. the only tricky thing is just getting one league. it # shouldn't be tricky to avoid manually loading opponents. person = Person.load_leagues(self.session.person_id) league = None if self._league_id is None: # if no league was requested than get the first league league = person.get_leagues().values()[0] else: league = person.get_leagues().get(self._league_id) # TODO: if league is None than throw some 'request invalid league # error' # TODO: we don't need to load Games from League when they can be loaded # from Opponents [or the vice-versa] all at the same time. we should # never be calling set_opponents() and set_games() outside the api. # RANKINGS LOAD opponents_list = League.load_opponents(league.id).get_opponents() league.set_opponents({o.id: o for o in opponents_list}) # GAMES LOAD (WITH OPPONENTS AND COMMENTERS) games_list = League.load_games(league.id).get_games() # TODO: iterating through this list is only temporary becaue the # multiload should have happened in the api. game_ids = [g.id for g in games_list] # load opponents and commenters and creator for each game {g_id: Game} games_dict = Game.multiload_important_persons(game_ids) # load league with opponents and games into generic context league.set_games(games_dict) self._context = league # league's opponents by Win Count # store the list because the league has a dict, and sort returns None opponents = self._context.get_opponents() opponents.sort( key=lambda x: x.win_count, reverse=True) self._aggregations["standings"] = opponents self._aggregations["activity"] = None # store opponents loaded games in reverse order (so it's new first) games = games_dict.values() # sort returns None as it's in-place games.sort( key=lambda x: x.created_ts, reverse=True) self._objects = games # load opponents into rivals as well self._rivals = self._context.get_opponents() # load the list of sports self._sports = SPORT.ALL