-
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 pull request #242 from Ibinola/feature/implement-tournament-system
feature: implement tournament system
- Loading branch information
Showing
8 changed files
with
213 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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,26 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { GameSession } from 'src/game-session/game-session.entity'; | ||
import { Player } from '../player/player.entity'; | ||
|
||
@Injectable() | ||
export class SchedulingService { | ||
schedule(tournament: any): GameSession[] { | ||
const participants: Player[] = tournament.participants; | ||
const matches: GameSession[] = []; | ||
|
||
for (let i = 0; i < participants.length; i++) { | ||
for (let j = i + 1; j < participants.length; j++) { | ||
const match = new GameSession(); | ||
match.players = [participants[i], participants[j]]; | ||
match.startTime = this.getMatchTime(); | ||
matches.push(match); | ||
} | ||
} | ||
|
||
return matches; | ||
} | ||
|
||
private getMatchTime(): Date { | ||
return new 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,33 @@ | ||
import { | ||
Entity, | ||
PrimaryGeneratedColumn, | ||
Column, | ||
ManyToMany, | ||
OneToMany, | ||
} from 'typeorm'; | ||
import { User } from '../user/user.entity'; | ||
import { GameSession } from '../game-session/game-session.entity'; | ||
|
||
@Entity() | ||
export class Tournament { | ||
@PrimaryGeneratedColumn('uuid') | ||
id: string; | ||
|
||
@Column() | ||
name: string; | ||
|
||
@Column('timestamp') | ||
startTime: Date; | ||
|
||
@Column('timestamp') | ||
endTime: Date; | ||
|
||
@Column('json', { nullable: true }) | ||
rules: Record<string, any>; | ||
|
||
@ManyToMany(() => User, { eager: true }) | ||
participants: User[]; | ||
|
||
@OneToMany(() => GameSession, (gameSession) => gameSession) | ||
matches: GameSession[]; | ||
} |
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,13 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { TypeOrmModule } from '@nestjs/typeorm'; | ||
import { TournamentService } from './tournament.service'; | ||
import { Tournament } from './tournament.entity'; | ||
import { User } from '../user/user.entity'; | ||
import { GameSession } from '../game-session/game-session.entity'; | ||
|
||
@Module({ | ||
imports: [TypeOrmModule.forFeature([Tournament, User, GameSession])], | ||
providers: [TournamentService], | ||
exports: [TournamentService], | ||
}) | ||
export class TournamentModule {} |
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 { TournamentService } from './tournament.service'; | ||
|
||
describe('TournamentService', () => { | ||
let service: TournamentService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [TournamentService], | ||
}).compile(); | ||
|
||
service = module.get<TournamentService>(TournamentService); | ||
}); | ||
|
||
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,80 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { Repository } from 'typeorm'; | ||
import { Tournament } from './tournament.entity'; | ||
import { Player } from '../player/player.entity'; | ||
import { GameSession } from 'src/game-session/game-session.entity'; | ||
import { SchedulingService } from './scheduling.service'; | ||
|
||
@Injectable() | ||
export class TournamentService { | ||
constructor( | ||
@InjectRepository(Tournament) | ||
private tournamentRepository: Repository<Tournament>, | ||
|
||
@InjectRepository(Player) | ||
private playerRepository: Repository<Player>, | ||
|
||
@InjectRepository(GameSession) | ||
private gameSessionRepository: Repository<GameSession>, | ||
|
||
private schedulingService: SchedulingService, | ||
) {} | ||
|
||
async createTournament( | ||
name: string, | ||
startTime: Date, | ||
endTime: Date, | ||
rules: Record<string, any>, | ||
participantIds: string[], | ||
): Promise<Tournament> { | ||
const participants = await this.playerRepository.find({ | ||
where: participantIds.map((id) => ({ id })), | ||
}); | ||
const tournament = this.tournamentRepository.create({ | ||
name, | ||
startTime, | ||
endTime, | ||
rules, | ||
participants, | ||
}); | ||
return this.tournamentRepository.save(tournament); | ||
} | ||
|
||
async scheduleMatches(tournamentId: string): Promise<GameSession[]> { | ||
const tournament = await this.tournamentRepository.findOne({ | ||
where: { id: tournamentId }, | ||
relations: ['participants'], | ||
}); | ||
|
||
if (!tournament) { | ||
throw new Error('Tournament not found'); | ||
} | ||
|
||
const matches = await this.schedulingService.schedule(tournament); | ||
return this.gameSessionRepository.save(matches); | ||
} | ||
|
||
async applyScoring(tournamentId: string): Promise<void> { | ||
const tournament = await this.tournamentRepository.findOne({ | ||
where: { id: tournamentId }, | ||
relations: ['matches'], | ||
}); | ||
|
||
if (!tournament) { | ||
throw new Error('Tournament not found'); | ||
} | ||
|
||
tournament.matches.forEach((match) => { | ||
this.applyMatchScoringRules(match); | ||
}); | ||
|
||
await this.gameSessionRepository.save(tournament.matches); | ||
} | ||
|
||
private applyMatchScoringRules(match: GameSession) { | ||
if (match.players.length === 2) { | ||
const [player1, player2] = match.players; | ||
} | ||
} | ||
} |