Skip to content
Snippets Groups Projects
Commit 506079bd authored by Samuli Virtapohja's avatar Samuli Virtapohja
Browse files

Merge branch 'piirto2' into HEAD

parents d1af9df6 4b3c37c3
No related branches found
No related tags found
3 merge requests!59Development to master,!31Development,!27Piirto2
Showing
with 331 additions and 343 deletions
{ {
"type": "postgres", "type": "postgres",
"host": "localhost", "host": "localhost",
"port": 5432, "port": 5432,
"username": "ehasa", "username": "ehasa",
"password": "salasana", "password": "salasana",
"database": "ehasa", "database": "ehasa",
"entities": ["src/**/*.entity{.ts,.js}"], "entities": ["src/**/*.entity{.ts,.js}"],
"synchronize": true, "synchronize": true,
"logging": true, "logging": true,
"dropSchema": false "dropSchema": true
} }
\ No newline at end of file
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
import { APP_FILTER, APP_INTERCEPTOR, APP_GUARD } from '@nestjs/core'; import { APP_FILTER, APP_INTERCEPTOR, APP_GUARD } from '@nestjs/core';
import { TypeOrmModule } from '@nestjs/typeorm'; import { TypeOrmModule } from '@nestjs/typeorm';
import { Connection } from 'typeorm';
import { AppController } from './app.controller'; import { AppController } from './app.controller';
import { AppService } from './app.service'; import { AppService } from './app.service';
import { Connection } from 'typeorm';
import { UserModule } from './user/user.module'; import { UserModule } from './user/user.module';
import { HttpErrorFilter } from './shared/http-error.filter'; import { HttpErrorFilter } from './shared/http-error.filter';
import { LoggingInterceptor } from './shared/logging.interceptor'; import { LoggingInterceptor } from './shared/logging.interceptor';
import { NotificationModule } from './notifications/notifications.module'; import { NotificationModule } from './notifications/notifications.module';
import { GameModule } from './game/game.module'; import { GameModule } from './game/game.module';
import { RolesGuard } from './shared/roles.guard'; import { RolesGuard } from './shared/roles.guard';
import { TaskModule } from './task/task.module'; import { TaskModule } from './task/task.module';
import { DrawModule } from './draw/draw.module'; import { DrawModule } from './draw/draw.module';
import { FactionModule } from './faction/faction.module';
@Module({ @Module({
imports: [ imports: [
...@@ -23,6 +23,7 @@ import { DrawModule } from './draw/draw.module'; ...@@ -23,6 +23,7 @@ import { DrawModule } from './draw/draw.module';
NotificationModule, NotificationModule,
TaskModule, TaskModule,
DrawModule, DrawModule,
FactionModule,
], ],
controllers: [AppController], controllers: [AppController],
providers: [ providers: [
......
...@@ -2,17 +2,12 @@ import { ...@@ -2,17 +2,12 @@ import {
Entity, Entity,
Column, Column,
PrimaryGeneratedColumn, PrimaryGeneratedColumn,
OneToMany,
ManyToOne, ManyToOne,
PrimaryColumn,
Timestamp, Timestamp,
} from 'typeorm'; } from 'typeorm';
import {
Game_PersonEntity, import { Game_PersonEntity, GameEntity } from '../game/game.entity';
ObjectivePointEntity, import { FactionEntity } from '../faction/faction.entity';
GameEntity,
} from './game.entity';
import { FactionEntity } from './faction.entity';
@Entity('MapDrawing') @Entity('MapDrawing')
export class MapDrawingEntity { export class MapDrawingEntity {
......
...@@ -7,17 +7,12 @@ import { ...@@ -7,17 +7,12 @@ import {
UsePipes, UsePipes,
ValidationPipe, ValidationPipe,
Body, Body,
Delete,
} from '@nestjs/common'; } from '@nestjs/common';
import { AuthGuard } from 'src/shared/auth.guard';
import { AuthGuard } from '../shared/auth.guard';
import { DrawService } from './draw.service'; import { DrawService } from './draw.service';
import { MapDrawingDTO, DrawMapDTO } from './mapdrawing.dto'; import { Roles } from '../shared/roles.decorator';
import { Roles } from 'src/shared/roles.decorator';
import { User } from 'src/user/user.decorator';
import { FactionDTO } from 'src/game/game.dto';
import { FactionEntity } from 'src/game/faction.entity';
import { async } from 'rxjs/internal/scheduler/async';
import { Game_PersonEntity } from 'src/game/game.entity';
/* /*
DrawController DrawController
...@@ -42,9 +37,4 @@ export class DrawController { ...@@ -42,9 +37,4 @@ export class DrawController {
async drawMap(@Param('id') id, @Body() data) { async drawMap(@Param('id') id, @Body() data) {
return this.drawService.drawMap(id, data); return this.drawService.drawMap(id, data);
} }
// @Put('location')
// @UseGuards(new AuthGuard())
// @UsePipes(new ValidationPipe())
// async trackLocation() {}
} }
...@@ -3,9 +3,9 @@ import { TypeOrmModule } from '@nestjs/typeorm'; ...@@ -3,9 +3,9 @@ import { TypeOrmModule } from '@nestjs/typeorm';
import { DrawController } from './draw.controller'; import { DrawController } from './draw.controller';
import { DrawService } from './draw.service'; import { DrawService } from './draw.service';
import { MapDrawingEntity } from 'src/game/coordinate.entity'; import { MapDrawingEntity } from '../draw/coordinate.entity';
import { FactionEntity } from 'src/game/faction.entity'; import { FactionEntity } from '../faction/faction.entity';
import { Game_PersonEntity } from 'src/game/game.entity'; import { Game_PersonEntity } from '../game/game.entity';
/* /*
Draw Draw
- contains everything to do with mapdrawing data. - contains everything to do with mapdrawing data.
......
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm'; import { InjectRepository } from '@nestjs/typeorm';
import { MapDrawingEntity } from 'src/game/coordinate.entity';
import { Repository } from 'typeorm'; import { Repository } from 'typeorm';
import { MapDrawingDTO, DrawMapDTO } from './mapdrawing.dto';
import { Game_PersonEntity, GameEntity } from '../game/game.entity'; import { MapDrawingEntity } from '../draw/coordinate.entity';
import { GameDTO, FactionDTO } from 'src/game/game.dto';
import { FactionEntity } from 'src/game/faction.entity';
@Injectable() @Injectable()
export class DrawService { export class DrawService {
constructor( constructor(
@InjectRepository(MapDrawingEntity) @InjectRepository(MapDrawingEntity)
private mapDrawingRepository: Repository<MapDrawingEntity>, private mapDrawingRepository: Repository<MapDrawingEntity>,
@InjectRepository(FactionEntity)
private factionRepository: Repository<FactionEntity>,
@InjectRepository(Game_PersonEntity)
private game_personRepository: Repository<Game_PersonEntity>,
) {} ) {}
async draw(gameId, data: MapDrawingEntity) { async draw(gameId, data: MapDrawingEntity) {
...@@ -42,8 +35,4 @@ export class DrawService { ...@@ -42,8 +35,4 @@ export class DrawService {
// return mapdrawings with given faction and gameid // return mapdrawings with given faction and gameid
return await this.mapDrawingRepository.find(mapDrawings); return await this.mapDrawingRepository.find(mapDrawings);
} }
// async trackLocation() {
// return 'location';
// }
} }
import { GameDTO, FactionDTO } from '../game/game.dto';
import { GameEntity, Game_PersonEntity } from 'src/game/game.entity';
import { FactionEntity } from 'src/game/faction.entity';
import { MapDrawingEntity } from 'src/game/coordinate.entity';
import { IsUUID } from 'class-validator'; import { IsUUID } from 'class-validator';
import { GameDTO } from '../game/game.dto';
import { GameEntity, Game_PersonEntity } from '../game/game.entity';
import { FactionEntity } from '../faction/faction.entity';
import { FactionDTO } from '../faction/faction.dto';
import { MapDrawingEntity } from '../draw/coordinate.entity';
export class MapDrawingDTO { export class MapDrawingDTO {
data: JSON; data: JSON;
gameId: GameDTO; gameId: GameDTO;
......
import {
Controller,
Post,
UseGuards,
UsePipes,
Param,
Body,
Get,
Put,
} from '@nestjs/common';
import { AuthGuard } from '../shared/auth.guard';
import { ValidationPipe } from '../shared/validation.pipe';
import { User } from '../user/user.decorator';
import { GameGroupDTO, PromotePlayerDTO, JoinFactionDTO } from './faction.dto';
import { FactionService } from './faction.service';
import { Roles } from '../shared/roles.decorator';
@Controller('faction')
export class FactionController {
constructor(private factionservice: FactionService) {}
@Post(':id')
@UseGuards(new AuthGuard())
@UsePipes(new ValidationPipe())
async createGroup(
@User('id') person,
@Param('id') id: string,
@Body() data: GameGroupDTO,
) {
try {
return this.factionservice.createGroup(person, id, data);
} catch (error) {}
}
@Get('get-groups')
async getGroups() {
return this.factionservice.showGroups();
}
@Put('groups/:id')
@UseGuards(new AuthGuard())
async joinGroup(@User('id') person, @Param('id') id) {
return this.factionservice.joinGroup(person, id);
}
@Get('get-faction-members/:id')
async getFactionMembers(@Param('id') factionId) {
return this.factionservice.listFactionMembers(factionId);
}
// param game ID is passed to @Roles
@Put('promote/:id')
@UseGuards(new AuthGuard())
@UsePipes(new ValidationPipe())
@Roles('admin')
promotePlayer(@Param('id') game, @Body() body: PromotePlayerDTO) {
return this.factionservice.promotePlayer(body);
}
@Put('join-faction')
@UseGuards(new AuthGuard())
@UsePipes(new ValidationPipe())
joinFaction(@User('id') person, @Body() data: JoinFactionDTO) {
return this.factionservice.joinFaction(person, data);
}
}
import {
IsUUID,
Length,
Validate,
IsString,
IsNotEmpty,
} from 'class-validator';
import { GameEntity } from '../game/game.entity';
import { RoleValidation } from '../shared/custom-validation';
import { GameDTO } from '../game/game.dto';
export class FactionDTO {
@IsString()
@IsNotEmpty()
@Length(2, 15)
factionName: string;
factionPassword: string;
multiplier?: number;
game: GameDTO;
}
export class JoinFactionDTO {
@IsUUID('4')
factionId: string;
@Length(3, 31)
factionPassword: string;
@IsUUID('4')
game: GameEntity;
}
export class PromotePlayerDTO {
@IsUUID('4')
player: string;
@Validate(RoleValidation)
role: string;
}
export class GameGroupDTO {
@IsString()
@Length(3, 31)
name: string;
}
...@@ -4,11 +4,13 @@ import { ...@@ -4,11 +4,13 @@ import {
PrimaryGeneratedColumn, PrimaryGeneratedColumn,
OneToMany, OneToMany,
ManyToOne, ManyToOne,
OneToOne,
Timestamp, Timestamp,
} from 'typeorm'; } from 'typeorm';
import { GameEntity } from './game.entity';
import { Game_PersonEntity } from './game.entity'; import { GameEntity } from '../game/game.entity';
import { MapDrawingEntity } from './coordinate.entity'; import { Game_PersonEntity } from '../game/game.entity';
import { MapDrawingEntity } from '../draw/coordinate.entity';
//Faction, PowerUp, Faction_powerUp, FP_History, Score //Faction, PowerUp, Faction_powerUp, FP_History, Score
...@@ -81,3 +83,20 @@ export class ScoreEntity { ...@@ -81,3 +83,20 @@ export class ScoreEntity {
@ManyToOne(type => FactionEntity, factionName => factionName.factionId) @ManyToOne(type => FactionEntity, factionName => factionName.factionId)
faction: FactionEntity; faction: FactionEntity;
} }
@Entity('GameGroup')
export class GameGroupEntity {
@PrimaryGeneratedColumn('uuid') id: string;
@Column('text') name: string;
@OneToOne(type => Game_PersonEntity, person => person.leaderGroup, {
onDelete: 'CASCADE',
})
//@JoinColumn({name:'leader'})
leader: Game_PersonEntity;
@OneToMany(type => Game_PersonEntity, person => person.group, {
onDelete: 'CASCADE',
})
players: Game_PersonEntity[];
@ManyToOne(type => GameEntity, game => game.groups)
game: GameEntity;
}
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { FactionController } from './faction.controller';
import { FactionService } from './faction.service';
import { GameGroupEntity, FactionEntity } from './faction.entity';
import { Game_PersonEntity } from '../game/game.entity';
@Module({
imports: [
TypeOrmModule.forFeature([
FactionEntity,
Game_PersonEntity,
GameGroupEntity,
]),
],
controllers: [FactionController],
providers: [FactionService],
})
export class FactionModule {}
import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { FactionEntity, GameGroupEntity } from './faction.entity';
import { JoinFactionDTO, GameGroupDTO } 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) {
// check if the person already is in a group in this game
const checkDuplicate = await this.game_PersonRepository.findOne({
person: person,
});
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,
game: gameId,
});
const gameGroup = await this.game_GroupRepository.insert(group);
// 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',
};
}
async showGroups() {
return await this.game_GroupRepository.find({
relations: ['leader', 'players', 'game'],
});
}
async joinGroup(person, groupId) {
const gameData = await this.game_GroupRepository.findOne({
where: { id: groupId },
relations: ['players', 'game'],
});
const gamePerson = await this.game_PersonRepository.create({
role: 'soldier',
faction: null,
game: gameData.game,
person: person,
leaderGroup: null,
group: groupId,
});
await this.game_PersonRepository.insert(gamePerson);
return {
message: 'Joined group',
};
}
async listFactionMembers(faction) {
return await this.game_PersonRepository.find({
where: { faction },
relations: ['person'],
order: { person: 'DESC' },
});
}
}
import { Test, TestingModule } from '@nestjs/testing';
import { GameController } from './game.controller';
describe('Game Controller', () => {
let controller: GameController;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [GameController],
}).compile();
controller = module.get<GameController>(GameController);
});
it('should be defined', () => {
expect(controller).toBeDefined();
});
});
...@@ -12,13 +12,7 @@ import { ...@@ -12,13 +12,7 @@ import {
import { GameService } from './game.service'; import { GameService } from './game.service';
import { AuthGuard } from '../shared/auth.guard'; import { AuthGuard } from '../shared/auth.guard';
import { User } from '../user/user.decorator'; import { User } from '../user/user.decorator';
import { import { GameDTO, FlagboxEventDTO } from './game.dto';
GameDTO,
GameGroupDTO,
FlagboxEventDTO,
JoinFactionDTO,
PromotePlayerDTO,
} from './game.dto';
import { ValidationPipe } from '../shared/validation.pipe'; import { ValidationPipe } from '../shared/validation.pipe';
import { Roles } from '../shared/roles.decorator'; import { Roles } from '../shared/roles.decorator';
...@@ -40,51 +34,6 @@ export class GameController { ...@@ -40,51 +34,6 @@ export class GameController {
return this.gameservice.editGame(id, body); return this.gameservice.editGame(id, body);
} }
@Post(':id')
@UseGuards(new AuthGuard())
@UsePipes(new ValidationPipe())
async createGroup(
@User('id') person,
@Param('id') id: string,
@Body() data: GameGroupDTO,
) {
try {
return this.gameservice.createGroup(person, id, data);
} catch (error) {}
}
@Get('get-groups')
async getGroups() {
return this.gameservice.showGroups();
}
@Put('groups/:id')
@UseGuards(new AuthGuard())
async joinGroup(@User('id') person, @Param('id') id) {
return this.gameservice.joinGroup(person, id);
}
@Get('get-faction-members/:id')
async getFactionMembers(@Param('id') factionId) {
return this.gameservice.listFactionMembers(factionId);
}
// param game ID is passed to @Roles
@Put('promote/:id')
@UseGuards(new AuthGuard())
@UsePipes(new ValidationPipe())
@Roles('admin')
promotePlayer(@Param('id') game, @Body() body: PromotePlayerDTO) {
return this.gameservice.promotePlayer(body);
}
@Put('join-faction')
@UseGuards(new AuthGuard())
@UsePipes(new ValidationPipe())
joinFaction(@User('id') person, @Body() data: JoinFactionDTO) {
return this.gameservice.joinFaction(person, data);
}
@Get('listgames') @Get('listgames')
async listGames() { async listGames() {
return this.gameservice.listGames(); return this.gameservice.listGames();
......
...@@ -7,10 +7,11 @@ import { ...@@ -7,10 +7,11 @@ import {
Validate, Validate,
Min, Min,
Max, Max,
IsUUID,
} from 'class-validator'; } from 'class-validator';
import { GameEntity, ObjectivePointEntity } from './game.entity';
import { CenterJSON, RoleValidation } from '../shared/custom-validation'; import { ObjectivePointEntity } from './game.entity';
import { CenterJSON } from '../shared/custom-validation';
import { FactionDTO } from '../faction/faction.dto';
export class GameDTO { export class GameDTO {
@IsString() @IsString()
...@@ -55,16 +56,6 @@ export class newGameDTO { ...@@ -55,16 +56,6 @@ export class newGameDTO {
enddate: string; enddate: string;
} }
export class FactionDTO {
@IsString()
@IsNotEmpty()
@Length(2, 15)
factionName: string;
factionPassword: string;
multiplier?: number;
game: GameDTO;
}
export class FlagboxDTO { export class FlagboxDTO {
@IsString() @IsString()
@IsNotEmpty() @IsNotEmpty()
...@@ -94,25 +85,3 @@ export class FlagboxEventDTO { ...@@ -94,25 +85,3 @@ export class FlagboxEventDTO {
oP_HistoryTimestamp?: string; oP_HistoryTimestamp?: string;
objective_point?: ObjectivePointEntity; objective_point?: ObjectivePointEntity;
} }
export class GameGroupDTO {
@IsString()
@Length(3, 31)
name: string;
}
export class JoinFactionDTO {
@IsUUID('4')
factionId: string;
@Length(3, 31)
factionPassword: string;
@IsUUID('4')
game: GameEntity;
}
export class PromotePlayerDTO {
@IsUUID('4')
player: string;
@Validate(RoleValidation)
role: string;
}
...@@ -9,10 +9,10 @@ import { ...@@ -9,10 +9,10 @@ import {
JoinColumn, JoinColumn,
} from 'typeorm'; } from 'typeorm';
import { MapDrawingEntity } from '../draw/coordinate.entity';
import { PersonEntity } from '../user/user.entity'; import { PersonEntity } from '../user/user.entity';
import { GameGroupEntity } from './group.entity'; import { GameGroupEntity } from '../faction/faction.entity';
import { FactionEntity } from './faction.entity'; import { FactionEntity } from '../faction/faction.entity';
import { MapDrawingEntity } from './coordinate.entity';
import { TaskEntity } from '../task/task.entity'; import { TaskEntity } from '../task/task.entity';
// table that stores all created games // table that stores all created games
......
...@@ -10,8 +10,8 @@ import { ...@@ -10,8 +10,8 @@ import {
ObjectivePoint_HistoryEntity, ObjectivePoint_HistoryEntity,
} from './game.entity'; } from './game.entity';
import { PersonEntity } from '../user/user.entity'; import { PersonEntity } from '../user/user.entity';
import { GameGroupEntity } from './group.entity'; import { GameGroupEntity } from '../faction/faction.entity';
import { FactionEntity } from './faction.entity'; import { FactionEntity } from '../faction/faction.entity';
import { NotificationModule } from '../notifications/notifications.module'; import { NotificationModule } from '../notifications/notifications.module';
@Module({ @Module({
......
import { Test, TestingModule } from '@nestjs/testing';
import { GameService } from './game.service';
describe('GameService', () => {
let service: GameService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [GameService],
}).compile();
service = module.get<GameService>(GameService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});
...@@ -8,16 +8,10 @@ import { ...@@ -8,16 +8,10 @@ import {
ObjectivePointEntity, ObjectivePointEntity,
ObjectivePoint_HistoryEntity, ObjectivePoint_HistoryEntity,
} from './game.entity'; } from './game.entity';
import { import { GameDTO, FlagboxEventDTO } from './game.dto';
GameDTO,
FlagboxEventDTO,
JoinFactionDTO,
GameGroupDTO,
} from './game.dto';
import { PersonEntity } from '../user/user.entity'; import { PersonEntity } from '../user/user.entity';
import { GameGroupEntity } from './group.entity'; import { FactionEntity } from '../faction/faction.entity';
import { FactionEntity } from './faction.entity'; import { NotificationGateway } from '../notifications/notifications.gateway';
import { NotificationGateway } from 'src/notifications/notifications.gateway';
@Injectable() @Injectable()
export class GameService { export class GameService {
...@@ -26,12 +20,8 @@ export class GameService { ...@@ -26,12 +20,8 @@ export class GameService {
private gameRepository: Repository<GameEntity>, private gameRepository: Repository<GameEntity>,
@InjectRepository(FactionEntity) @InjectRepository(FactionEntity)
private factionRepository: Repository<FactionEntity>, private factionRepository: Repository<FactionEntity>,
@InjectRepository(PersonEntity)
private personRepository: Repository<PersonEntity>,
@InjectRepository(Game_PersonEntity) @InjectRepository(Game_PersonEntity)
private game_PersonRepository: Repository<Game_PersonEntity>, private game_PersonRepository: Repository<Game_PersonEntity>,
@InjectRepository(GameGroupEntity)
private game_GroupRepository: Repository<GameGroupEntity>,
@InjectRepository(ObjectivePointEntity) @InjectRepository(ObjectivePointEntity)
private objectivePointRepository: Repository<ObjectivePointEntity>, private objectivePointRepository: Repository<ObjectivePointEntity>,
@InjectRepository(ObjectivePoint_HistoryEntity) @InjectRepository(ObjectivePoint_HistoryEntity)
...@@ -128,67 +118,6 @@ export class GameService { ...@@ -128,67 +118,6 @@ export class GameService {
}; };
} }
// checks the password, creates an entry in GamePerson table with associated role&faction
async createGroup(person, gameId, groupData: GameGroupDTO) {
// check if the person already is in a group in this game
const checkDuplicate = await this.game_PersonRepository.findOne({
person: person,
});
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,
game: gameId,
});
const gameGroup = await this.game_GroupRepository.insert(group);
// 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',
};
}
async showGroups() {
return await this.game_GroupRepository.find({
relations: ['leader', 'players', 'game'],
});
}
async joinGroup(person, groupId) {
const gameData = await this.game_GroupRepository.findOne({
where: { id: groupId },
relations: ['players', 'game'],
});
const gamePerson = await this.game_PersonRepository.create({
role: 'soldier',
faction: null,
game: gameData.game,
person: person,
leaderGroup: null,
group: groupId,
});
await this.game_PersonRepository.insert(gamePerson);
return {
message: 'Joined group',
};
}
// returns name and id of each game // returns name and id of each game
async listGames() { async listGames() {
const games = await this.gameRepository.find(); const games = await this.gameRepository.find();
...@@ -197,14 +126,6 @@ export class GameService { ...@@ -197,14 +126,6 @@ export class GameService {
}); });
} }
async listFactionMembers(faction) {
return await this.game_PersonRepository.find({
where: { faction },
relations: ['person'],
order: { person: 'DESC' },
});
}
// returns information about a game identified by id // returns information about a game identified by id
async returnGameInfo(id: string) { async returnGameInfo(id: string) {
const game = await this.gameRepository.findOne({ const game = await this.gameRepository.findOne({
...@@ -240,49 +161,4 @@ export class GameService { ...@@ -240,49 +161,4 @@ export class GameService {
// send flagbox event to flagbox subscribers // send flagbox event to flagbox subscribers
this.notificationGateway.server.emit('flagbox', 'event update'); this.notificationGateway.server.emit('flagbox', 'event update');
} }
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);
}
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);
}
}
} }
import {
Entity,
Column,
PrimaryGeneratedColumn,
ManyToOne,
OneToMany,
OneToOne,
JoinColumn,
} from 'typeorm';
import { Game_PersonEntity, GameEntity } from './game.entity';
@Entity('GameGroup')
export class GameGroupEntity {
@PrimaryGeneratedColumn('uuid') id: string;
@Column('text') name: string;
@OneToOne(type => Game_PersonEntity, person => person.leaderGroup, {
onDelete: 'CASCADE'
})
//@JoinColumn({name:'leader'})
leader: Game_PersonEntity;
@OneToMany(type => Game_PersonEntity, person => person.group, {
onDelete: 'CASCADE'
})
players: Game_PersonEntity[];
@ManyToOne(type => GameEntity, game => game.groups)
game: GameEntity;
}
\ No newline at end of file
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