Пример #1
0
 def publish_piece_status_update(self, reset_stacked_ids,
                                 stacked_piece_ids):
     """
     Publish the stacked status change so the piece can have the border
     around it be removed or added.
     """
     lines = []
     msg = ""
     for piece in reset_stacked_ids:
         lines.append(formatPieceMovementString(piece, s="0"))
     for piece in stacked_piece_ids:
         lines.append(formatPieceMovementString(piece, s="2"))
     msg = "\n".join(lines)
     r = requests.post(
         "http://{HOSTAPI}:{PORTAPI}/internal/puzzle/{puzzle_id}/publish_move/"
         .format(
             HOSTAPI=self.config["HOSTAPI"],
             PORTAPI=self.config["PORTAPI"],
             puzzle_id=self.puzzle_data["puzzle_id"],
         ),
         json={"msg": msg},
     )
     if r.status_code != 200:
         logger.warning(
             "Internal puzzle api error. Could not publish pieces move to update stack status on pieces"
         )
Пример #2
0
    def _join_pieces(self, pipe):
        "Join the piece and the pieces group to the adjacent piece merging the two piece groups together."
        lines = []
        msg = ""
        adjacent_piece_props = self.adjacent_piece_properties.get(
            self.can_join_adjacent_piece
        )

        # Move the piece
        pipe.hmset(self.pc_puzzle_piece_key, {"x": self.target_x, "y": self.target_y})
        lines.append(
            formatPieceMovementString(self.piece, x=self.target_x, y=self.target_y)
        )

        # Set immovable status if adjacent piece is immovable
        if self.can_join_adjacent_piece in self.pcfixed_puzzle:
            pipe.sadd(self.pcfixed_puzzle_key, self.piece)
            self.pcfixed_puzzle.add(self.piece)
            lines.append(formatPieceMovementString(self.piece, s="1"))
            for grouped_piece in self.all_other_pieces_in_piece_group:
                pipe.sadd(self.pcfixed_puzzle_key, grouped_piece)
                self.pcfixed_puzzle.add(grouped_piece)
                pipe.srem(self.pcstacked_puzzle_key, grouped_piece)
                try:
                    self.pcstacked_puzzle.remove(grouped_piece)
                except KeyError:
                    pass
                lines.append(formatPieceMovementString(grouped_piece, s="1"))

        new_piece_group = adjacent_piece_props.get("g", self.can_join_adjacent_piece)
        # Update Piece group to that of the adjacent piece since it may already be in a group
        pipe.sadd(
            "pcg:{puzzle}:{g}".format(puzzle=self.puzzle, g=new_piece_group),
            self.piece,
            self.can_join_adjacent_piece,
        )
        pipe.hset(
            self.pc_puzzle_piece_key,
            "g",
            new_piece_group,
        )
        pipe.hset(
            "pc:{puzzle}:{adjacent_piece}".format(
                puzzle=self.puzzle, adjacent_piece=self.can_join_adjacent_piece
            ),
            "g",
            new_piece_group,
        )
        lines.append(formatPieceMovementString(self.piece, g=new_piece_group))
        lines.append(
            formatPieceMovementString(self.can_join_adjacent_piece, g=new_piece_group)
        )
        if len(self.all_other_pieces_in_piece_group) != 0:
            lines.extend(
                self._update_grouped_pieces_positions(pipe, new_group=new_piece_group)
            )

        msg += "\n" + "\n".join(lines)
        return msg
Пример #3
0
    def _move_pieces(self, pipe):
        "Only move the piece and the other pieces in the group to the target position"
        lines = []
        msg = ""

        # Move the piece
        pipe.hmset(self.pc_puzzle_piece_key, {"x": self.target_x, "y": self.target_y})
        pipe.zadd(
            "pcx:{puzzle}".format(puzzle=self.puzzle), {self.piece: self.target_x}
        )
        pipe.zadd(
            "pcy:{puzzle}".format(puzzle=self.puzzle), {self.piece: self.target_y}
        )

        # Reset Piece Status for stacked (It's assumed that the piece being moved can't be an immovable piece)
        pipe.srem("pcstacked:{puzzle}".format(puzzle=self.puzzle), self.piece)
        pipe.hdel(
            "pc:{puzzle}:{piece}".format(puzzle=self.puzzle, piece=self.piece), "s"
        )
        lines.append(
            formatPieceMovementString(self.piece, x=self.target_x, y=self.target_y)
        )

        # If the piece is grouped move the other pieces in group
        if self.piece_properties.get("g") != None:
            lines.extend(self._update_grouped_pieces_positions(pipe))
        msg += "\n" + "\n".join(lines)
        return msg
