Files
ppc_amnet/amnet-native/models/AMCard.ts
T
2025-01-12 19:44:06 +00:00

76 lines
1.9 KiB
TypeScript

import {Realm} from "realm";
import {AmusementICVendor, ICType} from "../lib/iccard";
export interface AMCardData {
code: string;
name: string;
added: Date;
lastUsed?: Date | null;
physicalCardData?: AMPhysicalCardData | null;
}
export class AMCard extends Realm.Object<AMCard> implements AMCardData {
_id!: Realm.BSON.ObjectId;
code: string;
name: string;
added: Date;
lastUsed?: Date;
lastUsedGameId?: string | null;
physicalCardData?: AMPhysicalCardData | null = null;
static generate(code: string, name: string) {
return {
_id: new Realm.BSON.ObjectId(),
added: new Date(),
code: code,
name: name
};
}
static delete(realm: Realm, card: AMCard) {
realm.write(() => {
if (card.physicalCardData) {
realm.delete(card.physicalCardData);
}
realm.delete(card);
});
}
static schema: Realm.ObjectSchema = {
name: "card",
primaryKey: "_id",
properties: {
_id: "objectId",
code: "string",
name: {type: "string", default: "New Card"},
added: {type: "date", default: new Date()},
lastUsed: {type: "date", optional: true, indexed: true},
lastUsedGameId: {type: "string", optional: true, indexed: true},
physicalCardData: {type: "object", objectType: "physicalCard", optional: true}
}
};
}
export class AMPhysicalCardData extends Realm.Object<AMPhysicalCardData> {
cardIDm: string;
cardType: ICType;
cardVendor: AmusementICVendor | null;
static schema: Realm.ObjectSchema = {
name: "physicalCard",
primaryKey: "cardIDm",
properties: {
cardIDm: "string",
cardType: "int",
cardVendor: {type: "int", optional: true}
}
};
}