Initial commit
This commit is contained in:
commit
b3a51a4115
10336 changed files with 2381973 additions and 0 deletions
248
backend/dist/routes/games.js
vendored
Normal file
248
backend/dist/routes/games.js
vendored
Normal file
|
|
@ -0,0 +1,248 @@
|
|||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const express_1 = require("express");
|
||||
const index_js_1 = require("../index.js");
|
||||
const auth_js_1 = require("../middleware/auth.js");
|
||||
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_js_1.prisma.game.findMany({
|
||||
where,
|
||||
include: {
|
||||
gameMaster: { select: { id: true, name: true } },
|
||||
_count: { select: { teams: true, legs: 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_js_1.authenticate, async (req, res) => {
|
||||
try {
|
||||
const games = await index_js_1.prisma.game.findMany({
|
||||
where: { gameMasterId: req.user.id },
|
||||
include: {
|
||||
_count: { select: { teams: true, legs: 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_js_1.prisma.game.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
gameMaster: { select: { id: true, name: true } },
|
||||
legs: { orderBy: { sequenceNumber: 'asc' } },
|
||||
teams: {
|
||||
include: {
|
||||
members: { include: { user: { select: { id: true, name: true, email: true } } } },
|
||||
currentLeg: true
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
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' });
|
||||
}
|
||||
res.json(game);
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Get game error:', error);
|
||||
res.status(500).json({ error: 'Failed to get game' });
|
||||
}
|
||||
});
|
||||
router.post('/', auth_js_1.authenticate, async (req, res) => {
|
||||
try {
|
||||
const { name, description, prizeDetails, visibility, startDate, locationLat, locationLng, searchRadius, timeLimitPerLeg, timeDeductionPenalty } = 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_js_1.prisma.game.create({
|
||||
data: {
|
||||
name,
|
||||
description,
|
||||
prizeDetails,
|
||||
visibility: visibility || 'PUBLIC',
|
||||
startDate: startDate ? new Date(startDate) : null,
|
||||
locationLat,
|
||||
locationLng,
|
||||
searchRadius,
|
||||
timeLimitPerLeg,
|
||||
timeDeductionPenalty,
|
||||
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_js_1.authenticate, async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const { name, description, prizeDetails, visibility, startDate, locationLat, locationLng, searchRadius, timeLimitPerLeg, timeDeductionPenalty, status } = req.body;
|
||||
const game = await index_js_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_js_1.prisma.game.update({
|
||||
where: { id },
|
||||
data: {
|
||||
name,
|
||||
description,
|
||||
prizeDetails,
|
||||
visibility,
|
||||
startDate: startDate ? new Date(startDate) : undefined,
|
||||
locationLat,
|
||||
locationLng,
|
||||
searchRadius,
|
||||
timeLimitPerLeg,
|
||||
timeDeductionPenalty,
|
||||
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_js_1.authenticate, async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const game = await index_js_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' });
|
||||
}
|
||||
await index_js_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_js_1.authenticate, async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const game = await index_js_1.prisma.game.findUnique({
|
||||
where: { id },
|
||||
include: { legs: 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.legs.length === 0) {
|
||||
return res.status(400).json({ error: 'Game must have at least one leg' });
|
||||
}
|
||||
const updated = await index_js_1.prisma.game.update({
|
||||
where: { 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_js_1.authenticate, async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const game = await index_js_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_js_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.get('/:id/invite', async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const game = await index_js_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;
|
||||
const game = await index_js_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;
|
||||
Loading…
Add table
Add a link
Reference in a new issue