Пример #1
0
 def post(self):
     """
         POST request for Bats List
         Route: Routes['bat']
         Parameters :
             game_id: the id of the game (int)
             player_id: the id of the batter (int)
             rbi: the number of runs batted in (int)
             hit: the type of hit (string)
             inning: the inning the hit occurred (int)
             team_id: the id of the team (int)
         Returns:
             if successful
                 status: 200
                 mimetype: application/json
                 data: the created bat id (int)
             otherwise possible errors
                 status: 400, GDNESC, PDNESC, TDNESC
                 mimetype: application/json
                 data: None
     """
     # create a new bat
     args = post_parser.parse_args()
     game_id = None
     player_id = None
     team_id = None
     rbi = 0
     hit = None
     inning = 1  # just assume some first inning
     if args['game_id']:
         game_id = args['game_id']
     if args['player_id']:
         player_id = args['player_id']
     if args['team_id']:
         team_id = args['team_id']
     if args['hit']:
         hit = args['hit']
     if args['rbi']:
         rbi = args['rbi']
     if args['inning']:
         inning = args['inning']
     bat = Bat(player_id,
               team_id,
               game_id,
               hit,
               inning=inning,
               rbi=rbi)
     DB.session.add(bat)
     DB.session.commit()
     bat_id = bat.id
     resp = Response(dumps(bat_id), status=201, mimetype="application/json")
     handle_table_change(Tables.BAT, item=bat.json())
     return resp
def submit_score(game_id: int, captain_id: int, score: int,
                 homeruns: List[int], ss: List[int]) -> bool:
    """Captain submit a score"""
    unassigned_player = Player.query.filter_by(email=UNASSIGNED_EMAIL).first()
    unassigned_id = UNASSIGNED
    if unassigned_player is not None:
        unassigned_id = unassigned_player.id
    game = Game.query.get(game_id)
    captain = Player.query.get(captain_id)
    if captain is None:
        raise PlayerNotSubscribed(payload={'details': captain_id})
    if game is None:
        raise GameDoesNotExist(payload={'details': game_id})
    # find the team
    away_team = Team.query.get(game.away_team_id)
    home_team = Team.query.get(game.home_team_id)
    team = None
    if away_team.player_id == captain.id:
        team = away_team  # captain of the squad
    elif home_team.player_id == captain.id:
        team = home_team  # captain of the away squad
    else:
        # not a captain of a team
        raise NotTeamCaptain(payload={'details': captain_id})
    if score <= 0:
        # hmm that is so sad
        DB.session.add(
            Bat(unassigned_id, team.id, game.id, "fo", inning=1, rbi=0))
    if homeruns is not None:
        for player_id in homeruns:
            # add the homeruns
            DB.session.add(
                Bat(player_id, team.id, game.id, "hr", inning=1, rbi=1))
            score -= 1
    if ss is not None:
        for player_id in ss:
            # add the special singles
            try:
                bat = Bat(player_id, team.id, game.id, "ss", inning=1, rbi=0)
                DB.session.add(bat)
            except InvalidField:
                pass
    if score < 0:
        raise InvalidField(payload={'details': "More hr than score"})
    while score > 0:
        bat = Bat(unassigned_id, team.id, game.id, "s", inning=1, rbi=1)
        DB.session.add(bat)
        score -= 1
    DB.session.commit()  # good to add the submission
    handle_table_change(Tables.GAME)
    return True
Пример #3
0
def pull_bats(url, team_lookup, player_lookup, game_lookup):
    """
    pull_bats
        Returns a lookup of bats that were pulled
        from the website into local DB
        Parameters:
            url: the url of the main site
        Returns:
            a dictionary lookup
                for the website bat id to local bat object
                e.g. bat_lookup = {1: Bat(), etc..}
    """
    _bats = requests.get(url + "/api/bats").json()
    if isinstance(_bats, dict):
        _bats = pull_all_pages(url, _bats)
    bat_lookup = {}
    for bat in tqdm(_bats, desc="Pulling bats from {}".format(url)):
        temp = Bat(player_lookup[bat['player_id']].id,
                   team_lookup[bat['team_id']].id,
                   game_lookup[bat['game_id']].id, bat['hit'], bat['inning'],
                   bat['rbi'])
        bat_lookup[bat['bat_id']] = temp
        DB.session.add(temp)
    DB.session.commit()
    return bat_lookup
def captain_submit_full_game(team_id: int):
    """Submit a complete game batting information for some game"""
    bats = request.get_json(silent=True)
    result = submit_bats([
        Bat(bat.get('player_id', UNASSIGNED),
            bat.get('team_id'),
            bat.get('game_id'),
            bat.get('classification'),
            inning=bat.get('inning'),
            rbi=bat.get('rbi')) for bat in bats
    ])
    return Response(json.dumps(result),
                    status=200,
                    mimetype="application/json")
