Skip to content

Commit f8a313e

Browse files
authored
Merge pull request #14 from ishaandhyani/stake
Stake
2 parents 049ead6 + 852d455 commit f8a313e

34 files changed

+8066
-852
lines changed

695650-U2Q38ZH2/backend/package-lock.json

Lines changed: 6244 additions & 339 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

695650-U2Q38ZH2/backend/package.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,21 @@
1212
"license": "ISC",
1313
"dependencies": {
1414
"@prisma/client": "^5.0.0",
15+
"@taquito/signer": "^17.2.0",
16+
"@taquito/taquito": "^17.2.0",
17+
"cors": "^2.8.5",
1518
"express": "^4.18.2",
16-
"socket.io": "^4.7.1",
17-
"cors": "^2.8.5"
18-
19+
"socket.io": "^4.7.2"
1920
},
2021
"devDependencies": {
22+
"@babel/plugin-transform-runtime": "^7.22.10",
23+
"@babel/preset-env": "^7.22.10",
24+
"@babel/preset-typescript": "^7.22.5",
25+
"@babel/runtime": "^7.22.10",
2126
"@types/express": "^4.17.17",
2227
"@types/node": "^20.4.5",
2328
"@types/socket.io": "^3.0.2",
29+
"babel-cli": "^6.26.0",
2430
"nodemon": "^3.0.1",
2531
"prisma": "^5.0.0",
2632
"ts-node": "^10.9.1",

695650-U2Q38ZH2/backend/prisma/schema.prisma

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,23 @@ generator client {
88
datasource db {
99
provider = "postgresql"
1010
url = env("DATABASE_URL")
11-
1211
}
1312

1413
model User {
15-
id Int @id @default(autoincrement())
16-
walletAddress String @unique
14+
id Int @id @default(autoincrement())
15+
walletAddress String @unique
1716
avatarName String
18-
// add other fields as necessary
17+
createdAt DateTime @default(now())
18+
updatedAt DateTime? @updatedAt
19+
gameStats GameStats?
20+
}
21+
22+
model GameStats {
23+
id Int @id @default(autoincrement())
24+
gamesPlayed Int @default(0)
25+
gamesWon Int @default(0)
26+
gamesLost Int @default(0)
27+
gamesDrawn Int @default(0)
28+
userId Int @unique
29+
user User @relation(fields: [userId], references: [id])
1930
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Request, Response } from 'express';
2+
import * as GameStatsService from './gameStats.service';
3+
4+
export const getGameStatsByUserId = async (req: Request, res: Response) => {
5+
try {
6+
const gameStats = await GameStatsService.getGameStatsByUserId(parseInt(req.params.userId, 10));
7+
res.json(gameStats);
8+
} catch (err) {
9+
console.error(err);
10+
res.sendStatus(500);
11+
}
12+
};
13+
14+
export const updateGameStats = async (req: Request, res: Response) => {
15+
try {
16+
const { userId, result } = req.body;
17+
const updatedStats = await GameStatsService.updateGameStats(userId, result);
18+
res.json(updatedStats);
19+
} catch (err) {
20+
console.error(err);
21+
res.sendStatus(500);
22+
}
23+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import express from 'express';
2+
import * as GameStatsController from './gameStats.controller';
3+
4+
const router = express.Router();
5+
6+
router.get('/gameStats/:userId', GameStatsController.getGameStatsByUserId);
7+
router.post('/gameStats/update', GameStatsController.updateGameStats);
8+
9+
export default router;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { prisma } from '../../config/prisma';
2+
3+
4+
export const getGameStatsByUserId = async (userId: number) => {
5+
return prisma.gameStats.findUnique({
6+
where: { userId }
7+
});
8+
};
9+
10+
export const updateGameStats = async (userId: number, result: 'win' | 'loss' | 'draw') => {
11+
const userStats = await getGameStatsByUserId(userId);
12+
13+
if (!userStats) {
14+
throw new Error('User stats not found');
15+
}
16+
17+
switch (result) {
18+
case 'win':
19+
return prisma.gameStats.update({
20+
where: { userId },
21+
data: { gamesPlayed: userStats.gamesPlayed + 1, gamesWon: userStats.gamesWon + 1 }
22+
});
23+
24+
case 'loss':
25+
return prisma.gameStats.update({
26+
where: { userId },
27+
data: { gamesPlayed: userStats.gamesPlayed + 1, gamesLost: userStats.gamesLost + 1 }
28+
});
29+
30+
case 'draw':
31+
return prisma.gameStats.update({
32+
where: { userId },
33+
data: { gamesPlayed: userStats.gamesPlayed + 1, gamesDrawn: userStats.gamesDrawn + 1 }
34+
});
35+
36+
default:
37+
throw new Error('Invalid game result');
38+
}
39+
};

695650-U2Q38ZH2/backend/src/api/user/user.controller.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import { Request, Response } from 'express';
22
import * as UserService from './user.service';
33

4-
export const createUser = async (req: Request, res: Response) => {
4+
export const upsertUser = async (req: Request, res: Response) => {
55
try {
6-
const user = await UserService.createUser(req.body);
6+
const user = await UserService.upsertUser(req.body);
77
res.json(user);
88
} catch (err) {
9-
// Handle error
109
console.error(err);
1110
res.sendStatus(500);
1211
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import express from 'express';
2-
import * as UserController from './user.controller';
1+
import express from "express";
2+
import * as UserController from "./user.controller";
33

44
const router = express.Router();
55

6-
router.post('/user', UserController.createUser);
7-
router.get('/user/:walletAddress', UserController.getUserByWallet);
6+
router.post("/user/upsert", UserController.upsertUser);
7+
router.get("/user/:walletAddress", UserController.getUserByWallet);
88

99
export default router;
Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
1-
import { PrismaClient, Prisma } from '@prisma/client';
2-
import { prisma } from '../../config/prisma';
1+
import { PrismaClient, Prisma } from "@prisma/client";
2+
import { prisma } from "../../config/prisma";
33

4-
export const createUser = async (userData: Prisma.UserCreateInput) => {
5-
return prisma.user.create({ data: userData });
4+
export const upsertUser = async (userData: Prisma.UserCreateInput) => {
5+
return prisma.user.upsert({
6+
where: { walletAddress: userData.walletAddress },
7+
update: userData,
8+
create: {
9+
...userData,
10+
gameStats: {
11+
create: {}
12+
}
13+
},
14+
});
615
};
716

817
export const getUserByWallet = async (walletAddress: string) => {
918
return prisma.user.findUnique({
10-
where: { walletAddress }
19+
where: { walletAddress },
1120
});
1221
};

695650-U2Q38ZH2/backend/src/chess/helpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
function generateId(l: number = 10): string {
2-
const s = "abcdefghijklmnopqrstuvwxyz0123456789";
2+
const s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
33
return new Array(l).fill('_').map(() => {
44
let e = s[Math.floor(Math.random() * s.length)];
55
if (Math.random() < 0.5) {

0 commit comments

Comments
 (0)