diff --git a/src/game/game.controller.ts b/src/game/game.controller.ts index 92ca90da621844962966e7281c98f3e4ac95a9bb..9eb94eb6bf4ed09c6a359c72daf8cfdcb0634079 100644 --- a/src/game/game.controller.ts +++ b/src/game/game.controller.ts @@ -12,9 +12,8 @@ import { import { GameService } from './game.service'; import { AuthGuard } from '../shared/auth.guard'; import { User } from '../user/user.decorator'; -import { GameDTO } from './game.dto'; +import { GameDTO, GameGroupDTO } from './game.dto'; import { ValidationPipe } from '../shared/validation.pipe'; -import { Game_PersonEntity } from './game.entity'; import { Roles } from '../shared/roles.decorator'; @Controller('game') @@ -35,14 +34,25 @@ export class GameController { return this.gameservice.editGame(id, body); } - // @UseGuards(new AuthGuard()) - // @UsePipes(new ValidationPipe()) - // @Post(':id') - // async joinGame(@User('id') person, @Body() data: Game_PersonEntity) { - // try { - // return this.gameservice.joinGame(person, data, json); - // } catch (error) {} - // } + @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) + } @Put('joinfaction') @UseGuards(new AuthGuard()) diff --git a/src/game/game.dto.ts b/src/game/game.dto.ts index 12bde8680e866f7956fb69ac4667581ac9e96619..5f70a7d43e1acb68dba89e28e31445ee5c76af3d 100644 --- a/src/game/game.dto.ts +++ b/src/game/game.dto.ts @@ -47,3 +47,9 @@ export class FactionDTO { game: GameDTO; password: string; } + +export class GameGroupDTO { + @IsString() + @Length(3, 31) + name: string; +} diff --git a/src/game/game.entity.ts b/src/game/game.entity.ts index f4d5e24630c90ca10e296ee57c1d537e7f8238e3..08c46a6c95a6b52d778b4cf5a5b729591ad3c181 100644 --- a/src/game/game.entity.ts +++ b/src/game/game.entity.ts @@ -5,10 +5,11 @@ import { ManyToOne, OneToMany, Timestamp, + OneToOne, } from 'typeorm'; -import { PersonEntity } from '../user/user.entity'; -import * as jwt from 'jsonwebtoken'; +import { PersonEntity } from '../user/user.entity'; +import { GameGroupEntity } from './group.entity'; // table that stores all created games @Entity('Game') @@ -24,6 +25,8 @@ export class GameEntity { factions: FactionEntity[]; @OneToMany(type => Game_PersonEntity, game_persons => game_persons.game) game_persons: Game_PersonEntity[]; + @OneToMany(type => GameGroupEntity, group => group.game) + groups: GameGroupEntity[]; gameObject() { const { id, name } = this; @@ -56,9 +59,14 @@ export class Game_PersonEntity { game: GameEntity; @ManyToOne(type => PersonEntity, person => person.game_persons) person: PersonEntity; + @OneToOne(type => GameGroupEntity, group => group.leader) + leaderGroup: GameGroupEntity; + @ManyToOne(type => GameGroupEntity, group => group.players) + group: GameGroupEntity; /* @ManyToOne(type => PersonRoleEntity, person_role => person_role.game_persons) person_role: PersonRoleEntity; @ManyToMany(type => CoordinateEntity, game_person_coordinates => game_person_coordinates.game_persons) game_person_coordinates: CoordinateEntity[]; */ } + diff --git a/src/game/game.module.ts b/src/game/game.module.ts index 5926f1ca7a58df1db07a2c64304a8b3030a9a7f0..6851ea3d9d3ac299f44c91451e1cb960f053cff7 100644 --- a/src/game/game.module.ts +++ b/src/game/game.module.ts @@ -5,9 +5,10 @@ import { GameController } from './game.controller'; import { GameService } from './game.service'; import { GameEntity, FactionEntity, Game_PersonEntity } from './game.entity'; import { PersonEntity } from '../user/user.entity'; +import { GameGroupEntity } from './group.entity'; @Module({ - imports: [TypeOrmModule.forFeature([GameEntity, FactionEntity, Game_PersonEntity, PersonEntity])], + imports: [TypeOrmModule.forFeature([GameEntity, FactionEntity, Game_PersonEntity, PersonEntity, GameGroupEntity])], controllers: [GameController], providers: [GameService] }) diff --git a/src/game/game.service.ts b/src/game/game.service.ts index dd22aa05ffe6f40969dc2de706caff14f79e76fa..26d1c8da5b556ed7ffc3e892b0a0310b209b0867 100644 --- a/src/game/game.service.ts +++ b/src/game/game.service.ts @@ -1,10 +1,11 @@ -import { Injectable, Logger, HttpException, HttpStatus } from '@nestjs/common'; +import { Injectable, HttpException, HttpStatus } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; -import { Repository, In, QueryBuilder, Not } from 'typeorm'; +import { Repository, Not, In } from 'typeorm'; import { GameEntity, FactionEntity, Game_PersonEntity } from './game.entity'; import { GameDTO } from './game.dto'; import { PersonEntity } from '../user/user.entity'; +import { GameGroupEntity } from './group.entity'; @Injectable() export class GameService { @@ -17,6 +18,8 @@ export class GameService { private personRepository: Repository<PersonEntity>, @InjectRepository(Game_PersonEntity) private game_PersonRepository: Repository<Game_PersonEntity>, + @InjectRepository(GameGroupEntity) + private game_GroupRepository: Repository<GameGroupEntity>, ) {} // create a new game @@ -89,20 +92,93 @@ export class GameService { } // checks the password, creates an entry in GamePerson table with associated role&faction - async joinGame(person, gameId, json) { - const user = await this.personRepository.findOne({ - where: { id: person }, - }); - const game = await this.gameRepository.findOne({ where: { id: gameId } }); + async createGroup(person, gameId, groupData) { + try { + // create a partial game_Person entry and insert it to db + const gamePerson = await this.game_PersonRepository.create({ + role: 'soldier', + faction: null, + game: gameId, + person: person, + leaderGroup: null, + group: null, + }); + const gamePersonId = await this.game_PersonRepository.insert(gamePerson); - // create game_Person entry - /* const gamePerson = await this.game_PersonRepository.create({ - faction, - gameId, - person, - }); - */ - return 'WIP'; + // create a group entry and insert it to db + const group = await this.game_GroupRepository.create({ + ...groupData, + leader: gamePerson.gamepersonId, + players: gamePerson.gamepersonId, + game: gameId, + }); + const gameGroup = await this.game_GroupRepository.insert(group); + + // add the missing information to game_Person and update the db entry + gamePerson['gamepersonId'] = gamePersonId.identifiers[0]['gamepersonId']; + gamePerson['leaderGroup'] = gameGroup.identifiers[0]['id']; + gamePerson['group'] = gameGroup.identifiers[0]['id']; + await this.game_PersonRepository.save(gamePerson); + + return { + "message": "created new group" + }; + } catch (e) { + return e; + } + } + + async showGroups() { + try { + return await this.game_GroupRepository.find({ + relations: ['leader', 'players', 'game'], + }); + } catch (e) { + return e; + } + } + + async joinGroup(person, groupId) { + try { + // get the old group data to see earlier players + const oldData = await this.game_GroupRepository.findOne({ + where: { id: groupId }, + relations: ['players', 'game'], + }); + // create game_Person entry for the joining player and insert it + const gamePerson = await this.game_PersonRepository.create({ + role: 'soldier', + faction: null, + game: oldData.game, + person: person, + leaderGroup: null, + group: null, + }); + console.log(oldData) + console.log(gamePerson) + const gamePersonId = await this.game_PersonRepository.insert(gamePerson); + // map the players from old data to array and push the new one + const oldPlayers = oldData.players.map( + ({ gamepersonId }) => gamepersonId, + ); + oldPlayers.push(person); + // string[] to Game_PersonEntity[] + const newPlayers = await this.game_PersonRepository.find({gamepersonId: In(oldPlayers)}) + // create group entity and update the entry in db + const group = await this.game_GroupRepository.create({ + id: groupId, + players: newPlayers, + }); + const gameGroup = await this.game_GroupRepository.save(group); + // add the missing information to game_Person and update the db entry + gamePerson['gamepersonId'] = gamePersonId.identifiers[0]['gamepersonId']; + gamePerson['leaderGroup'] = gameGroup; + gamePerson['group'] = gameGroup; + await this.game_PersonRepository.save(gamePerson); + return { + "message" : "Joined group" + } + } catch(e) {return e} } // returns name and id of each game diff --git a/src/game/group.entity.ts b/src/game/group.entity.ts new file mode 100644 index 0000000000000000000000000000000000000000..66d2a85ddb75717d1b5b925ea4d42cf70f51abb8 --- /dev/null +++ b/src/game/group.entity.ts @@ -0,0 +1,24 @@ +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) + @JoinColumn({name:'leader'}) + leader: Game_PersonEntity; + @OneToMany(type => Game_PersonEntity, person => person.group) + players: Game_PersonEntity[]; + @ManyToOne(type => GameEntity, game => game.groups) + game: GameEntity; +} \ No newline at end of file