Пример #5
0
    def testBatInit(self):
        player_name = str(uuid.uuid1())
        email = player_name + "@mlsb.ca"
        player = self.add_player(player_name, email)
        player_id = player['player_id']
        sponsor = self.add_sponsor(str(uuid.uuid1()))
        league = self.add_league(str(uuid.uuid1()))
        division = self.add_division(str(uuid.uuid1()))
        home_team = self.add_team(str(uuid.uuid1()), sponsor, league,
                                  VALID_YEAR)
        home_team_id = home_team['team_id']
        away_team = self.add_team(str(uuid.uuid1()), sponsor, league,
                                  VALID_YEAR)
        game = self.add_game(getDateString(), getTimeString(), home_team,
                             away_team, league, division)
        game_id = game['game_id']

        # good bat
        Bat(player_id, home_team_id, game_id, "s", inning=1, rbi=1)

        # now for the bad stuff
        try:
            Bat(player_id, home_team_id, game_id, "XX", inning=1, rbi=1)
            self.assertEqual(True, False, "should raise invalid field")
        except InvalidField:
            pass
        try:
            Bat(player_id, home_team_id, game_id, "s", inning=-1, rbi=1)
            self.assertEqual(True, False, "should raise invalid field")
        except InvalidField:
            pass
        try:
            Bat(player_id, home_team_id, game_id, "s", inning=1, rbi=1000)
            self.assertEqual(True, False, "should raise invalid field")
        except InvalidField:
            pass
        try:
            Bat(INVALID_ID, home_team_id, game_id, "s", inning=1, rbi=1)
            self.assertEqual(True, False, "should raise no player")
        except PlayerDoesNotExist:
            pass
        try:
            Bat(player_id, INVALID_ID, game_id, "s", inning=1, rbi=1)
            self.assertEqual(True, False, "should raise no team")
        except TeamDoesNotExist:
            pass
        try:
            Bat(player_id, home_team_id, INVALID_ID, "s", inning=1, rbi=1)
            self.assertEqual(True, False, "should raise no league")
        except GameDoesNotExist:
            pass
Пример #6
0
    def testBatInit(self):
        player = self.add_player("ModelTestPlayer", "*****@*****.**")
        player_id = player['player_id']
        sponsor = self.add_sponsor("TestModelSponsor")
        league = self.add_league("TestModelLeague")
        home_team = self.add_team("TestModelHomeTeam", sponsor, league,
                                  VALID_YEAR)
        home_team_id = home_team['team_id']
        away_team = self.add_team("TestModelAwayTeam", sponsor, league,
                                  VALID_YEAR)
        game = self.add_game(self.d, self.t, home_team, away_team, league)
        game_id = game['game_id']

        # good bat
        Bat(player_id, home_team_id, game_id, "s", inning=1, rbi=1)

        # now for the bad stuff
        try:
            Bat(player_id, home_team_id, game_id, "XX", inning=1, rbi=1)
            self.assertEqual(True, False, "should raise invalid field")
        except InvalidField:
            pass
        try:
            Bat(player_id, home_team_id, game_id, "s", inning=-1, rbi=1)
            self.assertEqual(True, False, "should raise invalid field")
        except InvalidField:
            pass
        try:
            Bat(player_id, home_team_id, game_id, "s", inning=1, rbi=1000)
            self.assertEqual(True, False, "should raise invalid field")
        except InvalidField:
            pass
        try:
            Bat(INVALID_ID, home_team_id, game_id, "s", inning=1, rbi=1)
            self.assertEqual(True, False, "should raise no player")
        except PlayerDoesNotExist:
            pass
        try:
            Bat(player_id, INVALID_ID, game_id, "s", inning=1, rbi=1)
            self.assertEqual(True, False, "should raise no team")
        except TeamDoesNotExist:
            pass
        try:
            Bat(player_id, home_team_id, INVALID_ID, "s", inning=1, rbi=1)
            self.assertEqual(True, False, "should raise no league")
        except GameDoesNotExist:
            pass
Пример #7
0
def add_random_score(game_id, team_id, players):
    """Simulates a score by getting a random score and adding random bats."""
    score = random.randint(1, 15)
    while score > 0:
        batter = random_value_list(players)
        bat = random_value_list(HITS)
        rbis = 0
        if (bat.lower() == "s" or bat.lower() == "ss"):
            rbis = random.randint(0, 1)
        elif (bat.lower() == "d"):
            rbis = random.randint(0, 2)
        elif (bat.lower() == "hr"):
            rbis = random.randint(1, 4)

        # just make sure not submitting a guy hitting ss
        if (batter.gender.lower() == "m" and bat.lower() == "ss"):
            bat = "s"
        score = score - rbis
        DB.session.add(Bat(batter.id, team_id, game_id, bat, rbi=rbis))
    DB.session.commit()
