Adding a New Sport to pydfs_lineup_optimizer
Introduction
Now that we have established where the python files are located on our local machine for the pydfs-lineup-optimizer and have at least a basic understanding of the file structure, we are going to actually dig in and walk through how to add a new sport to the optimizer.
The sport we are going to be adding into the optimizer today will be the eSport DOTA 2 for DraftKings. The lineup optimizer does not currently have the lineup rules for DOTA built in (at least at the time of writing this), but DraftKings has recently started running Dota tournaments at the last major tournament and regional qualifiers for the upcoming world championships.
First up though, we're going to go ahead and attempt to run the optimizer over the DOTA players list using the LOL settings, and trying to push throught DOTA as a sport just to set the baseline of it not working as it.
Mimic Existing Sport
The first step in adding a new sport is identifying which sport the roster rules are most similar to, as we will be adapting an existing sport in this example. For DOTA, the closest existing ruleset is going to be League of Legends. The rules are slightly different (different position names and different numbers of each). DOTA is played in captains mode and on Draftkings, this is enough information to get us started.
First thing we need to do is walk through the optimizer and identify what happens when a LOL lineup is created, so we can make sure we are making the necessary changes.
If we start in the init.py file in the parent directory, we can see that what functions are being called, and where they are being imported from within the directory:
Import Statements
from pydfs_lineup_optimizer.version import version
from pydfs_lineup_optimizer.constants import Site, Sport
from pydfs_lineup_optimizer.player import Player
from pydfs_lineup_optimizer.exceptions import LineupOptimizerException, LineupOptimizerIncorrectTeamName, LineupOptimizerIncorrectPositionName, LineupOptimizerIncorrectCSV
from pydfs_lineup_optimizer.lineup_optimizer import LineupOptimizer
from pydfs_lineup_optimizer.lineup import Lineup
from pydfs_lineup_optimizer.sites import SitesRegistry
from pydfs_lineup_optimizer.lineup_exporter import CSVLineupExporter, FantasyDraftCSVLineupExporter
from pydfs_lineup_optimizer.tz import set_timezone
from pydfs_lineup_optimizer.stacks import PlayersGroup, TeamStack, PositionsStack, Stack
from pydfs_lineup_optimizer.exposure_strategy import TotalExposureStrategy, AfterEachExposureStrategy
Function
def get_optimizer(site: str, sport: str, **kwargs) -> LineupOptimizer:
return LineupOptimizer(SitesRegistry.get_settings(site, sport), **kwargs)
The first line should look familiar to us, as that is the code we run to initialize the optimizer in our optimization notebooks. What is shown below tells us what the function is doing. It is running the LineupOptimizer function being imported from the lineup_optimizer.py file, and using the Site and Sport classes from the Constants.py file
Constants.py
Starting with the Constants.py file, we can see that this is merely a storage location for the constants that feed the Site and Sport classes, aka the different site and sport options to use in the optimizer. We don't need to make any changes to the Sites class because we will be utilizing the Draftkings Captains mode settings. However, we will need to add Dota to the Sports class. This addition tells the optimizer that when we feed the string 'DOTA' as the sport, we will be using the associated rules to the variable we define as DOTA
This doesn't change anything in the optimizer, just allows it to recognize DOTA as a sport, but if we tried to run it now ther would be no settings or rules in place for DOTA to run.
Now that Dota is officially a sport we can call when we initialize the optimizer, it's time to dig in and make sure the optimizer knows what to do when we call it.
We could go back and follow the chain of imports and function calls from the initial lineup optimizer function, but i'm going to save us some time as most of that is establishing the baseline variables to be overwritten based on the sport and site utilized. Since we will be using the DraftKings Captains Mode ruleset, we can go ahead and navigate to that folder under the Sites directory.
C:\Users\nfwya\Anaconda3\Lib\site-packages\pydfs_lineup_optimizer\sites\draftkings\captain_mode
If we open the settings.py file within the captain_mode directory, we can see that this is where the specific rules get defined
A little investigation here and we can see that initially the base settings defined for captains mode are just for one captain and the rest utility players. Football and Basketball we can see here are using the base settings, however League of Legends have some additional settings defined for it. This is why we will be mimicking the LOL settings for Dota because they will be very similar. No need to reinvent the wheel here.
Lets go ahead and copy and paste the LOL settings and start updating them for DOTA
The key difference her is the position requirements for DOTA, rather than having different positions for every spot on the roster, DOTA is just broken into 1 captain, 3 cores, 2 supports, and a Team. We still have the restriction of Max 4 players from one team, and at least 2 games.
You will want to make sure the positions you define here match how they are specified on your player list csv that you download from your contest provider. Otherwise, the optimizer will be unable to create a lineup.
Another note, fortunately the draftkings player lists are standardized, so we do not need to worry about creating a new csv_importer class to handle the data. If you are going to be utilizing a completely different site, you can either alter that player list to match the format and columns as the draftkings standard, or edit the csv_importer functions to be able to read it properly, similarly to how we just altered the site/sport settings to read in the Dota data.
One important thing to note, is while we are only specifying these settings, if we look back up towards the top at the class DraftKingsCaptainModeSettings(BaseSettings) we can see all of the settings we COULD specify here if we need to.
Now, we can go ahead and save all of our files and try running the optimizer over a Dota players list and see if it works!