kozukata-toa/src/servers/aimedb/handlers/aime-account.ts

165 lines
4.5 KiB
TypeScript

import {
AimeAccountQueryStruct,
AimeAccountResponseStruct,
AimeAccountExtendedResponseStruct,
AimeAccountExtendedQueryStruct,
} from "../types/aime-account";
import { PacketHeaderStruct } from "../types/header";
import { CommandId, PortalRegistration, ResultCodes } from "../utils/misc";
import { eq } from "drizzle-orm";
import { db } from "external/db/db";
import { card } from "external/db/schemas";
import CreateLogCtx from "lib/logger/logger";
import type { AimeDBHandlerFn } from "../types/handlers";
const logger = CreateLogCtx(__filename);
export const GetAimeAccountHandler: AimeDBHandlerFn<"AimeAccountResponse"> = async (
header,
data
) => {
header.length = PacketHeaderStruct.baseSize + AimeAccountResponseStruct.baseSize;
header.commandId = CommandId.AIME_ACCOUNT_RESPONSE;
header.result = ResultCodes.SUCCESS;
const req = new AimeAccountQueryStruct(data);
const resp = new AimeAccountResponseStruct();
// TODO: Actually handle portal state when we get a webUI
resp.portalRegistered = PortalRegistration.UNREGISTERED;
resp.accountId = -1;
if (req.companyCode < 0 || req.companyCode > 4) {
logger.error("Received unknown company code. Expected a value between 0 and 4 inclusive.", {
req,
});
header.result = ResultCodes.INVALID_AIME_ID;
return resp;
}
const accessCode = Buffer.from(req.accessCode).toString("hex");
const cardRow = await db
.select()
.from(card)
.where(eq(card.accessCode, accessCode))
.then((r) => r[0]);
if (!cardRow) {
return resp;
}
if (cardRow.isBanned && cardRow.isLocked) {
header.result = ResultCodes.BAN_SYSTEM_AND_USER_LOCK;
} else if (cardRow.isBanned) {
header.result = ResultCodes.BAN_SYSTEM_LOCK;
} else if (cardRow.isLocked) {
header.result = ResultCodes.USER_LOCK;
}
resp.accountId = cardRow.id;
return resp;
};
export const RegisterAimeAccountHandler: AimeDBHandlerFn<"AimeAccountResponse"> = async (
header,
data
) => {
header.length = PacketHeaderStruct.baseSize + AimeAccountResponseStruct.baseSize;
header.commandId = CommandId.AIME_ACCOUNT_RESPONSE;
header.result = ResultCodes.SUCCESS;
const req = new AimeAccountQueryStruct(data);
const resp = new AimeAccountResponseStruct();
// TODO: Actually handle portal state when we get a webUI
resp.portalRegistered = PortalRegistration.UNREGISTERED;
resp.accountId = -1;
if (req.companyCode < 0 || req.companyCode > 4) {
logger.error("Received unknown company code. Expected a value between 0 and 4 inclusive.", {
req,
});
header.result = ResultCodes.INVALID_AIME_ID;
return resp;
}
const accessCode = Buffer.from(req.accessCode).toString("hex");
const cardRow = await db
.select()
.from(card)
.where(eq(card.accessCode, accessCode))
.then((r) => r[0]);
if (cardRow) {
header.result = ResultCodes.ID_ALREADY_REGISTERED;
resp.accountId = cardRow.id;
return resp;
}
const newCardRow = await db
.insert(card)
.values({ accessCode })
.returning()
.then((r) => r[0]);
if (!newCardRow) {
logger.crit("Failed to insert new lookup entry into the database.", { accessCode });
header.result = ResultCodes.UNKNOWN_ERROR;
return resp;
}
resp.accountId = newCardRow.id;
return resp;
};
export const GetAimeAccountExtendedHandler: AimeDBHandlerFn<"AimeAccountExtendedResponse"> = async (
header,
data
) => {
header.length = PacketHeaderStruct.baseSize + AimeAccountExtendedResponseStruct.baseSize;
header.commandId = CommandId.EXTENDED_ACCOUNT_RESPONSE;
header.result = ResultCodes.SUCCESS;
const req = new AimeAccountExtendedQueryStruct(data);
const resp = new AimeAccountExtendedResponseStruct();
// TODO: Actually handle portal state when we get a webUI
// TODO: What the fuck is an auth key
resp.portalRegistered = PortalRegistration.UNREGISTERED;
resp.accountId = -1;
if (req.companyCode < 0 || req.companyCode > 4) {
logger.error("Received unknown company code. Expected a value between 0 and 4 inclusive.", {
req,
});
header.result = ResultCodes.INVALID_AIME_ID;
return resp;
}
const accessCode = Buffer.from(req.accessCode).toString("hex");
const cardRow = await db
.select()
.from(card)
.where(eq(card.accessCode, accessCode))
.then((r) => r[0]);
if (!cardRow) {
resp.accountId = -1;
return resp;
}
if (cardRow.isBanned && cardRow.isLocked) {
header.result = ResultCodes.BAN_SYSTEM_AND_USER_LOCK;
} else if (cardRow.isBanned) {
header.result = ResultCodes.BAN_SYSTEM_LOCK;
} else if (cardRow.isLocked) {
header.result = ResultCodes.USER_LOCK;
}
resp.accountId = cardRow.id;
return resp;
};