diff --git a/ormconfig.json.example b/ormconfig.json.example index 93b73ae322c1d710e77555596d3f97d408c1a7f7..78ed436feede989d81d044795a41227e73e3f94a 100644 --- a/ormconfig.json.example +++ b/ormconfig.json.example @@ -1,13 +1,12 @@ - { - "type": "postgres", - "host": "localhost", - "port": 5432, - "username": "ehasa", - "password": "salasana", - "database": "ehasa", - "entities": ["src/**/*.entity{.ts,.js}"], - "synchronize": true, - "logging": true, - "dropSchema": false - } \ No newline at end of file + "type": "postgres", + "host": "localhost", + "port": 5432, + "username": "ehasa", + "password": "salasana", + "database": "ehasa", + "entities": ["src/**/*.entity{.ts,.js}"], + "synchronize": true, + "logging": true, + "dropSchema": true +} diff --git a/src/app.module.ts b/src/app.module.ts index b773771cd0c40e19e6c35a23bf27c7f2e18f158a..738a6c116590f6bd236f94f89030749a1905ac12 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -1,19 +1,19 @@ import { Module } from '@nestjs/common'; import { APP_FILTER, APP_INTERCEPTOR, APP_GUARD } from '@nestjs/core'; import { TypeOrmModule } from '@nestjs/typeorm'; +import { Connection } from 'typeorm'; + import { AppController } from './app.controller'; import { AppService } from './app.service'; -import { Connection } from 'typeorm'; import { UserModule } from './user/user.module'; - import { HttpErrorFilter } from './shared/http-error.filter'; import { LoggingInterceptor } from './shared/logging.interceptor'; import { NotificationModule } from './notifications/notifications.module'; - import { GameModule } from './game/game.module'; import { RolesGuard } from './shared/roles.guard'; import { TaskModule } from './task/task.module'; import { DrawModule } from './draw/draw.module'; +import { FactionModule } from './faction/faction.module'; @Module({ imports: [ @@ -23,6 +23,7 @@ import { DrawModule } from './draw/draw.module'; NotificationModule, TaskModule, DrawModule, + FactionModule, ], controllers: [AppController], providers: [ diff --git a/src/game/coordinate.entity.ts b/src/draw/coordinate.entity.ts similarity index 85% rename from src/game/coordinate.entity.ts rename to src/draw/coordinate.entity.ts index f1a2eff7aec47529eaa11b5de982d871b2533c05..9cee16c6a496d5e9c68a33eabf57514e88cdf7c1 100644 --- a/src/game/coordinate.entity.ts +++ b/src/draw/coordinate.entity.ts @@ -2,17 +2,12 @@ import { Entity, Column, PrimaryGeneratedColumn, - OneToMany, ManyToOne, - PrimaryColumn, Timestamp, } from 'typeorm'; -import { - Game_PersonEntity, - ObjectivePointEntity, - GameEntity, -} from './game.entity'; -import { FactionEntity } from './faction.entity'; + +import { Game_PersonEntity, GameEntity } from '../game/game.entity'; +import { FactionEntity } from '../faction/faction.entity'; @Entity('MapDrawing') export class MapDrawingEntity { diff --git a/src/draw/draw.controller.ts b/src/draw/draw.controller.ts index c721fc51fa418e8413c6993444b89cd361173f4c..21306d7ae91e565a5459138229a384e21fd68abd 100644 --- a/src/draw/draw.controller.ts +++ b/src/draw/draw.controller.ts @@ -7,17 +7,12 @@ import { UsePipes, ValidationPipe, Body, - Delete, } from '@nestjs/common'; -import { AuthGuard } from 'src/shared/auth.guard'; + +import { AuthGuard } from '../shared/auth.guard'; import { DrawService } from './draw.service'; -import { MapDrawingDTO, DrawMapDTO } from './mapdrawing.dto'; -import { Roles } from 'src/shared/roles.decorator'; -import { User } from 'src/user/user.decorator'; -import { FactionDTO } from 'src/game/game.dto'; -import { FactionEntity } from 'src/game/faction.entity'; -import { async } from 'rxjs/internal/scheduler/async'; -import { Game_PersonEntity } from 'src/game/game.entity'; +import { Roles } from '../shared/roles.decorator'; + /* DrawController @@ -42,9 +37,4 @@ export class DrawController { async drawMap(@Param('id') id, @Body() data) { return this.drawService.drawMap(id, data); } - - // @Put('location') - // @UseGuards(new AuthGuard()) - // @UsePipes(new ValidationPipe()) - // async trackLocation() {} } diff --git a/src/draw/draw.module.ts b/src/draw/draw.module.ts index 7f4ef3ad3496b1b3a9ebb5872c2f43108cbe2732..a56cbbbb7fb49b476842177a4a0a9e7e0b592b3c 100644 --- a/src/draw/draw.module.ts +++ b/src/draw/draw.module.ts @@ -3,9 +3,9 @@ import { TypeOrmModule } from '@nestjs/typeorm'; import { DrawController } from './draw.controller'; import { DrawService } from './draw.service'; -import { MapDrawingEntity } from 'src/game/coordinate.entity'; -import { FactionEntity } from 'src/game/faction.entity'; -import { Game_PersonEntity } from 'src/game/game.entity'; +import { MapDrawingEntity } from '../draw/coordinate.entity'; +import { FactionEntity } from '../faction/faction.entity'; +import { Game_PersonEntity } from '../game/game.entity'; /* Draw - contains everything to do with mapdrawing data. diff --git a/src/draw/draw.service.ts b/src/draw/draw.service.ts index 88a1e4116f6b3dccc3c9f5412378f0d6761a78d0..97556e6e7282919465f76026163bf1649bf8f3d1 100644 --- a/src/draw/draw.service.ts +++ b/src/draw/draw.service.ts @@ -1,21 +1,14 @@ import { Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; -import { MapDrawingEntity } from 'src/game/coordinate.entity'; import { Repository } from 'typeorm'; -import { MapDrawingDTO, DrawMapDTO } from './mapdrawing.dto'; -import { Game_PersonEntity, GameEntity } from '../game/game.entity'; -import { GameDTO, FactionDTO } from 'src/game/game.dto'; -import { FactionEntity } from 'src/game/faction.entity'; + +import { MapDrawingEntity } from '../draw/coordinate.entity'; @Injectable() export class DrawService { constructor( @InjectRepository(MapDrawingEntity) private mapDrawingRepository: Repository<MapDrawingEntity>, - @InjectRepository(FactionEntity) - private factionRepository: Repository<FactionEntity>, - @InjectRepository(Game_PersonEntity) - private game_personRepository: Repository<Game_PersonEntity>, ) {} async draw(gameId, data: MapDrawingEntity) { @@ -42,8 +35,4 @@ export class DrawService { // return mapdrawings with given faction and gameid return await this.mapDrawingRepository.find(mapDrawings); } - - // async trackLocation() { - // return 'location'; - // } } diff --git a/src/draw/mapdrawing.dto.ts b/src/draw/mapdrawing.dto.ts index 0e559fa052c8873cdfdfcfecfa325cfe6de0f5dd..03eca2d1057490558e271252d12a2cc1ef883273 100644 --- a/src/draw/mapdrawing.dto.ts +++ b/src/draw/mapdrawing.dto.ts @@ -1,9 +1,11 @@ -import { GameDTO, FactionDTO } from '../game/game.dto'; -import { GameEntity, Game_PersonEntity } from 'src/game/game.entity'; -import { FactionEntity } from 'src/game/faction.entity'; -import { MapDrawingEntity } from 'src/game/coordinate.entity'; import { IsUUID } from 'class-validator'; +import { GameDTO } from '../game/game.dto'; +import { GameEntity, Game_PersonEntity } from '../game/game.entity'; +import { FactionEntity } from '../faction/faction.entity'; +import { FactionDTO } from '../faction/faction.dto'; +import { MapDrawingEntity } from '../draw/coordinate.entity'; + export class MapDrawingDTO { data: JSON; gameId: GameDTO; diff --git a/src/faction/faction.controller.ts b/src/faction/faction.controller.ts new file mode 100644 index 0000000000000000000000000000000000000000..9ff84e5a81cb29d98f1f7bca7f747932fcdf7f8d --- /dev/null +++ b/src/faction/faction.controller.ts @@ -0,0 +1,67 @@ +import { + Controller, + Post, + UseGuards, + UsePipes, + Param, + Body, + Get, + Put, +} from '@nestjs/common'; + +import { AuthGuard } from '../shared/auth.guard'; +import { ValidationPipe } from '../shared/validation.pipe'; +import { User } from '../user/user.decorator'; +import { GameGroupDTO, PromotePlayerDTO, JoinFactionDTO } from './faction.dto'; +import { FactionService } from './faction.service'; +import { Roles } from '../shared/roles.decorator'; + +@Controller('faction') +export class FactionController { + constructor(private factionservice: FactionService) {} + + @Post(':id') + @UseGuards(new AuthGuard()) + @UsePipes(new ValidationPipe()) + async createGroup( + @User('id') person, + @Param('id') id: string, + @Body() data: GameGroupDTO, + ) { + try { + return this.factionservice.createGroup(person, id, data); + } catch (error) {} + } + + @Get('get-groups') + async getGroups() { + return this.factionservice.showGroups(); + } + + @Put('groups/:id') + @UseGuards(new AuthGuard()) + async joinGroup(@User('id') person, @Param('id') id) { + return this.factionservice.joinGroup(person, id); + } + + @Get('get-faction-members/:id') + async getFactionMembers(@Param('id') factionId) { + return this.factionservice.listFactionMembers(factionId); + } + + // param game ID is passed to @Roles + @Put('promote/:id') + @UseGuards(new AuthGuard()) + @UsePipes(new ValidationPipe()) + @Roles('admin') + promotePlayer(@Param('id') game, @Body() body: PromotePlayerDTO) { + return this.factionservice.promotePlayer(body); + } + + @Put('join-faction') + @UseGuards(new AuthGuard()) + @UsePipes(new ValidationPipe()) + joinFaction(@User('id') person, @Body() data: JoinFactionDTO) { + return this.factionservice.joinFaction(person, data); + } +} diff --git a/src/faction/faction.dto.ts b/src/faction/faction.dto.ts new file mode 100644 index 0000000000000000000000000000000000000000..ccc76721e1b16c936aed079e76a8ed0afb56be0e --- /dev/null +++ b/src/faction/faction.dto.ts @@ -0,0 +1,43 @@ +import { + IsUUID, + Length, + Validate, + IsString, + IsNotEmpty, +} from 'class-validator'; + +import { GameEntity } from '../game/game.entity'; +import { RoleValidation } from '../shared/custom-validation'; +import { GameDTO } from '../game/game.dto'; + +export class FactionDTO { + @IsString() + @IsNotEmpty() + @Length(2, 15) + factionName: string; + factionPassword: string; + multiplier?: number; + game: GameDTO; +} + +export class JoinFactionDTO { + @IsUUID('4') + factionId: string; + @Length(3, 31) + factionPassword: string; + @IsUUID('4') + game: GameEntity; +} + +export class PromotePlayerDTO { + @IsUUID('4') + player: string; + @Validate(RoleValidation) + role: string; +} + +export class GameGroupDTO { + @IsString() + @Length(3, 31) + name: string; +} diff --git a/src/game/faction.entity.ts b/src/faction/faction.entity.ts similarity index 77% rename from src/game/faction.entity.ts rename to src/faction/faction.entity.ts index 5d541c1783387bae995a09ff230b3f7c277d30b9..da8c418ef94dee8c841c92cb1fb173dfabdceacd 100644 --- a/src/game/faction.entity.ts +++ b/src/faction/faction.entity.ts @@ -4,11 +4,13 @@ import { PrimaryGeneratedColumn, OneToMany, ManyToOne, + OneToOne, Timestamp, } from 'typeorm'; -import { GameEntity } from './game.entity'; -import { Game_PersonEntity } from './game.entity'; -import { MapDrawingEntity } from './coordinate.entity'; + +import { GameEntity } from '../game/game.entity'; +import { Game_PersonEntity } from '../game/game.entity'; +import { MapDrawingEntity } from '../draw/coordinate.entity'; //Faction, PowerUp, Faction_powerUp, FP_History, Score @@ -81,3 +83,20 @@ export class ScoreEntity { @ManyToOne(type => FactionEntity, factionName => factionName.factionId) faction: FactionEntity; } + +@Entity('GameGroup') +export class GameGroupEntity { + @PrimaryGeneratedColumn('uuid') id: string; + @Column('text') name: string; + @OneToOne(type => Game_PersonEntity, person => person.leaderGroup, { + onDelete: 'CASCADE', + }) + //@JoinColumn({name:'leader'}) + leader: Game_PersonEntity; + @OneToMany(type => Game_PersonEntity, person => person.group, { + onDelete: 'CASCADE', + }) + players: Game_PersonEntity[]; + @ManyToOne(type => GameEntity, game => game.groups) + game: GameEntity; +} diff --git a/src/faction/faction.module.ts b/src/faction/faction.module.ts new file mode 100644 index 0000000000000000000000000000000000000000..dcb399aa1119851781270477c448ecec7362c804 --- /dev/null +++ b/src/faction/faction.module.ts @@ -0,0 +1,20 @@ +import { Module } from '@nestjs/common'; +import { TypeOrmModule } from '@nestjs/typeorm'; + +import { FactionController } from './faction.controller'; +import { FactionService } from './faction.service'; +import { GameGroupEntity, FactionEntity } from './faction.entity'; +import { Game_PersonEntity } from '../game/game.entity'; + +@Module({ + imports: [ + TypeOrmModule.forFeature([ + FactionEntity, + Game_PersonEntity, + GameGroupEntity, + ]), + ], + controllers: [FactionController], + providers: [FactionService], +}) +export class FactionModule {} diff --git a/src/faction/faction.service.ts b/src/faction/faction.service.ts new file mode 100644 index 0000000000000000000000000000000000000000..5814aeba88919f9917c5cddad4751c62cba3507b --- /dev/null +++ b/src/faction/faction.service.ts @@ -0,0 +1,133 @@ +import { Injectable, HttpException, HttpStatus } from '@nestjs/common'; +import { InjectRepository } from '@nestjs/typeorm'; +import { Repository } from 'typeorm'; + +import { FactionEntity, GameGroupEntity } from './faction.entity'; +import { JoinFactionDTO, GameGroupDTO } from './faction.dto'; +import { Game_PersonEntity } from '../game/game.entity'; + +@Injectable() +export class FactionService { + constructor( + @InjectRepository(FactionEntity) + private factionRepository: Repository<FactionEntity>, + @InjectRepository(Game_PersonEntity) + private game_PersonRepository: Repository<Game_PersonEntity>, + @InjectRepository(GameGroupEntity) + private game_GroupRepository: Repository<GameGroupEntity>, + ) {} + + async joinFaction(person, faction: JoinFactionDTO) { + // get faction + const factionInDb = await this.factionRepository.findOne({ + factionId: faction.factionId, + }); + + if (!factionInDb) { + throw new HttpException('No factions exist!', HttpStatus.BAD_REQUEST); + } + //check if password is correct + if (factionInDb.passwordCheck(faction.factionPassword)) { + const gameperson = await this.game_PersonRepository.create({ + faction: factionInDb, + game: faction.game, + role: 'soldier', + person: person, + }); + //check if user is already in a faction + if (await this.game_PersonRepository.findOne(gameperson)) { + throw new HttpException( + 'You have already joined faction!', + HttpStatus.BAD_REQUEST, + ); + } + // insert to database + return await this.game_PersonRepository.save(gameperson); + } else { + throw new HttpException('Invalid password!', HttpStatus.UNAUTHORIZED); + } + } + + async promotePlayer(body) { + const gamepersonId = body.player; + // get playerdata + const gameperson = await this.game_PersonRepository.findOne({ + where: { gamepersonId }, + }); + if (gameperson) { + const factionleader = await this.game_PersonRepository.create(gameperson); + factionleader.role = body.role; + return await this.game_PersonRepository.save(factionleader); + } + throw new HttpException('player does not exist', HttpStatus.BAD_REQUEST); + } + + // checks the password, creates an entry in GamePerson table with associated role&faction + async createGroup(person, gameId, groupData: GameGroupDTO) { + // check if the person already is in a group in this game + const checkDuplicate = await this.game_PersonRepository.findOne({ + person: person, + }); + if (checkDuplicate) { + throw new HttpException( + 'You already belong to a group!', + HttpStatus.BAD_REQUEST, + ); + } + + // create a group entry and insert it to db + const group = await this.game_GroupRepository.create({ + ...groupData, + game: gameId, + }); + const gameGroup = await this.game_GroupRepository.insert(group); + + // create game_Person entry and insert it to db + const gamePerson = await this.game_PersonRepository.create({ + role: 'soldier', + faction: null, + game: gameId, + person: person, + leaderGroup: gameGroup.identifiers[0]['id'], + group: gameGroup.identifiers[0]['id'], + }); + await this.game_PersonRepository.insert(gamePerson); + + return { + message: 'created new group', + }; + } + + async showGroups() { + return await this.game_GroupRepository.find({ + relations: ['leader', 'players', 'game'], + }); + } + + async joinGroup(person, groupId) { + const gameData = await this.game_GroupRepository.findOne({ + where: { id: groupId }, + relations: ['players', 'game'], + }); + const gamePerson = await this.game_PersonRepository.create({ + role: 'soldier', + faction: null, + game: gameData.game, + person: person, + leaderGroup: null, + group: groupId, + }); + await this.game_PersonRepository.insert(gamePerson); + return { + message: 'Joined group', + }; + } + + async listFactionMembers(faction) { + return await this.game_PersonRepository.find({ + where: { faction }, + relations: ['person'], + order: { person: 'DESC' }, + }); + } +} diff --git a/src/game/game.controller.spec.ts b/src/game/game.controller.spec.ts deleted file mode 100644 index 23d9a2f9c849e6b63b31ac707abca1cef9874012..0000000000000000000000000000000000000000 --- a/src/game/game.controller.spec.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { Test, TestingModule } from '@nestjs/testing'; -import { GameController } from './game.controller'; - -describe('Game Controller', () => { - let controller: GameController; - - beforeEach(async () => { - const module: TestingModule = await Test.createTestingModule({ - controllers: [GameController], - }).compile(); - - controller = module.get<GameController>(GameController); - }); - - it('should be defined', () => { - expect(controller).toBeDefined(); - }); -}); diff --git a/src/game/game.controller.ts b/src/game/game.controller.ts index 8f3fc988d39101362fae36fbd4dc0754208ab89e..3c6d003c98011ca14946230496345c65b74e9e6d 100644 --- a/src/game/game.controller.ts +++ b/src/game/game.controller.ts @@ -12,13 +12,7 @@ import { import { GameService } from './game.service'; import { AuthGuard } from '../shared/auth.guard'; import { User } from '../user/user.decorator'; -import { - GameDTO, - GameGroupDTO, - FlagboxEventDTO, - JoinFactionDTO, - PromotePlayerDTO, -} from './game.dto'; +import { GameDTO, FlagboxEventDTO } from './game.dto'; import { ValidationPipe } from '../shared/validation.pipe'; import { Roles } from '../shared/roles.decorator'; @@ -40,51 +34,6 @@ export class GameController { return this.gameservice.editGame(id, body); } - @Post(':id') - @UseGuards(new AuthGuard()) - @UsePipes(new ValidationPipe()) - async createGroup( - @User('id') person, - @Param('id') id: string, - @Body() data: GameGroupDTO, - ) { - try { - return this.gameservice.createGroup(person, id, data); - } catch (error) {} - } - - @Get('get-groups') - async getGroups() { - return this.gameservice.showGroups(); - } - - @Put('groups/:id') - @UseGuards(new AuthGuard()) - async joinGroup(@User('id') person, @Param('id') id) { - return this.gameservice.joinGroup(person, id); - } - - @Get('get-faction-members/:id') - async getFactionMembers(@Param('id') factionId) { - return this.gameservice.listFactionMembers(factionId); - } - - // param game ID is passed to @Roles - @Put('promote/:id') - @UseGuards(new AuthGuard()) - @UsePipes(new ValidationPipe()) - @Roles('admin') - promotePlayer(@Param('id') game, @Body() body: PromotePlayerDTO) { - return this.gameservice.promotePlayer(body); - } - - @Put('join-faction') - @UseGuards(new AuthGuard()) - @UsePipes(new ValidationPipe()) - joinFaction(@User('id') person, @Body() data: JoinFactionDTO) { - return this.gameservice.joinFaction(person, data); - } - @Get('listgames') async listGames() { return this.gameservice.listGames(); diff --git a/src/game/game.dto.ts b/src/game/game.dto.ts index d4acf7cd5baaf749e8c18ea4bf7a48bc3496caba..09c39b7ecf630b176a1098b703e8a59f7f550ee2 100644 --- a/src/game/game.dto.ts +++ b/src/game/game.dto.ts @@ -7,10 +7,11 @@ import { Validate, Min, Max, - IsUUID, } from 'class-validator'; -import { GameEntity, ObjectivePointEntity } from './game.entity'; -import { CenterJSON, RoleValidation } from '../shared/custom-validation'; + +import { ObjectivePointEntity } from './game.entity'; +import { CenterJSON } from '../shared/custom-validation'; +import { FactionDTO } from '../faction/faction.dto'; export class GameDTO { @IsString() @@ -55,16 +56,6 @@ export class newGameDTO { enddate: string; } -export class FactionDTO { - @IsString() - @IsNotEmpty() - @Length(2, 15) - factionName: string; - factionPassword: string; - multiplier?: number; - game: GameDTO; -} - export class FlagboxDTO { @IsString() @IsNotEmpty() @@ -94,25 +85,3 @@ export class FlagboxEventDTO { oP_HistoryTimestamp?: string; objective_point?: ObjectivePointEntity; } - -export class GameGroupDTO { - @IsString() - @Length(3, 31) - name: string; -} - -export class JoinFactionDTO { - @IsUUID('4') - factionId: string; - @Length(3, 31) - factionPassword: string; - @IsUUID('4') - game: GameEntity; -} - -export class PromotePlayerDTO { - @IsUUID('4') - player: string; - @Validate(RoleValidation) - role: string; -} diff --git a/src/game/game.entity.ts b/src/game/game.entity.ts index 036b31c8a3cf1d0343c8f31c691a50d749d97d0f..9038b5a193356d41923b0a9f1611d12edca70856 100644 --- a/src/game/game.entity.ts +++ b/src/game/game.entity.ts @@ -9,10 +9,10 @@ import { JoinColumn, } from 'typeorm'; +import { MapDrawingEntity } from '../draw/coordinate.entity'; import { PersonEntity } from '../user/user.entity'; -import { GameGroupEntity } from './group.entity'; -import { FactionEntity } from './faction.entity'; -import { MapDrawingEntity } from './coordinate.entity'; +import { GameGroupEntity } from '../faction/faction.entity'; +import { FactionEntity } from '../faction/faction.entity'; import { TaskEntity } from '../task/task.entity'; // table that stores all created games diff --git a/src/game/game.module.ts b/src/game/game.module.ts index c908e483b7b1a867d5d8dfb8e8fab54ea212ecfe..1c9e86faf59645bf537e76a8720bb6ab741fe6bd 100644 --- a/src/game/game.module.ts +++ b/src/game/game.module.ts @@ -10,8 +10,8 @@ import { ObjectivePoint_HistoryEntity, } from './game.entity'; import { PersonEntity } from '../user/user.entity'; -import { GameGroupEntity } from './group.entity'; -import { FactionEntity } from './faction.entity'; +import { GameGroupEntity } from '../faction/faction.entity'; +import { FactionEntity } from '../faction/faction.entity'; import { NotificationModule } from '../notifications/notifications.module'; @Module({ diff --git a/src/game/game.service.spec.ts b/src/game/game.service.spec.ts deleted file mode 100644 index f4a1db7e70bf2a0e38c6d430c95e54feb3934fdf..0000000000000000000000000000000000000000 --- a/src/game/game.service.spec.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { Test, TestingModule } from '@nestjs/testing'; -import { GameService } from './game.service'; - -describe('GameService', () => { - let service: GameService; - - beforeEach(async () => { - const module: TestingModule = await Test.createTestingModule({ - providers: [GameService], - }).compile(); - - service = module.get<GameService>(GameService); - }); - - it('should be defined', () => { - expect(service).toBeDefined(); - }); -}); diff --git a/src/game/game.service.ts b/src/game/game.service.ts index bce915d847e23055bf0a20d70486a3934e8aac21..1bad6aed11bc8a3512a711238599e46234e7b89c 100644 --- a/src/game/game.service.ts +++ b/src/game/game.service.ts @@ -8,16 +8,10 @@ import { ObjectivePointEntity, ObjectivePoint_HistoryEntity, } from './game.entity'; -import { - GameDTO, - FlagboxEventDTO, - JoinFactionDTO, - GameGroupDTO, -} from './game.dto'; +import { GameDTO, FlagboxEventDTO } from './game.dto'; import { PersonEntity } from '../user/user.entity'; -import { GameGroupEntity } from './group.entity'; -import { FactionEntity } from './faction.entity'; -import { NotificationGateway } from 'src/notifications/notifications.gateway'; +import { FactionEntity } from '../faction/faction.entity'; +import { NotificationGateway } from '../notifications/notifications.gateway'; @Injectable() export class GameService { @@ -26,12 +20,8 @@ export class GameService { private gameRepository: Repository<GameEntity>, @InjectRepository(FactionEntity) private factionRepository: Repository<FactionEntity>, - @InjectRepository(PersonEntity) - private personRepository: Repository<PersonEntity>, @InjectRepository(Game_PersonEntity) private game_PersonRepository: Repository<Game_PersonEntity>, - @InjectRepository(GameGroupEntity) - private game_GroupRepository: Repository<GameGroupEntity>, @InjectRepository(ObjectivePointEntity) private objectivePointRepository: Repository<ObjectivePointEntity>, @InjectRepository(ObjectivePoint_HistoryEntity) @@ -128,67 +118,6 @@ export class GameService { }; } - // checks the password, creates an entry in GamePerson table with associated role&faction - async createGroup(person, gameId, groupData: GameGroupDTO) { - // check if the person already is in a group in this game - const checkDuplicate = await this.game_PersonRepository.findOne({ - person: person, - }); - if (checkDuplicate) { - throw new HttpException( - 'You already belong to a group!', - HttpStatus.BAD_REQUEST, - ); - } - - // create a group entry and insert it to db - const group = await this.game_GroupRepository.create({ - ...groupData, - game: gameId, - }); - const gameGroup = await this.game_GroupRepository.insert(group); - - // create game_Person entry and insert it to db - const gamePerson = await this.game_PersonRepository.create({ - role: 'soldier', - faction: null, - game: gameId, - person: person, - leaderGroup: gameGroup.identifiers[0]['id'], - group: gameGroup.identifiers[0]['id'], - }); - await this.game_PersonRepository.insert(gamePerson); - - return { - message: 'created new group', - }; - } - - async showGroups() { - return await this.game_GroupRepository.find({ - relations: ['leader', 'players', 'game'], - }); - } - - async joinGroup(person, groupId) { - const gameData = await this.game_GroupRepository.findOne({ - where: { id: groupId }, - relations: ['players', 'game'], - }); - const gamePerson = await this.game_PersonRepository.create({ - role: 'soldier', - faction: null, - game: gameData.game, - person: person, - leaderGroup: null, - group: groupId, - }); - await this.game_PersonRepository.insert(gamePerson); - return { - message: 'Joined group', - }; - } - // returns name and id of each game async listGames() { const games = await this.gameRepository.find(); @@ -197,14 +126,6 @@ export class GameService { }); } - async listFactionMembers(faction) { - return await this.game_PersonRepository.find({ - where: { faction }, - relations: ['person'], - order: { person: 'DESC' }, - }); - } - // returns information about a game identified by id async returnGameInfo(id: string) { const game = await this.gameRepository.findOne({ @@ -240,49 +161,4 @@ export class GameService { // send flagbox event to flagbox subscribers this.notificationGateway.server.emit('flagbox', 'event update'); } - - async promotePlayer(body) { - const gamepersonId = body.player; - // get playerdata - const gameperson = await this.game_PersonRepository.findOne({ - where: { gamepersonId }, - }); - if (gameperson) { - const factionleader = await this.game_PersonRepository.create(gameperson); - factionleader.role = body.role; - return await this.game_PersonRepository.save(factionleader); - } - throw new HttpException('player does not exist', HttpStatus.BAD_REQUEST); - } - - async joinFaction(person, faction: JoinFactionDTO) { - // get faction - const factionInDb = await this.factionRepository.findOne({ - factionId: faction.factionId, - }); - - if (!factionInDb) { - throw new HttpException('No factions exist!', HttpStatus.BAD_REQUEST); - } - //check if password is correct - if (factionInDb.passwordCheck(faction.factionPassword)) { - const gameperson = await this.game_PersonRepository.create({ - faction: factionInDb, - game: faction.game, - role: 'soldier', - person: person, - }); - //check if user is already in a faction - if (await this.game_PersonRepository.findOne(gameperson)) { - throw new HttpException( - 'You have already joined faction!', - HttpStatus.BAD_REQUEST, - ); - } - // insert to database - return await this.game_PersonRepository.save(gameperson); - } else { - throw new HttpException('Invalid password!', HttpStatus.UNAUTHORIZED); - } - } } diff --git a/src/game/group.entity.ts b/src/game/group.entity.ts deleted file mode 100644 index 0b9f00ed621b41b8515a77126f9564c14a02d74b..0000000000000000000000000000000000000000 --- a/src/game/group.entity.ts +++ /dev/null @@ -1,28 +0,0 @@ -import { - Entity, - Column, - PrimaryGeneratedColumn, - ManyToOne, - OneToMany, - OneToOne, - JoinColumn, - } from 'typeorm'; - -import { Game_PersonEntity, GameEntity } from './game.entity'; - -@Entity('GameGroup') -export class GameGroupEntity { - @PrimaryGeneratedColumn('uuid') id: string; - @Column('text') name: string; - @OneToOne(type => Game_PersonEntity, person => person.leaderGroup, { - onDelete: 'CASCADE' - }) - //@JoinColumn({name:'leader'}) - leader: Game_PersonEntity; - @OneToMany(type => Game_PersonEntity, person => person.group, { - onDelete: 'CASCADE' - }) - players: Game_PersonEntity[]; - @ManyToOne(type => GameEntity, game => game.groups) - game: GameEntity; -} \ No newline at end of file diff --git a/src/main.ts b/src/main.ts index 1e3e4e147bc386a4ce15a36bf274face5fd14c1e..68871be82d89a37a410b93b252fb6f1e712ad143 100644 --- a/src/main.ts +++ b/src/main.ts @@ -3,7 +3,6 @@ import * as rateLimit from 'express-rate-limit'; import { AppModule } from './app.module'; - /* Main.ts starts the server. */ @@ -13,7 +12,7 @@ import { AppModule } from './app.module'; // see https://github.com/nfriedly/express-rate-limit/issues/138 const limiter = (rateLimit as any)({ windowMs: 60 * 1000, // one minute - max: 100 // limit each IP to 100 requests per windowMs + max: 100, // limit each IP to 100 requests per windowMs }); async function bootstrap() { diff --git a/src/mapmarkers/mapmarker.dto.ts b/src/mapmarkers/mapmarker.dto.ts deleted file mode 100644 index f9821f4cbc73b62582263ac63e29c07a75a46b44..0000000000000000000000000000000000000000 --- a/src/mapmarkers/mapmarker.dto.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { IsString, IsJSON } from 'class-validator'; -/* -DTO: MapMarker -- represents servers data handling. -*/ - - -export class MapMarkerDTO { - @IsString() - type:string; - @IsString() - latitude: string; - @IsString() - longitude: string; - @IsString() - timestamp: string; - @IsJSON() - features: JSON; -} \ No newline at end of file diff --git a/src/mapmarkers/mapmarker.entity.ts b/src/mapmarkers/mapmarker.entity.ts deleted file mode 100644 index 0b26cbaa9a869fd81c7669a557b233a32c67b0f4..0000000000000000000000000000000000000000 --- a/src/mapmarkers/mapmarker.entity.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { - Entity, - Column, - PrimaryGeneratedColumn, - Timestamp, - ManyToOne, -} from 'typeorm'; - -import { PersonEntity } from '../user/user.entity'; -import { GameEntity, Game_PersonEntity } from 'src/game/game.entity'; - -/* - Entity: MapMarker - - represents data that database contains on mapmarker - */ - -/* @Entity('MapMarker') - export class MapMarkerEntity { - @PrimaryGeneratedColumn('uuid') id: string; - @Column({ type: 'text', nullable: true }) latitude: string; - @Column({ type: 'text', nullable: true }) longitude: string; - @Column({ type: 'timestamp' }) timestamp: Timestamp; - @Column({ type: 'json', nullable: true }) features: JSON; - @ManyToOne(type => Game_PersonEntity, player => player.markers) - player: Game_PersonEntity; - } - */ diff --git a/src/mapmarkers/mapmarker.service.ts b/src/mapmarkers/mapmarker.service.ts deleted file mode 100644 index f320d17b0892cec302281a1ee6f99b13fb788b78..0000000000000000000000000000000000000000 --- a/src/mapmarkers/mapmarker.service.ts +++ /dev/null @@ -1,56 +0,0 @@ -import { Injectable } from '@nestjs/common'; -import { Repository } from 'typeorm'; -import { InjectRepository } from '@nestjs/typeorm'; - -import { MapMarkerEntity } from './mapmarker.entity'; -import { MapMarkerDTO } from './mapmarker.dto'; -import { PersonEntity } from '../user/user.entity'; - -@Injectable() -export class MapMarkerService { - constructor( - //create references to tables as repositories - @InjectRepository(MapMarkerEntity) - private mapmarkerRepository: Repository<MapMarkerEntity>, - @InjectRepository(PersonEntity) - private personRepository: Repository<PersonEntity>, - ) {} - - // insert marker - async insertLocation(personId: string, data: MapMarkerDTO) { - try { - //get functions runtime as timestamp - data.timestamp = new Date(Date.now()).toLocaleString(); - //check from database for the user who uploads the data - const user = await this.personRepository.findOne({ - where: { id: personId }, - }); - //create© entity properties - const location = await this.mapmarkerRepository.create({ - ...data, - }); - // insert created entity NOTE: insert method doesn't check for duplicates. - await this.mapmarkerRepository.insert(location); - // return data and player id&name - return { ...data }; - } catch (error) { - return error; - } - } - - // get all markers - async getAllMarkers() { - try { - // find all markers with specified player - const markers = await this.mapmarkerRepository.find({ - relations: ['player'], - }); - // return markers from database with said playerdata - return markers.map(marker => { - return { ...marker }; - }); - } catch (error) { - return error.message; - } - } -} diff --git a/src/mapmarkers/mapmarkers.controller.spec.ts b/src/mapmarkers/mapmarkers.controller.spec.ts deleted file mode 100644 index 95cbd74e5c45f9713337458335c52257bf5d1220..0000000000000000000000000000000000000000 --- a/src/mapmarkers/mapmarkers.controller.spec.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { Test, TestingModule } from '@nestjs/testing'; -import { MapMarkersController } from './mapmarkers.controller'; - -describe('Mapmarkers Controller', () => { - let controller: MapMarkersController; - - beforeEach(async () => { - const module: TestingModule = await Test.createTestingModule({ - controllers: [MapMarkersController], - }).compile(); - - controller = module.get<MapMarkersController>(MapMarkersController); - }); - - it('should be defined', () => { - expect(controller).toBeDefined(); - }); -}); diff --git a/src/mapmarkers/mapmarkers.controller.ts b/src/mapmarkers/mapmarkers.controller.ts deleted file mode 100644 index fef971652de596196169251c202378c67902babd..0000000000000000000000000000000000000000 --- a/src/mapmarkers/mapmarkers.controller.ts +++ /dev/null @@ -1,32 +0,0 @@ -import { Controller, Body, Get, Put, UseGuards } from '@nestjs/common'; - -import { MapMarkerService } from './mapmarker.service'; -import { MapMarkerDTO } from './mapmarker.dto'; -import { AuthGuard } from '../shared/auth.guard'; -import { User } from '../user/user.decorator'; - -@Controller('mapmarkers') -export class MapMarkersController { - constructor(private mapmarkerservice: MapMarkerService){} - - // Insert figure location, needs "authorization" header with valid Bearer token and content-type json - @Put('insert-location') - @UseGuards(new AuthGuard()) - async insertLocation(@User('id') person, @Body() data: MapMarkerDTO): Promise<string>{ - try { - return this.mapmarkerservice.insertLocation(person, data); - } catch (error) { - return error; - } - } - - // return all markers through service - @Get('getall') - async getAll(){ - try{ - return this.mapmarkerservice.getAllMarkers(); - }catch(error){ - return error.message; - } - } -} diff --git a/src/mapmarkers/mapmarkers.module.ts b/src/mapmarkers/mapmarkers.module.ts deleted file mode 100644 index a69970ec970f6f70adc8b38351364698c9e61a1e..0000000000000000000000000000000000000000 --- a/src/mapmarkers/mapmarkers.module.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { Module } from '@nestjs/common'; -import { TypeOrmModule } from '@nestjs/typeorm'; - -import { MapMarkersController } from './mapmarkers.controller'; -import { MapMarkerService } from './mapmarker.service'; -/*import { MapMarkerEntity } from './mapmarker.entity'; -import { PersonEntity } from '../user/user.entity'; - -@Module({ - imports: [TypeOrmModule.forFeature([MapMarkerEntity, PersonEntity])], - controllers: [MapMarkersController], - providers: [MapMarkerService] -}) -export class MapMarkerModule {}*/ diff --git a/src/notifications/notifications.module.ts b/src/notifications/notifications.module.ts index 97cf59c1f929cfc14b5335b0308e7615ac10dee8..194c5c38155d0f976d286456a53814a08f517050 100644 --- a/src/notifications/notifications.module.ts +++ b/src/notifications/notifications.module.ts @@ -1,7 +1,8 @@ import { Module } from '@nestjs/common'; +import { TypeOrmModule } from '@nestjs/typeorm'; + import { NotificationGateway } from './notifications.gateway'; import { NotificationEntity } from './notification.entity'; -import { TypeOrmModule } from '@nestjs/typeorm'; @Module({ imports: [TypeOrmModule.forFeature([NotificationEntity])], diff --git a/src/task/task.controller.ts b/src/task/task.controller.ts index 32766ffd6bc1c6f5f51a5234ae2024bc345582e2..2cb030ff4332cc890e212f9749f1c84f0aa81248 100644 --- a/src/task/task.controller.ts +++ b/src/task/task.controller.ts @@ -1,19 +1,10 @@ -import { - Controller, - Post, - Body, - UsePipes, - Get, - UseGuards, - Param, -} from '@nestjs/common'; +import { Controller, Post, Body, UsePipes, Get, Param } from '@nestjs/common'; import { TaskService } from './task.service'; import { CreateTaskDTO, EditTaskDTO } from './task.dto'; -import { AuthGuard } from '../shared/auth.guard'; import { Roles } from '../shared/roles.decorator'; import { ValidationPipe } from '../shared/validation.pipe'; -import { User } from 'src/user/user.decorator'; +import { User } from '../user/user.decorator'; @Controller('task') export class TaskController { diff --git a/src/task/task.dto.ts b/src/task/task.dto.ts index 3a363b5f95602d03294a5d256982558f19c1b937..a8830d7a770f8337c81b1922dcf02751eaf4e6ad 100644 --- a/src/task/task.dto.ts +++ b/src/task/task.dto.ts @@ -6,7 +6,7 @@ import { IsUUID, Equals, } from 'class-validator'; -import { FactionEntity } from '../game/faction.entity'; +import { FactionEntity } from '../faction/faction.entity'; import { GameEntity } from '../game/game.entity'; import { Uuid } from '../shared/custom-validation'; diff --git a/src/task/task.entity.ts b/src/task/task.entity.ts index e6fd61879325993ff34609d7e0b15f5dd89dfc18..85ee3c26d3830bdb81ebe89bccac93051e0562c7 100644 --- a/src/task/task.entity.ts +++ b/src/task/task.entity.ts @@ -5,8 +5,9 @@ import { ManyToOne, JoinColumn, } from 'typeorm'; -import { FactionEntity } from 'src/game/faction.entity'; -import { GameEntity } from 'src/game/game.entity'; + +import { FactionEntity } from '../faction/faction.entity'; +import { GameEntity } from '../game/game.entity'; @Entity('Task') export class TaskEntity { diff --git a/src/task/task.module.ts b/src/task/task.module.ts index 12112f92a47ef2c30f0c130d15dba0d5b4c41126..66d5ac5fce93d8447317343283a84b5002755ab6 100644 --- a/src/task/task.module.ts +++ b/src/task/task.module.ts @@ -4,7 +4,7 @@ import { TypeOrmModule } from '@nestjs/typeorm'; import { TaskService } from './task.service'; import { TaskController } from './task.controller'; import { TaskEntity } from './task.entity'; -import { FactionEntity } from '../game/faction.entity'; +import { FactionEntity } from '../faction/faction.entity'; import { Game_PersonEntity } from '../game/game.entity'; import { NotificationModule } from '../notifications/notifications.module'; diff --git a/src/task/task.service.ts b/src/task/task.service.ts index 3fbe8c51c827d516aedf97a789ccd9badd1cf50b..f340a99f3a7f09816754a4c3f327c64d43166853 100644 --- a/src/task/task.service.ts +++ b/src/task/task.service.ts @@ -4,7 +4,7 @@ import { InjectRepository } from '@nestjs/typeorm'; import { TaskEntity } from './task.entity'; import { CreateTaskDTO, EditTaskDTO } from './task.dto'; -import { FactionEntity } from '../game/faction.entity'; +import { FactionEntity } from '../faction/faction.entity'; import { Game_PersonEntity } from '../game/game.entity'; import { NotificationGateway } from '../notifications/notifications.gateway'; diff --git a/src/user/user.controller.spec.ts b/src/user/user.controller.spec.ts deleted file mode 100644 index 95e4e6222d74fb868ba0480ec5da337c636359c5..0000000000000000000000000000000000000000 --- a/src/user/user.controller.spec.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { Test, TestingModule } from '@nestjs/testing'; -import { UserController } from './user.controller'; - -describe('User Controller', () => { - let controller: UserController; - - beforeEach(async () => { - const module: TestingModule = await Test.createTestingModule({ - controllers: [UserController], - }).compile(); - - controller = module.get<UserController>(UserController); - }); - - it('should be defined', () => { - expect(controller).toBeDefined(); - }); -}); diff --git a/src/user/user.entity.ts b/src/user/user.entity.ts index 1e44121e0eed81e1a1b7a7f35e9a95e557c89e5e..20f16a07b0a0eb2ea7759953b22dc9e3d0a5653e 100644 --- a/src/user/user.entity.ts +++ b/src/user/user.entity.ts @@ -7,6 +7,7 @@ import { } from 'typeorm'; import * as bcrypt from 'bcryptjs'; import * as jwt from 'jsonwebtoken'; + import { Game_PersonEntity } from '../game/game.entity'; @Entity('Person') diff --git a/src/user/user.service.spec.ts b/src/user/user.service.spec.ts deleted file mode 100644 index 873de8ac4d8e77fc827e4f3939f0068391a3800c..0000000000000000000000000000000000000000 --- a/src/user/user.service.spec.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { Test, TestingModule } from '@nestjs/testing'; -import { UserService } from './user.service'; - -describe('UserService', () => { - let service: UserService; - - beforeEach(async () => { - const module: TestingModule = await Test.createTestingModule({ - providers: [UserService], - }).compile(); - - service = module.get<UserService>(UserService); - }); - - it('should be defined', () => { - expect(service).toBeDefined(); - }); -}); diff --git a/test/app.e2e-spec.ts b/test/app.e2e-spec.ts deleted file mode 100644 index 61920f1f89db627dfcb9f94c2e7651cb83f5134b..0000000000000000000000000000000000000000 --- a/test/app.e2e-spec.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { Test, TestingModule } from '@nestjs/testing'; -import * as request from 'supertest'; -import { AppModule } from './../src/app.module'; - -describe('AppController (e2e)', () => { - let app; - - beforeEach(async () => { - const moduleFixture: TestingModule = await Test.createTestingModule({ - imports: [AppModule], - }).compile(); - - app = moduleFixture.createNestApplication(); - await app.init(); - }); - - it('/ (GET)', () => { - return request(app.getHttpServer()) - .get('/') - .expect(200) - .expect('Hello World!'); - }); -}); diff --git a/test/jest-e2e.json b/test/jest-e2e.json deleted file mode 100644 index e9d912f3e3cefc18505d3cd19b3a5a9f567f5de0..0000000000000000000000000000000000000000 --- a/test/jest-e2e.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "moduleFileExtensions": ["js", "json", "ts"], - "rootDir": ".", - "testEnvironment": "node", - "testRegex": ".e2e-spec.ts$", - "transform": { - "^.+\\.(t|j)s$": "ts-jest" - } -}