157 lines
4.2 KiB
TypeScript
157 lines
4.2 KiB
TypeScript
import { CommandId, PortalRegistration, ResultCodes } from "../../../lib/constants/aimedb";
|
|
import {
|
|
AimeAccountQueryStruct,
|
|
AimeAccountResponseStruct,
|
|
AimeAccountExtendedResponseStruct,
|
|
AimeAccountExtendedQueryStruct,
|
|
} from "../types/aime-account";
|
|
import { PacketHeaderStruct } from "../types/header";
|
|
import { AimeCard } from "external/db/entity/aimedb";
|
|
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;
|
|
}
|
|
|
|
// TODO: Verify access code validity
|
|
const accessCode = Buffer.from(req.accessCode).toString("hex");
|
|
|
|
const card = await AimeCard.findOne({
|
|
where: { accessCode },
|
|
});
|
|
|
|
if (!card) {
|
|
return resp;
|
|
}
|
|
|
|
if (card.isBanned && card.isLocked) {
|
|
header.result = ResultCodes.BAN_SYSTEM_AND_USER_LOCK;
|
|
} else if (card.isBanned) {
|
|
header.result = ResultCodes.BAN_SYSTEM_LOCK;
|
|
} else if (card.isLocked) {
|
|
header.result = ResultCodes.USER_LOCK;
|
|
}
|
|
|
|
resp.accountId = card.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 card = await AimeCard.findOne({
|
|
where: {
|
|
accessCode,
|
|
},
|
|
});
|
|
|
|
if (card) {
|
|
header.result = ResultCodes.ID_ALREADY_REGISTERED;
|
|
resp.accountId = card.id;
|
|
return resp;
|
|
}
|
|
|
|
const newCard = AimeCard.construct({ accessCode });
|
|
|
|
try {
|
|
await newCard.save();
|
|
} catch (err) {
|
|
logger.crit("Failed to insert new lookup entry into the database.", { err });
|
|
header.result = ResultCodes.UNKNOWN_ERROR;
|
|
return resp;
|
|
}
|
|
|
|
resp.accountId = newCard.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
|
|
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 card = await AimeCard.findOne({
|
|
where: { accessCode },
|
|
});
|
|
|
|
if (!card) {
|
|
resp.accountId = -1;
|
|
return resp;
|
|
}
|
|
|
|
if (card.isBanned && card.isLocked) {
|
|
header.result = ResultCodes.BAN_SYSTEM_AND_USER_LOCK;
|
|
} else if (card.isBanned) {
|
|
header.result = ResultCodes.BAN_SYSTEM_LOCK;
|
|
} else if (card.isLocked) {
|
|
header.result = ResultCodes.USER_LOCK;
|
|
}
|
|
|
|
resp.accountId = card.id;
|
|
return resp;
|
|
};
|