Skip to content
Snippets Groups Projects

Join game

Merged Ghost User requested to merge JoinGame into Development
1 file
+ 34
50
Compare changes
  • Side-by-side
  • Inline
+ 34
50
import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
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 { GameDTO } from './game.dto';
import { PersonEntity } from '../user/user.entity';
import { PersonEntity } from '../user/user.entity';
import { GameGroupEntity } from './group.entity';
import { GameGroupEntity } from './group.entity';
 
import { FactionEntity } from './faction.entity';
@Injectable()
@Injectable()
export class GameService {
export class GameService {
@@ -68,10 +69,10 @@ export class GameService {
@@ -68,10 +69,10 @@ export class GameService {
// get all the factions that are associated with the game to deny duplicate entries
// get all the factions that are associated with the game to deny duplicate entries
const factions = await this.factionRepository.find({
const factions = await this.factionRepository.find({
select: ['name'],
select: ['factionName'],
where: { game: gameId },
where: { game: gameId },
});
});
const factionNames = factions.map(({ name }) => name);
const factionNames = factions.map(({ factionName }) => factionName);
// add the factions to db
// add the factions to db
if (gameData.factions) {
if (gameData.factions) {
@@ -94,34 +95,37 @@ export class GameService {
@@ -94,34 +95,37 @@ export class GameService {
// checks the password, creates an entry in GamePerson table with associated role&faction
// checks the password, creates an entry in GamePerson table with associated role&faction
async createGroup(person, gameId, groupData) {
async createGroup(person, gameId, groupData) {
try {
try {
// create a partial game_Person entry and insert it to db
// check if the person already is in a group in this game
const gamePerson = await this.game_PersonRepository.create({
const checkDuplicate = await this.game_PersonRepository.findOne({
role: 'soldier',
faction: null,
game: gameId,
person: person,
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
// create a group entry and insert it to db
const group = await this.game_GroupRepository.create({
const group = await this.game_GroupRepository.create({
...groupData,
...groupData,
leader: gamePerson.gamepersonId,
players: gamePerson.gamepersonId,
game: gameId,
game: gameId,
});
});
const gameGroup = await this.game_GroupRepository.insert(group);
const gameGroup = await this.game_GroupRepository.insert(group);
// add the missing information to game_Person and update the db entry
// create game_Person entry and insert it to db
gamePerson['gamepersonId'] = gamePersonId.identifiers[0]['gamepersonId'];
const gamePerson = await this.game_PersonRepository.create({
gamePerson['leaderGroup'] = gameGroup.identifiers[0]['id'];
role: 'soldier',
gamePerson['group'] = gameGroup.identifiers[0]['id'];
faction: null,
await this.game_PersonRepository.save(gamePerson);
game: gameId,
 
person: person,
 
leaderGroup: gameGroup.identifiers[0]['id'],
 
group: gameGroup.identifiers[0]['id'],
 
});
 
await this.game_PersonRepository.insert(gamePerson);
return {
return {
"message": "created new group"
message: 'created new group',
};
};
} catch (e) {
} catch (e) {
return e;
return e;
@@ -140,45 +144,25 @@ export class GameService {
@@ -140,45 +144,25 @@ export class GameService {
async joinGroup(person, groupId) {
async joinGroup(person, groupId) {
try {
try {
// get the old group data to see earlier players
const gameData = await this.game_GroupRepository.findOne({
const oldData = await this.game_GroupRepository.findOne({
where: { id: groupId },
where: { id: groupId },
relations: ['players', 'game'],
relations: ['players', 'game'],
});
});
// create game_Person entry for the joining player and insert it
const gamePerson = await this.game_PersonRepository.create({
const gamePerson = await this.game_PersonRepository.create({
role: 'soldier',
role: 'soldier',
faction: null,
faction: null,
game: oldData.game,
game: gameData.game,
person: person,
person: person,
leaderGroup: null,
leaderGroup: null,
group: null,
group: groupId,
});
});
console.log(oldData)
await this.game_PersonRepository.insert(gamePerson);
console.log(gamePerson)
return {
const gamePersonId = await this.game_PersonRepository.insert(gamePerson);
message: 'Joined group',
// map the players from old data to array and push the new one
};
const oldPlayers = oldData.players.map(
} catch (e) {
({ gamepersonId }) => gamepersonId,
return e;
);
}
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}
}
}
// returns name and id of each game
// returns name and id of each game
Loading