Files
ppc_amnet/amnet-native/models/AMCard.ts
T
2025-04-01 20:20:06 +01:00

84 lines
2.5 KiB
TypeScript

import {Realm} from "realm";
import {AmusementICVendor, ICType} from "../lib/iccard";
import {AMCardRecentUse} from "./AMCardRecentUse";
import {AMCardData} from "./AMCardData";
export class AMCard extends Realm.Object<AMCard> implements AMCardData {
_id!: Realm.BSON.ObjectId;
code: string;
name: string;
added: Date;
lastUsed: Date;
recentUses: Realm.Results<AMCardRecentUse>;
physicalCardData?: AMPhysicalCardData | null = null;
get isLimitedAimeCard() {
// limited cards use the company code 500
return this.physicalCardData?.cardType === ICType.Amusement && this.code.startsWith('500');
}
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);
}
// remove any usage history tied to the card
realm.delete(card.recentUses);
realm.delete(card);
});
}
static schema: Realm.ObjectSchema = {
name: "card",
primaryKey: "_id",
properties: {
_id: "objectId",
code: "string",
name: {type: "string", default: "New Card", indexed: true},
added: {type: "date", default: new Date(), indexed: true},
lastUsed: {type: "date", optional: true, indexed: true},
usageHistory: {type: "linkingObjects", objectType: "cardUsageHistory", property: "card"},
physicalCardData: {type: "object", objectType: "physicalCard", optional: true}
}
};
}
/**
* Holds physical card information when a card is imported via NFC/FeliCa
*/
export class AMPhysicalCardData extends Realm.Object<AMPhysicalCardData> {
cardIDm: string;
cardType: ICType;
cardVendor: AmusementICVendor | null;
get isBootlegAmusementCard() {
// real cards have an IDm manufacturer code of 012E (first two bytes of the IDm or in this case, the first 4 characters)
return this.cardType === ICType.Amusement && !this.cardIDm.startsWith("012E");
}
static schema: Realm.ObjectSchema = {
name: "physicalCard",
primaryKey: "cardIDm",
properties: {
cardIDm: "string",
cardType: "int",
cardVendor: {type: "int", optional: true}
}
};
}