From 4616442a5c2426bde3f628f77f012343035921be Mon Sep 17 00:00:00 2001 From: L4168 <L4168@student.jamk.fi> Date: Thu, 20 Jun 2019 09:25:49 +0300 Subject: [PATCH] cleaning up joinGroup and createGroup functions --- src/game/game.service.ts | 84 ++++++++++++++++------------------------ 1 file changed, 34 insertions(+), 50 deletions(-) diff --git a/src/game/game.service.ts b/src/game/game.service.ts index 26d1c8d..dd3b704 100644 --- a/src/game/game.service.ts +++ b/src/game/game.service.ts @@ -1,11 +1,12 @@ import { Injectable, HttpException, HttpStatus } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; -import { Repository, Not, In } from 'typeorm'; +import { Repository, Not } from 'typeorm'; -import { GameEntity, FactionEntity, Game_PersonEntity } from './game.entity'; +import { GameEntity, Game_PersonEntity } from './game.entity'; import { GameDTO } from './game.dto'; import { PersonEntity } from '../user/user.entity'; import { GameGroupEntity } from './group.entity'; +import { FactionEntity } from './faction.entity'; @Injectable() export class GameService { @@ -68,10 +69,10 @@ export class GameService { // get all the factions that are associated with the game to deny duplicate entries const factions = await this.factionRepository.find({ - select: ['name'], + select: ['factionName'], where: { game: gameId }, }); - const factionNames = factions.map(({ name }) => name); + const factionNames = factions.map(({ factionName }) => factionName); // add the factions to db if (gameData.factions) { @@ -94,34 +95,37 @@ export class GameService { // checks the password, creates an entry in GamePerson table with associated role&faction 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, + // check if the person already is in a group in this game + const checkDuplicate = await this.game_PersonRepository.findOne({ person: person, - leaderGroup: null, - group: null, }); - const gamePersonId = await this.game_PersonRepository.insert(gamePerson); + 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, - 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); + // 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" + message: 'created new group', }; } catch (e) { return e; @@ -140,45 +144,25 @@ export class GameService { async joinGroup(person, groupId) { try { - // get the old group data to see earlier players - const oldData = await this.game_GroupRepository.findOne({ + const gameData = 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, + game: gameData.game, person: person, leaderGroup: null, - group: null, + group: groupId, }); - 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} + await this.game_PersonRepository.insert(gamePerson); + return { + message: 'Joined group', + }; + } catch (e) { + return e; + } } // returns name and id of each game -- GitLab