def get_game_overview(self, gameid):
     # Request the game overview
     overview = ""
     try:
         overview = nhl_api.overview(gameid)
     except:
         print(
             "failed overview refresh for series game id {}".format(gameid))
     self.game_overviews[gameid] = overview
     return overview
Exemple #2
0
 def refresh_overview(self):
     """
         Get a all the data of the main event.
     :return:
     """
     attempts_remaining = 5
     while attempts_remaining > 0:
         try:
             self.overview = nhl_api.overview(self.current_game_id)
             if self.time_stamp != self.overview.time_stamp:
                 self.time_stamp = self.overview.time_stamp
                 self.new_data = True
             self.needs_refresh = False
             self.network_issues = False
             break
         except ValueError as error_message:
             self.network_issues = True
             debug.error("Failed to refresh the Overview. {} attempt remaining.".format(attempts_remaining))
             debug.error(error_message)
             attempts_remaining -= 1
             sleep(NETWORK_RETRY_SLEEP_TIME)
Exemple #3
0
    def render(self):
        for team_id in self.preferred_teams:
            self.team_id = team_id

            team = self.teams_info[team_id]
            team_data = Team(team.team_id, team.abbreviation, team.name)

            team_colors = self.data.config.team_colors
            bg_color = team_colors.color("{}.primary".format(team_id))
            txt_color = team_colors.color("{}.text".format(team_id))
            prev_game = team.previous_game
            next_game = team.next_game

            logo_renderer = LogoRenderer(self.matrix, self.data.config,
                                         self.layout.logo, team_data,
                                         'team_summary')

            try:
                if prev_game:
                    prev_game_id = self.teams_info[
                        team_id].previous_game.dates[0]["games"][0]["gamePk"]
                    prev_game_scoreboard = Scoreboard(
                        nhl_api.overview(prev_game_id), self.data)
                else:
                    prev_game_scoreboard = False

                self.data.network_issues = False
            except ValueError:
                prev_game_scoreboard = False
                self.data.network_issues = True

            try:
                if next_game:
                    next_game_id = self.teams_info[team_id].next_game.dates[0][
                        "games"][0]["gamePk"]
                    next_game_scoreboard = Scoreboard(
                        nhl_api.overview(next_game_id), self.data)
                else:
                    next_game_scoreboard = False

                self.data.network_issues = False
            except ValueError:
                next_game_scoreboard = False
                self.data.network_issues = True

            stats = team.stats
            im_height = 67
            team_abbrev = team.abbreviation
            team_logo = Image.open(
                get_file('assets/logos/{}.png'.format(team_abbrev)))

            i = 0

            if not self.sleepEvent.is_set():
                image = self.draw_team_summary(stats, prev_game_scoreboard,
                                               next_game_scoreboard, bg_color,
                                               txt_color, im_height)

            self.matrix.clear()

            logo_renderer.render()

            self.matrix.draw_image_layout(
                self.layout.info,
                image,
            )

            self.matrix.render()
            if self.data.network_issues:
                self.matrix.network_issue_indicator()
            self.sleepEvent.wait(5)

            # Move the image up until we hit the bottom.
            while i > -(im_height -
                        self.matrix.height) and not self.sleepEvent.is_set():
                i -= 1

                self.matrix.clear()

                logo_renderer.render()
                self.matrix.draw_image_layout(self.layout.info, image, (0, i))

                self.matrix.render()
                if self.data.network_issues:
                    self.matrix.network_issue_indicator()
                self.sleepEvent.wait(0.3)

            # Show the bottom before we change to the next table.
            self.sleepEvent.wait(5)
    def render(self):
        for team_id in self.preferred_teams:
            self.team_id = team_id
            team_colors = self.data.config.team_colors
            bg_color = team_colors.color("{}.primary".format(team_id))
            txt_color = team_colors.color("{}.text".format(team_id))
            prev_game = self.teams_info[team_id].previous_game
            next_game = self.teams_info[team_id].next_game

            try:
                if prev_game:
                    prev_game_id = self.teams_info[team_id].previous_game.dates[0]["games"][0]["gamePk"]
                    prev_game_scoreboard = Scoreboard(nhl_api.overview(prev_game_id), self.teams_info, self.data.config)
                else:
                    prev_game_scoreboard = False

                self.data.network_issues = False
            except ValueError:
                prev_game_scoreboard = False
                self.data.network_issues = True

            try:
                if next_game:
                    next_game_id = self.teams_info[team_id].next_game.dates[0]["games"][0]["gamePk"]
                    next_game_scoreboard = Scoreboard(nhl_api.overview(next_game_id), self.teams_info, self.data.config)
                else:
                    next_game_scoreboard = False

                self.data.network_issues = False
            except ValueError:
                next_game_scoreboard = False
                self.data.network_issues = True

            stats = self.teams_info[team_id].stats
            im_height = 67
            team_abbrev = self.teams_info[team_id].abbreviation
            logo_coord = self.layout._get_summary_logo_coord(team_id)
            team_logo = Image.open(get_file('assets/logos/{}.png'.format(team_abbrev)))

            i = 0
            image = self.draw_team_summary(stats, prev_game_scoreboard, next_game_scoreboard, bg_color, txt_color,
                                           im_height)

            self.matrix.clear()
            self.matrix.draw_image((0, 0), image)
            self.matrix.draw_image((logo_coord["x"], logo_coord["y"]), team_logo.convert("RGB"))
            self.matrix.render()
            if self.data.network_issues:
                self.matrix.network_issue_indicator()
            sleep(5)

            # Move the image up until we hit the bottom.
            while i > -(im_height - self.matrix.height):
                i -= 1

                self.matrix.clear()
                self.matrix.draw_image((0, i), image)
                self.matrix.draw_image((logo_coord["x"], logo_coord["y"]), team_logo.convert("RGB"))
                self.matrix.render()
                if self.data.network_issues:
                    self.matrix.network_issue_indicator()
                sleep(0.3)
            # Show the bottom before we change to the next table.
            sleep(5)
