예제 #1
0
class MainRenderer:
    def __init__(self, matrix, data, sleepEvent):
        self.matrix = matrix
        self.data = data
        self.status = self.data.status
        self.refresh_rate = self.data.config.live_game_refresh_rate
        self.boards = Boards()
        self.sleepEvent = sleepEvent
        self.sog_display_frequency = data.config.sog_display_frequency
        self.alternate_data_counter = 1

    def render(self):
        while self.data.network_issues:
            Clock(self.data, self.matrix, self.sleepEvent, duration=60)
            self.data.refresh_data()

        while True:
            try:
                debug.info('Rendering...')
                self.data.refresh_data()
                if self.status.is_offseason(self.data.date()):
                    # Offseason (Show offseason related stuff)
                    debug.info("It's offseason")
                    self.__render_offday()
                else:
                    # Season.
                    if not self.data.config.live_mode:
                        debug.info(
                            "Live mode is off. Going through the boards")
                        self.__render_offday()
                    elif self.data.is_pref_team_offday():
                        debug.info("Your preferred teams are Off today")
                        self.__render_offday()
                    elif self.data.is_nhl_offday():
                        debug.info("There is no game in the NHL today")
                        self.__render_offday()
                    else:
                        debug.info("Game Day Wooooo")
                        self.__render_game_day()

            except AttributeError as e:
                debug.log(f"ERROR WHILE RENDERING: {e}")
                debug.log("Refreshing data in a minute")
                self.boards.fallback(self.data, self.matrix, self.sleepEvent)
                self.data.refresh_data()

    def __render_offday(self):
        while True:
            debug.log('PING !!! Render off day')
            if self.data._is_new_day():
                debug.info('This is a new day')
                return
            self.data.refresh_data()
            self.boards._off_day(self.data, self.matrix, self.sleepEvent)

    def __render_game_day(self):
        debug.info("Showing Game")
        # Initialize the scoreboard. get the current status at startup
        self.data.refresh_overview()
        self.scoreboard = Scoreboard(self.data.overview, self.data)
        self.away_score = self.scoreboard.away_team.goals
        self.home_score = self.scoreboard.home_team.goals
        self.sleepEvent.clear()

        while not self.sleepEvent.is_set():

            if self.data._is_new_day():
                debug.log('This is a new day')
                return

            # Display the pushbutton board
            if self.data.pb_trigger:
                debug.info(
                    'PushButton triggered in game day loop....will display ' +
                    self.data.config.pushbutton_state_triggered1 + ' board')
                self.data.pb_trigger = False
                #Display the board from the config
                self.boards._pb_board(self.data, self.matrix, self.sleepEvent)

            if self.status.is_live(self.data.overview.status):
                """ Live Game state """
                debug.info("Game is Live")
                self.scoreboard = Scoreboard(self.data.overview, self.data)
                self.check_new_goals()
                self.__render_live(self.scoreboard)
                if self.scoreboard.intermission:
                    debug.info("Main event is in Intermission")
                    # Show Boards for Intermission
                    self.draw_end_period_indicator()
                    #sleep(self.refresh_rate)
                    self.sleepEvent.wait(self.refresh_rate)
                    self.boards._intermission(self.data, self.matrix,
                                              self.sleepEvent)
                else:
                    self.sleepEvent.wait(self.refresh_rate)

            elif self.status.is_game_over(self.data.overview.status):
                debug.info("Game Over")
                self.scoreboard = Scoreboard(self.data.overview, self.data)
                self.__render_postgame(self.scoreboard)
                # sleep(self.refresh_rate)
                self.sleepEvent.wait(self.refresh_rate)

            elif self.status.is_final(self.data.overview.status):
                """ Post Game state """
                debug.info("FINAL")
                self.scoreboard = Scoreboard(self.data.overview, self.data)
                self.__render_postgame(self.scoreboard)
                #sleep(self.refresh_rate)
                self.sleepEvent.wait(self.refresh_rate)
                if self.data._next_game():
                    debug.info("moving to the next preferred game")
                    return
                self.boards._post_game(self.data, self.matrix, self.sleepEvent)

            elif self.status.is_scheduled(self.data.overview.status):
                """ Pre-game state """
                debug.info("Game is Scheduled")
                self.scoreboard = Scoreboard(self.data.overview, self.data)
                self.__render_pregame(self.scoreboard)
                #sleep(self.refresh_rate)
                self.sleepEvent.wait(self.refresh_rate)
                self.boards._scheduled(self.data, self.matrix, self.sleepEvent)

            elif self.status.is_irregular(self.data.overview.status):
                """ Pre-game state """
                debug.info("Game is irregular")
                self.scoreboard = Scoreboard(self.data.overview, self.data)
                self.__render_irregular(self.scoreboard)
                #sleep(self.refresh_rate)
                self.sleepEvent.wait(self.refresh_rate)
                self.boards._scheduled(self.data, self.matrix, self.sleepEvent)

            sleep(5)
            self.data.refresh_data()
            self.data.refresh_overview()
            if self.data.network_issues:
                self.matrix.network_issue_indicator()

    def __render_pregame(self, scoreboard):
        debug.info("Showing Pre-Game")
        self.matrix.clear()
        ScoreboardRenderer(self.data, self.matrix, scoreboard).render()

    def __render_postgame(self, scoreboard):
        debug.info("Showing Post-Game")
        self.matrix.clear()
        ScoreboardRenderer(self.data, self.matrix, scoreboard).render()
        self.draw_end_of_game_indicator()

    def __render_live(self, scoreboard):
        debug.info("Showing Main Event")
        self.matrix.clear()
        show_SOG = False
        if self.alternate_data_counter % self.sog_display_frequency == 0:
            show_SOG = True
        ScoreboardRenderer(self.data, self.matrix, scoreboard,
                           show_SOG).render()
        self.alternate_data_counter += 1

    def __render_irregular(self, scoreboard):
        debug.info("Showing Irregular")
        self.matrix.clear()
        ScoreboardRenderer(self.data, self.matrix, scoreboard).render()

    def check_new_goals(self):
        debug.log("Check new goal")
        pref_team_only = self.data.config.goal_anim_pref_team_only
        away_id = self.scoreboard.away_team.id
        away_name = self.scoreboard.away_team.name
        away_goals = self.scoreboard.away_team.goals
        away_score = self.away_score
        home_id = self.scoreboard.home_team.id
        home_name = self.scoreboard.home_team.name
        home_goals = self.scoreboard.home_team.goals
        home_score = self.home_score

        if away_score < away_goals:
            self.away_score = away_goals
            if away_id not in self.data.pref_teams and pref_team_only:
                return
            self._draw_goal(away_id, away_name)
        if home_score < home_goals:
            self.home_score = home_goals
            if home_id not in self.data.pref_teams and pref_team_only:
                return
            self._draw_goal(home_id, home_name)

    def _draw_goal(self, id, name):
        debug.info('Score by team: ' + name)

        # Get the list of gif's under the preferred and opposing directory
        preferred_gifs = glob.glob("assets/animations/preferred/*.gif")
        opposing_gifs = glob.glob("assets/animations/opposing/*.gif")

        # Set opposing team goal animation here
        filename = random.choice(opposing_gifs)
        debug.info("Opposing animation is: " + filename)
        if id in self.data.pref_teams:
            # Set your preferred team goal animation here
            filename = random.choice(preferred_gifs)
            debug.info("Preferred animation is: " + filename)

        im = Image.open(get_file(filename))

        # Set the frame index to 0
        frame_nub = 0

        self.matrix.clear()

        # Go through the frames
        x = 0
        while x is not 5:
            try:
                im.seek(frame_nub)
            except EOFError:
                x += 1
                frame_nub = 0
                im.seek(frame_nub)

            self.matrix.draw_image((0, 0), im, "center")
            self.matrix.render()

            frame_nub += 1
            sleep(0.1)

    def draw_end_period_indicator(self):
        """TODO: change the width depending how much time is left to the intermission"""
        color = self.matrix.graphics.Color(0, 255, 0)
        self.matrix.graphics.DrawLine(self.matrix.matrix, 24,
                                      self.matrix.height - 2, 40,
                                      self.matrix.height - 2, color)
        self.matrix.graphics.DrawLine(self.matrix.matrix, 23,
                                      self.matrix.height - 1, 41,
                                      self.matrix.height - 1, color)

    def draw_end_of_game_indicator(self):
        """TODO: change the width depending how much time is left to the intermission"""
        color = self.matrix.graphics.Color(255, 0, 0)
        self.matrix.graphics.DrawLine(self.matrix.matrix, 24,
                                      self.matrix.height - 2, 40,
                                      self.matrix.height - 2, color)
        self.matrix.graphics.DrawLine(self.matrix.matrix, 23,
                                      self.matrix.height - 1, 41,
                                      self.matrix.height - 1, color)
