aboutsummaryrefslogtreecommitdiffstats
path: root/backend/src/routes/score.ts
blob: 9e9c597840796395bb7721e3637b124a22739462 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import express from 'express';
import { prisma } from '../config/db';
import { timeStamp } from 'node:console';

export const handleScoreUpload = async (req: express.Request, res: express.Response) => {
  try {
    const { meta, scores } = req.body;
    const userId = req.session.userId;
    if (!userId) {
      return res.status(401).json({ error: 'Unauthorized. Please log in to upload scores.' });
    }

    // Basic universal validation
    if (!meta || !meta.game || !meta.service || !scores) {
      return res.status(400).json({ error: 'Invalid request format. Expected meta with game/service and scores array' });
    }
    let game = await prisma.game.findUnique({
      where: { internalName: meta.game }
    });
    if (!game) {
      game = await prisma.game.findFirst({
        where: { formattedName: meta.game }
      });
    }
    if (!game) {
      return res.status(400).json({ error: `Game '${meta.game}' is not supported. Ensure that you are using the case-sensitive version of either the internal name or formatted name` });
    }
    const internalGameName = game.internalName;
    const scoresArray = Array.isArray(scores) ? scores : [scores];

    // Create score records
    const createdScores = await prisma.score.createMany({
      data: scoresArray.map(scoreData => ({
        gameInternalName: internalGameName,
        userId: userId,
        timestamp: scoreData.timestamp,
        data: scoreData
      }))
    });

    res.status(200).json({
      message: 'Score upload received successfully',
      game: meta.game,
      service: meta.service,
      scoreCount: createdScores.count
    });

  } catch (error) {
    console.error('Score upload endpoint error:', error);
    res.status(500).json({ error: 'Internal server error. Unable to process score upload' });
  }
}
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage