async def change_shape_floor(sid: int, data: Dict[str, Any]): pr: PlayerRoom = game_state.get(sid) if pr.role != Role.DM: logger.warning( f"{pr.player.name} attempted to move the floor of a shape") return floor: Floor = Floor.get(location=pr.active_location, name=data["floor"]) shape: Shape = Shape.get(uuid=data["uuid"]) layer: Layer = Layer.get(floor=floor, name=shape.layer.name) old_layer = shape.layer old_index = shape.index shape.layer = layer shape.index = layer.shapes.count() shape.save() Shape.update(index=Shape.index - 1).where((Shape.layer == old_layer) & (Shape.index >= old_index)).execute() await sio.emit( "Shape.Floor.Change", data, room=pr.active_location.get_path(), skip_sid=sid, namespace=GAME_NS, )
async def change_shape_layer(sid, data): sid_data = state.sid_map[sid] user = sid_data["user"] room = sid_data["room"] location = sid_data["location"] if room.creator != user: logger.warning(f"{user.name} attempted to move the layer of a shape") return layer = Layer.get(location=location, name=data["layer"]) shape = Shape.get(uuid=data["uuid"]) old_layer = shape.layer old_index = shape.index shape.layer = layer shape.index = layer.shapes.count() shape.save() Shape.update(index=Shape.index - 1).where((Shape.layer == old_layer) & (Shape.index >= old_index)).execute() await sio.emit( "Shape.Layer.Change", data, room=location.get_path(), skip_sid=sid, namespace="/planarally", )
async def move_shape_order(sid: str, data: ShapeOrder): pr: PlayerRoom = game_state.get(sid) if not data["temporary"]: shape = Shape.get(uuid=data["uuid"]) layer = shape.layer if pr.role != Role.DM and not layer.player_editable: logger.warning( f"{pr.player.name} attempted to move a shape order on a dm layer" ) return target = data["index"] sign = 1 if target < shape.index else -1 case = Case( None, ( (Shape.index == shape.index, target), ( (sign * Shape.index) < (sign * shape.index), (Shape.index + (sign * 1)), ), ), Shape.index, ) Shape.update(index=case).where(Shape.layer == layer).execute() await sio.emit( "Shape.Order.Set", data, room=pr.active_location.get_path(), skip_sid=sid, namespace=GAME_NS, )
async def change_shape_floor(sid: str, data: ShapeFloorChange): pr: PlayerRoom = game_state.get(sid) if pr.role != Role.DM: logger.warning(f"{pr.player.name} attempted to move the floor of a shape") return floor: Floor = Floor.get(location=pr.active_location, name=data["floor"]) shapes: List[Shape] = [s for s in Shape.select().where(Shape.uuid << data["uuids"])] layer: Layer = Layer.get(floor=floor, name=shapes[0].layer.name) old_layer = shapes[0].layer for shape in shapes: old_index = shape.index shape.layer = layer shape.index = layer.shapes.count() shape.save() Shape.update(index=Shape.index - 1).where( (Shape.layer == old_layer) & (Shape.index >= old_index) ).execute() await sio.emit( "Shapes.Floor.Change", data, room=pr.active_location.get_path(), skip_sid=sid, namespace=GAME_NS, )
async def remove_shape(sid: int, data: Dict[str, Any]): pr: PlayerRoom = game_state.get(sid) # We're first gonna retrieve the existing server side shape for some validation checks if data["temporary"]: if not has_ownership_temp(data["shape"], pr): logger.warning( f"User {pr.player.name} tried to update a shape it does not own." ) return # This stuff is not stored so we cannot do any server side validation /shrug shape = data["shape"] floor = pr.active_location.floors.select().where( Floor.name == data["shape"]["floor"])[0] layer = floor.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 {pr.player.name}") return layer = shape.layer if not has_ownership(shape, pr): logger.warning( f"User {pr.player.name} tried to update a shape it does not own." ) return if data["temporary"]: game_state.remove_temp(sid, data["shape"]["uuid"]) else: old_index = shape.index shape.delete_instance(True) Shape.update(index=Shape.index - 1).where((Shape.layer == layer) & (Shape.index >= old_index)).execute() if layer.player_visible: await sio.emit( "Shape.Remove", data["shape"], room=pr.active_location.get_path(), skip_sid=sid, namespace=GAME_NS, ) else: for csid in game_state.get_sids(player=pr.room.creator, active_location=pr.active_location): if csid == sid: continue await sio.emit("Shape.Remove", data["shape"], room=csid, namespace=GAME_NS)
async def remove_shapes(sid: str, data: TemporaryShapesList): pr: PlayerRoom = game_state.get(sid) if data["temporary"]: # This stuff is not stored so we cannot do any server side validation /shrug for shape in data["uuids"]: game_state.remove_temp(sid, shape) else: # Use the server version of the shapes. try: shapes: List[Shape] = [ s for s in Shape.select().where(Shape.uuid << data["uuids"]) ] except Shape.DoesNotExist: logger.warning( f"Attempt to update unknown shape by {pr.player.name}") return layer = shapes[0].layer group_ids = set() for shape in shapes: if not has_ownership(shape, pr): logger.warning( f"User {pr.player.name} tried to update a shape it does not own." ) return if shape.group: group_ids.add(shape.group) old_index = shape.index shape.delete_instance(True) Shape.update(index=Shape.index - 1).where((Shape.layer == layer) & (Shape.index >= old_index)).execute() for group_id in group_ids: await remove_group_if_empty(group_id) await sio.emit( "Shapes.Remove", data["uuids"], room=pr.active_location.get_path(), skip_sid=sid, namespace=GAME_NS, )
async def move_shape_order(sid, data): sid_data = state.sid_map[sid] user = sid_data["user"] room = sid_data["room"] location = sid_data["location"] shape = Shape.get(uuid=data["shape"]["uuid"]) layer = shape.layer if room.creator != user and not layer.player_editable: logger.warning( f"{user.name} attempted to move a shape order on a dm layer") return target = data["index"] + 1 sign = 1 if target <= 1 else -1 polarity = 1 if shape.index > 0 else -1 case = Case( None, ( (Shape.index == shape.index, target * (-polarity)), ( (polarity * sign * Shape.index) < (polarity * sign * shape.index), (Shape.index + (polarity * sign * 1)) * -1, ), ), Shape.index * -1, ) Shape.update(index=case).where(Shape.layer == layer).execute() if layer.player_visible: await sio.emit( "Shape.Order.Set", data, room=location.get_path(), skip_sid=sid, namespace="/planarally", ) else: for csid in state.get_sids(user=room.creator, room=room): if csid == sid: continue await sio.emit("Shape.Order.Set", data["shape"], room=csid, namespace="/planarally")
async def move_shape_order(sid: int, data: Dict[str, Any]): pr: PlayerRoom = game_state.get(sid) shape = Shape.get(uuid=data["shape"]["uuid"]) layer = shape.layer if pr.role != Role.DM and not layer.player_editable: logger.warning( f"{pr.player.name} attempted to move a shape order on a dm layer") return target = data["index"] sign = 1 if target < shape.index else -1 case = Case( None, ( (Shape.index == shape.index, target), ((sign * Shape.index) < (sign * shape.index), (Shape.index + (sign * 1))), ), Shape.index, ) Shape.update(index=case).where(Shape.layer == layer).execute() if layer.player_visible: await sio.emit( "Shape.Order.Set", data, room=pr.active_location.get_path(), skip_sid=sid, namespace=GAME_NS, ) else: for csid in game_state.get_sids(player=pr.room.creator, room=pr.room): if csid == sid: continue await sio.emit("Shape.Order.Set", data["shape"], room=csid, namespace=GAME_NS)
async def change_shape_layer(sid: int, data: Dict[str, Any]): pr: PlayerRoom = game_state.get(sid) if pr.role != Role.DM: logger.warning( f"{pr.player.name} attempted to move the layer of a shape") return floor = Floor.get(location=pr.active_location, name=data["floor"]) layer = Layer.get(floor=floor, name=data["layer"]) shape = Shape.get(uuid=data["uuid"]) old_layer = shape.layer old_index = shape.index if old_layer.player_visible and not layer.player_visible: for room_player in pr.room.players: if room_player.role == Role.DM: continue for psid in game_state.get_sids( player=room_player.player, active_location=pr.active_location): if psid == sid: continue await sio.emit( "Shape.Remove", shape.as_dict(room_player.player, False), room=psid, namespace=GAME_NS, ) shape.layer = layer shape.index = layer.shapes.count() shape.save() Shape.update(index=Shape.index - 1).where((Shape.layer == old_layer) & (Shape.index >= old_index)).execute() if old_layer.player_visible and layer.player_visible: await sio.emit( "Shape.Layer.Change", data, room=pr.active_location.get_path(), skip_sid=sid, namespace=GAME_NS, ) else: 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 is_dm: await sio.emit( "Shape.Layer.Change", data, room=pr.active_location.get_path(), skip_sid=sid, namespace=GAME_NS, ) elif layer.player_visible: await sio.emit( "Shape.Add", shape.as_dict(room_player.player, False), room=psid, namespace=GAME_NS, )
async def change_shape_layer(sid, data): sid_data = state.sid_map[sid] user = sid_data["user"] room = sid_data["room"] location = sid_data["location"] if room.creator != user: logger.warning(f"{user.name} attempted to move the layer of a shape") return layer = Layer.get(location=location, name=data["layer"]) shape = Shape.get(uuid=data["uuid"]) old_layer = shape.layer old_index = shape.index if old_layer.player_visible and not 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 await sio.emit( "Shape.Remove", shape.as_dict(room_player.player, False), room=psid, namespace="/planarally", ) shape.layer = layer shape.index = layer.shapes.count() shape.save() Shape.update(index=Shape.index - 1).where((Shape.layer == old_layer) & (Shape.index >= old_index)).execute() if old_layer.player_visible and layer.player_visible: await sio.emit( "Shape.Layer.Change", data, room=location.get_path(), skip_sid=sid, namespace="/planarally", ) else: for csid in state.get_sids(user=room.creator, room=room): if csid == sid: continue await sio.emit( "Shape.Layer.Change", data, room=location.get_path(), skip_sid=sid, namespace="/planarally", ) 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 await sio.emit( "Shape.Add", shape.as_dict(room_player.player, False), room=psid, namespace="/planarally", )
async def remove_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 remove a shape on a dm layer") return if data["temporary"]: if user.name not in shape["owners"]: logger.warning( f"{user.name} attempted to remove asset it does not own") return else: if not ShapeOwner.get_or_none(shape=shape, user=user): logger.warning( f"{user.name} attempted to remove asset it does not own") return if data["temporary"]: state.remove_temp(sid, data["shape"]["uuid"]) else: old_index = shape.index shape.delete_instance(True) Shape.update(index=Shape.index - 1).where((Shape.layer == layer) & (Shape.index >= old_index)).execute() if layer.player_visible: await sio.emit( "Shape.Remove", data["shape"], room=location.get_path(), skip_sid=sid, namespace="/planarally", ) else: for csid in state.get_sids(user=room.creator, room=room): if csid == sid: continue await sio.emit("Shape.Remove", data["shape"], room=csid, namespace="/planarally")