Exemple #5
0
    def draw_series_table(self, series):

        color_top_bg = self.team_colors.color("{}.primary".format(
            series.top_team.id))
        color_top_team = self.team_colors.color("{}.text".format(
            series.top_team.id))

        color_bottom_bg = self.team_colors.color("{}.primary".format(
            series.bottom_team.id))
        color_bottom_team = self.team_colors.color("{}.text".format(
            series.bottom_team.id))

        # Table
        self.matrix.draw.line([(0, 21), (self.matrix.width, 21)],
                              width=1,
                              fill=(150, 150, 150))

        # use rectangle because I want to keep symmetry for the background of team's abbrev
        self.matrix.draw.rectangle([0, 14, 12, 20],
                                   fill=(color_top_bg['r'], color_top_bg['g'],
                                         color_top_bg['b']))
        self.matrix.draw_text((1, 15),
                              series.top_team.abbrev,
                              font=self.font,
                              fill=(color_top_team['r'], color_top_team['g'],
                                    color_top_team['b']))

        self.matrix.draw.rectangle([0, 22, 12, 28],
                                   fill=(color_bottom_bg['r'],
                                         color_bottom_bg['g'],
                                         color_bottom_bg['b']))
        self.matrix.draw_text(
            (1, 23),
            series.bottom_team.abbrev,
            font=self.font,
            fill=(color_bottom_team['r'], color_bottom_team['g'],
                  color_bottom_team['b']))

        rec_width = 0
        top_row = 15
        bottom_row = 23
        loosing_color = (150, 150, 150)

        # text offset for loosing score if the winning team has a score of 10 or higher and loosing team
        # have a score lower then 10
        """
            TODO: Grabbing all the games of a series cause delay up to 15 sec for certain users. I think its time to put all the data
            refresh into a thread and refresh everything from there
            . 
        """
        offset_correction = 0
        for game in series.games:
            attempts_remaining = 5
            while attempts_remaining > 0:
                try:
                    # Request the game overview
                    overview = nhl_api.overview(game["gameId"])

                    # get the scoreboard
                    scoreboard = Scoreboard(overview, self.data)

                    if self.data.status.is_final(overview.status) and hasattr(
                            scoreboard, "winning_team"):
                        if scoreboard.winning_team == series.top_team.id:
                            winning_row = top_row
                            loosing_row = bottom_row
                            winning_team_color = color_top_team
                            winning_bg_color = color_top_bg
                        else:
                            winning_row = bottom_row
                            loosing_row = top_row
                            winning_team_color = color_bottom_team
                            winning_bg_color = color_bottom_bg

                        # Look loosing score text needs an offset
                        if len(str(scoreboard.winning_score)) == 2 and len(
                                str(scoreboard.winning_score)) == 1:
                            offset_correction = 1

                        self.matrix.draw_text(
                            ((rec_width + 15 + offset_correction),
                             loosing_row),
                            str(scoreboard.loosing_score),
                            font=self.font,
                            fill=loosing_color,
                            backgroundColor=None,
                            backgroundOffset=[1, 1, 1, 1])

                        position = self.matrix.draw_text(
                            (rec_width + 15, winning_row),
                            str(scoreboard.winning_score),
                            font=self.font,
                            fill=(winning_team_color['r'],
                                  winning_team_color['g'],
                                  winning_team_color['b']),
                            backgroundColor=(winning_bg_color['r'],
                                             winning_bg_color['g'],
                                             winning_bg_color['b']),
                            backgroundOffset=[1, 1, 1, 1])

                        # Increment
                        rec_width += (position["size"][0] + 4)
                    break

                except ValueError as error_message:
                    self.data.network_issues = True
                    debug.error(
                        "Failed to get the Games for the {} VS {} series: {} attempts remaining"
                        .format(series.top_team.abbrev,
                                series.bottom_team.abbrev, attempts_remaining))
                    debug.error(error_message)
                    attempts_remaining -= 1
                    self.sleepEvent.wait(1)
                except KeyError as error_message:
                    debug.error(
                        "Failed to get the overview for game id {}. Data unavailable or is TBD"
                        .format(game["gameId"]))
                    debug.error(error_message)
                    break
            # If one of the request for player info failed after 5 attempts, return an empty dictionary
            if attempts_remaining == 0:
                return False