-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/main'
- Loading branch information
Showing
28 changed files
with
1,301 additions
and
26 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common'; | ||
|
||
@Injectable() | ||
export class JwtAuthGuard implements CanActivate { | ||
canActivate(context: ExecutionContext): boolean { | ||
// Static guard: Always allow access (replace with real authentication later) | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm'; | ||
|
||
@Entity('leaderboard_entries') | ||
export class LeaderboardEntry { | ||
@PrimaryGeneratedColumn('uuid') | ||
id: string; | ||
|
||
@Column({ unique: true }) | ||
playerId: string; | ||
|
||
@Column() | ||
playerName: string; | ||
|
||
@Column({ type: 'int', default: 0 }) | ||
score: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,58 @@ | ||
// import { Injectable } from '@nestjs/common'; | ||
|
||
// // Service responsible for leaderboard operations. | ||
// @Injectable() | ||
// export class LeaderboardService { | ||
// // Retrieve the global leaderboard. | ||
// getLeaderboard() { | ||
// // Implement get leaderboard logic | ||
// } | ||
|
||
// // Retrieve the rank of a specific player. | ||
// getPlayerRank() { | ||
// // Implement get player rank logic | ||
// } | ||
// } | ||
|
||
|
||
import { Injectable } from '@nestjs/common'; | ||
import { RedisService } from 'src/redis/redis.service'; | ||
import { Repository } from 'typeorm'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { LeaderboardEntry } from '../leaderboard-entry.entity'; | ||
|
||
// Service responsible for leaderboard operations. | ||
@Injectable() | ||
export class LeaderboardService { | ||
// Retrieve the global leaderboard. | ||
getLeaderboard() { | ||
// Implement get leaderboard logic | ||
constructor( | ||
@InjectRepository(LeaderboardEntry) | ||
private leaderboardRepository: Repository<LeaderboardEntry>, | ||
private redisService: RedisService | ||
) {} | ||
|
||
// Helper method for caching | ||
private async getCachedData<T>(key: string, fetchFunction: () => Promise<T>, ttl = 3600): Promise<T> { | ||
const cachedData = await this.redisService.get(key); | ||
if (cachedData) return JSON.parse(cachedData); | ||
|
||
const freshData = await fetchFunction(); | ||
await this.redisService.set(key, JSON.stringify(freshData), ttl); | ||
|
||
return freshData; | ||
} | ||
|
||
// Retrieve the global leaderboard | ||
async getLeaderboard() { | ||
return this.getCachedData('leaderboard:global', () => | ||
this.leaderboardRepository.find({ order: { score: 'DESC' }, take: 100 }) | ||
); | ||
} | ||
|
||
// Retrieve the rank of a specific player. | ||
getPlayerRank() { | ||
// Implement get player rank logic | ||
// Retrieve the rank of a specific player | ||
async getPlayerRank(playerId: string) { | ||
return this.getCachedData(`leaderboard:rank:${playerId}`, async () => { | ||
const leaderboard = await this.getLeaderboard(); // Fetch cached leaderboard | ||
const rank = leaderboard.findIndex(entry => entry.playerId === playerId) + 1; | ||
return rank > 0 ? { playerId, rank } : null; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Module, Global } from '@nestjs/common'; | ||
import { RedisService } from './redis.service'; | ||
|
||
@Global() | ||
@Module({ | ||
providers: [RedisService], | ||
exports: [RedisService], | ||
}) | ||
export class RedisModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { RedisService } from './redis.service'; | ||
|
||
describe('RedisService', () => { | ||
let service: RedisService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [RedisService], | ||
}).compile(); | ||
|
||
service = module.get<RedisService>(RedisService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { Redis } from 'ioredis'; | ||
|
||
@Injectable() | ||
export class RedisService { | ||
private client: Redis; | ||
|
||
constructor() { | ||
this.client = new Redis({ | ||
host: '127.0.0.1', | ||
port: 6379, | ||
}); | ||
} | ||
|
||
async get(key: string): Promise<string | null> { | ||
return this.client.get(key); | ||
} | ||
|
||
async set(key: string, value: string, ttl?: number): Promise<void> { | ||
if (ttl) { | ||
await this.client.set(key, value, 'EX', ttl); | ||
} else { | ||
await this.client.set(key, value); | ||
} | ||
} | ||
|
||
async del(key: string): Promise<void> { | ||
await this.client.del(key); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Controller, Get, Post, Body, Param, UseGuards } from '@nestjs/common'; | ||
import { GenreService } from '../services/genre.service'; | ||
import { JwtAuthGuard } from '../../auth/guard/jwt-auth.guard'; | ||
import { CurrentUser } from '../../auth/decorators/current-user.decorator'; | ||
|
||
@Controller('genres') | ||
export class GenreController { | ||
constructor(private readonly genreService: GenreService) {} | ||
|
||
@Get() | ||
findAll() { | ||
return this.genreService.findAll(); | ||
} | ||
|
||
@Post(':genreId/preferences') | ||
@UseGuards(JwtAuthGuard) | ||
updatePreference( | ||
@CurrentUser() userId: string, | ||
@Param('genreId') genreId: string, | ||
@Body() performanceData: any, | ||
) { | ||
return this.genreService.updateUserPreference(userId, genreId, performanceData); | ||
} | ||
|
||
@Get('preferences') | ||
@UseGuards(JwtAuthGuard) | ||
getUserPreferences(@CurrentUser() userId: string) { | ||
return this.genreService.getUserGenrePreferences(userId); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
backend/src/song-genre/controllers/song.controller.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { SongGenreController } from './song.controller'; | ||
import { SongGenreService } from '../services/song.service'; | ||
|
||
describe('SongGenreController', () => { | ||
let controller: SongGenreController; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [SongGenreController], | ||
providers: [SongGenreService], | ||
}).compile(); | ||
|
||
controller = module.get<SongGenreController>(SongGenreController); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(controller).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Controller, Post, Body, Get, Param, Patch, UseGuards } from '@nestjs/common'; | ||
import { SongService } from '../services/song.service'; | ||
import { CreateSongDto } from '../dtos/create-song.dto'; | ||
import { JwtAuthGuard } from '../../auth/guard/jwt-auth.guard'; | ||
|
||
@Controller('songs') | ||
export class SongController { | ||
constructor(private readonly songService: SongService) {} | ||
|
||
@Post() | ||
@UseGuards(JwtAuthGuard) | ||
create(@Body() createSongDto: CreateSongDto) { | ||
return this.songService.create(createSongDto); | ||
} | ||
|
||
@Get('genre/:genreId') | ||
findByGenre(@Param('genreId') genreId: string) { | ||
return this.songService.findByGenre(genreId); | ||
} | ||
|
||
@Patch(':id/difficulty') | ||
@UseGuards(JwtAuthGuard) | ||
updateDifficulty(@Param('id') id: string, @Body() performanceData: any[]) { | ||
return this.songService.updateDifficulty(id, performanceData); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { IsString, IsNumber, IsObject, IsUUID, IsArray, IsOptional } from 'class-validator'; | ||
|
||
export class CreateSongDto { | ||
@IsString() | ||
title: string; | ||
|
||
@IsString() | ||
artist: string; | ||
|
||
@IsNumber() | ||
durationSeconds: number; | ||
|
||
@IsNumber() | ||
baseDifficulty: number; | ||
|
||
@IsObject() | ||
difficultyFactors: Record<string, number>; | ||
|
||
@IsUUID() | ||
genreId: string; | ||
|
||
@IsOptional() | ||
@IsArray() | ||
@IsString({ each: true }) | ||
tagIds?: string[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { PartialType } from '@nestjs/swagger'; | ||
import { CreateSongGenreDto } from './create-song.dto'; | ||
|
||
export class UpdateSongGenreDto extends PartialType(CreateSongGenreDto) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Entity, Column, PrimaryGeneratedColumn, ManyToOne, CreateDateColumn } from 'typeorm'; | ||
import { Genre } from './genre.entity'; | ||
|
||
@Entity() | ||
export class GenreChallenge { | ||
@PrimaryGeneratedColumn('uuid') | ||
id: string; | ||
|
||
@Column() | ||
title: string; | ||
|
||
@ManyToOne(() => Genre) | ||
genre: Genre; | ||
|
||
@Column('text') | ||
description: string; | ||
|
||
@Column('json') | ||
requirements: Record<string, any>; | ||
|
||
@Column('int') | ||
experienceReward: number; | ||
|
||
@Column() | ||
expiresAt: Date; | ||
|
||
@CreateDateColumn() | ||
createdAt: Date; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Entity, Column, PrimaryGeneratedColumn, OneToMany, CreateDateColumn, UpdateDateColumn } from 'typeorm'; | ||
import { Song } from './song.entity'; | ||
|
||
@Entity() | ||
export class Genre { | ||
@PrimaryGeneratedColumn('uuid') | ||
id: string; | ||
|
||
@Column({ unique: true }) | ||
name: string; | ||
|
||
@Column('text') | ||
description: string; | ||
|
||
@Column() | ||
icon: string; | ||
|
||
@Column('float', { default: 1.0 }) | ||
difficultyMultiplier: number; | ||
|
||
@OneToMany(() => Song, song => song.genre) | ||
songs: Song[]; | ||
|
||
@CreateDateColumn() | ||
createdAt: Date; | ||
|
||
@UpdateDateColumn() | ||
updatedAt: Date; | ||
} |
Oops, something went wrong.