Skip to content

Commit

Permalink
feat: calcPoker
Browse files Browse the repository at this point in the history
  • Loading branch information
w8385 committed Jan 24, 2024
1 parent 50d04cb commit 3a960fb
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/poker/poker.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,11 @@ export class PokerController {
async get(@Param('id') id: string) {
return this.pokerService.get(id);
}

@Get('calc/:id')
@ApiOperation({ summary: '포커 결과 계산' })
@ApiParam({ name: 'id', description: '포커 id' })
async calc(@Param('id') id: string) {
return this.pokerService.calc(id);
}
}
62 changes: 60 additions & 2 deletions src/poker/poker.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@ import { CreatePokerDto } from './dto/create-user.dto';
import { UserService } from '../user/user.service';
import { v4 as uuidv4 } from 'uuid';
import { RedisClientType } from 'redis';
import { HttpService } from '@nestjs/axios';

@Injectable()
export class PokerService {
constructor(
private readonly httpService: HttpService,
private readonly userService: UserService,
@Inject('REDIS') private readonly redisClient: RedisClientType,
) {}

async create(name: string, createPokerDto: CreatePokerDto) {
const now = new Date().toString();
console.log(now);
const poker = {
id: uuidv4(),
name,
createdAt: now,
userInfos: {},
};
console.log(poker);
for (const userInfo in createPokerDto.userInfos) {
const point = createPokerDto.userInfos[userInfo];
poker.userInfos[userInfo] = {
Expand Down Expand Up @@ -57,4 +57,62 @@ export class PokerService {
async get(id: string) {
return await this.redisClient.json.get(id);
}

async calc(id: string) {
const poker = await this.redisClient.json.get(id);
const result = {
id,
name: poker['name'],
createdAt: poker['createdAt'],
result: {},
};

for (const userInfo in poker['userInfos']) {
const point = poker['userInfos'][userInfo]['point'];
const acientProblems = poker['userInfos'][userInfo]['problems'];
const recentProblems =
await this.userService.getProblemsByHandle(userInfo);

const solved = [];
for (const problem of recentProblems) {
if (!acientProblems.includes(problem)) {
solved.push(problem);
}
}

console.log(solved);
const solvedPoint = await this.calcPointFromSolved(solved);
result['result'][userInfo] = `${solvedPoint} / ${point}`;
}

return result;
}

private async calcPointFromSolved(problems: number[]) {
let point = 0;
for (let page = 1; page <= (problems.length + 49) / 50; page++) {
let url = `https://solved.ac/api/v3/search/problem?page=${page}&query=`;
for (let i = (page - 1) * 50; i < page * 50; i++) {
if (i >= problems.length) {
break;
}
url = url.concat('id:' + problems[i] + '|');
}
console.log(url);

const response = await this.httpService.axiosRef
.get(url)
.then((res) => res.data)
.catch((err) => {
throw new Error(
err?.message + ': ' + JSON.stringify(err?.response?.data),
);
});

for (const problem of response['items']) {
point += Number(problem['level']);
}
}
return point;
}
}

0 comments on commit 3a960fb

Please sign in to comment.