Update loaded item weight and fix reordering/auto advance.

This commit is contained in:
Matthew Stratford 2021-05-02 00:45:03 +01:00
parent dc64ada5b2
commit 23aefe4ba6
2 changed files with 65 additions and 20 deletions

View file

@ -290,6 +290,7 @@ class Player:
item.timeslotitemid = "GHOST-{}-{}".format(self.state.get()["channel"], time.time_ns()) item.timeslotitemid = "GHOST-{}-{}".format(self.state.get()["channel"], time.time_ns())
return item return item
# TODO Allow just moving an item inside the channel instead of removing and adding.
def add_to_plan(self, new_item: Dict[str, Any]) -> bool: def add_to_plan(self, new_item: Dict[str, Any]) -> bool:
new_item_obj = PlanItem(new_item) new_item_obj = PlanItem(new_item)
new_item_obj = self._check_ghosts(new_item_obj) new_item_obj = self._check_ghosts(new_item_obj)
@ -304,6 +305,27 @@ class Player:
plan_copy = self._fix_weights(plan_copy) plan_copy = self._fix_weights(plan_copy)
self.state.update("show_plan", plan_copy) self.state.update("show_plan", plan_copy)
loaded_item = self.state.get()["loaded_item"]
if loaded_item:
# If we added the loaded item back into this channel, update it's weight
# So we know how/not to autoadvance.
if loaded_item.timeslotitemid == new_item_obj.timeslotitemid:
loaded_item.weight = new_item_obj.weight
# Bump the loaded_item's weight if we just added a new item above it.
elif loaded_item.weight >= new_item_obj.weight:
loaded_item.weight += 1
# Else, new weight stays the same.
else:
return True
self.state.update("loaded_item", loaded_item)
return True return True
def remove_from_plan(self, weight: int) -> bool: def remove_from_plan(self, weight: int) -> bool:
@ -316,6 +338,22 @@ class Player:
if found: if found:
plan_copy = self._fix_weights(plan_copy) plan_copy = self._fix_weights(plan_copy)
self.state.update("show_plan", plan_copy) self.state.update("show_plan", plan_copy)
# If we removed the loaded item from this channel, update it's weight
# So we know how/not to autoadvance.
loaded_item = self.state.get()["loaded_item"]
if loaded_item:
# We're removing the loaded item form the channel.
if loaded_item.weight == weight:
loaded_item.weight = -1
# We removed an item above it. Shift it up.
elif loaded_item.weight > weight:
loaded_item.weight -= 1
# Else, new weight stays the same.
else:
return True
self.state.update("loaded_item", loaded_item)
return True return True
return False return False
@ -579,7 +617,9 @@ class Player:
def _ended(self): def _ended(self):
self._potentially_end_tracklist() self._potentially_end_tracklist()
loaded_item = self.state.get()["loaded_item"] state = self.state.get()
loaded_item = state["loaded_item"]
if not loaded_item: if not loaded_item:
return return
@ -589,28 +629,35 @@ class Player:
# Repeat 1 # Repeat 1
# TODO ENUM # TODO ENUM
if self.state.get()["repeat"] == "one": if state["repeat"] == "one":
self.play() self.play()
return return
# Auto Advance # Auto Advance
if self.state.get()["auto_advance"]: if state["auto_advance"]:
for i in range(len(self.state.get()["show_plan"])):
if self.state.get()["show_plan"][i].weight == loaded_item.weight:
if len(self.state.get()["show_plan"]) > i + 1:
self.load(self.state.get()["show_plan"][i + 1].weight)
return
# Repeat All # Check for loaded item in show plan.
# TODO ENUM # If it's been removed, weight will be -1.
elif self.state.get()["repeat"] == "all": # Just stop in this case.
self.load(self.state.get()["show_plan"][0].weight) if loaded_item.weight < 0:
return self.logger.log.debug("Loaded item is no longer in channel (weight {}), not auto advancing.".format(loaded_item.weight))
else:
self.logger.log.debug("Found current loaded item in this channel show plan. Auto Advancing.")
# If there's another item after this one, load that.
if len(state["show_plan"]) > loaded_item.weight+1:
self.load(loaded_item.weight+1)
return
# Repeat All (Jump to top again)
# TODO ENUM
elif state["repeat"] == "all":
self.load(0) # Jump to the top.
return
# No automations, just stop playing. # No automations, just stop playing.
self.stop() self.stop()
if self.out_q: self._retAll("STOPPED") # Tell clients that we've stopped playing.
self._retAll("STOPPED") # Tell clients that we've stopped playing.
def _updateState(self, pos: Optional[float] = None): def _updateState(self, pos: Optional[float] = None):
@ -661,7 +708,8 @@ class Player:
self._retAll("POS:" + str(self.state.get()["pos_true"])) self._retAll("POS:" + str(self.state.get()["pos_true"]))
def _retAll(self, msg): def _retAll(self, msg):
self.out_q.put("ALL:" + msg) if self.out_q:
self.out_q.put("ALL:" + msg)
def _retMsg( def _retMsg(
self, msg: Any, okay_str: bool = False, custom_prefix: Optional[str] = None self, msg: Any, okay_str: bool = False, custom_prefix: Optional[str] = None

View file

@ -152,6 +152,7 @@ class WebsocketServer:
json.dumps(data["marker"]) json.dumps(data["marker"])
) )
# TODO: Move this to player handler.
# SPECIAL CASE ALERT! We need to talk to two channels here. # SPECIAL CASE ALERT! We need to talk to two channels here.
elif command == "MOVE": elif command == "MOVE":
@ -167,10 +168,6 @@ class WebsocketServer:
item = data["item"] item = data["item"]
item["weight"] = int(data["new_weight"]) item["weight"] = int(data["new_weight"])
# If we're moving within the same channel, add 1 to the weight, since we're adding the new item before we remove the old one, UI gave us the weight expected after removing.
if channel == new_channel and data["new_weight"] > data["weight"]:
item["weight"] += 1
# Now send the special case. # Now send the special case.
self.channel_to_q[new_channel].put( self.channel_to_q[new_channel].put(
"WEBSOCKET:ADD:" + json.dumps(item)) "WEBSOCKET:ADD:" + json.dumps(item))