125 lines
3.0 KiB
TypeScript
125 lines
3.0 KiB
TypeScript
import {
|
|
AimeLogStruct,
|
|
AimeLogExtendedResponseStruct,
|
|
ExtendedAimeLogStruct,
|
|
StatusLogStruct,
|
|
} from "../types/aime-log";
|
|
import { PacketHeaderStruct } from "../types/header";
|
|
import { CommandId, LogStatus, ResultCodes } from "../utils/misc";
|
|
import { db } from "external/db/db";
|
|
import { eventLog } from "external/db/schemas";
|
|
import CreateLogCtx from "lib/logger/logger";
|
|
import type { AimeDBHandlerFn } from "../types/handlers";
|
|
|
|
const logger = CreateLogCtx(__filename);
|
|
|
|
export const StatusLogHandler: AimeDBHandlerFn = async (header, data) => {
|
|
header.commandId = CommandId.STATUS_LOG_RESPONSE;
|
|
header.result = ResultCodes.SUCCESS;
|
|
header.length = PacketHeaderStruct.baseSize;
|
|
|
|
const req = new StatusLogStruct(data);
|
|
const statusName = LogStatus[req.status];
|
|
|
|
if (!statusName) {
|
|
logger.error("Unknown status for logging. Expected a value between 0 and 4.", { req });
|
|
header.result = ResultCodes.UNKNOWN_ERROR;
|
|
return null;
|
|
}
|
|
|
|
await db.insert(eventLog).values({
|
|
system: "aimedb",
|
|
type: `AIMEDB_LOG_${statusName}`,
|
|
severity: "info",
|
|
details: { aimeId: req.aimeId },
|
|
});
|
|
|
|
return null;
|
|
};
|
|
|
|
export const AimeLogHandler: AimeDBHandlerFn = async (header, data) => {
|
|
header.commandId = CommandId.AIME_LOG_RESPONSE;
|
|
header.result = ResultCodes.SUCCESS;
|
|
header.length = PacketHeaderStruct.baseSize;
|
|
|
|
const req = new AimeLogStruct(data);
|
|
const statusName = LogStatus[req.status];
|
|
|
|
if (!statusName) {
|
|
logger.error("Unknown status for logging. Expected a value between 0 and 4.", { req });
|
|
header.result = ResultCodes.UNKNOWN_ERROR;
|
|
return null;
|
|
}
|
|
|
|
await db.insert(eventLog).values({
|
|
system: "aimedb",
|
|
type: `AIMEDB_LOG_${statusName}`,
|
|
severity: "info",
|
|
details: {
|
|
aimeId: req.aimeId,
|
|
userId: req.userId,
|
|
creditCount: req.creditCount,
|
|
betCount: req.betCount,
|
|
wonCount: req.wonCount,
|
|
},
|
|
});
|
|
|
|
return null;
|
|
};
|
|
|
|
export const AimeExtendedLogHandler: AimeDBHandlerFn<"AimeLogExtendedResponse"> = async (
|
|
header,
|
|
data
|
|
) => {
|
|
header.commandId = CommandId.EXTENDED_AIME_LOG_RESPONSE;
|
|
header.result = ResultCodes.SUCCESS;
|
|
header.length = PacketHeaderStruct.baseSize;
|
|
|
|
const req = new ExtendedAimeLogStruct(data);
|
|
const resp = new AimeLogExtendedResponseStruct();
|
|
|
|
await db.transaction(async (tx) => {
|
|
const ops = [];
|
|
|
|
for (let i = 0; i < req.count; i++) {
|
|
const entry = req.entries[i];
|
|
|
|
if (!entry) {
|
|
throw new Error(
|
|
"There was an undefined value in the log entries. This should not be possible!"
|
|
);
|
|
}
|
|
|
|
const statusName = LogStatus[entry.status];
|
|
|
|
if (!statusName) {
|
|
logger.error(
|
|
"Unknown status for logging. Expected a value between 0 and 4.",
|
|
entry
|
|
);
|
|
continue;
|
|
}
|
|
|
|
ops.push(
|
|
tx.insert(eventLog).values({
|
|
system: "aimedb",
|
|
type: `AIMEDB_LOG_${statusName}`,
|
|
severity: "info",
|
|
details: {
|
|
aimeId: entry.aimeId,
|
|
userId: entry.userId.toString(),
|
|
creditCount: entry.creditCount,
|
|
betCount: entry.betCount,
|
|
wonCount: entry.wonCount,
|
|
},
|
|
})
|
|
);
|
|
resp.result[i] = 1;
|
|
}
|
|
|
|
await Promise.all(ops);
|
|
});
|
|
|
|
return resp;
|
|
};
|