diff --git a/baps_types/plan.py b/baps_types/plan.py index 3c12286..b63054e 100644 --- a/baps_types/plan.py +++ b/baps_types/plan.py @@ -28,6 +28,7 @@ class PlanItem: _trackid: Optional[int] _managedid: Optional[int] _markers: List[Marker] = [] + _play_count: int @property def weight(self) -> int: @@ -53,6 +54,19 @@ class PlanItem: def filename(self, value: Optional[str]): self._filename = value + @property + def play_count(self) -> int: + return self._play_count + + def play_count_increment(self): + self._play_count += 1 + + def play_count_decrement(self): + self._play_count = max(0,self._play_count - 1) + + def play_count_reset(self): + self._play_count = 0 + @property def name(self) -> str: return ( @@ -129,7 +143,9 @@ class PlanItem: "intro": self.intro, "cue": self.cue, "outro": self.outro, - "markers": self.markers + "markers": self.markers, + "played": self.play_count > 0, + "play_count": self.play_count } def __init__(self, new_item: Dict[str, Any]): @@ -150,6 +166,7 @@ class PlanItem: self._markers = ( [Marker(marker) for marker in new_item["markers"]] if "markers" in new_item else [] ) + self._play_count = new_item["play_count"] if "play_count" in new_item else 0 # TODO: Edit this to handle markers when MyRadio supports them if "intro" in new_item and (isinstance(new_item["intro"], int) or isinstance(new_item["intro"], float)) and new_item["intro"] > 0: diff --git a/player.py b/player.py index bb6c8b4..1f2ee80 100644 --- a/player.py +++ b/player.py @@ -190,7 +190,8 @@ class Player: def unpause(self): if not self.isPlaying: - position: float = self.state.get()["pos_true"] + state = self.state.get() + position: float = state["pos_true"] try: self.play(position) except Exception: @@ -200,6 +201,13 @@ class Player: return False self.state.update("paused", False) + + # Increment Played count + loaded_item = state["loaded_item"] + if loaded_item: + loaded_item.play_count_increment() + self.state.update("loaded_item", loaded_item) + return True return False @@ -537,6 +545,20 @@ class Player: return success + def reset_played(self, weight: int): + plan: List[PlanItem] = self.state.get()["show_plan"] + if weight == -1: + for item in plan: + item.play_count_reset() + self.state.update("show_plan", plan) + elif len(plan) > weight: + plan[weight].play_count_reset() + self.state.update("show_plan", plan[weight], weight) + else: + return False + return True + + # Helper functions # This essentially allows the tracklist end API call to happen in a separate thread, to avoid hanging playout/loading. @@ -565,6 +587,13 @@ class Player: self.tracklist_start_timer.cancel() self.tracklist_start_timer = None + # Decrement Played count on track we didn't play much of. + state = self.state.get() + loaded_item = state["loaded_item"] + if loaded_item and loaded_item.type == "central": + loaded_item.play_count_decrement() + self.state.update("loaded_item", loaded_item) + # Make a copy of the tracklist_id, it will get reset as we load the next item. tracklist_id = self.state.get()["tracklist_id"] if not tracklist_id: @@ -900,6 +929,7 @@ class Player: ), "CLEAR": lambda: self._retMsg(self.clear_channel_plan()), "SETMARKER": lambda: self._retMsg(self.set_marker(self.last_msg.split(":")[1], self.last_msg.split(":", 2)[2])), + "RESETPLAYED": lambda: self._retMsg(self.reset_played(int(self.last_msg.split(":")[1]))) } message_type: str = self.last_msg.split(":")[0] diff --git a/websocket_server.py b/websocket_server.py index 11cecfa..594b552 100644 --- a/websocket_server.py +++ b/websocket_server.py @@ -144,6 +144,8 @@ class WebsocketServer: extra += json.dumps(data["newItem"]) elif command == "REMOVE": extra += str(data["weight"]) + elif command == "RESETPLAYED": + extra += str(data["weight"]) elif command == "GET_PLAN": extra += str(data["timeslotId"]) elif command == "SETMARKER":