games_app/backend/dist-test-faq/controllers/gamePlayController.js

161 lines
5.2 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getState = getState;
exports.placeBet = placeBet;
exports.getMyBets = getMyBets;
exports.getAutoSettings = getAutoSettings;
exports.updateAutoSettings = updateAutoSettings;
exports.customGet = customGet;
const errors_1 = require("../games/errors");
const gamePlayService_1 = require("../services/gamePlayService");
const placeBetResponse_1 = require("../utils/placeBetResponse");
function buildCtx(req) {
return {
userId: req.user?.userId,
role: req.user?.role,
roles: req.user?.roles,
query: req.query,
body: (req.body ?? {}),
params: req.params,
};
}
function sendGameError(res, e, fallbackStatus = 500) {
if (e instanceof errors_1.GameNotFoundError) {
res.status(404).json({ error: e.message });
return;
}
if (e instanceof errors_1.GameNotImplementedError || e instanceof errors_1.GameNotRegisteredError) {
res.status(501).json({ error: e.message });
return;
}
if (e instanceof errors_1.UnsupportedGameActionError) {
res.status(404).json({ error: e.message });
return;
}
if (e instanceof Error && e.message === 'Unauthorized') {
res.status(401).json({ error: 'Unauthorized' });
return;
}
const msg = e instanceof Error ? e.message : 'Request failed';
res.status(fallbackStatus).json({ error: msg });
}
function assertAction(mod, action) {
if (!mod.capabilities.actions.includes(action)) {
throw new errors_1.UnsupportedGameActionError(action);
}
}
async function getState(req, res) {
try {
const { mod } = await (0, gamePlayService_1.resolveModuleBySlug)(req.params.slug);
assertAction(mod, 'state');
const state = await mod.getState(buildCtx(req));
res.json(state);
}
catch (e) {
sendGameError(res, e, 500);
}
}
async function placeBet(req, res) {
try {
if (!req.user?.userId) {
res.status(401).json({ error: 'Unauthorized' });
return;
}
const { mod } = await (0, gamePlayService_1.resolveModuleBySlug)(req.params.slug);
assertAction(mod, 'placeBet');
const body = (req.body ?? {});
const ctx = buildCtx(req);
ctx.body = {
...body,
idempotencyKey: (0, placeBetResponse_1.readIdempotencyKey)(req, body) ?? body.idempotencyKey,
};
const bet = await mod.placeBet(ctx);
if (bet &&
typeof bet === 'object' &&
'status' in bet &&
bet.status === 'queued') {
(0, placeBetResponse_1.sendPlaceBetResult)(res, bet);
return;
}
if (bet &&
typeof bet === 'object' &&
'status' in bet &&
bet.status === 'accepted') {
(0, placeBetResponse_1.sendPlaceBetResult)(res, bet);
return;
}
res.status(201).json(bet);
}
catch (e) {
sendGameError(res, e, 400);
}
}
async function getMyBets(req, res) {
try {
if (!req.user?.userId) {
res.status(401).json({ error: 'Unauthorized' });
return;
}
const { mod } = await (0, gamePlayService_1.resolveModuleBySlug)(req.params.slug);
assertAction(mod, 'myBets');
const bets = await mod.getMyBets(buildCtx(req));
res.json(bets);
}
catch (e) {
sendGameError(res, e, 500);
}
}
async function getAutoSettings(req, res) {
try {
if (!req.user?.userId) {
res.status(401).json({ error: 'Unauthorized' });
return;
}
const { mod } = await (0, gamePlayService_1.resolveModuleBySlug)(req.params.slug);
assertAction(mod, 'auto.get');
if (!mod.getAutoSettings) {
throw new errors_1.UnsupportedGameActionError('auto.get');
}
const settings = await mod.getAutoSettings(buildCtx(req));
res.json(settings);
}
catch (e) {
sendGameError(res, e, 500);
}
}
async function updateAutoSettings(req, res) {
try {
if (!req.user?.userId) {
res.status(401).json({ error: 'Unauthorized' });
return;
}
const { mod } = await (0, gamePlayService_1.resolveModuleBySlug)(req.params.slug);
assertAction(mod, 'auto.update');
if (!mod.updateAutoSettings) {
throw new errors_1.UnsupportedGameActionError('auto.update');
}
const settings = await mod.updateAutoSettings(buildCtx(req));
res.json(settings);
}
catch (e) {
sendGameError(res, e, 400);
}
}
async function customGet(req, res) {
try {
const action = req.params.customAction;
const { mod } = await (0, gamePlayService_1.resolveModuleBySlug)(req.params.slug);
if (!mod.capabilities.customGet?.includes(action)) {
throw new errors_1.UnsupportedGameActionError(action);
}
if (!mod.handleCustom) {
throw new errors_1.UnsupportedGameActionError(action);
}
const result = await mod.handleCustom(action, 'GET', buildCtx(req));
res.json(result);
}
catch (e) {
sendGameError(res, e, 500);
}
}
//# sourceMappingURL=gamePlayController.js.map