Пример #4
0
 def _reject_pieces(self, pipe):
     "Revert piece movment back to origin."
     lines = []
     msg = ""
     lines.append(
         formatPieceMovementString(self.piece, x=self.origin_x, y=self.origin_y)
     )
     msg += "\n".join(lines)
     msg += "\n"
     return msg
Пример #5
0
    def _stack_pieces(self, pipe):
        "When too many pieces are within proximity to each other, skip trying to join any of them and mark them as stacked"
        lines = []
        msg = ""
        pipe.sadd(
            "pcstacked:{puzzle}".format(puzzle=self.puzzle),
            *self.pieces_in_proximity_to_target
        )
        for piece_in_proximity in self.pieces_in_proximity_to_target:
            pipe.hset(
                "pc:{puzzle}:{piece_in_proximity}".format(
                    puzzle=self.puzzle, piece_in_proximity=piece_in_proximity
                ),
                "s",
                "2",
            )
            lines.append(formatPieceMovementString(piece_in_proximity, s="2"))

        # Move the piece
        pipe.hmset(
            self.pc_puzzle_piece_key, {"x": self.target_x, "y": self.target_y},
        )
        pipe.zadd(
            "pcx:{puzzle}".format(puzzle=self.puzzle), {self.piece: self.target_x}
        )
        pipe.zadd(
            "pcy:{puzzle}".format(puzzle=self.puzzle), {self.piece: self.target_y}
        )
        lines.append(
            formatPieceMovementString(
                self.piece, x=self.target_x, y=self.target_y, s="2"
            )
        )

        msg += "\n".join(lines)
        msg += "\n"

        # If the piece is grouped move the other pieces in group
        if self.piece_properties.get("g") != None:
            lines = self._update_grouped_pieces_positions(pipe)
            msg += "\n" + "\n".join(lines)
        return msg
Пример #6
0
 def _reset_pieces_in_proximity_stacked_status(self, pipe):
     msg = ""
     lines = []
     pipe.srem("pcstacked:{puzzle}".format(puzzle=self.puzzle),
               *self.pieces_in_proximity_to_target)
     for piece_in_proximity in self.pieces_in_proximity_to_target:
         pipe.hdel(
             "pc:{puzzle}:{piece_in_proximity}".format(
                 puzzle=self.puzzle, piece_in_proximity=piece_in_proximity),
             "s",
         )
         lines.append(formatPieceMovementString(piece_in_proximity, s=None))
     msg = "\n".join(lines)
     msg += "\n"
     return msg
Пример #7
0
    def _move_pieces(self, pipe):
        "Only move the piece and the other pieces in the group to the target position"
        lines = []
        msg = ""

        # Move the piece
        pipe.hmset(self.pc_puzzle_piece_key, {"x": self.target_x, "y": self.target_y})

        lines.append(
            formatPieceMovementString(self.piece, x=self.target_x, y=self.target_y)
        )

        # If the piece is grouped move the other pieces in group
        if self.piece_properties.get("g") is not None:
            lines.extend(self._update_grouped_pieces_positions(pipe))
        msg += "\n" + "\n".join(lines)
        return msg
Пример #8
0
    def _update_grouped_pieces_positions(
        self,
        pipe,
        new_group=None,
        status=None,
    ):
        "Update all other pieces x,y in group to the offset, if new_group then assign them to the new_group"

        lines = []
        for grouped_piece in self.grouped_piece_properties.keys():
            origin_x = self.grouped_piece_properties[grouped_piece]["x"]
            origin_y = self.grouped_piece_properties[grouped_piece]["y"]
            new_x = origin_x + self.offset_x
            new_y = origin_y + self.offset_y
            new_pc = {"x": new_x, "y": new_y}
            if new_group is not None:
                # Remove from the old group and place in new_group
                new_pc["g"] = new_group
                pipe.sadd(
                    "pcg:{puzzle}:{g}".format(puzzle=self.puzzle, g=new_group),
                    grouped_piece,
                )
                pipe.srem(
                    "pcg:{puzzle}:{g}".format(
                        puzzle=self.puzzle, g=self.piece_properties.get("g")
                    ),
                    grouped_piece,
                )
            if status == "1":
                pipe.sadd(self.pcfixed_puzzle_key, grouped_piece)
                self.pcfixed_puzzle.add(grouped_piece)
                pipe.srem(
                    self.pcstacked_puzzle_key, grouped_piece
                )
                try:
                    self.pcstacked_puzzle.remove(grouped_piece)
                except KeyError:
                    pass
            pipe.hmset(
                "pc:{puzzle}:{grouped_piece}".format(
                    puzzle=self.puzzle, grouped_piece=grouped_piece
                ),
                new_pc,
            )
            lines.append(
                formatPieceMovementString(
                    grouped_piece, x=new_x, y=new_y, g=new_group, s=status
                )
            )
            self.publish_message.append(
                f"{self.user}:{grouped_piece}:{new_x}:{new_y}"
            )
        if status == "1":
            pipe.sadd(self.pcfixed_puzzle_key, self.piece)
            self.pcfixed_puzzle.add(self.piece)
            pipe.srem(self.pcstacked_puzzle_key, self.piece)
            try:
                self.pcstacked_puzzle.remove(self.piece)
            except KeyError:
                pass
        if new_group is not None:
            # For the piece that doesn't need x,y updated remove from the old group and place in new_group
            pipe.sadd(
                "pcg:{puzzle}:{g}".format(puzzle=self.puzzle, g=new_group), self.piece
            )
            pipe.srem(
                "pcg:{puzzle}:{g}".format(
                    puzzle=self.puzzle, g=self.piece_properties.get("g")
                ),
                self.piece,
            )
            pipe.hset(
                "pc:{puzzle}:{piece}".format(puzzle=self.puzzle, piece=self.piece),
                "g",
                new_group,
            )
            lines.append(formatPieceMovementString(self.piece, g=new_group))
        return lines
Пример #9
0
    def _update_grouped_pieces_positions(
        self, pipe, new_group=None, status=None,
    ):
        "Update all other pieces x,y in group to the offset, if new_group then assign them to the new_group"

        lines = []
        for grouped_piece in self.grouped_piece_properties.keys():
            new_x = self.grouped_piece_properties[grouped_piece]["x"] + self.offset_x
            new_y = self.grouped_piece_properties[grouped_piece]["y"] + self.offset_y
            new_pc = {"x": new_x, "y": new_y}
            if new_group != None:
                # Remove from the old group and place in new_group
                new_pc["g"] = new_group
                pipe.sadd(
                    "pcg:{puzzle}:{g}".format(puzzle=self.puzzle, g=new_group),
                    grouped_piece,
                )
                pipe.srem(
                    "pcg:{puzzle}:{g}".format(
                        puzzle=self.puzzle, g=self.piece_properties.get("g")
                    ),
                    grouped_piece,
                )
            if status == "1":
                new_pc["s"] = "1"
                pipe.sadd("pcfixed:{puzzle}".format(puzzle=self.puzzle), grouped_piece)
                pipe.srem(
                    "pcstacked:{puzzle}".format(puzzle=self.puzzle), grouped_piece
                )
            pipe.hmset(
                "pc:{puzzle}:{grouped_piece}".format(
                    puzzle=self.puzzle, grouped_piece=grouped_piece
                ),
                new_pc,
            )
            pipe.zadd("pcx:{puzzle}".format(puzzle=self.puzzle), {grouped_piece: new_x})
            pipe.zadd("pcy:{puzzle}".format(puzzle=self.puzzle), {grouped_piece: new_y})
            lines.append(
                formatPieceMovementString(
                    grouped_piece, x=new_x, y=new_y, g=new_group, s=status
                )
            )
        if status == "1":
            pipe.sadd("pcfixed:{puzzle}".format(puzzle=self.puzzle), self.piece)
            pipe.srem("pcstacked:{puzzle}".format(puzzle=self.puzzle), self.piece)
            pipe.hset(
                "pc:{puzzle}:{piece}".format(puzzle=self.puzzle, piece=self.piece),
                "s",
                "1",
            )
        if new_group != None:
            # For the piece that doesn't need x,y updated remove from the old group and place in new_group
            pipe.sadd(
                "pcg:{puzzle}:{g}".format(puzzle=self.puzzle, g=new_group), self.piece
            )
            pipe.srem(
                "pcg:{puzzle}:{g}".format(
                    puzzle=self.puzzle, g=self.piece_properties.get("g")
                ),
                self.piece,
            )
            pipe.hset(
                "pc:{puzzle}:{piece}".format(puzzle=self.puzzle, piece=self.piece),
                "g",
                new_group,
            )
            lines.append(formatPieceMovementString(self.piece, g=new_group))
        return lines
Пример #10
0
    def _join_pieces(self, pipe):
        "Join the piece and the pieces group to the adjacent piece merging the two piece groups together."
        lines = []
        msg = ""
        adjacent_piece_props = self.adjacent_piece_properties.get(
            self.can_join_adjacent_piece
        )

        # Move the piece
        pipe.hmset(self.pc_puzzle_piece_key, {"x": self.target_x, "y": self.target_y})
        pipe.zadd(
            "pcx:{puzzle}".format(puzzle=self.puzzle), {self.piece: self.target_x}
        )
        pipe.zadd(
            "pcy:{puzzle}".format(puzzle=self.puzzle), {self.piece: self.target_y}
        )
        # Reset Piece Status for stacked (It's assumed that the piece being moved can't be an immovable piece)
        pipe.srem("pcstacked:{puzzle}".format(puzzle=self.puzzle), self.piece)
        pipe.hdel(
            "pc:{puzzle}:{piece}".format(puzzle=self.puzzle, piece=self.piece), "s"
        )
        lines.append(
            formatPieceMovementString(self.piece, x=self.target_x, y=self.target_y)
        )

        # Set immovable status if adjacent piece is immovable
        if adjacent_piece_props.get("s") == "1":
            pipe.hset(
                self.pc_puzzle_piece_key, "s", "1",
            )
            pipe.sadd("pcfixed:{puzzle}".format(puzzle=self.puzzle), self.piece)
            lines.append(formatPieceMovementString(self.piece, s="1"))
            for grouped_piece in self.all_other_pieces_in_piece_group:
                pc_puzzle_grouped_piece_key = "pc:{puzzle}:{grouped_piece}".format(
                    puzzle=self.puzzle, grouped_piece=grouped_piece
                )
                pipe.hset(pc_puzzle_grouped_piece_key, "s", "1")
                pipe.sadd("pcfixed:{puzzle}".format(puzzle=self.puzzle), grouped_piece)
                lines.append(formatPieceMovementString(grouped_piece, s="1"))

        new_piece_group = adjacent_piece_props.get("g", self.can_join_adjacent_piece)
        # Update Piece group to that of the adjacent piece since it may already be in a group
        pipe.sadd(
            "pcg:{puzzle}:{g}".format(puzzle=self.puzzle, g=new_piece_group),
            self.piece,
            self.can_join_adjacent_piece,
        )
        pipe.hset(
            self.pc_puzzle_piece_key, "g", new_piece_group,
        )
        pipe.hset(
            "pc:{puzzle}:{adjacent_piece}".format(
                puzzle=self.puzzle, adjacent_piece=self.can_join_adjacent_piece
            ),
            "g",
            new_piece_group,
        )
        lines.append(formatPieceMovementString(self.piece, g=new_piece_group))
        lines.append(
            formatPieceMovementString(self.can_join_adjacent_piece, g=new_piece_group)
        )
        if len(self.all_other_pieces_in_piece_group) != 0:
            lines.extend(
                self._update_grouped_pieces_positions(pipe, new_group=new_piece_group)
            )

        msg += "\n" + "\n".join(lines)
        return msg