refactor: Use a generic serializer for attributableObject and use polymorphism for world

This commit is contained in:
2026-07-06 12:42:43 +05:30
parent 0a227d8046
commit 8148e7cbbb
5 changed files with 42 additions and 37 deletions

View File

@@ -112,4 +112,14 @@ export abstract class AttributableObject implements IAttribute {
attr.hasAccess(viewerId),
);
}
serialize(): string {
const lines: string[] = [];
for (const [name, attr] of this.attributes.entries()) {
const aclList = Array.from(attr.getAllowedEntities());
const aclStr = aclList.length > 0 ? ` (Visible to: ${aclList.join(", ")})` : "";
lines.push(`* ${name}: ${attr.getValue()} (Visibility: ${attr.getVisibility()})${aclStr}`);
}
return lines.join("\n");
}
}

View File

@@ -27,4 +27,33 @@ export class WorldState extends AttributableObject {
getEntity(id: string): Entity | undefined {
return this.entities.get(id);
}
override serialize(): string {
const lines: string[] = [];
// 1. Serialize self attributes (world attributes)
const selfSerialized = super.serialize();
if (selfSerialized) {
lines.push("World Attributes:");
lines.push(selfSerialized.split("\n").map(l => " " + l).join("\n"));
}
// 2. Serialize entities
lines.push("Entities:");
if (this.entities.size > 0) {
for (const entity of this.entities.values()) {
lines.push(` - Entity [ID: ${entity.id}]:`);
const entitySerialized = entity.serialize();
if (entitySerialized) {
lines.push(entitySerialized.split("\n").map(l => " " + l).join("\n"));
} else {
lines.push(" * (No attributes)");
}
}
} else {
lines.push(" (No entities)");
}
return lines.join("\n");
}
}