update realm models

This commit is contained in:
ppc
2025-01-12 19:44:06 +00:00
parent c8763dd65e
commit adb60dd928
4 changed files with 50 additions and 12 deletions
+10 -5
View File
@@ -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<ICCard | null> {
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<ICCard | null> {
switch (tag['systemCode'] as string) {
case "0003":
return {
type: "transport",
type: ICType.Transport,
idm
};
@@ -111,7 +116,7 @@ async function readAmusementICCardContents(idm: string): Promise<AmusementICCard
const dfcValue = response[37] | (response[36] << 8);
return {
type: "amusement",
type: ICType.Amusement,
accessCode: [...accessCode.accessCodeBytes].map(x => x.toString(16).padStart(2, '0')).join(''),
vendor: dfcValue in AmusementICVendor ? dfcValue : AmusementICVendor.Unknown,
idm
+35 -2
View File
@@ -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<AMCard> implements AMCardData {
@@ -18,6 +20,8 @@ export class AMCard extends Realm.Object<AMCard> 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<AMCard> 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<AMCard> 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<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}
}
};
}
+2 -2
View File
@@ -40,9 +40,9 @@ export class AMServer extends Realm.Object<AMServer> 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}
}
};
}
+3 -3
View File
@@ -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
});