async def add_shape(sid, data): sid_data = state.sid_map[sid] user = sid_data["user"] room = sid_data["room"] location = sid_data["location"] if "temporary" not in data: data["temporary"] = False floor = location.floors.select().where( Floor.name == data["shape"]["floor"])[0] layer = floor.layers.where(Layer.name == data["shape"]["layer"])[0] if room.creator != user and not layer.player_editable: logger.warning(f"{user.name} attempted to add a shape to a dm layer") return if data["temporary"]: state.add_temp(sid, data["shape"]["uuid"]) else: with db.atomic(): data["shape"]["layer"] = layer data["shape"]["index"] = layer.shapes.count() # Shape itself shape = Shape.create(**reduce_data_to_model(Shape, data["shape"])) # Subshape type_table = get_table(shape.type_) type_table.create(shape=shape, **reduce_data_to_model(type_table, data["shape"])) # Owners ShapeOwner.create(shape=shape, user=user) # Trackers for tracker in data["shape"]["trackers"]: Tracker.create(**reduce_data_to_model(Tracker, tracker), shape=shape) # Auras for aura in data["shape"]["auras"]: Aura.create(**reduce_data_to_model(Aura, aura), shape=shape) if layer.player_visible: for room_player in room.players: for psid in state.get_sids(user=room_player.player, room=room): if psid == sid: continue if not data["temporary"]: data["shape"] = shape.as_dict(room_player.player, False) await sio.emit("Shape.Add", data["shape"], room=psid, namespace="/planarally") for csid in state.get_sids(user=room.creator, room=room): if csid == sid: continue if not data["temporary"]: data["shape"] = shape.as_dict(room.creator, True) await sio.emit("Shape.Add", data["shape"], room=csid, namespace="/planarally")
async def add_shape(sid: int, data: Dict[str, Any]): pr: PlayerRoom = game_state.get(sid) if "temporary" not in data: data["temporary"] = False floor = pr.active_location.floors.select().where( Floor.name == data["shape"]["floor"])[0] layer = floor.layers.where(Layer.name == data["shape"]["layer"])[0] if pr.role != Role.DM and not layer.player_editable: logger.warning( f"{pr.player.name} attempted to add a shape to a dm layer") return if data["temporary"]: game_state.add_temp(sid, data["shape"]["uuid"]) else: with db.atomic(): data["shape"]["layer"] = layer data["shape"]["index"] = layer.shapes.count() # Shape itself shape = Shape.create(**reduce_data_to_model(Shape, data["shape"])) # Subshape type_table = get_table(shape.type_) type_table.create( shape=shape, **type_table.pre_create( **reduce_data_to_model(type_table, data["shape"])), ) # Owners for owner in data["shape"]["owners"]: ShapeOwner.create( shape=shape, user=User.by_name(owner["user"]), edit_access=owner["edit_access"], movement_access=owner["movement_access"], vision_access=owner["vision_access"], ) # Trackers for tracker in data["shape"]["trackers"]: Tracker.create(**reduce_data_to_model(Tracker, tracker), shape=shape) # Auras for aura in data["shape"]["auras"]: Aura.create(**reduce_data_to_model(Aura, aura), shape=shape) for room_player in pr.room.players: is_dm = room_player.role == Role.DM for psid in game_state.get_sids(player=room_player.player, active_location=pr.active_location): if psid == sid: continue if not is_dm and not layer.player_visible: continue if not data["temporary"]: data["shape"] = shape.as_dict(room_player.player, is_dm) await sio.emit("Shape.Add", data["shape"], room=psid, namespace=GAME_NS)
async def create_aura(sid: str, data: AuraDelta): pr: PlayerRoom = game_state.get(sid) shape = get_shape_or_none(pr, data["shape"], "Aura.Create") if shape is None: return model = reduce_data_to_model(Aura, data) aura = Aura.create(**model) aura.save() owners = [*get_owner_sids(pr, shape, skip_sid=sid)] for psid in owners: await sio.emit( "Shape.Options.Aura.Create", data, room=psid, namespace=GAME_NS, ) if aura.visible: for psid in game_state.get_sids( active_location=pr.active_location, skip_sid=sid ): if psid in owners: continue await sio.emit( "Shape.Options.Aura.Create", data, room=psid, namespace=GAME_NS, )
async def update_aura(sid: str, data: AuraDelta): pr: PlayerRoom = game_state.get(sid) shape = get_shape_or_none(pr, data["shape"], "Aura.Update") if shape is None: return aura = Aura.get_by_id(data["uuid"]) changed_visible = aura.visible != data.get("visible", aura.visible) update_model_from_dict(aura, data) aura.save() owners = [*get_owner_sids(pr, shape, skip_sid=sid)] for psid in owners: await sio.emit( "Shape.Options.Aura.Update", data, room=psid, namespace=GAME_NS, ) for psid in game_state.get_sids(active_location=pr.active_location, skip_sid=sid): if psid in owners: continue if changed_visible: if aura.visible: await sio.emit( "Shape.Options.Aura.Create", { "shape": shape.uuid, **aura.as_dict() }, room=psid, namespace=GAME_NS, ) else: await sio.emit( "Shape.Options.Aura.Remove", { "shape": shape.uuid, "value": aura.uuid }, room=psid, namespace=GAME_NS, ) else: await sio.emit( "Shape.Options.Aura.Update", data, room=psid, namespace=GAME_NS, )
async def remove_aura(sid: str, data: ShapeSetStringValue): pr: PlayerRoom = game_state.get(sid) shape = get_shape_or_none(pr, data["shape"], "Aura.Remove") if shape is None: return aura = Aura.get_by_id(data["value"]) aura.delete_instance(True) await sio.emit( "Shape.Options.Aura.Remove", data, skip_sid=sid, room=pr.active_location.get_path(), namespace=GAME_NS, )
async def update_shape_tracker(sid: str, data: TrackerUpdateData): pr: PlayerRoom = game_state.get(sid) if data["_type"] == "tracker": tracker = Tracker.get_by_id(data["uuid"]) else: tracker = Aura.get_by_id(data["uuid"]) tracker.value = data["value"] tracker.save() await sio.emit( "Shapes.Trackers.Update", data, room=pr.active_location.get_path(), skip_sid=sid, namespace=GAME_NS, )
async def move_aura(sid: str, data: AuraMove): pr: PlayerRoom = game_state.get(sid) new_shape = get_shape_or_none(pr, data["new_shape"], "Aura.Options.Tracker.Move") if new_shape is None: return aura = Aura.get_by_id(data["aura"]) aura.shape = new_shape aura.save() await sio.emit( "Shape.Options.Aura.Move", data, skip_sid=sid, room=pr.active_location.get_path(), namespace=GAME_NS, )
async def update_shape(sid: int, data: Dict[str, Any]): pr: PlayerRoom = game_state.get(sid) if data["temporary"] and not has_ownership_temp(data["shape"], pr): logger.warning( f"User {pr.player.name} tried to update a shape it does not own.") return # todo clean up this mess that deals with both temporary and non temporary shapes shape, layer = await _get_shape(data, pr) # Overwrite the old data with the new data if not data["temporary"]: if not has_ownership(shape, pr): logger.warning( f"User {pr.player.name} tried to update a shape it does not own." ) return with db.atomic(): # Shape update_model_from_dict(shape, reduce_data_to_model(Shape, data["shape"])) shape.save() # Subshape type_instance = shape.subtype # no backrefs on these tables type_instance.update_from_dict(data["shape"], ignore_unknown=True) type_instance.save() # Trackers old_trackers = {tracker.uuid for tracker in shape.trackers} new_trackers = { tracker["uuid"] for tracker in data["shape"]["trackers"] } for tracker_id in old_trackers | new_trackers: remove = tracker_id in old_trackers - new_trackers if not remove: tracker = next(tr for tr in data["shape"]["trackers"] if tr["uuid"] == tracker_id) reduced = reduce_data_to_model(Tracker, tracker) reduced["shape"] = shape if tracker_id in new_trackers - old_trackers: Tracker.create(**reduced) continue tracker_db = Tracker.get(uuid=tracker_id) if remove: tracker_db.delete_instance(True) else: update_model_from_dict(tracker_db, reduced) tracker_db.save() # Auras old_auras = {aura.uuid for aura in shape.auras} new_auras = {aura["uuid"] for aura in data["shape"]["auras"]} for aura_id in old_auras | new_auras: remove = aura_id in old_auras - new_auras if not remove: aura = next(au for au in data["shape"]["auras"] if au["uuid"] == aura_id) reduced = reduce_data_to_model(Aura, aura) reduced["shape"] = shape if aura_id in new_auras - old_auras: Aura.create(**reduced) continue aura_db = Aura.get_or_none(uuid=aura_id) if remove: aura_db.delete_instance(True) else: update_model_from_dict(aura_db, reduced) aura_db.save() # Labels for label in data["shape"]["labels"]: label_db = Label.get_or_none(uuid=label["uuid"]) reduced = reduce_data_to_model(Label, label) reduced["user"] = User.by_name(reduced["user"]) if label_db: update_model_from_dict(label_db, reduced) label_db.save() else: Label.create(**reduced) old_labels = { shape_label.label.uuid for shape_label in shape.labels } new_labels = set(label["uuid"] for label in data["shape"]["labels"]) for label in old_labels ^ new_labels: if label == "": continue if label in new_labels: ShapeLabel.create(shape=shape, label=Label.get(uuid=label)) else: ShapeLabel.get(label=Label.get(uuid=label), shape=shape).delete_instance(True) await sync_shape_update(layer, pr, data, sid, shape)
async def update_shape(sid, data): sid_data = state.sid_map[sid] user = sid_data["user"] room = sid_data["room"] location = sid_data["location"] shape, layer = await _get_shape(data, location, user) if not await has_ownership(layer, room, data, user, shape): return # Overwrite the old data with the new data if not data["temporary"]: with db.atomic(): data["shape"]["layer"] = Layer.get(location=location, name=data["shape"]["layer"]) # Shape update_model_from_dict(shape, reduce_data_to_model(Shape, data["shape"])) shape.save() # Subshape type_instance = shape.subtype # no backrefs on these tables type_instance.update_from_dict(data["shape"], ignore_unknown=True) type_instance.save() # Owners old_owners = {owner.user.name for owner in shape.owners} new_owners = set(data["shape"]["owners"]) for owner in old_owners ^ new_owners: if owner == "": continue delta_owner = User.by_name(owner) if owner in new_owners: ShapeOwner.create(shape=shape, user=delta_owner) else: ShapeOwner.get(shape=shape, user=delta_owner).delete_instance(True) await send_client_initiatives(room, location, delta_owner) # Trackers old_trackers = {tracker.uuid for tracker in shape.trackers} new_trackers = { tracker["uuid"] for tracker in data["shape"]["trackers"] } for tracker_id in old_trackers | new_trackers: remove = tracker_id in old_trackers - new_trackers if not remove: tracker = next(tr for tr in data["shape"]["trackers"] if tr["uuid"] == tracker_id) reduced = reduce_data_to_model(Tracker, tracker) reduced["shape"] = shape if tracker_id in new_trackers - old_trackers: Tracker.create(**reduced) continue tracker_db = Tracker.get(uuid=tracker_id) if remove: tracker_db.delete_instance(True) else: update_model_from_dict(tracker_db, reduced) tracker_db.save() # Auras old_auras = {aura.uuid for aura in shape.auras} new_auras = {aura["uuid"] for aura in data["shape"]["auras"]} for aura_id in old_auras | new_auras: remove = aura_id in old_auras - new_auras if not remove: aura = next(au for au in data["shape"]["auras"] if au["uuid"] == aura_id) reduced = reduce_data_to_model(Aura, aura) reduced["shape"] = shape if aura_id in new_auras - old_auras: Aura.create(**reduced) continue aura_db = Aura.get_or_none(uuid=aura_id) if remove: aura_db.delete_instance(True) else: update_model_from_dict(aura_db, reduced) aura_db.save() # Labels for label in data["shape"]["labels"]: label_db = Label.get_or_none(uuid=label["uuid"]) reduced = reduce_data_to_model(Label, label) reduced["user"] = User.by_name(reduced["user"]) if label_db: update_model_from_dict(label_db, reduced) label_db.save() else: Label.create(**reduced) shape_label_db = ShapeLabel.get_or_none(shape=shape, label=label_db) old_labels = { shape_label.label.uuid for shape_label in shape.labels } new_labels = set(label["uuid"] for label in data["shape"]["labels"]) for label in old_labels ^ new_labels: if label == "": continue if label in new_labels: ShapeLabel.create(shape=shape, label=Label.get(uuid=label)) else: ShapeLabel.get(label=Label.get(uuid=label), shape=shape).delete_instance(True) await sync_shape_update(layer, room, data, sid, shape)
async def update_shape(sid, data): sid_data = state.sid_map[sid] user = sid_data["user"] room = sid_data["room"] location = sid_data["location"] # We're first gonna retrieve the existing server side shape for some validation checks if data["temporary"]: # This stuff is not stored so we cannot do any server side validation /shrug shape = data["shape"] layer = location.layers.where(Layer.name == data["shape"]["layer"])[0] else: # Use the server version of the shape. try: shape = Shape.get(uuid=data["shape"]["uuid"]) except Shape.DoesNotExist: logger.warning(f"Attempt to update unknown shape by {user.name}") return layer = shape.layer # Ownership validatation if room.creator != user: if not layer.player_editable: logger.warning( f"{user.name} attempted to move a shape on a dm layer") return if data["temporary"]: if user.name not in shape["owners"]: logger.warning( f"{user.name} attempted to move asset it does not own") return else: if not ShapeOwner.get_or_none(shape=shape, user=user): logger.warning( f"{user.name} attempted to move asset it does not own") return # Overwrite the old data with the new data if not data["temporary"]: with db.atomic(): data["shape"]["layer"] = Layer.get(location=location, name=data["shape"]["layer"]) # Shape update_model_from_dict(shape, reduce_data_to_model(Shape, data["shape"])) shape.save() # Subshape type_table = get_table(shape.type_) type_instance = type_table.get(uuid=shape.uuid) # no backrefs on these tables update_model_from_dict(type_instance, data["shape"], ignore_unknown=True) type_instance.save() # Owners old_owners = {owner.user.name for owner in shape.owners} new_owners = set(data["shape"]["owners"]) for owner in old_owners ^ new_owners: if owner == "": continue delta_owner = User.by_name(owner) if owner in new_owners: ShapeOwner.create(shape=shape, user=delta_owner) else: ShapeOwner.get(shape=shape, user=delta_owner).delete_instance(True) await send_client_initiatives(room, location, delta_owner) # Trackers for tracker in data["shape"]["trackers"]: tracker_db = Tracker.get_or_none(uuid=tracker['uuid']) reduced = reduce_data_to_model(Tracker, tracker) reduced['shape'] = shape if tracker_db: update_model_from_dict(tracker_db, reduced) tracker_db.save() else: Tracker.create(**reduced) # Auras for aura in data["shape"]["auras"]: aura_db = Aura.get_or_none(uuid=aura['uuid']) reduced = reduce_data_to_model(Aura, aura) reduced['shape'] = shape if aura_db: update_model_from_dict(aura_db, reduced) aura_db.save() else: Aura.create(**reduced) # Send to players if layer.player_visible: for room_player in room.players: for psid in state.get_sids(user=room_player.player, room=room): if psid == sid: continue if not data["temporary"]: data["shape"] = shape.as_dict(room_player.player, False) await sio.emit("Shape.Update", data, room=psid, namespace="/planarally") # Send to DM for csid in state.get_sids(user=room.creator, room=room): if csid == sid: continue if not data["temporary"]: data["shape"] = shape.as_dict(room.creator, True) await sio.emit("Shape.Update", data, room=csid, namespace="/planarally")
async def update_shape(sid, data): sid_data = state.sid_map[sid] user = sid_data["user"] room = sid_data["room"] location = sid_data["location"] shape, layer = await _get_shape(data, location, user) if not await has_ownership(layer, room, data, user, shape): return # Overwrite the old data with the new data if not data["temporary"]: with db.atomic(): data["shape"]["layer"] = Layer.get(location=location, name=data["shape"]["layer"]) # Shape update_model_from_dict(shape, reduce_data_to_model(Shape, data["shape"])) shape.save() # Subshape type_table = get_table(shape.type_) type_instance = type_table.get(uuid=shape.uuid) # no backrefs on these tables update_model_from_dict(type_instance, data["shape"], ignore_unknown=True) type_instance.save() # Owners old_owners = {owner.user.name for owner in shape.owners} new_owners = set(data["shape"]["owners"]) for owner in old_owners ^ new_owners: if owner == "": continue delta_owner = User.by_name(owner) if owner in new_owners: ShapeOwner.create(shape=shape, user=delta_owner) else: ShapeOwner.get(shape=shape, user=delta_owner).delete_instance(True) await send_client_initiatives(room, location, delta_owner) # Trackers for tracker in data["shape"]["trackers"]: tracker_db = Tracker.get_or_none(uuid=tracker["uuid"]) reduced = reduce_data_to_model(Tracker, tracker) reduced["shape"] = shape if tracker_db: update_model_from_dict(tracker_db, reduced) tracker_db.save() else: Tracker.create(**reduced) # Auras for aura in data["shape"]["auras"]: aura_db = Aura.get_or_none(uuid=aura["uuid"]) reduced = reduce_data_to_model(Aura, aura) reduced["shape"] = shape if aura_db: update_model_from_dict(aura_db, reduced) aura_db.save() else: Aura.create(**reduced) await sync_shape_update(layer, room, data, sid, shape)