Ejemplo n.º 1
0
def save_league(this_league):
    if not league_db.league_exists(this_league.name):
        league_db.init_league_db(this_league)
        with open(os.path.join(data_dir, league_dir, this_league.name, f"{this_league.name}.league"), "w") as league_file:
            league_json_string = jsonpickle.encode(this_league.league, keys=True)
            json.dump(league_json_string, league_file, indent=4)
    league_db.save_league(this_league)
Ejemplo n.º 2
0
def load_league_file(league_name):
    if league_db.league_exists(league_name):
        state = league_db.state(league_name)
        this_league = league_structure(league_name)
        with open(
                os.path.join(data_dir, league_dir, league_name,
                             f"{this_league.name}.league")) as league_file:
            this_league.league = jsonpickle.decode(json.load(league_file),
                                                   keys=True,
                                                   classes=team)
        with open(
                os.path.join(data_dir, league_dir, league_name,
                             f"{this_league.name}.state")) as state_file:
            state_dic = json.load(state_file)

        this_league.day = state_dic["day"]
        this_league.schedule = state_dic["schedule"]
        this_league.constraints = state_dic["constraints"]
        this_league.game_length = state_dic["game_length"]
        this_league.series_length = state_dic["series_length"]
        this_league.owner = state_dic["owner"]
        this_league.games_per_hour = state_dic["games_per_hour"]
        this_league.historic = state_dic["historic"]
        this_league.season = state_dic["season"]
        try:
            this_league.champion = state_dic["champion"]
        except:
            this_league.champion = None
        return this_league
Ejemplo n.º 3
0
def load_league_file(league_name):
    if league_db.league_exists(league_name):
        state = league_db.state(league_name)
        this_league = league_structure(league_name)
        with open(
                os.path.join(data_dir, league_dir, league_name,
                             f"{this_league.name}.league")) as league_file:
            this_league.league = jsonpickle.decode(json.load(league_file),
                                                   keys=True,
                                                   classes=team)
        with open(
                os.path.join(data_dir, league_dir, league_name,
                             f"{this_league.name}.state")) as state_file:
            state_dic = json.load(state_file)

        this_league.day = state_dic["day"]
        this_league.schedule = state_dic["schedule"]
        this_league.constraints = state_dic["constraints"]
        this_league.game_length = state_dic["game_length"]
        this_league.series_length = state_dic["series_length"]
        this_league.owner = state_dic["owner"]
        this_league.games_per_hour = state_dic["games_per_hour"]
        this_league.historic = state_dic["historic"]
        this_league.season = state_dic["season"]
        try:
            this_league.champion = state_dic["champion"]
        except:
            this_league.champion = None
        try:
            this_league.weather_forecast = state_dic[
                "forecasts"]  #handles legacy leagues
        except:
            this_league.weather_forecast = {}
            for this_team in this_league.teams_in_league(
            ):  #give them all fresh forecasts starting at current day
                this_league.new_weathers_midseason(this_team.name)
            save_league(this_league)
        try:
            this_league.last_weather_event_day = state_dic[
                "last_weather_event"]
        except:
            this_league.last_weather_event_day = 0
        try:
            this_league.subbed_channels = state_dic["subs"]
        except:
            this_league.subbed_channels = []
        return this_league
Ejemplo n.º 4
0
def create_league():
    config = json.loads(request.data)

    if league_exists(config['name']):
        return jsonify({'status':'err_league_exists'}), 400

    num_subleagues = len(config['structure']['subleagues'])
    if num_subleagues < 1 or num_subleagues % 2 != 0:
        return jsonify({'status':'err_invalid_subleague_count'}), 400

    num_divisions = len(config['structure']['subleagues'][0]['divisions'])
    if num_subleagues * (num_divisions + 1) > MAX_SUBLEAGUE_DIVISION_TOTAL:
        return jsonify({'status':'err_invalid_subleague_division_total'}), 400

    league_dic = {}
    err_teams = []
    for subleague in config['structure']['subleagues']:
        if subleague['name'] in league_dic:
            return jsonify({'status':'err_duplicate_name', 'cause':subleague['name']})

        subleague_dic = {}
        for division in subleague['divisions']:
            if division['name'] in subleague_dic:
                return jsonify({'status':'err_duplicate_name', 'cause':f"{subleague['name']}/{division['name']}"}), 400
            elif len(division['teams']) > MAX_TEAMS_PER_DIVISION:
                return jsonify({'status':'err_too_many_teams', 'cause':f"{subleague['name']}/{division['name']}"})

            teams = []
            for team_name in division['teams']:
                team = games.get_team(team_name)
                if team is None:
                    err_teams.append(team_name)
                else:
                    teams.append(team)
            subleague_dic[division['name']] = teams
        league_dic[subleague['name']] = subleague_dic

    if len(err_teams) > 0:
        return jsonify({'status':'err_no_such_team', 'cause': err_teams}), 400

    for (key, min_val) in [
        ('division_series', 1), 
        ('inter_division_series', 1), 
        ('inter_league_series', 1)
    ]:
        if config[key] < min_val:
            return jsonify({'status':'err_invalid_optiion_value', 'cause':key}), 400

    new_league = league_structure(config['name'])
    new_league.setup(
        league_dic, 
        division_games=config['division_series'],
        inter_division_games=config['inter_division_series'],
        inter_league_games=config['inter_league_series'],
    )
    new_league.constraints["division_leaders"] = config["top_postseason"]
    new_league.constraints["wild_cards"] = config["wildcards"]
    new_league.generate_schedule()
    leagues.save_league(new_league)

    return jsonify({'status':'success_league_created'})