Newer
Older
import { Injectable, Logger, HttpException, HttpStatus } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { GameEntity, FactionEntity, Game_PersonEntity } from './game.entity';
@Injectable()
export class GameService {
constructor(
@InjectRepository(GameEntity)
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>,
async createNewGame(personId: string, gameData: GameDTO) {
// checks if a game with the same name exists already
const { name } = gameData;
if (await this.gameRepository.findOne({ where: { name } })) {
throw new HttpException('Game already exists', HttpStatus.BAD_REQUEST);
}
// else add the game to the database
const game = await this.gameRepository.create({
...gameData,
});
await this.gameRepository.insert(game);
const gameid = await this.gameRepository.findOne({
where: { name: gameData.name },
});
// 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 } });
const index = game.passwords.indexOf(json.password);
// create game_Person entry
/* const gamePerson = await this.game_PersonRepository.create({
faction,
gameId,
person,
});
*/
async listGames() {
const games = await this.gameRepository.find({ relations: ['factions'] });
return games.map(game => {
// returns information about a game identified by id
async returnGameInfo(id: string) {
const game = await this.gameRepository.findOne({
where: { id: id },
relations: ['factions'],
});