diff --git a/amnet-native/lib/iccard.ts b/amnet-native/lib/iccard.ts index 88288de..662ae55 100644 --- a/amnet-native/lib/iccard.ts +++ b/amnet-native/lib/iccard.ts @@ -3,6 +3,11 @@ import NfcManager, {NfcTech} from "react-native-nfc-manager"; import {performDecrypt} from "./iccard-crypto"; +export enum ICType { + Amusement = 1, + Transport = 2 +} + export enum AmusementICVendor { Sega = 0x0078, Konami = 0x0068, @@ -13,7 +18,7 @@ export enum AmusementICVendor { } type AmusementICCard = { - type: "amusement", + type: ICType.Amusement, idm: string, accessCode: string, @@ -21,7 +26,7 @@ type AmusementICCard = { } type TransportICCard = { - type: "transport", + type: ICType.Transport, idm: string } @@ -45,7 +50,7 @@ export async function felicaCardReadAsync(): Promise { await NfcManager.requestTechnology(NfcTech.FelicaIOS, { isReaderModeEnabled: true, invalidateAfterFirstRead: false, - alertMessage: "Scan an Amusement or Transport card to get started" + alertMessage: "Place an Amusement or Transport IC card on your device." }); const tag = await NfcManager.getTag(); @@ -54,7 +59,7 @@ export async function felicaCardReadAsync(): Promise { switch (tag['systemCode'] as string) { case "0003": return { - type: "transport", + type: ICType.Transport, idm }; @@ -111,7 +116,7 @@ async function readAmusementICCardContents(idm: string): Promise x.toString(16).padStart(2, '0')).join(''), vendor: dfcValue in AmusementICVendor ? dfcValue : AmusementICVendor.Unknown, idm diff --git a/amnet-native/models/AMCard.ts b/amnet-native/models/AMCard.ts index 978faba..c46412a 100644 --- a/amnet-native/models/AMCard.ts +++ b/amnet-native/models/AMCard.ts @@ -1,4 +1,5 @@ import {Realm} from "realm"; +import {AmusementICVendor, ICType} from "../lib/iccard"; export interface AMCardData { code: string; @@ -6,6 +7,7 @@ export interface AMCardData { added: Date; lastUsed?: Date | null; + physicalCardData?: AMPhysicalCardData | null; } export class AMCard extends Realm.Object implements AMCardData { @@ -18,6 +20,8 @@ export class AMCard extends Realm.Object implements AMCardData { lastUsed?: Date; lastUsedGameId?: string | null; + physicalCardData?: AMPhysicalCardData | null = null; + static generate(code: string, name: string) { return { _id: new Realm.BSON.ObjectId(), @@ -27,6 +31,16 @@ export class AMCard extends Realm.Object implements AMCardData { }; } + 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", @@ -36,8 +50,27 @@ export class AMCard extends Realm.Object implements AMCardData { code: "string", name: {type: "string", default: "New Card"}, added: {type: "date", default: new Date()}, - lastUsed: {type: "date", optional: true}, - lastUsedGameId: {type: "string", optional: true} + 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 { + + 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} } }; } \ No newline at end of file diff --git a/amnet-native/models/AMServer.ts b/amnet-native/models/AMServer.ts index 08e5f33..b7b227d 100644 --- a/amnet-native/models/AMServer.ts +++ b/amnet-native/models/AMServer.ts @@ -40,9 +40,9 @@ export class AMServer extends Realm.Object implements AMServerConnecti apiVersion: {type: "int", default: 0}, gameId: {type: "string", optional: true}, serverAddress: "string", - serverName: {type: "string", default: "New Server"}, + serverName: {type: "string", default: "New Server", indexed: "full-text"}, createdAt: {type: "date", default: new Date()}, - lastUsed: {type: "date", optional: true} + lastUsed: {type: "date", optional: true, indexed: true} } }; } \ No newline at end of file diff --git a/amnet-native/models/RealmContext.ts b/amnet-native/models/RealmContext.ts index 5bd5ecf..6ecd846 100644 --- a/amnet-native/models/RealmContext.ts +++ b/amnet-native/models/RealmContext.ts @@ -1,10 +1,10 @@ import {createRealmContext} from "@realm/react"; -import {AMCard} from "./AMCard"; +import {AMCard, AMPhysicalCardData} from "./AMCard"; import {AMServer} from "./AMServer"; // @ts-ignore export default createRealmContext({ - schemaVersion: 3, - schema: [AMCard, AMServer], + schemaVersion: 4, + schema: [AMPhysicalCardData, AMCard, AMServer], deleteRealmIfMigrationNeeded: false }); \ No newline at end of file