async def set_initiative_sort(sid: str, sort: int): pr: PlayerRoom = game_state.get(sid) if pr.role != Role.DM: logger.warning(f"{pr.player.name} attempted to change initiative sort") return with db.atomic(): location_data = Initiative.get(location=pr.active_location) location_data.sort = sort json_data = json.loads(location_data.data) json_data = sort_initiative(json_data, location_data.sort) location_data.data = json.dumps(json_data) location_data.save() await sio.emit( "Initiative.Sort.Set", room=pr.active_location.get_path(), namespace=GAME_NS, ) await sio.emit( "Initiative.Set", location_data.as_dict(), room=pr.active_location.get_path(), namespace=GAME_NS, )
async def remove_initiative_effect(sid: str, data: ServerRemoveInitiativeEffectActor): pr: PlayerRoom = game_state.get(sid) if not has_ownership(Shape.get_or_none(uuid=data["shape"]), pr): logger.warning( f"{pr.player.name} attempted to remove an initiative effect") return location_data = Initiative.get(location=pr.active_location) with db.atomic(): json_data = json.loads(location_data.data) for initiative in json_data: if initiative["shape"] == data["shape"]: initiative["effects"].pop(data["index"]) location_data.data = json.dumps(json_data) location_data.save() await sio.emit( "Initiative.Effect.Remove", data, room=pr.active_location.get_path(), skip_sid=sid, namespace=GAME_NS, )
async def remove_initiative(sid: str, data: str): pr: PlayerRoom = game_state.get(sid) shape = Shape.get_or_none(uuid=data) if shape is not None and not has_ownership(shape, pr): logger.warning( f"{pr.player.name} attempted to remove initiative of an asset it does not own" ) return with db.atomic(): location_data = Initiative.get(location=pr.active_location) json_data = json.loads(location_data.data) location_data.data = json.dumps([ initiative for initiative in json_data if initiative["shape"] != data ]) location_data.save() await sio.emit( "Initiative.Remove", data, room=pr.active_location.get_path(), skip_sid=sid, namespace=GAME_NS, )
async def update_initiative_round(sid: str, data: int): pr: PlayerRoom = game_state.get(sid) location_data = Initiative.get(location=pr.active_location) if pr.role != Role.DM: json_data = json.loads(location_data.data) if not has_ownership( Shape.get_or_none(uuid=json_data[location_data.turn]["shape"]), pr): logger.warning( f"{pr.player.name} attempted to advance the initiative tracker" ) return with db.atomic(): location_data.round = data location_data.save() await sio.emit( "Initiative.Round.Update", data, room=pr.active_location.get_path(), skip_sid=sid, namespace=GAME_NS, )
async def change_initiative_order(sid: str, data: ServerInitiativeOrderChange): pr: PlayerRoom = game_state.get(sid) if pr.role != Role.DM: logger.warning(f"{pr.player.name} attempted to reorder initiatives") return old_index = data["oldIndex"] new_index = data["newIndex"] with db.atomic(): location_data = Initiative.get(location=pr.active_location) json_data = json.loads(location_data.data) if json_data[old_index]["shape"] != data["shape"]: return if json_data[new_index].get( "initiative", 0) != json_data[old_index].get("initiative", 0): location_data.sort = 2 json_data.insert(new_index, json_data.pop(old_index)) json_data = sort_initiative(json_data, location_data.sort) location_data.data = json.dumps(json_data) location_data.save() await sio.emit( "Initiative.Set", location_data.as_dict(), room=pr.active_location.get_path(), namespace=GAME_NS, )
async def set_initiative_value(sid: str, data: ServerSetInitiativeValue): pr: PlayerRoom = game_state.get(sid) shape = Shape.get_or_none(uuid=data) if shape is not None and not has_ownership(shape, pr): logger.warning( f"{pr.player.name} attempted to remove initiative of an asset it does not own" ) return with db.atomic(): location_data = Initiative.get(location=pr.active_location) json_data = json.loads(location_data.data) for initiative in json_data: if initiative["shape"] == data["shape"]: initiative["initiative"] = data["value"] break json_data = sort_initiative(json_data, location_data.sort) location_data.data = json.dumps(json_data) location_data.save() await sio.emit( "Initiative.Set", location_data.as_dict(), room=pr.active_location.get_path(), namespace=GAME_NS, )
async def update_initiative_order(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 change the initiative order") return with db.atomic(): for i, uuid in enumerate(data): init = Initiative.get(uuid=uuid) init.index = i init.save() await send_client_initiatives(pr)
async def update_initiative_order(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 change the initiative order") return location_data = InitiativeLocationData.get(location=location) with db.atomic(): for i, uuid in enumerate(data): init = Initiative.get(uuid=uuid) init.index = i init.save() await send_client_initiatives(room, location)
async def update_initiative_round(sid: str, data: int): pr: PlayerRoom = game_state.get(sid) if pr.role != Role.DM: logger.warning( f"{pr.player.name} attempted to advance the initiative tracker") return location_data = Initiative.get(location=pr.active_location) with db.atomic(): location_data.round = data location_data.save() await sio.emit( "Initiative.Round.Update", data, room=pr.active_location.get_path(), skip_sid=sid, namespace=GAME_NS, )
async def update_initiative_turn(sid: str, turn: int): pr: PlayerRoom = game_state.get(sid) location_data: Initiative = Initiative.get(location=pr.active_location) json_data = json.loads(location_data.data) if pr.role != Role.DM and not has_ownership( Shape.get_or_none(uuid=json_data[location_data.turn]["shape"]), pr): logger.warning( f"{pr.player.name} attempted to advance the initiative tracker") return with db.atomic(): nextTurn = turn > location_data.turn location_data.turn = turn for i, effect in enumerate(json_data[turn]["effects"][-1:]): try: turns = int(effect["turns"]) if turns <= 0 and nextTurn: json_data[turn]["effects"].pop(i) elif turns > 0 and nextTurn: effect["turns"] = str(turns - 1) else: effect["turns"] = str(turns + 1) except ValueError: # For non-number inputs do not update the effect pass location_data.data = json.dumps(json_data) location_data.save() await sio.emit( "Initiative.Turn.Update", turn, room=pr.active_location.get_path(), skip_sid=sid, namespace=GAME_NS, )
async def clear_initiatives(sid: str): pr: PlayerRoom = game_state.get(sid) if pr.role != Role.DM: logger.warning(f"{pr.player.name} attempted to clear all initiatives") return with db.atomic(): location_data = Initiative.get(location=pr.active_location) json_data = json.loads(location_data.data) for initiative in json_data: initiative["initiative"] = None location_data.data = json.dumps(json_data) location_data.save() await sio.emit( "Initiative.Clear", room=pr.active_location.get_path(), namespace=GAME_NS, )