Пример #8
0
 def testBatUpdate(self):
     self.insertGame()
     # good bat
     b =  Bat(1, 
              1, 
              1, 
              "s", 
              inning=1, 
              rbi=1)
     # now for the bad stuff
     try:
         b.update(player_id=1, 
                  team_id=1, 
                  game_id=1, 
                  hit="XX", 
                  inning=1, 
                  rbi=1)
         self.assertEqual(True, False, "should raise invalid field")
     except InvalidField:
         pass
     try:
         b.update(player_id=1, 
                  team_id=1, 
                  game_id=1, 
                  hit="s", 
                  inning=-1, 
                  rbi=1)
         self.assertEqual(True, False, "should raise invalid field")
     except InvalidField:
         pass
     try:
         b.update(player_id=1, 
                  team_id=1, 
                  game_id=1, 
                  hit="s", 
                  inning=1, 
                  rbi=1000)
         self.assertEqual(True, False, "should raise invalid field")
     except InvalidField:
         pass
     try:
         b.update(player_id=999, 
                  team_id=1, 
                  game_id=1, 
                  hit="s", 
                  inning=1, 
                  rbi=1)
         self.assertEqual(True, False, "should raise no player")
     except PlayerDoesNotExist:
         pass
     try:
         b.update(player_id=1, 
                  team_id=999, 
                  game_id=1, 
                  hit="s", 
                  inning=1, 
                  rbi=1)
         self.assertEqual(True, False, "should raise no team")
     except TeamDoesNotExist:
         pass
     try:
         b.update(player_id=1, 
                  team_id=1, 
                  game_id=999, 
                  hit="s", 
                  inning=1, 
                  rbi=1)
         self.assertEqual(True, False, "should raise no league")
     except GameDoesNotExist:
         pass
Пример #9
0
 def post(self):
     """
         POST request for submitting Score Summaries
         Route: Route['kiksubmitscore']
         Parameters:
             game_id: the game_id (int)
             player_id: the player_id of the captain (str)
             score: the score of the captains team (int)
             hr: a list of player's name who hit homeruns(List of str)
             ss: a list of player's name who hit sentry singles (List - str)
         Returns:
             status: 200
             mimetype: application/json
             data: True
     """
     unassigned_player = Player.query.filter_by(
         email=UNASSIGNED_EMAIL).first()
     unassigned_id = UNASSIGNED
     if unassigned_player is not None:
         unassigned_id = unassigned_player.id
     args = parser.parse_args()
     game_id = args['game_id']
     game = Game.query.get(game_id)
     player_id = args['player_id']
     captain = Player.query.get(player_id)
     if captain is None:
         raise PlayerNotSubscribed(payload={'details': player_id})
     if game is None:
         raise GameDoesNotExist(payload={'details': game_id})
     # find the team
     away_team = Team.query.get(game.away_team_id)
     home_team = Team.query.get(game.home_team_id)
     team = None
     if away_team.player_id == captain.id:
         team = away_team  # captain of the squad
     elif home_team.player_id == captain.id:
         team = home_team  # captain of the away squad
     else:
         # not a captain of a team
         raise NotTeamCaptain(payload={'details': kik})
     homeruns = args['hr']
     ss = args['ss']
     score = args['score']
     if score <= 0:
         # hmm that is so sad
         DB.session.add(
             Bat(unassigned_id, team.id, game.id, "fo", inning=1, rbi=0))
     if homeruns is not None:
         for player_id in homeruns:
             # add the homeruns
             DB.session.add(
                 Bat(player_id, team.id, game.id, "hr", inning=1, rbi=1))
             score -= 1
     if ss is not None:
         for player_id in ss:
             # add the special singles
             try:
                 bat = Bat(player_id,
                           team.id,
                           game.id,
                           "ss",
                           inning=1,
                           rbi=0)
                 DB.session.add(bat)
             except InvalidField:
                 pass
     if score < 0:
         raise InvalidField(payload={'details': "More hr than score"})
     while score > 0:
         bat = Bat(unassigned_id, team.id, game.id, "s", inning=1, rbi=1)
         DB.session.add(bat)
         score -= 1
     DB.session.commit()  # good to add the submission
     return Response(dumps(True), status=200, mimetype="application/json")