import {Realm} from "realm"; import {AmusementICVendor, ICType} from "../lib/iccard"; import {AMCardRecentUse} from "./AMCardRecentUse"; import {AMCardData} from "./AMCardData"; export class AMCard extends Realm.Object implements AMCardData { _id!: Realm.BSON.ObjectId; code: string; name: string; added: Date; lastUsed: Date; recentUses: Realm.Results; 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 { 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} } }; }