Skip to content

Commit

Permalink
chore: Fix some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
PleBea committed Jan 8, 2024
1 parent efea207 commit 8eb0ccc
Show file tree
Hide file tree
Showing 13 changed files with 116 additions and 13 deletions.
Binary file not shown.
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
"private": true,
"license": "UNLICENSED",
"scripts": {
"build": "nest build",
"build": "TZ=Asia/Seoul nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start:dev": "cross-env NODE_ENV=development nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"start": "TZ=Asia/Seoul nest start",
"start:dev": "TZ=Asia/Seoul cross-env NODE_ENV=development nest start --watch",
"start:debug": "TZ=Asia/Seoul nest start --debug --watch",
"start:prod": "TZ=Asia/Seoul node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest",
"test:watch": "jest --watch",
Expand All @@ -37,6 +37,7 @@
"class-validator": "^0.14.0",
"cookie-parser": "^1.4.6",
"cross-env": "^7.0.3",
"crypto": "^1.0.1",
"dockerode": "^4.0.1",
"joi": "^17.11.0",
"mysql2": "^3.6.4",
Expand Down Expand Up @@ -91,4 +92,4 @@
"testEnvironment": "node"
},
"packageManager": "[email protected]"
}
}
19 changes: 16 additions & 3 deletions src/modules/challenge/challenge.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { Challenge } from '../../shared/entities/challenge.entity';
import { CreateChallengeDto } from './dto/create-challenge.dto';
import { UpdateChallengeDto } from './dto/update-challenge.dto';
import { Solve } from 'src/shared/entities';
import { sha256 } from 'src/utils/enc';
import { calculateScore } from 'src/utils/score';

@Injectable()
export class ChallengeService {
Expand All @@ -23,6 +25,7 @@ export class ChallengeService {
async create(body: CreateChallengeDto): Promise<Challenge> {
const newChallenge = this.challengeRepository.create({
...body,
flag: sha256(body.flag),
category: {
id: body.categoryId,
},
Expand Down Expand Up @@ -86,19 +89,29 @@ export class ChallengeService {
}

const solve = await this.solveRepository.findOne({
where: { challenge, user, correct: true },
relations: ['user', 'challenge'],
select: ['id'],
where: {
challenge: {
id: challenge.id,
},
user: {
id: user.id,
},
correct: true,
},
});

if (solve) {
throw new HttpException('You already solved this challenge', 400);
}

const correct = challenge.flag === flag;
const correct = challenge.flag === sha256(flag);

await this.solveRepository.save({
challenge,
user,
flag,
flag: correct ? sha256(flag) : flag,
correct,
});

Expand Down
2 changes: 2 additions & 0 deletions src/modules/root/root.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { NoticeModule } from '../notice/notice.module';
import { CategoryModule } from '../category/category.module';
import { ChallengeModule } from '../challenge/challenge.module';
import { DockerModule } from '../docker/docker.module';
import { ScoreModule } from '../score/score.module';

@Module({
imports: [
Expand All @@ -28,6 +29,7 @@ import { DockerModule } from '../docker/docker.module';
CategoryModule,
ChallengeModule,
DockerModule,
ScoreModule,
],
controllers: [RootController],
providers: [RootService],
Expand Down
17 changes: 17 additions & 0 deletions src/modules/score/score.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Controller, Get, UseGuards } from '@nestjs/common';
import { ScoreService } from './score.service';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { AccessGuard } from '../auth/guards/access.guard';

@Controller('score')
@ApiTags('score')
export class ScoreController {
constructor(private readonly scoreService: ScoreService) {}

@Get()
@ApiBearerAuth()
@UseGuards(AccessGuard)
async findAll() {
return await this.scoreService.findAll();
}
}
12 changes: 12 additions & 0 deletions src/modules/score/score.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Module } from '@nestjs/common';
import { ScoreService } from './score.service';
import { ScoreController } from './score.controller';
import { TypeOrmModule } from '@nestjs/typeorm';
import { User } from 'src/shared/entities/user.entity';

@Module({
imports: [TypeOrmModule.forFeature([User])],
controllers: [ScoreController],
providers: [ScoreService],
})
export class ScoreModule {}
32 changes: 32 additions & 0 deletions src/modules/score/score.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { User } from 'src/shared/entities/user.entity';
import { Repository } from 'typeorm';

@Injectable()
export class ScoreService {
constructor(
@InjectRepository(User)
private readonly userRepository: Repository<User>,
) {}

public async findAll() {
const users = await this.userRepository.find({
select: ['id', 'name', 'score', 'solves'],
order: {
score: 'DESC',
solves: {
createdAt: 'ASC',
},
},
where: {
isAdmin: false,
solves: {
correct: true,
},
},
});

return users;
}
}
4 changes: 1 addition & 3 deletions src/shared/entities/challenge.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ export class Challenge {
@Column({ default: false })
visible: boolean;

@OneToMany(() => Solve, (solve) => solve.challenge, {
eager: true,
})
@OneToMany(() => Solve, (solve) => solve.challenge)
solves: Solve[];

@ManyToOne(() => Category, (category) => category.challenges, {
Expand Down
4 changes: 3 additions & 1 deletion src/shared/entities/solve.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ export class Solve {
@ManyToOne(() => User, (user) => user.solves)
user: User;

@ManyToOne(() => Challenge, (challenge) => challenge.solves)
@ManyToOne(() => Challenge, (challenge) => challenge.solves, {
eager: true,
})
challenge: Challenge;

@Column()
Expand Down
1 change: 1 addition & 0 deletions src/shared/providers/database.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import entities from '../entities';
entities: entities,
synchronize: true,
type: 'mysql',
timezone: '+09:00',
}),
}),
],
Expand Down
5 changes: 5 additions & 0 deletions src/utils/enc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as crypto from 'crypto';

export const sha256 = (data) => {
return crypto.createHash('sha256').update(data).digest('hex');
};
12 changes: 12 additions & 0 deletions src/utils/score.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export const calculateScore = (count) => {
const minimumPoint = 100;
const maximumPoint = 500;
const decay = 10;

const score = Math.ceil(
((minimumPoint - Number(maximumPoint)) / decay ** 2) * count ** 2 +
Number(maximumPoint),
);
if (score < minimumPoint) return minimumPoint;
else return score;
};
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3174,6 +3174,13 @@ __metadata:
languageName: node
linkType: hard

"crypto@npm:^1.0.1":
version: 1.0.1
resolution: "crypto@npm:1.0.1"
checksum: fcf7dbd68ac5415b7fde7d7208fe203038e92e83e8a8fcf6e86ab4771ce3dd026d6967a990ba56b9d1c771378210814d5c90d907d3739fbd1723d552ad6c8ab8
languageName: node
linkType: hard

"date-fns@npm:^2.29.3":
version: 2.30.0
resolution: "date-fns@npm:2.30.0"
Expand Down Expand Up @@ -5691,6 +5698,7 @@ __metadata:
class-validator: "npm:^0.14.0"
cookie-parser: "npm:^1.4.6"
cross-env: "npm:^7.0.3"
crypto: "npm:^1.0.1"
dockerode: "npm:^4.0.1"
eslint: "npm:^8.42.0"
eslint-config-prettier: "npm:^9.0.0"
Expand Down

0 comments on commit 8eb0ccc

Please sign in to comment.