async def set_group_leader(sid: str, data: GroupLeaderData): pr: PlayerRoom = game_state.get(sid) leader_shape = Shape.get_by_id(data["leader"]) leader_options = leader_shape.get_options() if "groupId" in leader_options: del leader_options["groupId"] leader_options["groupInfo"] = data["members"] leader_shape.set_options(leader_options) leader_shape.save() for member in data["members"]: shape = Shape.get_by_id(member) options = shape.get_options() options["groupId"] = data["leader"] shape.set_options(options) shape.save() await sio.emit( "Shapes.Group.Leader.Set", data, room=pr.active_location.get_path(), skip_sid=sid, namespace=GAME_NS, )
async def get_location_spawn_info(sid: str, location_id: int): pr: PlayerRoom = game_state.get(sid) if pr.role != Role.DM: logger.warning( f"{pr.player.name} attempted to retrieve spawn locations.") return data = [] try: location = Location.get_by_id(location_id) if location.options is not None: for spawn in json.loads(location.options.spawn_locations): try: shape = Shape.get_by_id(spawn) except Shape.DoesNotExist: pass else: data.append(shape.as_dict(pr.player, True)) except: logger.exception("Could not load spawn locations") await sio.emit("Location.Spawn.Info", data=data, room=sid, namespace=GAME_NS)
async def leave_group(sid: str, client_shapes: List[LeaveGroup]): pr: PlayerRoom = game_state.get(sid) group_ids = set() for client_shape in client_shapes: try: shape = Shape.get_by_id(client_shape["uuid"]) except Shape.DoesNotExist: logger.exception( f"Could not remove shape group for unknown shape {client_shape['uuid']}" ) else: group_ids.add(client_shape["group_id"]) shape.group = None shape.show_badge = False shape.save() for group_id in group_ids: await remove_group_if_empty(group_id) for psid, _ in game_state.get_users(room=pr.room): await sio.emit( "Group.Leave", client_shapes, room=psid, skip_sid=sid, namespace=GAME_NS, )
async def join_group(sid: str, group_join: GroupJoin): pr: PlayerRoom = game_state.get(sid) group_ids = set() for member in group_join["members"]: try: shape = Shape.get_by_id(member["uuid"]) except Shape.DoesNotExist: logger.exception( f"Could not update shape group for unknown shape {member['uuid']}" ) else: if shape.group is not None and shape.group != group_join[ "group_id"]: group_ids.add(shape.group) shape.group = group_join["group_id"] shape.badge = member["badge"] shape.save() # Group joining can be the result of a merge or a split and thus other groups might be empty now for group_id in group_ids: await remove_group_if_empty(group_id) for psid, _ in game_state.get_users(room=pr.room): await sio.emit( "Group.Join", group_join, room=psid, skip_sid=sid, namespace=GAME_NS, )
async def move_shapes(sid: str, data: ServerShapeLocationMove): pr: PlayerRoom = game_state.get(sid) if pr.role != Role.DM: logger.warning(f"{pr.player.name} attempted to move shape locations") return location = Location.get_by_id(data["target"]["location"]) floor = location.floors.select().where(Floor.name == data["target"]["floor"])[0] x = data["target"]["x"] y = data["target"]["y"] shapes = [Shape.get_by_id(sh) for sh in data["shapes"]] await sio.emit( "Shapes.Remove", [sh.uuid for sh in shapes], room=pr.active_location.get_path(), namespace=GAME_NS, ) for shape in shapes: shape.layer = floor.layers.where(Layer.name == shape.layer.name)[0] shape.center_at(x, y) shape.save() for psid, player in game_state.get_users(active_location=location): await sio.emit( "Shapes.Add", [sh.as_dict(player, game_state.get(psid).role == Role.DM) for sh in shapes], room=psid, namespace=GAME_NS, )
async def add_group_member(sid: str, data: GroupMemberAddData): pr: PlayerRoom = game_state.get(sid) leader_shape = Shape.get_by_id(data["leader"]) leader_options = leader_shape.get_options() leader_options["groupInfo"].append(data["member"]) leader_shape.set_options(leader_options) leader_shape.save() await sio.emit( "Shapes.Group.Member.Add", data, room=pr.active_location.get_path(), skip_sid=sid, namespace=GAME_NS, )
async def update_group_badges(sid: str, member_badges: List[MemberBadge]): pr: PlayerRoom = game_state.get(sid) for member in member_badges: try: shape = Shape.get_by_id(member["uuid"]) except Shape.DoesNotExist: logger.exception( f"Could not update shape badge for unknown shape {member['uuid']}" ) else: shape.badge = member["badge"] shape.save() for psid, player in game_state.get_users(room=pr.room): await sio.emit( "Group.Members.Update", member_badges, room=psid, skip_sid=sid, namespace=GAME_NS, )