From ff4bca781dbcac99c6be0857edef91641db1ed7c Mon Sep 17 00:00:00 2001 From: AloeSapling Date: Mon, 18 Aug 2025 22:21:31 +0200 Subject: [PATCH] Added schemaManager and schema conversion --- src/schemaManager.ts | 61 ++++++++++++++++++++++++++++++++++++++++++++ types/schemas.ts | 8 +----- types/types.ts | 9 ++++++- 3 files changed, 70 insertions(+), 8 deletions(-) create mode 100644 src/schemaManager.ts diff --git a/src/schemaManager.ts b/src/schemaManager.ts new file mode 100644 index 0000000..fdc82fc --- /dev/null +++ b/src/schemaManager.ts @@ -0,0 +1,61 @@ +import { Schemas, SchemaTypeNames } from "../types/types"; +import { BlueMarbleJSON, CharityJSON, TBlueMarbleJSON, TCharityJSON } from "../types/schemas"; + +class SchemaManager { + constructor(data?: Schemas){ + this.data = data; + if(data){ + if(CharityJSON.parse(data)){this.type = "CHA"} + else if(BlueMarbleJSON.parse(data)){this.type = "BM"} + else {this.type = "N/A"} + } + } + data?: Schemas; + private type: SchemaTypeNames; + toCharity(){ + if(this.type=="CHA"){return;} + if(BlueMarbleJSON.parse(this.data)){ + this.data = this.data as TBlueMarbleJSON; + this.data = CharityJSON.parse({ + meta:{ + whoami: this.data.whoami, + schemaVersion: this.data.schemaVersion, + scriptVersion: this.data.scriptVersion, + }, + templates: this.data.templates.map((template)=>({ + name: template.name, + enabled: template.enabled, + coords: { + tx: template.coords[0], + ty: template.coords[1], + px: template.coords[2], + py: template.coords[3], + }, + sources: [template.urlLink], + author: template.idUser, + uuid: template.idUser, + })), + whitelist: [], + blacklist: [], + }); + } + } + toBlueMarble(){ + if(this.type=="BM"){return;} + if(CharityJSON.parse(this.data)){ + this.data = this.data as TCharityJSON; + this.data = BlueMarbleJSON.parse({ + whoami: this.data.meta.whoami, + schemaVersion: this.data.meta.schemaVersion, + scriptVersion: this.data.meta.scriptVersion, + templates: this.data.templates.map((template)=>({ + name: template.name, + coords: Object.values(template.coords), + idUser: template.author, + enabled: template.enabled, + urlLink: template.sources[0], // IDK about this one + })) + }) + } + } +} \ No newline at end of file diff --git a/types/schemas.ts b/types/schemas.ts index 28a6f6d..0b029bb 100644 --- a/types/schemas.ts +++ b/types/schemas.ts @@ -62,18 +62,12 @@ export const BlueMarbleJSON = z.object({ templates: z.array(z.object({ name: z.string().optional(), coords: z.array(z.number()), - idSort: z.number(), idUser: z.string(), enabled: z.boolean().optional(), urlLink: z.string().optional(), - urlType: z.string().optional(), - file: z.string().optional(), - tiles: z.array(z.object({ - image: z.string(), - coords: z.string(), - })).optional() })) }) + export type TCharityJSON = z.infer; export type TBlueMarbleJSON = z.infer; \ No newline at end of file diff --git a/types/types.ts b/types/types.ts index e5654e2..ccbf7eb 100644 --- a/types/types.ts +++ b/types/types.ts @@ -1,5 +1,12 @@ +import { TBlueMarbleJSON, TCharityJSON } from "./schemas"; + // The Context objects contains data to be passed into the inject function that can only be accessed outside of it export type Context = { HTMLData?: string; CSSUrl?: string; -} \ No newline at end of file +} + + + +export type Schemas = TCharityJSON | TBlueMarbleJSON; +export type SchemaTypeNames = "CHA" | "BM" | "N/A"; // Charity, BlueMarble or unknown \ No newline at end of file