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

128 lines
3.1 KiB
TypeScript

import { CommandId, LogStatus, ResultCodes } from "../../../lib/constants/aimedb";
import {
AimeLogStruct,
AimeLogExtendedResponseStruct,
ExtendedAimeLogStruct,
StatusLogStruct,
} from "../types/aime-log";
import { PacketHeaderStruct } from "../types/header";
import { AppDataSource } from "external/db/data-source";
import { EventLog } from "external/db/entity/base";
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;
}
const eventLog = EventLog.construct({
system: "aimedb",
type: `LOG_${statusName}`,
severity: "info",
details: { aimeId: req.aimeId },
});
await eventLog.save();
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;
}
const eventLog = EventLog.construct({
system: "aimedb",
type: `LOG_${statusName}`,
severity: "info",
details: {
aimeId: req.aimeId,
userId: req.userId,
creditCount: req.creditCount,
betCount: req.betCount,
wonCount: req.wonCount,
},
});
await eventLog.save();
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 AppDataSource.transaction(async (em) => {
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;
}
const eventLog = EventLog.construct({
system: "aimedb",
type: `LOG_${statusName}`,
severity: "info",
details: {
aimeId: entry.aimeId,
userId: entry.userId.toString(),
creditCount: entry.creditCount,
betCount: entry.betCount,
wonCount: entry.wonCount,
},
});
ops.push(em.save(eventLog));
}
await Promise.all(ops);
});
return resp;
};