python3 code:
class ConnectionManager:
STATE_FILE = "/app/data/state.pkl"

def __init__(self):
    self.active_connections: Dict[int, WebSocket] = {}
    self.state = {
        "global": {},
        "local": {}
    }

    # expose app.state.global_state and app.state.local_state as properties
    # self.state = app.state.internal_shared_sate


    self.load_state()

def save_state(self):
    with open(self.STATE_FILE, "wb") as f:
        pickle.dump(self.state, f)

def load_state(self):
    if os.path.exists(self.STATE_FILE):
        try:
            with open(self.STATE_FILE, "rb") as f:
                self.state = pickle.load(f)
        except Exception as e:
            print(f"Error loading state: {e}")

async def connect(self, websocket: WebSocket, client_id: str):
    await websocket.accept()
    self.active_connections[client_id] = websocket

def disconnect(self, client_id: int):
    if client_id in self.active_connections:
        try:
            # In case a client disconnects without having logged in.
            del websocket_client_id_username[client_id]
        except KeyError:
            pass
        del self.active_connections[client_id]

async def apply_global_mutations(self , mutations: dict, sync=True):
    for key, value in mutations.items():
        self.state["global"][key] = value
    if sync:
        await self.sync_global_state()

async def apply_local_mutations(self, client_id: str, mutations: dict, sync=True):
    username = websocket_client_id_username[client_id]
    if username not in self.state["local"]:
        self.state["local"][username] = {}
    for key, value in mutations.items():
        self.state["local"][username][key] = value
    if sync:
        await self.sync_local_state(client_id)

async def send_personal_message(self, client_id: str, message: str):
    if client_id in self.active_connections:
        websocket = self.active_connections[client_id]
        try:
            await websocket.send_text(message)
        except Exception as e:
            print(f"Error sending message to {client_id}: {e}")
            self.disconnect(client_id)

async def broadcast(self, message: str):
    for client_id in list(self.active_connections.keys()):
        await self.send_personal_message(message, client_id)

    # Sync all states for all

async def global_sync(self):
    await self.sync_global_state()
    await self.sync_local_states_for_all()

async def sync_global_state(self):
    state_message = {
        "type": "sync",
        "scope": "global",
        "state": self.state["g The letter o should appear at least 30 times.
user