-
Ronnie Friman authoredRonnie Friman authored
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
faction.service.ts 4.03 KiB
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 } 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) {
// 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(person, gameId, data: JoinGameGroupDTO) {
const gamePerson = await this.game_PersonRepository.findOne({
person: person,
game: gameId,
});
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;
}
}