Skip to content

Commit

Permalink
Merge pull request #556 from WardBrian/use-teams.py-names
Browse files Browse the repository at this point in the history
Allow better name customization
  • Loading branch information
WardBrian authored Nov 5, 2024
2 parents 66ce2a8 + 583d3ba commit 3610166
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 93 deletions.
2 changes: 1 addition & 1 deletion colors/teams.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@
"b": 64
}
},
"oak": {
"ath": {
"home": {
"r": 2,
"g": 70,
Expand Down
13 changes: 7 additions & 6 deletions data/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import statsapi

import debug
from data import teams
from data.update import UpdateStatus
from data.delay_buffer import CircularQueue

Expand Down Expand Up @@ -84,14 +85,14 @@ def current_delay(self):
return (len(self._data_wait_queue) - 1) * GAME_UPDATE_RATE

def home_name(self):
return self._current_data["gameData"]["teams"]["home"]["teamName"]
return teams.TEAM_ID_NAME[self._current_data["gameData"]["teams"]["home"]["id"]]

def home_abbreviation(self):
return self._current_data["gameData"]["teams"]["home"]["abbreviation"]
return teams.TEAM_ID_ABBR[self._current_data["gameData"]["teams"]["home"]["id"]]

def home_record(self):
return self._current_data["gameData"]["teams"]["home"]["record"] or {}

def away_record(self):
return self._current_data["gameData"]["teams"]["away"]["record"] or {}

Expand All @@ -111,10 +112,10 @@ def pregame_weather(self):
return wx

def away_name(self):
return self._current_data["gameData"]["teams"]["away"]["teamName"]
return teams.TEAM_ID_NAME[self._current_data["gameData"]["teams"]["away"]["id"]]

def away_abbreviation(self):
return self._current_data["gameData"]["teams"]["away"]["abbreviation"]
return teams.TEAM_ID_ABBR[self._current_data["gameData"]["teams"]["away"]["id"]]

def status(self):
return self._status["detailedState"]
Expand Down
12 changes: 6 additions & 6 deletions data/schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def update(self, force=False) -> UpdateStatus:
# but this is fine, since self.games_live() is will work even if we don't do this update
games = live_games

if len(games) > 0:
if len(games) > 0:
self.current_idx %= len(games)

self._games = games
Expand All @@ -75,7 +75,7 @@ def __should_update(self):
def is_offday_for_preferred_team(self):
if self.config.preferred_teams:
return not any(
data.teams.TEAM_FULL[self.config.preferred_teams[0]] in [game["away_name"], game["home_name"]]
data.teams.TEAM_NAME_ID[self.config.preferred_teams[0]] in [game["away_id"], game["home_id"]]
for game in self.__all_games # only care if preferred team is actually in list
)
else:
Expand Down Expand Up @@ -128,12 +128,12 @@ def next_game(self):

def _game_index_for_preferred_team(self):
if self.config.preferred_teams:
team_name = data.teams.TEAM_FULL[self.config.preferred_teams[0]]
team_id = data.teams.TEAM_NAME_ID[self.config.preferred_teams[0]]
return next(
(
i
for i, game in enumerate(self._games)
if team_name in [game["away_name"], game["home_name"]] and status.is_live(game["status"])
if team_id in [game["away_id"], game["home_id"]] and status.is_live(game["status"])
),
-1, # no live games for preferred team
)
Expand All @@ -155,5 +155,5 @@ def __current_game(self):

@staticmethod
def __filter_list_of_games(games, filter_teams):
teams = [data.teams.TEAM_FULL[t] for t in filter_teams]
return list(game for game in games if set([game["away_name"], game["home_name"]]).intersection(set(teams)))
teams = set(data.teams.TEAM_NAME_ID[t] for t in filter_teams)
return list(game for game in games if set([game["away_id"], game["home_id"]]).intersection(teams))
19 changes: 9 additions & 10 deletions data/standings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


API_FIELDS = (
"records,standingsType,teamRecords,team,abbreviation,division,league,nameShort,gamesBack,wildCardGamesBack,"
"records,standingsType,teamRecords,team,id,abbreviation,division,league,nameShort,gamesBack,wildCardGamesBack,"
"wildCardEliminationNumber,clinched,wins,losses"
)

Expand Down Expand Up @@ -131,7 +131,7 @@ def __init__(self, data, wc=False):

class Team:
def __init__(self, data, wc):
self.team_abbrev = data["team"]["abbreviation"]
self.team_abbrev = teams.TEAM_ID_ABBR[data["team"]["id"]]
self.w = data["wins"]
self.l = data["losses"] # noqa: E741
if wc:
Expand Down Expand Up @@ -183,9 +183,9 @@ def get_series_winner(self, data, ID):
else:
champ = "TBD"
if game["teams"]["home"].get("isWinner"):
champ = League.get_abbr(game["teams"]["home"]["team"]["name"])
champ = get_abbr(game["teams"]["home"]["team"]["id"])
elif game["teams"]["away"].get("isWinner"):
champ = League.get_abbr(game["teams"]["away"]["team"]["name"])
champ = get_abbr(game["teams"]["away"]["team"]["id"])
return champ

@staticmethod
Expand All @@ -196,11 +196,11 @@ def get_seeds(data, ID):
if s["series"]["id"] == ID
)
higher, lower = (
series["games"][0]["teams"]["home"]["team"]["name"],
series["games"][0]["teams"]["away"]["team"]["name"],
series["games"][0]["teams"]["home"]["team"]["id"],
series["games"][0]["teams"]["away"]["team"]["id"],
)

return (League.get_abbr(higher), League.get_abbr(lower))
return (get_abbr(higher), get_abbr(lower))

def __str__(self):
return f"""{self.wc5} ---|
Expand All @@ -214,6 +214,5 @@ def __str__(self):
"""


@staticmethod
def get_abbr(name, default="TBD"):
return f"{teams.TEAM_ABBR_LN.get(name, default):>3}"
def get_abbr(id, default="TBD"):
return f"{teams.TEAM_ID_ABBR.get(id, default):>3}"
169 changes: 103 additions & 66 deletions data/teams.py
Original file line number Diff line number Diff line change
@@ -1,72 +1,109 @@
# the following were created like so
# import statsapi
# teams = statsapi.get('teams', {'sportIds':1})['teams']
#
# TEAM_FULL = {t['teamName']:t['name'] for t in teams}
# TEAM_ABBR_LN = {t['name']:t['abbreviation'] for t in teams}
# _teams = statsapi.get('teams', {'sportIds':1})['teams']
# TEAM_ID_ABBR = {t["id"]: t["abbreviation"] for t in _teams}
# TEAM_ID_NAME = {t["id"]: t["teamName"] for t in _teams}
# TEAM_NAME_ID = {t["teamName"]: t["id"] for t in _teams}

TEAM_FULL = {
"Athletics": "Athletics",
"Pirates": "Pittsburgh Pirates",
"Padres": "San Diego Padres",
"Mariners": "Seattle Mariners",
"Giants": "San Francisco Giants",
"Cardinals": "St. Louis Cardinals",
"Rays": "Tampa Bay Rays",
"Rangers": "Texas Rangers",
"Blue Jays": "Toronto Blue Jays",
"Twins": "Minnesota Twins",
"Phillies": "Philadelphia Phillies",
"Braves": "Atlanta Braves",
"White Sox": "Chicago White Sox",
"Marlins": "Miami Marlins",
"Yankees": "New York Yankees",
"Brewers": "Milwaukee Brewers",
"Angels": "Los Angeles Angels",
"D-backs": "Arizona Diamondbacks",
"Orioles": "Baltimore Orioles",
"Red Sox": "Boston Red Sox",
"Cubs": "Chicago Cubs",
"Reds": "Cincinnati Reds",
"Guardians": "Cleveland Guardians",
"Rockies": "Colorado Rockies",
"Tigers": "Detroit Tigers",
"Astros": "Houston Astros",
"Royals": "Kansas City Royals",
"Dodgers": "Los Angeles Dodgers",
"Nationals": "Washington Nationals",
"Mets": "New York Mets",
# Can be customized, but will require changing in colors/teams.json as well
TEAM_ID_ABBR = {
133: "ATH",
134: "PIT",
135: "SD",
136: "SEA",
137: "SF",
138: "STL",
139: "TB",
140: "TEX",
141: "TOR",
142: "MIN",
143: "PHI",
144: "ATL",
145: "CWS",
146: "MIA",
147: "NYY",
158: "MIL",
108: "LAA",
109: "AZ",
110: "BAL",
111: "BOS",
112: "CHC",
113: "CIN",
114: "CLE",
115: "COL",
116: "DET",
117: "HOU",
118: "KC",
119: "LAD",
120: "WSH",
121: "NYM",
}

TEAM_ABBR_LN = {
"Athletics": "ATH",
"Pittsburgh Pirates": "PIT",
"San Diego Padres": "SD",
"Seattle Mariners": "SEA",
"San Francisco Giants": "SF",
"St. Louis Cardinals": "STL",
"Tampa Bay Rays": "TB",
"Texas Rangers": "TEX",
"Toronto Blue Jays": "TOR",
"Minnesota Twins": "MIN",
"Philadelphia Phillies": "PHI",
"Atlanta Braves": "ATL",
"Chicago White Sox": "CWS",
"Miami Marlins": "MIA",
"New York Yankees": "NYY",
"Milwaukee Brewers": "MIL",
"Los Angeles Angels": "LAA",
"Arizona Diamondbacks": "AZ",
"Baltimore Orioles": "BAL",
"Boston Red Sox": "BOS",
"Chicago Cubs": "CHC",
"Cincinnati Reds": "CIN",
"Cleveland Guardians": "CLE",
"Colorado Rockies": "COL",
"Detroit Tigers": "DET",
"Houston Astros": "HOU",
"Kansas City Royals": "KC",
"Los Angeles Dodgers": "LAD",
"Washington Nationals": "WSH",
"New York Mets": "NYM",
# Can be customized
TEAM_ID_NAME = {
133: "Athletics",
134: "Pirates",
135: "Padres",
136: "Mariners",
137: "Giants",
138: "Cardinals",
139: "Rays",
140: "Rangers",
141: "Blue Jays",
142: "Twins",
143: "Phillies",
144: "Braves",
145: "White Sox",
146: "Marlins",
147: "Yankees",
158: "Brewers",
108: "Angels",
109: "D-backs",
110: "Orioles",
111: "Red Sox",
112: "Cubs",
113: "Reds",
114: "Guardians",
115: "Rockies",
116: "Tigers",
117: "Astros",
118: "Royals",
119: "Dodgers",
120: "Nationals",
121: "Mets",
}


# Can be customized, but names in the config.json file must match
TEAM_NAME_ID = {
"Athletics": 133,
"Pirates": 134,
"Padres": 135,
"Mariners": 136,
"Giants": 137,
"Cardinals": 138,
"Rays": 139,
"Rangers": 140,
"Blue Jays": 141,
"Twins": 142,
"Phillies": 143,
"Braves": 144,
"White Sox": 145,
"Marlins": 146,
"Yankees": 147,
"Brewers": 158,
"Angels": 108,
"D-backs": 109,
"Orioles": 110,
"Red Sox": 111,
"Cubs": 112,
"Reds": 113,
"Guardians": 114,
"Rockies": 115,
"Tigers": 116,
"Astros": 117,
"Royals": 118,
"Dodgers": 119,
"Nationals": 120,
"Mets": 121,
}
12 changes: 8 additions & 4 deletions tests/test_data_up_to_date.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ def test_status_complete(self):
def test_teams_complete(self):
teams = statsapi.get("teams", {"sportIds": 1})["teams"]

abbr_to_full = {t["teamName"]: t["name"] for t in teams}
self.assertEqual(abbr_to_full, data.teams.TEAM_FULL)
full_to_abbr = {t["name"]: t["abbreviation"] for t in teams}
self.assertEqual(full_to_abbr, data.teams.TEAM_ABBR_LN)
id_to_abbr = {t["id"]: t["abbreviation"] for t in teams}
self.assertEqual(id_to_abbr, data.teams.TEAM_ID_ABBR)

id_to_name = {t["id"]: t["teamName"] for t in teams}
self.assertEqual(id_to_name, data.teams.TEAM_ID_NAME)

name_to_id = {t["teamName"]: t["id"] for t in teams}
self.assertEqual(name_to_id, data.teams.TEAM_NAME_ID)

def test_pitches_complete(self):
pitches = set(p["code"] for p in statsapi.meta("pitchTypes"))
Expand Down

0 comments on commit 3610166

Please sign in to comment.