Example #1
0
 def get(self, game_id):
     """
         GET request for Game Object matching given Game_id
         Route: Routes['game']/<game_id: int>
         Returns:
             if found
                 status: 200
                 mimetype: application/json
                 data:   {
                         home_team: string
                         home_team_id: int,
                         away_team: string
                         away_team_id: int,
                         date: string,
                         time: string,
                         league_id: int,
                         game_id: int,
                         status: string,
                         field: string
                            }
             otherwise
                 status: 404
                 mimetype: application/json
                 data:   None
     """
     # expose a single game
     entry = Game.query.get(game_id)
     if entry is None:
         raise GameDoesNotExist(payload={'details': game_id})
     response = Response(dumps(entry.json()),
                         status=200,
                         mimetype="application/json")
     return response
Example #2
0
 def put(self, game_id):
     """
         PUT request for game
         Route: Routes['game']/<game_id: int>
         Parameters :
             home_team_id: The home team id (int)
             away_team_id: The away team id (int)
             date: The date of the game with the format YYYY-MM-DD (string)
             time: The time of the game in the format HH:MM (string)
             league_id: The league this game belongs to (int),
             status: the game's status (string)
             field: the game's field (string)
         Returns:
             if found and successful
                 status: 200
                 mimetype: application/json
                 data: None
             otherwise possible errors
                 status: 404, IFSC, TDNESC, LDNESC
                 mimetype: application/json
                 data: None
     """
     game = Game.query.get(game_id)
     args = parser.parse_args()
     home_team_id = None
     away_team_id = None
     league_id = None
     date = None
     time = None
     field = None
     status = None
     if game is None:
         raise GameDoesNotExist(payload={'details': game_id})
     if args['home_team_id']:
         home_team_id = args['home_team_id']
     if args['away_team_id']:
         away_team_id = args['away_team_id']
     if args['date']:
         date = args['date']
     if args['time']:
         time = args['time']
     if args['field']:
         field = args['field']
     if args['status']:
         status = args['status']
     if args['league_id']:
         league_id = args['league_id']
     game.update(date=date,
                 time=time,
                 home_team_id=home_team_id,
                 away_team_id=away_team_id,
                 league_id=league_id,
                 status=status,
                 field=field)
     DB.session.commit()
     response = Response(dumps(None),
                         status=200,
                         mimetype="application/json")
     return response
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
Example #4
0
    def update(self,
               player_id=None,
               team_id=None,
               game_id=None,
               rbi=None,
               hit=None,
               inning=None):
        """Update an existing bat.

        Raises:
            TeamDoesNotExist
            GameDoesNotExist
            PlayerDoesNotExist
            InvalidField
        """
        if team_id is not None and Team.query.get(team_id) is not None:
            self.team_id = team_id
        elif team_id is not None:
            raise TeamDoesNotExist(payload={'details': team_id})
        if game_id is not None and Game.query.get(game_id) is not None:
            self.game_id = game_id
        elif game_id is not None:
            raise GameDoesNotExist(payload={'details': game_id})
        if player_id is not None and Player.query.get(player_id) is not None:
            self.player_id = player_id
        elif player_id is not None:
            raise PlayerDoesNotExist(payload={'details': player_id})
        if rbi is not None and rbi_validator(rbi):
            self.rbi = rbi
        elif rbi is not None:
            raise InvalidField(payload={'details': "Bat - rbi"})
        if hit is not None and hit_validator(hit):
            self.classification = hit
        elif hit is not None:
            raise InvalidField(payload={'details': "Bat - hit"})
        if inning is not None and inning_validator(inning):
            self.inning = inning
        elif inning is not None:
            raise InvalidField(payload={'details': "Bat - inning"})
Example #5
0
 def put(self, game_id):
     """
         PUT request for game
         Route: Routes['game']/<game_id: int>
         Parameters :
             home_team_id: The home team id (int)
             away_team_id: The away team id (int)
             date: The date of the game with the format YYYY-MM-DD (string)
             time: The time of the game in the format HH:MM (string)
             league_id: The league this game belongs to (int),
             status: the game's status (string)
             field: the game's field (string)
         Returns:
             if found and successful
                 status: 200
                 mimetype: application/json
                 data: None
             otherwise possible errors
                 status: 404, IFSC, TDNESC, LDNESC
                 mimetype: application/json
                 data: None
     """
     game = Game.query.get(game_id)
     args = parser.parse_args()
     if game is None:
         raise GameDoesNotExist(payload={'details': game_id})
     game.update(date=args.get('date', None),
                 time=args.get('time', None),
                 home_team_id=args.get('home_team_id', None),
                 away_team_id=args.get('away_team_id', None),
                 league_id=args.get('league_id', None),
                 division_id=args.get('division_id', None),
                 status=args.get('status', None),
                 field=args.get('field', None))
     DB.session.commit()
     response = Response(dumps(None), status=200,
                         mimetype="application/json")
     handle_table_change(Tables.GAME, item=game.json())
     return response
Example #6
0
 def delete(self, game_id):
     """
         DELETE request for Game
         Route: Routes['game']/<game_id: int>
         Returns:
             if found
                 status: 200
                 mimetype: application/json
                 data: None
             otherwise
                 status: 404
                 mimetype: application/json
                 data: None
     """
     game = Game.query.get(game_id)
     if game is None:
         raise GameDoesNotExist(payload={'details': game_id})
     DB.session.delete(game)
     DB.session.commit()
     response = Response(dumps(None),
                         status=200,
                         mimetype="application/json")
     return response
Example #7
0
    def __init__(self,
                 player_id,
                 team_id,
                 game_id,
                 classification,
                 inning=1,
                 rbi=0):
        """The constructor.

        Raises:
            PlayerDoesNotExist
            InvalidField
            TeamDoesNotExist
            GameDoesNotExist
        """
        # check for exceptions
        classification = classification.lower().strip()
        player = Player.query.get(player_id)
        if player is None:
            raise PlayerDoesNotExist(payload={'details': player_id})
        if not hit_validator(classification, player.gender):
            raise InvalidField(payload={'details': "Bat - hit"})
        if not rbi_validator(rbi):
            raise InvalidField(payload={'details': "Bat - rbi"})
        if not inning_validator(inning):
            raise InvalidField(payload={'details': "Bat - inning"})
        if Team.query.get(team_id) is None:
            raise TeamDoesNotExist(payload={'details': team_id})
        if Game.query.get(game_id) is None:
            raise GameDoesNotExist(payload={'details': game_id})
        # otherwise good and a valid object
        self.classification = classification
        self.rbi = rbi
        self.player_id = player_id
        self.team_id = team_id
        self.game_id = game_id
        self.inning = inning
Example #8
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")