diff --git a/plan.py b/plan.py index 0ffdeec..d386d75 100644 --- a/plan.py +++ b/plan.py @@ -28,6 +28,10 @@ class PlanItem: def weight(self) -> int: return self._weight + @weight.setter + def weight(self, value: int): + self._weight = value + @property def timeslotitemid(self) -> int: return self._timeslotitemid diff --git a/player.py b/player.py index 925ead3..b30deef 100644 --- a/player.py +++ b/player.py @@ -232,6 +232,7 @@ class Player(): plan = self.api.get_showplan(message) self.clear_channel_plan() channel = self.state.state["channel"] + self.logger.log.info(plan) if len(plan) > channel: for plan_item in plan[str(channel)]: try: @@ -242,18 +243,37 @@ class Player(): return True - def add_to_plan(self, new_item: Dict[str, Any]) -> bool: - self.state.update("show_plan", self.state.state["show_plan"] + [PlanItem(new_item)]) + new_item_obj = PlanItem(new_item) + plan_copy: List[PlanItem] = copy.copy(self.state.state["show_plan"]) + # Shift any plan items after the new position down one to make space. + for item in plan_copy: + if item.weight >= new_item_obj.weight: + item.weight += 1 + + + plan_copy += [new_item_obj] # Add the new item. + + def sort_weight(e: PlanItem): + return e.weight + + plan_copy.sort(key=sort_weight) # Sort into weighted order. + + self.state.update("show_plan", plan_copy) return True def remove_from_plan(self, weight: int) -> bool: plan_copy: List[PlanItem] = copy.copy(self.state.state["show_plan"]) + found = False for i in plan_copy: if i.weight == weight: plan_copy.remove(i) - self.state.update("show_plan", plan_copy) - return True + found = True + elif i.weight > weight: # Shuffle up the weights of the items following the deleted one. + i.weight -= 1 + if found: + self.state.update("show_plan", plan_copy) + return True return False def clear_channel_plan(self) -> bool: diff --git a/websocket_server.py b/websocket_server.py index d36161b..6a1a8ba 100644 --- a/websocket_server.py +++ b/websocket_server.py @@ -48,6 +48,13 @@ async def websocket_handler(websocket, path): elif data["command"] == "REPEAT": channel_to_q[channel].put("REPEAT:" + str(data["mode"]).lower()) + elif data["command"] == "MOVE": + # Should we trust the client with the item info? + new_channel = int(data["new_channel"]) + channel_to_q[channel].put("REMOVE:" + str(data["weight"])) + item = data["item"] + item["weight"] = int(data["new_weight"]) + channel_to_q[new_channel].put("ADD:" + json.dumps(item)) elif data["command"] == "ADD": channel_to_q[channel].put("ADD:" + json.dumps(data["newItem"]))