import { Injectable, HttpException, HttpStatus } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository, Not } from 'typeorm'; import { FactionEntity, GameGroupEntity } from './faction.entity'; import { JoinFactionDTO, GameGroupDTO, JoinGameGroupDTO, FactionDTO } from './faction.dto'; import { Game_PersonEntity } from '../game/game.entity'; import { async } from 'rxjs/internal/scheduler/async'; @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({ person: person, game: faction.game, }) ) { 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 changeFactionMultiplier(body: FactionDTO){ const faction = await this.factionRepository.findOne({ where: {factionId: body.factionId} }); faction.multiplier = body.multiplier; return await this.factionRepository.save(faction); } 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) { // get gamePerson ref const gamePerson = await this.game_PersonRepository.findOne({ person: person, game: gameId, }); // check if the authenticated person already belongs to a group if ( await this.game_PersonRepository.findOne({ group: Not(null), person: person, }) ) { 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, }); const gameGroup = await this.game_GroupRepository.insert(group); // update the gamePerson entry with group data gamePerson.group = gamePerson.leaderGroup = gameGroup.identifiers[0]['id']; await this.game_PersonRepository.save(gamePerson); return { message: 'created new group', }; } async showGroups(factionId) { return await this.game_GroupRepository.find({ relations: ['leader', 'players'], where: { faction: factionId }, }); } async joinGroup(gameperson, data: JoinGameGroupDTO) { gameperson.group = data.groupId; await this.game_PersonRepository.save(gameperson); return { message: 'Joined group', }; } async listFactionMembers(faction) { const members = await this.game_PersonRepository.find({ where: { faction }, relations: ['person'], }); members.sort(function(a, b) { return a['person']['name'].localeCompare(b['person']['name']); }); return members; } async verifyUser(person, game) { const gameperson = await this.game_PersonRepository.findOne({ where: { person, game }, relations: ['faction'], }); if (gameperson.faction) { return { factionId: gameperson.faction.factionId, factionName: gameperson.faction.factionName, }; } else { throw new HttpException( gameperson ? 'You are admin for this game!' : 'No faction was found', HttpStatus.BAD_REQUEST, ); } } }