Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"feat: game moode create, voting and share" #287

Merged
merged 2 commits into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ import { TournamentModule } from './tournament/tournament.module';
import { GameGateway } from './websocket-game comms/providers/gamegateway';
import { GameModule } from './websocket-game comms/game.module';
import { AchievementModule } from './achievement/achievement.module';

import { GameModeModule } from './game-mode/game-mode.module';

import { SongGenreModule } from './song-genre/song-genre.module';

import { SocialModule } from './social/social.module';
// import { AchievementModule } from './achievement/achievement.module';
import { CacheModule } from '@nestjs/cache-manager';
Expand Down Expand Up @@ -63,7 +67,11 @@ import * as redisStore from 'cache-manager-redis-store';
TournamentModule,
AchievementModule,
SocialModule,

GameModeModule,

SongGenreModule,

],
controllers: [AppController],
providers: [
Expand Down
20 changes: 20 additions & 0 deletions backend/src/game-mode/dto/create-game-mode.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { IsString, IsBoolean, IsObject, IsNotEmpty } from 'class-validator';

export class GameModeDto {
@IsString()
@IsNotEmpty()
name: string;

@IsObject()
@IsNotEmpty()
rules: {
timeLimit: number;
pointSystem: Record<string, number>;
powerUpsAllowed: string[];
minimumPlayers: number;
specialConditions: string[];
};

@IsBoolean()
isPublic: boolean;
}
4 changes: 4 additions & 0 deletions backend/src/game-mode/dto/update-game-mode.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { PartialType } from '@nestjs/swagger';
import { CreateGameModeDto } from './create-game-mode.dto';

export class UpdateGameModeDto extends PartialType(CreateGameModeDto) {}
26 changes: 26 additions & 0 deletions backend/src/game-mode/entities/game-mode.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { User } from 'src/user/user.entity';
import { Entity, PrimaryGeneratedColumn, Column, ManyToOne } from 'typeorm';

@Entity()
export class GameMode {
@PrimaryGeneratedColumn()
id: string;

@Column()
name: string;

@Column('json')
rules: {
timeLimit: number;
pointSystem: Record<string, number>;
powerUpsAllowed: string[];
minimumPlayers: number;
specialConditions: string[];
};

@Column()
isPublic: boolean;

@ManyToOne(() => User, (user) => user.gameModes)
creator: User;
}
20 changes: 20 additions & 0 deletions backend/src/game-mode/game-mode.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Test, TestingModule } from '@nestjs/testing';
import { GameModeController } from './game-mode.controller';
import { GameModeService } from './game-mode.service';

describe('GameModeController', () => {
let controller: GameModeController;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [GameModeController],
providers: [GameModeService],
}).compile();

controller = module.get<GameModeController>(GameModeController);
});

it('should be defined', () => {
expect(controller).toBeDefined();
});
});
18 changes: 18 additions & 0 deletions backend/src/game-mode/game-mode.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Controller, Get, Post, Body, Request} from '@nestjs/common';
import { GameModeService } from './game-mode.service';
import { GameModeDto } from './dto/create-game-mode.dto';

@Controller('game-mode')
export class GameModeController {
constructor(private readonly gameModeService: GameModeService) {}

@Post()
async createGameMode(@Body() data: GameModeDto, @Request() req) {
return this.gameModeService.createGameMode(data, req.user);
}

@Get()
async getAllGameModes() {
return this.gameModeService.getAllGameModes();
}
}
9 changes: 9 additions & 0 deletions backend/src/game-mode/game-mode.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { GameModeService } from './game-mode.service';
import { GameModeController } from './game-mode.controller';

@Module({
controllers: [GameModeController],
providers: [GameModeService],
})
export class GameModeModule {}
18 changes: 18 additions & 0 deletions backend/src/game-mode/game-mode.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { GameModeService } from './game-mode.service';

describe('GameModeService', () => {
let service: GameModeService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [GameModeService],
}).compile();

service = module.get<GameModeService>(GameModeService);
});

it('should be defined', () => {
expect(service).toBeDefined();
});
});
28 changes: 28 additions & 0 deletions backend/src/game-mode/game-mode.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { User } from 'src/user/user.entity';
import { Repository } from 'typeorm';
import { GameModeDto } from './dto/create-game-mode.dto';
import { GameMode } from './entities/game-mode.entity';


@Injectable()
export class GameModeService {
constructor(
@InjectRepository(GameMode)
private gameModeRepository: Repository<GameMode>
) {}

async createGameMode(data: GameModeDto, user: User) {
const gameMode = this.gameModeRepository.create({ ...data, creator: user });
return this.gameModeRepository.save(gameMode);
}

async getAllGameModes() {
return this.gameModeRepository.find({ relations: ['creator'] });
}

async upvoteGameMode(id: string) {
return this.gameModeRepository.increment({ id }, 'votes', 1);
}
}