예제 #2
0
class MainRenderer:
    def __init__(self, matrix, data, sleepEvent):
        self.matrix = matrix
        self.data = data
        self.status = self.data.status
        self.refresh_rate = self.data.config.live_game_refresh_rate
        self.boards = Boards()
        self.sleepEvent = sleepEvent
        self.sog_display_frequency = data.config.sog_display_frequency
        self.alternate_data_counter = 1

    def render(self):
        while self.data.network_issues:
            Clock(self.data, self.matrix, self.sleepEvent, duration=60)
            self.data.refresh_data()

        while True:
            try:
                debug.info('Rendering...')
                self.data.refresh_data()
                if self.status.is_offseason(self.data.date()):
                    # Offseason (Show offseason related stuff)
                    debug.info("It's offseason")
                    self.__render_offday()
                else:
                    # Season.
                    if not self.data.config.live_mode:
                        debug.info(
                            "Live mode is off. Going through the boards")
                        self.__render_offday()
                    elif self.data.is_pref_team_offday():
                        debug.info("Your preferred teams are Off today")
                        self.__render_offday()
                    elif self.data.is_nhl_offday():
                        debug.info("There is no game in the NHL today")
                        self.__render_offday()
                    else:
                        debug.info("Game Day Wooooo")
                        self.__render_game_day()

            except AttributeError as e:
                debug.log(f"ERROR WHILE RENDERING: {e}")
                debug.log("Refreshing data in a minute")
                self.boards.fallback(self.data, self.matrix, self.sleepEvent)
                self.data.refresh_data()

    def __render_offday(self):
        while True:
            debug.log('PING !!! Render off day')
            if self.data._is_new_day():
                debug.info('This is a new day')
                return
            self.data.refresh_data()

            self.boards._off_day(self.data, self.matrix, self.sleepEvent)

    def __render_game_day(self):
        debug.info("Showing Game")
        # Initialize the scoreboard. get the current status at startup
        self.data.refresh_overview()
        self.scoreboard = Scoreboard(self.data.overview, self.data)
        self.away_score = self.scoreboard.away_team.goals
        self.home_score = self.scoreboard.home_team.goals
        # Cache to save goals and allow all the details to be collected on the API.
        self.goal_team_cache = []
        self.sleepEvent.clear()

        while not self.sleepEvent.is_set():

            if self.data._is_new_day():
                debug.log('This is a new day')
                return

            # Display the pushbutton board
            if self.data.pb_trigger:
                debug.info(
                    'PushButton triggered in game day loop....will display ' +
                    self.data.config.pushbutton_state_triggered1 + ' board')
                self.data.pb_trigger = False
                #Display the board from the config
                self.boards._pb_board(self.data, self.matrix, self.sleepEvent)

            # Display the Weather Alert board
            if self.data.wx_alert_interrupt:
                debug.info(
                    'Weather Alert triggered in game day loop....will display weather alert board'
                )
                self.data.wx_alert_interrupt = False
                #Display the board from the config
                self.boards._wx_alert(self.data, self.matrix, self.sleepEvent)

            if self.status.is_live(self.data.overview.status):
                """ Live Game state """
                debug.info("Game is Live")
                self.scoreboard = Scoreboard(self.data.overview, self.data)
                self.__render_live(self.scoreboard)
                if self.scoreboard.intermission:
                    debug.info("Main event is in Intermission")
                    # Show Boards for Intermission
                    self.draw_end_period_indicator()
                    self.sleepEvent.wait(self.refresh_rate)
                    self.boards._intermission(self.data, self.matrix,
                                              self.sleepEvent)
                else:
                    self.sleepEvent.wait(self.refresh_rate)

            elif self.status.is_game_over(self.data.overview.status):
                print(self.data.overview.status)
                debug.info("Game Over")
                self.scoreboard = Scoreboard(self.data.overview, self.data)
                self.__render_postgame(self.scoreboard)
                # sleep(self.refresh_rate)
                self.sleepEvent.wait(self.refresh_rate)

            elif self.status.is_final(self.data.overview.status):
                """ Post Game state """
                debug.info("FINAL")
                self.scoreboard = Scoreboard(self.data.overview, self.data)

                self.__render_postgame(self.scoreboard)

                self.sleepEvent.wait(self.refresh_rate)
                if self.data._next_game():
                    debug.info("moving to the next preferred game")
                    return
                if not self.goal_team_cache:
                    self.boards._post_game(self.data, self.matrix,
                                           self.sleepEvent)

            elif self.status.is_scheduled(self.data.overview.status):
                """ Pre-game state """
                debug.info("Game is Scheduled")
                self.scoreboard = Scoreboard(self.data.overview, self.data)
                self.__render_pregame(self.scoreboard)
                #sleep(self.refresh_rate)
                self.sleepEvent.wait(self.refresh_rate)
                self.boards._scheduled(self.data, self.matrix, self.sleepEvent)

            elif self.status.is_irregular(self.data.overview.status):
                """ Pre-game state """
                debug.info("Game is irregular")
                self.scoreboard = Scoreboard(self.data.overview, self.data)
                self.__render_irregular(self.scoreboard)
                #sleep(self.refresh_rate)
                self.sleepEvent.wait(self.refresh_rate)
                self.boards._scheduled(self.data, self.matrix, self.sleepEvent)

            sleep(5)
            self.data.refresh_data()
            self.data.refresh_overview()
            if self.data.network_issues:
                self.matrix.network_issue_indicator()

            if self.data.newUpdate and not self.data.config.clock_hide_indicators:
                self.matrix.update_indicator()

    def __render_pregame(self, scoreboard):
        debug.info("Showing Pre-Game")
        self.matrix.clear()
        ScoreboardRenderer(self.data, self.matrix, scoreboard).render()

    def __render_postgame(self, scoreboard):
        debug.info("Showing Post-Game")
        self.matrix.clear()
        ScoreboardRenderer(self.data, self.matrix, scoreboard).render()
        self.draw_end_of_game_indicator()

    def __render_live(self, scoreboard):
        debug.info("Showing Main Event")
        self.matrix.clear()
        show_SOG = False
        if self.alternate_data_counter % self.sog_display_frequency == 0:
            show_SOG = True
        ScoreboardRenderer(self.data, self.matrix, scoreboard,
                           show_SOG).render()
        self.alternate_data_counter += 1

    def __render_irregular(self, scoreboard):
        debug.info("Showing Irregular")
        self.matrix.clear()
        ScoreboardRenderer(self.data, self.matrix, scoreboard).render()

    def draw_end_period_indicator(self):
        """TODO: change the width depending how much time is left to the intermission"""
        color = self.matrix.graphics.Color(0, 255, 0)
        self.matrix.graphics.DrawLine(self.matrix.matrix,
                                      (self.matrix.width * .5) - 8,
                                      self.matrix.height - 2,
                                      (self.matrix.width * .5) + 8,
                                      self.matrix.height - 2, color)
        self.matrix.graphics.DrawLine(self.matrix.matrix,
                                      (self.matrix.width * .5) - 9,
                                      self.matrix.height - 1,
                                      (self.matrix.width * .5) + 9,
                                      self.matrix.height - 1, color)

    def draw_end_of_game_indicator(self):
        """TODO: change the width depending how much time is left to the intermission"""
        color = self.matrix.graphics.Color(255, 0, 0)
        self.matrix.graphics.DrawLine(self.matrix.matrix,
                                      (self.matrix.width * .5) - 8,
                                      self.matrix.height - 2,
                                      (self.matrix.width * .5) + 8,
                                      self.matrix.height - 2, color)
        self.matrix.graphics.DrawLine(self.matrix.matrix,
                                      (self.matrix.width * .5) - 9,
                                      self.matrix.height - 1,
                                      (self.matrix.width * .5) + 9,
                                      self.matrix.height - 1, color)
