Added schemaManager and schema conversion

This commit is contained in:
AloeSapling 2025-08-18 22:21:31 +02:00
parent f0396ab5be
commit ff4bca781d
3 changed files with 70 additions and 8 deletions

61
src/schemaManager.ts Normal file
View file

@ -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
}))
})
}
}
}

View file

@ -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<typeof CharityJSON>;
export type TBlueMarbleJSON = z.infer<typeof BlueMarbleJSON>;

View file

@ -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;
}
}
export type Schemas = TCharityJSON | TBlueMarbleJSON;
export type SchemaTypeNames = "CHA" | "BM" | "N/A"; // Charity, BlueMarble or unknown