Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
.next
.env*.local
tsconfig.tsbuildinfo
25 changes: 23 additions & 2 deletions app/api/sessions/[id]/rounds/[roundId]/end/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,29 @@ export async function POST(req: Request, { params }: { params: { id: string, rou
if (session.length === 0) return NextResponse.json({ error: 'Session not found' }, { status: 404 });
if (session[0].passphrase !== passphrase) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });

// End round
await sql`UPDATE rounds SET status = 'ended', ended_at = NOW() WHERE id = ${roundId}`;
// Verify round belongs to this session
const round = await sql`SELECT id, status FROM rounds WHERE id = ${roundId} AND session_id = ${id}`;
if (round.length === 0) return NextResponse.json({ error: 'Round not found' }, { status: 404 });

// Idempotent: if round already ended, return the existing summary without re-applying penalties
if (round[0].status === 'ended') {
const txs = await sql`SELECT price FROM transactions WHERE round_id = ${roundId} AND status = 'confirmed'`;
const playerRounds = await sql`SELECT * FROM player_rounds WHERE round_id = ${roundId}`;
const tradeCount = txs.length;
const avgPrice = tradeCount > 0 ? txs.reduce((acc, t) => acc + parseFloat(t.price), 0) / tradeCount : 0;
const penaltyCount = playerRounds.filter(pr => !pr.has_traded).length;
return NextResponse.json({
summary: {
tradeCount,
avgPrice: Math.round(avgPrice * 100) / 100,
penaltyCount,
totalPlayers: playerRounds.length
}
});
}

// End round — scoped to this session to prevent cross-session tampering
await sql`UPDATE rounds SET status = 'ended', ended_at = NOW() WHERE id = ${roundId} AND session_id = ${id}`;

// Apply penalties and update total surplus
const playerRounds = await sql`SELECT * FROM player_rounds WHERE round_id = ${roundId}`;
Expand Down
1 change: 1 addition & 0 deletions tsconfig.tsbuildinfo

Large diffs are not rendered by default.