Skip to content
Snippets Groups Projects
Commit 4616442a authored by L4168's avatar L4168
Browse files

cleaning up joinGroup and createGroup functions

parent 54ba2839
No related branches found
No related tags found
2 merge requests!59Development to master,!18Join game
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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment