mirror of
https://github.com/sortedcord/omnia.git
synced 2026-07-22 03:52:48 +05:30
refactor: Use a generic serializer for attributableObject and use polymorphism for world
This commit is contained in:
@@ -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");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user