예제 #3
0
class MainRenderer:
    def __init__(self, matrix, data, sleepEvent):
        self.matrix = matrix
        self.data = data
        self.status = self.data.status
        self.refresh_rate = self.data.config.live_game_refresh_rate
        self.boards = Boards()
        self.sleepEvent = sleepEvent
        self.sog_display_frequency = data.config.sog_display_frequency
        self.alternate_data_counter = 1

    def render(self):
        if self.data.config.testing_mode:
            debug.info("Rendering in Testing Mode")
            while True:
                ScoreboardRenderer(self.data, self.matrix,
                                   Scoreboard(self.data.games[1],
                                              self.data)).render()
                self.matrix.render()
                sleep(15)
                debug.info("Testing Mode Refresh")

        if self.data.config.test_goal_animation:
            debug.info("Rendering in Testing Mode")
            while True:
                self._draw_goal_animation()
                self.matrix.render()
                sleep(1)

        while self.data.network_issues:
            Clock(self.data, self.matrix, self.sleepEvent, duration=60)
            self.data.refresh_data()

        while True:
            try:
                debug.info('Rendering...')

                if self.status.is_offseason(self.data.date()):
                    # Offseason (Show offseason related stuff)
                    debug.info("It's offseason")
                    self.__render_offday()
                elif self.data.config.testScChampions:
                    self.test_stanley_cup_champion(
                        self.data.config.testScChampions)

                else:
                    # Season.
                    if not self.data.config.live_mode:
                        debug.info(
                            "Live mode is off. Going through the boards")
                        self.__render_offday()
                    elif self.data.is_pref_team_offday():
                        debug.info("Your preferred teams are Off today")
                        self.__render_offday()
                    elif self.data.is_nhl_offday():
                        debug.info("There is no game in the NHL today")
                        self.__render_offday()
                    else:
                        debug.info("Game Day Wooooo")
                        self.__render_game_day()

                self.data.refresh_data()

            except AttributeError as e:
                debug.info(f"ERROR WHILE RENDERING: {e}")
                debug.info("Refreshing data in a minute")
                self.boards.fallback(self.data, self.matrix, self.sleepEvent)
                self.data.refresh_data()

    def __render_offday(self):
        while True:
            debug.info('PING !!! Render off day')
            if self.data._is_new_day():
                debug.info('This is a new day')
                return
            self.data.refresh_data()
            self.boards._off_day(self.data, self.matrix, self.sleepEvent)

    def __render_game_day(self):
        debug.info("Showing Game")
        # Initialize the scoreboard. get the current status at startup
        self.data.refresh_overview()
        self.scoreboard = Scoreboard(self.data.overview, self.data)
        self.away_score = self.scoreboard.away_team.goals
        self.home_score = self.scoreboard.home_team.goals
        # Cache to save goals and allow all the details to be collected on the API.
        self.goal_team_cache = []
        self.sleepEvent.clear()

        while not self.sleepEvent.is_set():

            if self.data._is_new_day():
                debug.log('This is a new day')
                return

            # Display the pushbutton board
            if self.data.pb_trigger:
                debug.info(
                    'PushButton triggered in game day loop....will display ' +
                    self.data.config.pushbutton_state_triggered1 + ' board')
                if not self.data.screensaver:
                    self.data.pb_trigger = False
                #Display the board from the config
                self.boards._pb_board(self.data, self.matrix, self.sleepEvent)

            # Display the Weather Alert board
            if self.data.wx_alert_interrupt:
                debug.info(
                    'Weather Alert triggered in game day loop....will display weather alert board'
                )
                self.data.wx_alert_interrupt = False
                #Display the board from the config
                self.boards._wx_alert(self.data, self.matrix, self.sleepEvent)

            # Display the screensaver board
            if self.data.screensaver:
                if not self.data.pb_trigger:
                    debug.info('Screensaver triggered in game day loop....')
                    #self.data.wx_alert_interrupt = False
                    #Display the board from the config
                    self.boards._screensaver(self.data, self.matrix,
                                             self.sleepEvent)
                else:
                    self.data.pb_trigger = False

            if self.status.is_live(self.data.overview.status):
                """ Live Game state """
                #blocks the screensaver from running if game is live
                self.data.screensaver_livegame = True
                debug.info("Game is Live")
                self.scoreboard = Scoreboard(self.data.overview, self.data)
                self.check_new_goals()
                self.__render_live(self.scoreboard)
                if self.scoreboard.intermission:
                    debug.info("Main event is in Intermission")
                    # Show Boards for Intermission
                    self.draw_end_period_indicator()
                    self.sleepEvent.wait(self.refresh_rate)
                    self.check_new_goals()
                    self.boards._intermission(self.data, self.matrix,
                                              self.sleepEvent)
                else:
                    self.sleepEvent.wait(self.refresh_rate)

            elif self.status.is_game_over(self.data.overview.status):
                print(self.data.overview.status)
                debug.info("Game Over")
                self.scoreboard = Scoreboard(self.data.overview, self.data)
                self.check_new_goals()
                if self.data.isPlayoff and self.stanleycup_round:
                    self.check_stanley_cup_champion()
                    if self.data.ScChampions_id:
                        StanleyCupChampions(self.data,
                                            self.data.ScChampions_id,
                                            self.matrix,
                                            self.sleepEvent).render()

                self.__render_postgame(self.scoreboard)

                self.sleepEvent.wait(self.refresh_rate)

            elif self.status.is_final(self.data.overview.status):
                """ Post Game state """
                debug.info("FINAL")
                self.scoreboard = Scoreboard(self.data.overview, self.data)
                self.check_new_goals()
                if self.data.isPlayoff and self.stanleycup_round:
                    self.check_stanley_cup_champion()
                    if self.data.ScChampions_id:
                        StanleyCupChampions(self.data, self.matrix,
                                            self.sleepEvent).render()
                self.__render_postgame(self.scoreboard)

                self.sleepEvent.wait(self.refresh_rate)
                if self.data._next_game():
                    debug.info("moving to the next preferred game")
                    return
                if not self.goal_team_cache:
                    self.boards._post_game(self.data, self.matrix,
                                           self.sleepEvent)

            elif self.status.is_scheduled(self.data.overview.status):
                """ Pre-game state """
                debug.info("Game is Scheduled")
                self.scoreboard = Scoreboard(self.data.overview, self.data)
                self.__render_pregame(self.scoreboard)
                #sleep(self.refresh_rate)
                self.sleepEvent.wait(self.refresh_rate)
                self.boards._scheduled(self.data, self.matrix, self.sleepEvent)

            elif self.status.is_irregular(self.data.overview.status):
                """ Pre-game state """
                debug.info("Game is irregular")
                self.scoreboard = Scoreboard(self.data.overview, self.data)
                self.__render_irregular(self.scoreboard)
                #sleep(self.refresh_rate)
                self.sleepEvent.wait(self.refresh_rate)
                self.boards._scheduled(self.data, self.matrix, self.sleepEvent)

            self.data.refresh_data()
            self.data.refresh_overview()
            if self.data.network_issues:
                self.matrix.network_issue_indicator()

            if self.data.newUpdate and not self.data.config.clock_hide_indicators:
                self.matrix.update_indicator()

    def __render_pregame(self, scoreboard):
        debug.info("Showing Pre-Game")
        self.matrix.clear()
        ScoreboardRenderer(self.data, self.matrix, scoreboard).render()

    def __render_postgame(self, scoreboard):
        debug.info("Showing Post-Game")
        self.matrix.clear()
        ScoreboardRenderer(self.data, self.matrix, scoreboard).render()
        self.draw_end_of_game_indicator()

    def __render_live(self, scoreboard):
        debug.info("Showing Main Event")
        self.matrix.clear()
        show_SOG = False
        if self.alternate_data_counter % self.sog_display_frequency == 0:
            show_SOG = True
        ScoreboardRenderer(self.data, self.matrix, scoreboard,
                           show_SOG).render()
        self.alternate_data_counter += 1

    def __render_irregular(self, scoreboard):
        debug.info("Showing Irregular")
        self.matrix.clear()
        ScoreboardRenderer(self.data, self.matrix, scoreboard).render()

    def check_new_goals(self):
        debug.log("Check new goal")

        pref_team_only = self.data.config.goal_anim_pref_team_only
        away_id = self.scoreboard.away_team.id
        away_name = self.scoreboard.away_team.name
        away_goals = self.scoreboard.away_team.goals
        away_score = self.away_score
        home_id = self.scoreboard.home_team.id
        home_name = self.scoreboard.home_team.name
        home_goals = self.scoreboard.home_team.goals
        home_score = self.home_score
        # Display goal details that are cached if there is any
        # GoalRenderer(self.data, self.matrix, self.sleepEvent, self.scoreboard.away_team).render()
        if self.goal_team_cache:
            try:
                while self.goal_team_cache:
                    # create a goal object first to see if there are any missing data
                    if self.goal_team_cache[0] == "away":
                        GoalRenderer(self.data, self.matrix, self.sleepEvent,
                                     self.scoreboard.away_team).render()
                    else:
                        GoalRenderer(self.data, self.matrix, self.sleepEvent,
                                     self.scoreboard.home_team).render()
                    # Remove the first cached goal
                    self.goal_team_cache.pop(0)
            except IndexError:
                debug.error(
                    "The scoreboard object failed to get the goal details, trying on the next data refresh"
                )
            except KeyError:
                debug.error(
                    "Last Goal is a No goal. Or the API is missing some information."
                )
                self.goal_team_cache.pop(0)

        if away_score < away_goals:
            self.away_score = away_goals
            self.goal_team_cache.append("away")
            if away_id not in self.data.pref_teams and pref_team_only:
                return
            # run the goal animation
            self._draw_goal_animation(away_id, away_name)

        if home_score < home_goals:
            self.home_score = home_goals
            self.goal_team_cache.append("home")
            if home_id not in self.data.pref_teams and pref_team_only:
                return
            # run the goal animation
            self._draw_goal_animation(home_id, home_name)

    def _draw_goal_animation(self, id=14, name="test"):
        debug.info('Score by team: ' + name)
        preferred_team_only = self.data.config.goal_anim_pref_team_only
        # Get the list of gif's under the preferred and opposing directory
        all_gifs = glob.glob("assets/animations/goal/all/*.gif")
        preferred_gifs = glob.glob("assets/animations/goal/preferred/*.gif")
        opposing_gifs = glob.glob("assets/animations/goal/opposing/*.gif")

        filename = "assets/animations/goal_light_animation.gif"

        # Use alternate animations if there is any in the respective folder
        if all_gifs:
            # Set opposing team goal animation here
            filename = random.choice(all_gifs)
            debug.info("General animation is: " + filename)

        if opposing_gifs and not preferred_team_only:
            # Set opposing team goal animation here
            filename = random.choice(opposing_gifs)
            debug.info("Opposing animation is: " + filename)

        if id in self.data.pref_teams and preferred_gifs:
            # Set your preferred team goal animation here
            filename = random.choice(preferred_gifs)
            debug.info("Preferred animation is: " + filename)

        im = Image.open(get_file(filename))

        # Set the frame index to 0
        frame_nub = 0

        self.matrix.clear()

        # Go through the frames
        x = 0
        while x is not 5:
            try:
                im.seek(frame_nub)
            except EOFError:
                x += 1
                frame_nub = 0
                im.seek(frame_nub)

            self.matrix.draw_image(("50%", 0), im, "center")
            self.matrix.render()

            frame_nub += 1
            sleep(0.1)

    def draw_end_period_indicator(self):
        """TODO: change the width depending how much time is left to the intermission"""
        color = self.matrix.graphics.Color(0, 255, 0)
        self.matrix.graphics.DrawLine(self.matrix.matrix,
                                      (self.matrix.width * .5) - 8,
                                      self.matrix.height - 2,
                                      (self.matrix.width * .5) + 8,
                                      self.matrix.height - 2, color)
        self.matrix.graphics.DrawLine(self.matrix.matrix,
                                      (self.matrix.width * .5) - 9,
                                      self.matrix.height - 1,
                                      (self.matrix.width * .5) + 9,
                                      self.matrix.height - 1, color)

    def draw_end_of_game_indicator(self):
        """TODO: change the width depending how much time is left to the intermission"""
        color = self.matrix.graphics.Color(255, 0, 0)
        self.matrix.graphics.DrawLine(self.matrix.matrix,
                                      (self.matrix.width * .5) - 8,
                                      self.matrix.height - 2,
                                      (self.matrix.width * .5) + 8,
                                      self.matrix.height - 2, color)
        self.matrix.graphics.DrawLine(self.matrix.matrix,
                                      (self.matrix.width * .5) - 9,
                                      self.matrix.height - 1,
                                      (self.matrix.width * .5) + 9,
                                      self.matrix.height - 1, color)

    def test_stanley_cup_champion(self, team_id):
        StanleyCupChampions(self.data, team_id, self.matrix,
                            self.sleepEvent).render()
