332 lines
12 KiB
JavaScript
332 lines
12 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
const express_1 = require("express");
|
|
const index_1 = require("../index");
|
|
const auth_1 = require("../middleware/auth");
|
|
const uuid_1 = require("uuid");
|
|
const router = (0, express_1.Router)();
|
|
router.get('/', async (req, res) => {
|
|
try {
|
|
const { search, status } = req.query;
|
|
const where = {
|
|
visibility: 'PUBLIC'
|
|
};
|
|
if (status) {
|
|
where.status = status;
|
|
}
|
|
if (search) {
|
|
where.name = { contains: search, mode: 'insensitive' };
|
|
}
|
|
const games = await index_1.prisma.game.findMany({
|
|
where,
|
|
include: {
|
|
gameMaster: { select: { id: true, name: true } },
|
|
_count: { select: { teams: true, routes: true } }
|
|
},
|
|
orderBy: { createdAt: 'desc' }
|
|
});
|
|
res.json(games);
|
|
}
|
|
catch (error) {
|
|
console.error('List games error:', error);
|
|
res.status(500).json({ error: 'Failed to list games' });
|
|
}
|
|
});
|
|
router.get('/my-games', auth_1.authenticate, async (req, res) => {
|
|
try {
|
|
const games = await index_1.prisma.game.findMany({
|
|
where: { gameMasterId: req.user.id },
|
|
include: {
|
|
_count: { select: { teams: true, routes: true } }
|
|
},
|
|
orderBy: { createdAt: 'desc' }
|
|
});
|
|
res.json(games);
|
|
}
|
|
catch (error) {
|
|
console.error('My games error:', error);
|
|
res.status(500).json({ error: 'Failed to get games' });
|
|
}
|
|
});
|
|
router.get('/:id', async (req, res) => {
|
|
try {
|
|
const { id } = req.params;
|
|
const game = await index_1.prisma.game.findUnique({
|
|
where: { id: id },
|
|
include: {
|
|
gameMaster: { select: { id: true, name: true } },
|
|
routes: {
|
|
include: {
|
|
routeLegs: { orderBy: { sequenceNumber: 'asc' } }
|
|
}
|
|
},
|
|
teams: {
|
|
include: {
|
|
members: { include: { user: { select: { id: true, name: true, email: true } } } },
|
|
teamRoutes: {
|
|
include: {
|
|
route: {
|
|
include: {
|
|
routeLegs: { orderBy: { sequenceNumber: 'asc' } }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
});
|
|
if (!game) {
|
|
return res.status(404).json({ error: 'Game not found' });
|
|
}
|
|
const isOwner = req.user?.id === game.gameMasterId;
|
|
if (game.visibility === 'PRIVATE' && !isOwner) {
|
|
return res.status(403).json({ error: 'Access denied' });
|
|
}
|
|
const result = {
|
|
...game,
|
|
rules: game.rules ? JSON.parse(game.rules) : []
|
|
};
|
|
res.json(result);
|
|
}
|
|
catch (error) {
|
|
console.error('Get game error:', error);
|
|
res.status(500).json({ error: 'Failed to get game' });
|
|
}
|
|
});
|
|
router.post('/', auth_1.authenticate, async (req, res) => {
|
|
try {
|
|
const { name, description, prizeDetails, visibility, startDate, locationLat, locationLng, searchRadius, rules, randomizeRoutes } = req.body;
|
|
if (!name) {
|
|
return res.status(400).json({ error: 'Name is required' });
|
|
}
|
|
if (startDate && new Date(startDate) < new Date()) {
|
|
return res.status(400).json({ error: 'Start date must be in the future' });
|
|
}
|
|
const inviteCode = (0, uuid_1.v4)().slice(0, 8);
|
|
const game = await index_1.prisma.game.create({
|
|
data: {
|
|
name,
|
|
description,
|
|
prizeDetails,
|
|
visibility: visibility || 'PUBLIC',
|
|
startDate: startDate ? new Date(startDate) : null,
|
|
locationLat,
|
|
locationLng,
|
|
searchRadius,
|
|
rules: rules ? JSON.stringify(rules) : null,
|
|
randomizeRoutes: randomizeRoutes || false,
|
|
gameMasterId: req.user.id,
|
|
inviteCode
|
|
}
|
|
});
|
|
res.json(game);
|
|
}
|
|
catch (error) {
|
|
console.error('Create game error:', error);
|
|
res.status(500).json({ error: 'Failed to create game' });
|
|
}
|
|
});
|
|
router.put('/:id', auth_1.authenticate, async (req, res) => {
|
|
try {
|
|
const id = req.params.id;
|
|
const { name, description, prizeDetails, visibility, startDate, locationLat, locationLng, searchRadius, rules, status, randomizeRoutes } = req.body;
|
|
const game = await index_1.prisma.game.findUnique({ where: { id } });
|
|
if (!game) {
|
|
return res.status(404).json({ error: 'Game not found' });
|
|
}
|
|
if (game.gameMasterId !== req.user.id) {
|
|
return res.status(403).json({ error: 'Not authorized' });
|
|
}
|
|
const updated = await index_1.prisma.game.update({
|
|
where: { id: id },
|
|
data: {
|
|
name,
|
|
description,
|
|
prizeDetails,
|
|
visibility,
|
|
startDate: startDate ? new Date(startDate) : undefined,
|
|
locationLat,
|
|
locationLng,
|
|
searchRadius,
|
|
rules: rules ? JSON.stringify(rules) : undefined,
|
|
randomizeRoutes,
|
|
status
|
|
}
|
|
});
|
|
res.json(updated);
|
|
}
|
|
catch (error) {
|
|
console.error('Update game error:', error);
|
|
res.status(500).json({ error: 'Failed to update game' });
|
|
}
|
|
});
|
|
router.delete('/:id', auth_1.authenticate, async (req, res) => {
|
|
try {
|
|
const id = req.params.id;
|
|
const game = await index_1.prisma.game.findUnique({ where: { id } });
|
|
if (!game) {
|
|
return res.status(404).json({ error: 'Game not found' });
|
|
}
|
|
if (game.gameMasterId !== req.user.id) {
|
|
return res.status(403).json({ error: 'Not authorized' });
|
|
}
|
|
if (game.status !== 'DRAFT') {
|
|
return res.status(400).json({ error: 'Only draft games can be deleted' });
|
|
}
|
|
await index_1.prisma.game.delete({ where: { id } });
|
|
res.json({ message: 'Game deleted' });
|
|
}
|
|
catch (error) {
|
|
console.error('Delete game error:', error);
|
|
res.status(500).json({ error: 'Failed to delete game' });
|
|
}
|
|
});
|
|
router.post('/:id/publish', auth_1.authenticate, async (req, res) => {
|
|
try {
|
|
const { id } = req.params;
|
|
const game = await index_1.prisma.game.findUnique({
|
|
where: { id: id },
|
|
include: { routes: { include: { routeLegs: true } } }
|
|
});
|
|
if (!game) {
|
|
return res.status(404).json({ error: 'Game not found' });
|
|
}
|
|
if (game.gameMasterId !== req.user.id) {
|
|
return res.status(403).json({ error: 'Not authorized' });
|
|
}
|
|
if (!game.routes || game.routes.length === 0) {
|
|
return res.status(400).json({ error: 'Game must have at least one route' });
|
|
}
|
|
const hasValidRoute = game.routes.some(route => route.routeLegs.length > 0);
|
|
if (!hasValidRoute) {
|
|
return res.status(400).json({ error: 'At least one route must have legs' });
|
|
}
|
|
if (game.randomizeRoutes) {
|
|
const teams = await index_1.prisma.team.findMany({
|
|
where: { gameId: id }
|
|
});
|
|
const shuffledRoutes = [...game.routes].sort(() => Math.random() - 0.5);
|
|
for (let i = 0; i < teams.length; i++) {
|
|
const routeIndex = i % game.routes.length;
|
|
await index_1.prisma.teamRoute.upsert({
|
|
where: { teamId: teams[i].id },
|
|
create: {
|
|
teamId: teams[i].id,
|
|
routeId: shuffledRoutes[routeIndex].id
|
|
},
|
|
update: {
|
|
routeId: shuffledRoutes[routeIndex].id
|
|
}
|
|
});
|
|
}
|
|
}
|
|
const updated = await index_1.prisma.game.update({
|
|
where: { id: id },
|
|
data: { status: 'LIVE' }
|
|
});
|
|
res.json(updated);
|
|
}
|
|
catch (error) {
|
|
console.error('Publish game error:', error);
|
|
res.status(500).json({ error: 'Failed to publish game' });
|
|
}
|
|
});
|
|
router.post('/:id/end', auth_1.authenticate, async (req, res) => {
|
|
try {
|
|
const id = req.params.id;
|
|
const game = await index_1.prisma.game.findUnique({ where: { id } });
|
|
if (!game) {
|
|
return res.status(404).json({ error: 'Game not found' });
|
|
}
|
|
if (game.gameMasterId !== req.user.id) {
|
|
return res.status(403).json({ error: 'Not authorized' });
|
|
}
|
|
const updated = await index_1.prisma.game.update({
|
|
where: { id },
|
|
data: { status: 'ENDED' }
|
|
});
|
|
res.json(updated);
|
|
}
|
|
catch (error) {
|
|
console.error('End game error:', error);
|
|
res.status(500).json({ error: 'Failed to end game' });
|
|
}
|
|
});
|
|
router.post('/:id/archive', auth_1.authenticate, async (req, res) => {
|
|
try {
|
|
const id = req.params.id;
|
|
const game = await index_1.prisma.game.findUnique({ where: { id } });
|
|
if (!game) {
|
|
return res.status(404).json({ error: 'Game not found' });
|
|
}
|
|
if (game.gameMasterId !== req.user.id) {
|
|
return res.status(403).json({ error: 'Not authorized' });
|
|
}
|
|
const updated = await index_1.prisma.game.update({
|
|
where: { id },
|
|
data: { status: 'ARCHIVED' }
|
|
});
|
|
res.json(updated);
|
|
}
|
|
catch (error) {
|
|
console.error('Archive game error:', error);
|
|
res.status(500).json({ error: 'Failed to archive game' });
|
|
}
|
|
});
|
|
router.post('/:id/unarchive', auth_1.authenticate, async (req, res) => {
|
|
try {
|
|
const id = req.params.id;
|
|
const game = await index_1.prisma.game.findUnique({ where: { id } });
|
|
if (!game) {
|
|
return res.status(404).json({ error: 'Game not found' });
|
|
}
|
|
if (game.gameMasterId !== req.user.id) {
|
|
return res.status(403).json({ error: 'Not authorized' });
|
|
}
|
|
const updated = await index_1.prisma.game.update({
|
|
where: { id },
|
|
data: { status: 'ENDED' }
|
|
});
|
|
res.json(updated);
|
|
}
|
|
catch (error) {
|
|
console.error('Unarchive game error:', error);
|
|
res.status(500).json({ error: 'Failed to unarchive game' });
|
|
}
|
|
});
|
|
router.get('/:id/invite', async (req, res) => {
|
|
try {
|
|
const id = req.params.id;
|
|
const game = await index_1.prisma.game.findUnique({ where: { id } });
|
|
if (!game) {
|
|
return res.status(404).json({ error: 'Game not found' });
|
|
}
|
|
if (!game.inviteCode) {
|
|
return res.status(404).json({ error: 'No invite code' });
|
|
}
|
|
res.json({ inviteCode: game.inviteCode });
|
|
}
|
|
catch (error) {
|
|
console.error('Get invite error:', error);
|
|
res.status(500).json({ error: 'Failed to get invite code' });
|
|
}
|
|
});
|
|
router.get('/invite/:code', async (req, res) => {
|
|
try {
|
|
const code = req.params.code;
|
|
const game = await index_1.prisma.game.findUnique({
|
|
where: { inviteCode: code },
|
|
include: { gameMaster: { select: { id: true, name: true } } }
|
|
});
|
|
if (!game) {
|
|
return res.status(404).json({ error: 'Game not found' });
|
|
}
|
|
res.json(game);
|
|
}
|
|
catch (error) {
|
|
console.error('Get game by invite error:', error);
|
|
res.status(500).json({ error: 'Failed to get game' });
|
|
}
|
|
});
|
|
exports.default = router;
|