示例#1
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)
示例#2
0
class ScoreboardRenderer:
    def __init__(self, data, matrix, scoreboard, shot_on_goal=False):
        self.data = data
        self.status = data.status
        self.layout = self.data.config.config.layout.get_board_layout('scoreboard')
        self.font = self.data.config.layout.font
        self.font_large = self.data.config.layout.font_large
        self.team_colors = data.config.team_colors
        self.scoreboard = scoreboard
        self.matrix = matrix
        self.show_SOG = shot_on_goal

        self.home_logo_renderer = LogoRenderer(
            self.matrix,
            data.config,
            self.layout.home_logo,
            self.scoreboard.home_team.abbrev,
            'scoreboard',
            'home'
        )
        self.away_logo_renderer = LogoRenderer(
            self.matrix,
            data.config,
            self.layout.away_logo,
            self.scoreboard.away_team.abbrev,
            'scoreboard',
            'away'
        )

    def render(self):
        self.matrix.clear()
        # bg_away = self.team_colors.color("{}.primary".format(self.scoreboard.away_team.id))
        # bg_home = self.team_colors.color("{}.primary".format(self.scoreboard.home_team.id))
        # self.matrix.draw_rectangle((0,0), (64,64), (bg_away['r'],bg_away['g'],bg_away['b']))
        # self.matrix.draw_rectangle((64,0), (128,64), (bg_home['r'],bg_home['g'],bg_home['b']))
        self.matrix.draw_rectangle((0,0), (32,32), (0,0,0))
        self.away_logo_renderer.render()
        self.matrix.draw_rectangle((32,0), (64,32), (0,0,0))
        self.home_logo_renderer.render()
        
        #self.matrix.draw.polygon([(37,0), (91,0), (80,64), (48,64)], fill=(0,0,0))
        #Work in progress. testing gradients
        gradient = Image.open(get_file('assets/images/64x32_scoreboard_center_gradient.png'))
        self.matrix.draw_image((32,0), gradient, align="center")
        
        if self.status.is_scheduled(self.scoreboard.status):
            self.draw_scheduled()

        if self.status.is_live(self.scoreboard.status):
            self.draw_live()

        if self.status.is_game_over(self.scoreboard.status):
            self.draw_final()

        if self.status.is_final(self.scoreboard.status):
            self.draw_final()

        if self.status.is_irregular(self.scoreboard.status):
            '''TODO: Need to figure out the irregular status'''
            self.draw_irregular()

    def draw_scheduled(self):
        start_time = self.scoreboard.start_time

        # Draw the text on the Data image.
        self.matrix.draw_text_layout(
          self.layout.scheduled_date, 
          'TODAY'
        )
        self.matrix.draw_text_layout(
          self.layout.scheduled_time, 
          start_time
        )

        self.matrix.draw_text_layout(
          self.layout.vs, 
          'VS'
        )


        self.matrix.render()

    def draw_live(self):
        # Get the Info
        period = self.scoreboard.periods.ordinal
        clock = self.scoreboard.periods.clock
        score = '{}-{}'.format(self.scoreboard.away_team.goals, self.scoreboard.home_team.goals)
        

        if self.show_SOG:
            self.draw_SOG()
            self.show_SOG = False
        else:
            # Draw the info
            self.matrix.draw_text_layout(
                self.layout.period,
                period,
            )
            self.matrix.draw_text_layout(
                self.layout.clock,
                clock
            )

        self.matrix.draw_text_layout(
            self.layout.score,
            score
        )

        self.matrix.render()
        if (3 <= self.scoreboard.away_team.num_skaters <= 4) or (3 <= self.scoreboard.home_team.num_skaters <= 4):
            self.draw_power_play()


    def draw_final(self):
        # Get the Info
        period = self.scoreboard.periods.ordinal
        result = self.scoreboard.periods.clock
        score = '{}-{}'.format(self.scoreboard.away_team.goals, self.scoreboard.home_team.goals)
        date = convert_date_format(self.scoreboard.date)

        # Draw the info
        self.matrix.draw_text_layout(
            self.layout.center_top, 
            date
        )

        end_text = result
        if self.scoreboard.periods.number > 3:
            end_text = "F/{}".format(period)

        self.matrix.draw_text_layout(
            self.layout.period_final, 
            end_text
        )

        self.matrix.draw_text_layout(
            self.layout.score, 
            score
        )

        self.matrix.render()

    def draw_irregular(self):
        status = self.scoreboard.status
        if status == "Postponed":
            status = "PPD"

        # Draw the text on the Data image.
        self.matrix.draw_text_layout(
            self.layout.center_top,
            'TODAY'
        )
        self.matrix.draw_text_layout(
            self.layout.irregular_status,
            status
        )
        self.matrix.draw_text_layout(
            self.layout.vs,
            'VS'
        )
        self.matrix.render()

    def draw_power_play(self):
        away_number_skaters = self.scoreboard.away_team.num_skaters
        home_number_skaters = self.scoreboard.home_team.num_skaters
        yellow = self.matrix.graphics.Color(255, 255, 0)
        red = self.matrix.graphics.Color(255, 0, 0)
        green = self.matrix.graphics.Color(0, 255, 0)
        colors = {"6": green, "5": green, "4": yellow, "3": red}

        self.matrix.graphics.DrawLine(self.matrix.matrix, 0, self.matrix.height - 1, 3, self.matrix.height - 1,
                                      colors[str(away_number_skaters)])
        self.matrix.graphics.DrawLine(self.matrix.matrix, 0, self.matrix.height - 2, 1, self.matrix.height - 2,
                                      colors[str(away_number_skaters)])
        self.matrix.graphics.DrawLine(self.matrix.matrix, 0, self.matrix.height - 3, 0, self.matrix.height - 3,
                                      colors[str(away_number_skaters)])

        self.matrix.graphics.DrawLine(self.matrix.matrix, self.matrix.width - 1, self.matrix.height - 1, self.matrix.width - 4,
                                      self.matrix.height - 1, colors[str(home_number_skaters)])
        self.matrix.graphics.DrawLine(self.matrix.matrix, self.matrix.width - 1, self.matrix.height - 2, self.matrix.width - 2,
                                      self.matrix.height - 2, colors[str(home_number_skaters)])
        self.matrix.graphics.DrawLine(self.matrix.matrix, self.matrix.width - 1, self.matrix.height - 3, self.matrix.width - 1,
                                      self.matrix.height - 3, colors[str(home_number_skaters)])

    def draw_SOG(self):

        # Draw the Shot on goal
        SOG = '{}-{}'.format(self.scoreboard.away_team.shot_on_goal, self.scoreboard.home_team.shot_on_goal)
        
        self.matrix.draw_text_layout(
            self.layout.SOG_label,
            "SHOTS"
        )
        self.matrix.draw_text_layout(
            self.layout.SOG,
            SOG
        )