예제 #4
0
class MainRenderer:
    def __init__(self, matrix, data, sleepEvent):
        self.matrix = matrix
        self.data = data
        self.status = self.data.status
        self.refresh_rate = self.data.config.live_game_refresh_rate
        self.boards = Boards()
        self.sleepEvent = sleepEvent
        self.sog_display_frequency = data.config.sog_display_frequency
        self.alternate_data_counter = 1
        self.last_home_name = "Warpigs"

    def render(self):
        if self.data.config.testing_mode:
            debug.info("Rendering in Testing Mode")
            while True:
                self.data.refresh_overview()
                self.scoreboard = Scoreboard(self.data.overview, self.data)
                ScoreboardRenderer(self.data, self.matrix,
                                   Scoreboard(self.data.games[0],
                                              self.data)).render()
                #self._draw_event_animation("goal", self.scoreboard.home_team.id, self.scoreboard.home_team.name)
                #PenaltyRenderer(self.data, self.matrix, self.sleepEvent, self.scoreboard.away_team).render()
                #TeamSummary(self.data, self.matrix, self.sleepEvent).render()
                sleep(1)
                debug.info("Testing Mode Refresh")

        if self.data.config.test_goal_animation:
            debug.info("Rendering in Testing Mode")
            while True:
                self._draw_event_animation("goal", id=9)

        while self.data.network_issues:
            Clock(self.data, self.matrix, self.sleepEvent, duration=60)
            self.data.refresh_data()

        while True:
            debug.info('Rendering...')
            #if self.status.is_offseason(self.data.date()):
            # Offseason (Show offseason related stuff)
            #debug.info("It's offseason")
            #self.__render_offday()
            if self.data.config.testScChampions:
                self.test_stanley_cup_champion(
                    self.data.config.testScChampions)

            else:
                # Season.
                if not self.data.config.live_mode:
                    debug.info("Live mode is off. Going through the boards")
                    self.__render_offday()
                elif self.data.is_pref_team_offday():
                    debug.info("Your preferred teams are Off today")
                    self.__render_offday()
                elif self.data.is_nhl_offday():
                    debug.info("There is no game in the NHL today")
                    self.__render_offday()
                else:
                    debug.info("Game Day Wooooo")
                    self.__render_game_day()

            self.data.refresh_data()

    def __render_offday(self):
        i = 0
        while True:
            debug.info('PING !!! Render off day')
            if self.data._is_new_day():
                debug.info('This is a new day')
                return
            self.boards._off_day(self.data, self.matrix, self.sleepEvent)

            if i >= 1:
                debug.info("off day data refresh")
                self.data.refresh_data()
                i = 0
                # Announce no game today to /dev/shm/getNHL.txt.1
                f = open('/dev/shm/getNHL.txt.1', 'w')
                f.write('NHL: No game today')
                f.close()
                subprocess_cmd = 'sudo -H -u pi bash -c "rsync /dev/shm/getNHL.txt.1 [email protected]:/dev/shm/"'
                subprocess.run([subprocess_cmd], shell=True)

            else:
                i += 1

    def __render_game_day(self):
        debug.info("Showing Game")
        # Initialize the scoreboard. get the current status at startup
        self.data.refresh_overview()
        self.scoreboard = Scoreboard(self.data.overview, self.data)
        self.away_score = self.scoreboard.away_team.goals
        self.home_score = self.scoreboard.home_team.goals
        self.away_penalties = self.scoreboard.away_team.penalties
        self.home_penalties = self.scoreboard.home_team.penalties

        # Announce a game today to /dev/shm/getNHL.txt.1
        home_name = self.scoreboard.home_team.name
        away_name = self.scoreboard.away_team.name
        stime = self.scoreboard.start_time
        f = open('/dev/shm/getNHL.txt.1', 'w')
        #f.write('Blues game today!')
        f.write('NHL: %s vs %s, %spm\n' % (home_name, away_name, stime))
        f.close()
        subprocess_cmd = 'sudo -H -u pi bash -c "rsync /dev/shm/getNHL.txt.1 [email protected]:/dev/shm/"'
        subprocess.run([subprocess_cmd], shell=True)

        # Cache to save goals and penalties and allow all the details to be collected on the API.
        self.goal_team_cache = []
        self.penalties_team_cache = []
        self.sleepEvent.clear()

        while not self.sleepEvent.is_set():

            if self.data._is_new_day():
                debug.log('This is a new day')
                return

            # Display the pushbutton board
            if self.data.pb_trigger:
                debug.info(
                    'PushButton triggered in game day loop....will display ' +
                    self.data.config.pushbutton_state_triggered1 + ' board')
                if not self.data.screensaver:
                    self.data.pb_trigger = False
                #Display the board from the config
                self.boards._pb_board(self.data, self.matrix, self.sleepEvent)

            # Display the Weather Alert board
            if self.data.wx_alert_interrupt:
                debug.info(
                    'Weather Alert triggered in game day loop....will display weather alert board'
                )
                self.data.wx_alert_interrupt = False
                #Display the board from the config
                self.boards._wx_alert(self.data, self.matrix, self.sleepEvent)

            # Display the screensaver board
            if self.data.screensaver:
                if not self.data.pb_trigger:
                    debug.info('Screensaver triggered in game day loop....')
                    #self.data.wx_alert_interrupt = False
                    #Display the board from the config
                    self.boards._screensaver(self.data, self.matrix,
                                             self.sleepEvent)
                else:
                    self.data.pb_trigger = False

            if self.status.is_live(self.data.overview.status):
                """ Live Game state """
                #blocks the screensaver from running if game is live
                self.data.screensaver_livegame = True
                debug.info("Game is Live")
                sbrenderer = ScoreboardRenderer(self.data, self.matrix,
                                                self.scoreboard)

                self.check_new_penalty()
                self.check_new_goals()
                self.__render_live(sbrenderer)
                if self.scoreboard.intermission:
                    debug.info("Main event is in Intermission")
                    # Show Boards for Intermission
                    self.draw_end_period_indicator()
                    self.sleepEvent.wait(self.refresh_rate)

                    self.check_new_penalty()
                    self.check_new_goals()
                    self.boards._intermission(self.data, self.matrix,
                                              self.sleepEvent)

                else:
                    self.sleepEvent.wait(self.refresh_rate)

            elif self.status.is_game_over(self.data.overview.status):
                debug.info("Game Over")
                sbrenderer = ScoreboardRenderer(self.data, self.matrix,
                                                self.scoreboard)
                self.check_new_goals()
                if self.data.isPlayoff and self.data.stanleycup_round:
                    self.check_stanley_cup_champion()
                    if self.data.ScChampions_id:
                        StanleyCupChampions(self.data, self.matrix,
                                            self.sleepEvent).render()

                self.__render_postgame(sbrenderer)

                self.sleepEvent.wait(self.refresh_rate)

            elif self.status.is_final(self.data.overview.status):
                """ Post Game state """
                debug.info("FINAL")
                sbrenderer = ScoreboardRenderer(self.data, self.matrix,
                                                self.scoreboard)
                self.check_new_goals()

                # Announce a game today to /dev/shm/getNHL.txt.1
                home_name = self.scoreboard.home_team.name
                away_name = self.scoreboard.away_team.name
                home_goals = self.scoreboard.home_team.goals
                away_goals = self.scoreboard.away_team.goals
                f = open('/dev/shm/getNHL.txt.1', 'w')
                #f.write('Blues game today!')
                f.write('%s %d, %s %d FINAL\n' %
                        (home_name, home_goals, away_name, away_goals))
                f.close()
                subprocess_cmd = 'sudo -H -u pi bash -c "rsync /dev/shm/getNHL.txt.1 [email protected]:/dev/shm/"'
                subprocess.run([subprocess_cmd], shell=True)

                if self.data.isPlayoff and self.data.stanleycup_round:
                    self.check_stanley_cup_champion()
                    if self.data.ScChampions_id:
                        StanleyCupChampions(self.data, self.matrix,
                                            self.sleepEvent).render()
                self.__render_postgame(sbrenderer)

                self.sleepEvent.wait(self.refresh_rate)
                if not self.goal_team_cache:
                    self.boards._post_game(self.data, self.matrix,
                                           self.sleepEvent)

            elif self.status.is_scheduled(self.data.overview.status):
                """ Pre-game state """
                debug.info("Game is Scheduled")
                sbrenderer = ScoreboardRenderer(self.data, self.matrix,
                                                self.scoreboard)
                self.__render_pregame(sbrenderer)
                #sleep(self.refresh_rate)
                self.sleepEvent.wait(self.refresh_rate)
                self.boards._scheduled(self.data, self.matrix, self.sleepEvent)
                #announce upcoming game
                home_name = self.scoreboard.home_team.name
                away_name = self.scoreboard.away_team.name
                stime = self.scoreboard.start_time
                f = open('/dev/shm/getNHL.txt.1', 'w')
                f.write('NHL: %s vs %s, %spm\n' %
                        (home_name, away_name, stime))
                f.close()
                subprocess_cmd = 'sudo -H -u pi bash -c "rsync /dev/shm/getNHL.txt.1 [email protected]:/dev/shm/"'
                subprocess.run([subprocess_cmd], shell=True)

            elif self.status.is_irregular(self.data.overview.status):
                """ Pre-game state """
                debug.info("Game is irregular")
                sbrenderer = ScoreboardRenderer(self.data, self.matrix,
                                                self.scoreboard)
                self.__render_irregular(sbrenderer)
                #sleep(self.refresh_rate)
                self.sleepEvent.wait(self.refresh_rate)
                self.boards._scheduled(self.data, self.matrix, self.sleepEvent)

            self.data.refresh_data()
            self.data.refresh_overview()
            self.scoreboard = Scoreboard(self.data.overview, self.data)
            if self.data.network_issues:
                self.matrix.network_issue_indicator()

            if self.data.newUpdate and not self.data.config.clock_hide_indicators:
                self.matrix.update_indicator()

    def __render_pregame(self, sbrenderer):
        debug.info("Showing Pre-Game")
        self.matrix.clear()
        sbrenderer.render()

    def __render_postgame(self, sbrenderer):
        debug.info("Showing Post-Game")
        self.matrix.clear()
        sbrenderer.render()
        self.draw_end_of_game_indicator()

    def __render_live(self, sbrenderer):
        debug.info("Showing Main Event")
        self.matrix.clear()
        sbrenderer.show_SOG = False
        if self.alternate_data_counter % self.sog_display_frequency == 0:
            sbrenderer.show_SOG = True
        sbrenderer.render()
        self.alternate_data_counter += 1

    def __render_irregular(self, sbrenderer):
        debug.info("Showing Irregular")
        self.matrix.clear()
        sbrenderer.render()

    def check_new_goals(self):
        debug.log("Check new goal")
        pref_team_only = self.data.config.goal_anim_pref_team_only
        away_id = self.scoreboard.away_team.id
        away_name = self.scoreboard.away_team.name
        away_goals = self.scoreboard.away_team.goals
        away_score = self.away_score
        home_id = self.scoreboard.home_team.id
        home_name = self.scoreboard.home_team.name
        home_goals = self.scoreboard.home_team.goals
        home_score = self.home_score
        # Display goal details that are cached if there is any
        # GoalRenderer(self.data, self.matrix, self.sleepEvent, self.scoreboard.away_team).render()
        if self.goal_team_cache:
            try:
                while self.goal_team_cache:
                    # create a goal object first to see if there are any missing data
                    if self.goal_team_cache[0] == "away":
                        GoalRenderer(self.data, self.matrix, self.sleepEvent,
                                     self.scoreboard.away_team).render()
                    else:
                        GoalRenderer(self.data, self.matrix, self.sleepEvent,
                                     self.scoreboard.home_team).render()
                    # Remove the first cached goal
                    self.goal_team_cache.pop(0)
            except IndexError:
                debug.error(
                    "The scoreboard object failed to get the goal details, trying on the next data refresh"
                )
            except KeyError:
                debug.error(
                    "Last Goal is a No goal. Or the API is missing some information."
                )
                self.goal_team_cache.pop(0)

        # Save home_name,home_goals,away_name,away_goals to /dev/shm/getNHL.txt.1
        f = open('/dev/shm/getNHL.txt.1', 'w')
        f.write('%s %d, %s %d\n' %
                (home_name, home_goals, away_name, away_goals))
        f.close()
        subprocess_cmd = 'sudo -H -u pi bash -c "rsync /dev/shm/getNHL.txt.1 [email protected]:/dev/shm/"'

        # Check if first run, or "team name" changed (aka switched to a different game)
        if self.last_home_name != home_name:
            subprocess.run([subprocess_cmd], shell=True)
            self.last_home_name = home_name

        if away_score < away_goals:
            subprocess.run([subprocess_cmd], shell=True)
            self.away_score = away_goals
            self.goal_team_cache.append("away")
            if away_id not in self.data.pref_teams and pref_team_only:
                return
            # run the goal animation
            self._draw_event_animation("goal", away_id, away_name)

        if home_score < home_goals:
            subprocess.run([subprocess_cmd], shell=True)
            self.home_score = home_goals
            self.goal_team_cache.append("home")
            if home_id not in self.data.pref_teams and pref_team_only:
                return
            # run the goal animation
            self._draw_event_animation("goal", home_id, home_name)

    def check_new_penalty(self):
        debug.log("Check new penalty")

        #pref_team_only = self.data.config.penalty_anim_pref_team_only
        away_id = self.scoreboard.away_team.id
        away_name = self.scoreboard.away_team.name
        away_data_penalties = self.scoreboard.away_team.penalties
        a_penalties = self.away_penalties
        home_id = self.scoreboard.home_team.id
        home_name = self.scoreboard.home_team.name
        home_data_penalties = self.scoreboard.home_team.penalties
        h_penalties = self.home_penalties
        # Display goal details that are cached if there is any
        if self.penalties_team_cache:
            try:
                while self.penalties_team_cache:
                    # create a goal object first to see if there are any missing data
                    if self.penalties_team_cache[0] == "away":
                        PenaltyRenderer(self.data, self.matrix,
                                        self.sleepEvent,
                                        self.scoreboard.away_team).render()
                    else:
                        PenaltyRenderer(self.data, self.matrix,
                                        self.sleepEvent,
                                        self.scoreboard.home_team).render()
                    # Remove the first cached goal
                    self.penalties_team_cache.pop(0)
            except IndexError:
                debug.error(
                    "The Penalty object failed to get the Penalty details, trying on the next data refresh"
                )
            except AttributeError:
                debug.error(
                    "The Penalty object failed to get the Penalty details, trying on the next data refresh"
                )

        if len(a_penalties) < len(away_data_penalties):
            self.away_penalties = away_data_penalties
            self.penalties_team_cache.append("away")
            #if away_id not in self.data.pref_teams: and pref_team_only:
            #    return
            # run the goal animation
            self._draw_event_animation("penalty", away_id, away_name)

        if len(h_penalties) < len(home_data_penalties):
            self.home_penalties = home_data_penalties
            self.penalties_team_cache.append("home")
            #if home_id not in self.data.pref_teams: #and pref_team_only:
            #    return
            # run the goal animation
            self._draw_event_animation("penalty", home_id, home_name)

    def _draw_event_animation(self, event, id=14, name="test"):
        preferred_team_only = self.data.config.goal_anim_pref_team_only
        # Get the list of gif's under the preferred and opposing directory
        ANIMATIONS = "assets/animations/{}".format(event)
        general_gifs = glob.glob("{}/general/*.gif".format(ANIMATIONS))
        preferred_gifs = glob.glob("{}/preferred/*.gif".format(ANIMATIONS))
        opposing_gifs = glob.glob("{}/opposing/*.gif".format(ANIMATIONS))

        if event == "goal":
            filename = "{}/goal_light_animation.gif".format(ANIMATIONS)
        elif event == "penalty":
            filename = "{}/penalty_animation.gif".format(ANIMATIONS)

        # Use alternate animations if there is any in the respective folder
        if general_gifs:
            # Set opposing team goal animation here
            filename = random.choice(general_gifs)
            debug.info("General animation is: " + filename)

        if opposing_gifs and not preferred_team_only:
            # Set opposing team goal animation here
            filename = random.choice(opposing_gifs)
            debug.info("Opposing animation is: " + filename)

        if id in self.data.pref_teams and preferred_gifs:
            # Set your preferred team goal animation here
            filename = random.choice(preferred_gifs)
            debug.info("Preferred animation is: " + filename)

        self.play_gif(filename)

    def play_gif(self, file):
        im = Image.open(get_file(file))

        # Set the frame index to 0
        frame_nub = 0
        # Set number of loop to 1 (if you want to play you animation more then once, change this variable)
        numloop = 1
        self.matrix.clear()

        # Go through the frames
        x = 0
        while x is not numloop:
            try:
                im.seek(frame_nub)
            except EOFError:
                x += 1
                if x == numloop:
                    return
                frame_nub = 0
                im.seek(frame_nub)

            self.matrix.draw_image(("50%", 0), im, "center")
            self.matrix.render()

            frame_nub += 1
            self.sleepEvent.wait(0.1)

    def draw_end_period_indicator(self):
        """TODO: change the width depending how much time is left to the intermission"""
        color = self.matrix.graphics.Color(0, 255, 0)
        self.matrix.graphics.DrawLine(self.matrix.matrix,
                                      (self.matrix.width * .5) - 8,
                                      self.matrix.height - 2,
                                      (self.matrix.width * .5) + 8,
                                      self.matrix.height - 2, color)
        self.matrix.graphics.DrawLine(self.matrix.matrix,
                                      (self.matrix.width * .5) - 9,
                                      self.matrix.height - 1,
                                      (self.matrix.width * .5) + 9,
                                      self.matrix.height - 1, color)

    def draw_end_of_game_indicator(self):
        """TODO: change the width depending how much time is left to the intermission"""
        color = self.matrix.graphics.Color(255, 0, 0)
        self.matrix.graphics.DrawLine(self.matrix.matrix,
                                      (self.matrix.width * .5) - 8,
                                      self.matrix.height - 2,
                                      (self.matrix.width * .5) + 8,
                                      self.matrix.height - 2, color)
        self.matrix.graphics.DrawLine(self.matrix.matrix,
                                      (self.matrix.width * .5) - 9,
                                      self.matrix.height - 1,
                                      (self.matrix.width * .5) + 9,
                                      self.matrix.height - 1, color)

    def test_stanley_cup_champion(self, team_id):
        self.data.ScChampions_id = team_id
        StanleyCupChampions(self.data, self.matrix, self.sleepEvent).render()