-
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 #287 from portableDD/gameMode
"feat: game moode create, voting and share"
- Loading branch information
Showing
9 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
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,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; | ||
} |
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 { CreateGameModeDto } from './create-game-mode.dto'; | ||
|
||
export class UpdateGameModeDto extends PartialType(CreateGameModeDto) {} |
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 { 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; | ||
} |
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 { 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(); | ||
}); | ||
}); |
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 { 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(); | ||
} | ||
} |
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 } from '@nestjs/common'; | ||
import { GameModeService } from './game-mode.service'; | ||
import { GameModeController } from './game-mode.controller'; | ||
|
||
@Module({ | ||
controllers: [GameModeController], | ||
providers: [GameModeService], | ||
}) | ||
export class GameModeModule {} |
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 { 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(); | ||
}); | ||
}); |
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,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); | ||
} | ||
} |