class ScoreboardRenderer:
    def __init__(self, data, matrix, scoreboard, shot_on_goal=False):
        self.data = data
        self.status = data.status
        self.layout = self.data.config.config.layout.get_board_layout(
            'scoreboard')
        self.font = self.data.config.layout.font
        self.font_large = self.data.config.layout.font_large
        self.scoreboard = scoreboard
        self.matrix = matrix
        self.show_SOG = shot_on_goal

        self.home_logo_renderer = LogoRenderer(self.matrix, data.config,
                                               self.layout.home_logo,
                                               self.scoreboard.home_team,
                                               'scoreboard', 'home')
        self.away_logo_renderer = LogoRenderer(self.matrix, data.config,
                                               self.layout.away_logo,
                                               self.scoreboard.away_team,
                                               'scoreboard', 'away')

    def render(self):
        self.away_logo_renderer.render()
        self.home_logo_renderer.render()

        if self.status.is_scheduled(self.scoreboard.status):
            self.draw_scheduled()

        if self.status.is_live(self.scoreboard.status):
            self.draw_live()

        if self.status.is_final(self.scoreboard.status):
            self.draw_final()

        if self.status.is_irregular(self.scoreboard.status):
            '''TODO: Need to figure out the irregular status'''
            self.draw_irregular()

    def draw_scheduled(self):
        start_time = self.scoreboard.start_time

        # Draw the text on the Data image.
        self.matrix.draw_text_layout(self.layout.scheduled_date, 'TODAY')
        self.matrix.draw_text_layout(self.layout.scheduled_time, start_time)
        self.matrix.draw_text_layout(self.layout.vs, 'VS')

        self.matrix.render()

    def draw_live(self):
        # Get the Info
        period = self.scoreboard.periods.ordinal
        clock = self.scoreboard.periods.clock
        score = '{}-{}'.format(self.scoreboard.away_team.goals,
                               self.scoreboard.home_team.goals)

        if self.show_SOG:
            self.draw_SOG()
            self.show_SOG = False
        else:
            # Draw the info
            self.matrix.draw_text_layout(
                self.layout.period,
                period,
            )
            self.matrix.draw_text_layout(self.layout.clock, clock)

        self.matrix.draw_text_layout(self.layout.score, score)

        self.matrix.render()
        if (3 <= self.scoreboard.away_team.num_skaters <=
                4) or (3 <= self.scoreboard.home_team.num_skaters <= 4):
            self.draw_power_play()

    def draw_final(self):
        # Get the Info
        period = self.scoreboard.periods.ordinal
        result = self.scoreboard.periods.clock
        score = '{}-{}'.format(self.scoreboard.away_team.goals,
                               self.scoreboard.home_team.goals)
        date = convert_date_format(self.scoreboard.date)

        # Draw the info
        self.matrix.draw_text_layout(self.layout.scheduled_date, date)

        end_text = result
        if self.scoreboard.periods.number > 3:
            end_text = "F/{}".format(period)

        self.matrix.draw_text_layout(self.layout.period_final, end_text)

        self.matrix.draw_text_layout(self.layout.score, score)

        self.matrix.render()

    def draw_irregular(self):
        status = self.scoreboard.status
        if status == "Postponed":
            status = "PPD"

        # Draw the text on the Data image.
        self.matrix.draw_text_layout(self.layout.scheduled_date, 'TODAY')
        self.matrix.draw_text_layout(self.layout.irregular_status, status)
        self.matrix.draw_text_layout(self.layout.vs, 'VS')
        self.matrix.render()

    def draw_power_play(self):
        away_number_skaters = self.scoreboard.away_team.num_skaters
        home_number_skaters = self.scoreboard.home_team.num_skaters
        yellow = (255, 255, 0)
        red = (255, 0, 0)
        green = (0, 255, 0)
        colors = {"6": green, "5": green, "4": yellow, "3": red}

        self.matrix.draw_line(start_coords=(0, self.matrix.height - 1),
                              end_coords=(3, self.matrix.height - 1),
                              colour=colors[str(away_number_skaters)])

        self.matrix.draw_line(start_coords=(0, self.matrix.height - 2),
                              end_coords=(1, self.matrix.height - 2),
                              colour=colors[str(away_number_skaters)])

        self.matrix.draw_line(start_coords=(0, self.matrix.height - 3),
                              end_coords=(0, self.matrix.height - 3),
                              colour=colors[str(away_number_skaters)])

        self.matrix.draw_line(start_coords=(63, self.matrix.height - 1),
                              end_coords=(60, self.matrix.height - 1),
                              colour=colors[str(home_number_skaters)])

        self.matrix.draw_line(start_coords(63, self.matrix.height - 2),
                              end_coords=(62, self.matrix.height - 2),
                              colour=colors[str(home_number_skaters)])

        self.matrix.draw_line(start_coords=(63, self.matrix.height - 3),
                              end_coords=(63, self.matrix.height - 3),
                              colour=colors[str(home_number_skaters)])

    # def draw_power_play(self):
    #     away_number_skaters = self.scoreboard.away_team.num_skaters
    #     home_number_skaters = self.scoreboard.home_team.num_skaters
    #     yellow = self.matrix.graphics.Color(255, 255, 0)
    #     red = self.matrix.graphics.Color(255, 0, 0)
    #     green = self.matrix.graphics.Color(0, 255, 0)
    #     colors = {"6": green, "5": green, "4": yellow, "3": red}

    #     self.matrix.graphics.DrawLine(self.matrix.matrix, 0, self.matrix.height - 1, 3, self.matrix.height - 1,
    #                                   colors[str(away_number_skaters)])
    #     self.matrix.graphics.DrawLine(self.matrix.matrix, 0, self.matrix.height - 2, 1, self.matrix.height - 2,
    #                                   colors[str(away_number_skaters)])
    #     self.matrix.graphics.DrawLine(self.matrix.matrix, 0, self.matrix.height - 3, 0, self.matrix.height - 3,
    #                                   colors[str(away_number_skaters)])

    #     self.matrix.graphics.DrawLine(self.matrix.matrix, 63, self.matrix.height - 1, 60,
    #                                   self.matrix.height - 1, colors[str(home_number_skaters)])
    #     self.matrix.graphics.DrawLine(self.matrix.matrix, 63, self.matrix.height - 2, 62,
    #                                   self.matrix.height - 2, colors[str(home_number_skaters)])
    #     self.matrix.graphics.DrawLine(self.matrix.matrix, 63, self.matrix.height - 3, 63,
    #                                   self.matrix.height - 3, colors[str(home_number_skaters)])

    def draw_SOG(self):

        # Draw the Shot on goal
        SOG = '{}-{}'.format(self.scoreboard.away_team.shot_on_goal,
                             self.scoreboard.home_team.shot_on_goal)

        self.matrix.draw_text_layout(self.layout.SOG_label, "SHOTS")
        self.matrix.draw_text_layout(self.layout.SOG, SOG)