Skip to content
Snippets Groups Projects
Commit b955007d authored by L4072's avatar L4072
Browse files

Edited games service and controller,

trying to finish it eventually
parent ffe8f3ef
No related branches found
No related tags found
4 merge requests!59Development to master,!14Type orm,!13Type orm,!11Type orm
import { Entity, Column, PrimaryGeneratedColumn, BeforeInsert, ManyToMany, OneToMany, ManyToOne, JoinTable, Timestamp } from 'typeorm'; import { Entity, Column, PrimaryGeneratedColumn, OneToMany, ManyToOne, PrimaryColumn } from 'typeorm';
import {Game_PersonEntity, ObjectivePointEntity, GameEntity} from './game.entity' import {Game_PersonEntity, ObjectivePointEntity, GameEntity} from './game.entity'
import { FactionEntity } from './faction.entity' import { FactionEntity } from './faction.entity'
//import { MapEntity, MapDrawingEntity } from '../map/map.entity'
@Entity('Coordinate') @Entity('Coordinate')
export class CoordinateEntity { export class CoordinateEntity {
@PrimaryGeneratedColumn('uuid') coordinateId: string; @PrimaryColumn({type: 'json', nullable: true}) features: JSON;
@Column({type: 'geometry'}) longitude: string;
@Column({type: 'geometry'}) latitude: string;
@Column() coordinateTimestamp: Timestamp;
@ManyToMany(type => MapDrawingEntity, mapDrawings => mapDrawings.mapDrawings_coordinates) @OneToMany(type => MapDrawingEntity, mapDrawings => mapDrawings.mapDrawings_coordinates)
mapDrawings: MapDrawingEntity[]; mapDrawings: MapDrawingEntity[];
@ManyToMany(type => Game_PersonEntity, game_persons => game_persons.game_person_coordinates) @OneToMany(type => Game_PersonEntity, game_persons => game_persons.game_person_coordinates)
game_persons: Game_PersonEntity[]; game_persons: Game_PersonEntity[];
@OneToMany(type => ObjectivePointEntity, objective_points => objective_points.coordinate) @OneToMany(type => ObjectivePointEntity, objective_points => objective_points.coordinate)
objective_points: ObjectivePointEntity[]; objective_points: ObjectivePointEntity[];
...@@ -39,9 +35,8 @@ export class MapDrawingEntity { ...@@ -39,9 +35,8 @@ export class MapDrawingEntity {
@Column({type: 'bool'}) drawingIsActive: boolean; @Column({type: 'bool'}) drawingIsActive: boolean;
@Column({type: 'time'}) drawingValidTill: string; @Column({type: 'time'}) drawingValidTill: string;
@ManyToMany(type => CoordinateEntity, mapDrawings_coordinates => mapDrawings_coordinates.mapDrawings) @ManyToOne(type => CoordinateEntity, mapDrawings_coordinates => mapDrawings_coordinates.mapDrawings)
@JoinTable() mapDrawings_coordinates: CoordinateEntity;
mapDrawings_coordinates: CoordinateEntity[];
@ManyToOne(type => MapEntity, map => map.mapDrawings) @ManyToOne(type => MapEntity, map => map.mapDrawings)
map: MapEntity; map: MapEntity;
@ManyToOne(type => MapDrawingTypeEntity, mapDrawingType => mapDrawingType.mapDrawings) @ManyToOne(type => MapDrawingTypeEntity, mapDrawingType => mapDrawingType.mapDrawings)
......
...@@ -4,6 +4,8 @@ import {GameEntity} from './game.entity' ...@@ -4,6 +4,8 @@ import {GameEntity} from './game.entity'
import {Game_PersonEntity} from './game.entity' import {Game_PersonEntity} from './game.entity'
import { MapDrawingEntity } from './coordinate.entity' import { MapDrawingEntity } from './coordinate.entity'
//Faction, PowerUp, Faction_powerUp, FP_History, Score, Task
@Entity('Faction') @Entity('Faction')
export class FactionEntity { export class FactionEntity {
@PrimaryGeneratedColumn('uuid') factionId: string; @PrimaryGeneratedColumn('uuid') factionId: string;
......
import { Controller } from '@nestjs/common'; import { Controller, Post, UsePipes, Body, Get, UseGuards, Param, Delete, Put } from '@nestjs/common';
import { GameService } from './game.service';
import { CreateGameDTO/*, EditGameDTO */} from './game.dto';
import { AuthGuard } from '../shared/auth.guard';
import { ValidationPipe } from '../shared/validation.pipe';
import { User } from 'src/user/user.decorator';
@Controller('game') @Controller('game')
export class GameController { export class GameController {
constructor(private gameservice: GameService) { }
@Post('new')
@UseGuards(new AuthGuard())
@UsePipes(new ValidationPipe())
async newGame(@User('id') person, @Body() body: CreateGameDTO ) {
return this.gameservice.createNewGame(person, body);
}
@Put(':id')
@UseGuards(new AuthGuard())
@UsePipes(new ValidationPipe())
async editGame(@Param('id') id: string, @Body() body: CreateGameDTO) {
return this.gameservice.editGame(id, body);
}
@Delete(':id')
@UseGuards(new AuthGuard())
async deleteGame(@Param('id') id) {
return this.gameservice.deleteGame(id);
}
@UseGuards(new AuthGuard())
@UsePipes(new ValidationPipe())
@Post(':id')
async joinGame(@User('id') person, @Param('id') id: string, @Body() password: string) {
return this.gameservice.joinGame(person, id, password);
}
@Get('listgames')
async listGames() {
return this.gameservice.listGames();
}
@Get(':id')
async returnGameInfo(@Param('id') id: string) {
return this.gameservice.returnGameInfo(id);
}
} }
import { IsNotEmpty, IsString, IsDate, Length, IsInt, Min, Max, IsArray, IsJSON } from 'class-validator'; import { IsNotEmpty, IsString, IsDate, Length, IsInt, Min, Max, IsArray, IsJSON } from 'class-validator';
import { Timestamp } from 'typeorm'; import { Timestamp } from 'typeorm';
import { FactionEntity } from './faction.entity';
export class CreateGameDTO { export class CreateGameDTO {
@IsString() @IsNotEmpty()
gameId: string;
@IsString() @IsNotEmpty() @Length(3, 30) @IsString() @IsNotEmpty() @Length(3, 30)
gameName: string; gameName: string;
...@@ -20,7 +15,7 @@ export class CreateGameDTO { ...@@ -20,7 +15,7 @@ export class CreateGameDTO {
endDate: string; endDate: string;
@IsArray() @IsNotEmpty() @IsArray() @IsNotEmpty()
factions?: FactionEntity[]; factions?: FactionDTO[];
@IsArray() @IsNotEmpty() @IsArray() @IsNotEmpty()
objectivePoint?: ObjectivePointDTO[]; objectivePoint?: ObjectivePointDTO[];
...@@ -32,12 +27,38 @@ export class CreateGameDTO { ...@@ -32,12 +27,38 @@ export class CreateGameDTO {
GM_Password?: string; GM_Password?: string;
} }
export class FactionDTO { /*export class EditGameDTO {
@IsString() @IsNotEmpty() @IsString() @IsNotEmpty() @Length(3, 30)
FactionID: string; gameName?: string;
@IsString()
gameDescription?: string;
@IsDate() @IsNotEmpty()
startDate: string;
@IsDate() @IsNotEmpty()
endDate: string;
@IsArray() @IsNotEmpty()
factions?: FactionDTO[];
@IsArray() @IsNotEmpty()
objectivePoint?: ObjectivePointDTO[];
@IsJSON() @IsNotEmpty()
mapCoordinates: JSON;
@IsString() @IsNotEmpty() @Length(3, 255)
GM_Password?: string;
}*/
export class FactionDTO {
@IsString() @IsNotEmpty() @Length(3, 30) @IsString() @IsNotEmpty() @Length(3, 30)
FactionName: string; factionName: string;
@IsString() @IsNotEmpty() @Length(3, 255)
faction_Password?: string;
} }
...@@ -56,9 +77,6 @@ export class FactionDTO { ...@@ -56,9 +77,6 @@ export class FactionDTO {
}*/ }*/
export class ObjectivePointDTO { export class ObjectivePointDTO {
@IsString() @IsNotEmpty()
objectivePointID: string;
@IsString() @IsNotEmpty() @IsString() @IsNotEmpty()
objectivePointDescription: string; objectivePointDescription: string;
...@@ -71,7 +89,7 @@ export class ObjectivePointDTO { ...@@ -71,7 +89,7 @@ export class ObjectivePointDTO {
export class ObjectivePointHistoryDTO { export class ObjectivePointHistoryDTO {
@IsString() @IsString()
FactionId: string; factionId: string;
@IsNotEmpty() @IsInt() @Min(0) @Max(2) @IsNotEmpty() @IsInt() @Min(0) @Max(2)
oP_HistoryStatus: number; oP_HistoryStatus: number;
......
import { Entity, Column, PrimaryGeneratedColumn, OneToMany, ManyToOne, ManyToMany, Timestamp, JoinTable } from 'typeorm'; import { Entity, Column, PrimaryGeneratedColumn, OneToMany, ManyToOne, Timestamp} from 'typeorm';
import * as bcrypt from 'bcryptjs'; import * as bcrypt from 'bcryptjs';
import {PersonEntity, PersonRoleEntity} from '../user/user.entity' import {PersonEntity, PersonRoleEntity} from '../user/user.entity'
import {FactionEntity} from './faction.entity' import {FactionEntity} from './faction.entity'
import {CoordinateEntity, MapEntity} from './coordinate.entity' import {CoordinateEntity, MapEntity} from './coordinate.entity'
//Game, Game_Person, ObjectivePoint, ObjectivePoint_History
@Entity('Game') @Entity('Game')
export class GameEntity { export class GameEntity {
@PrimaryGeneratedColumn('uuid') gameId: string; @PrimaryGeneratedColumn('uuid') gameId: string;
...@@ -44,9 +46,8 @@ export class Game_PersonEntity { ...@@ -44,9 +46,8 @@ export class Game_PersonEntity {
person: PersonEntity; person: PersonEntity;
@ManyToOne(type => PersonRoleEntity, person_role => person_role.game_persons) @ManyToOne(type => PersonRoleEntity, person_role => person_role.game_persons)
person_role: PersonRoleEntity; person_role: PersonRoleEntity;
@ManyToMany(type => CoordinateEntity, game_person_coordinates => game_person_coordinates.game_persons) @ManyToOne(type => CoordinateEntity, game_person_coordinates => game_person_coordinates.game_persons)
@JoinTable() game_person_coordinates: CoordinateEntity;
game_person_coordinates: CoordinateEntity[];
} }
@Entity('ObjectivePoint') @Entity('ObjectivePoint')
......
...@@ -3,10 +3,12 @@ import { TypeOrmModule } from '@nestjs/typeorm'; ...@@ -3,10 +3,12 @@ import { TypeOrmModule } from '@nestjs/typeorm';
import { GameController } from './game.controller'; import { GameController } from './game.controller';
import { GameService } from './game.service'; import { GameService } from './game.service';
import { GameEntity } from './game.entity'; import { GameEntity, Game_PersonEntity } from './game.entity';
import { FactionEntity } from './faction.entity';
import { PersonEntity } from 'src/user/user.entity';
@Module({ @Module({
imports: [TypeOrmModule.forFeature([GameEntity])], imports: [TypeOrmModule.forFeature([GameEntity, FactionEntity, Game_PersonEntity, PersonEntity])],
controllers: [GameController], controllers: [GameController],
providers: [GameService] providers: [GameService]
}) })
......
import { Injectable, HttpException, HttpStatus } from '@nestjs/common'; import { Injectable, Logger, HttpException, HttpStatus } from '@nestjs/common';
import { Repository } from "typeorm"; import { InjectRepository } from '@nestjs/typeorm';
import { InjectRepository } from "@nestjs/typeorm"; import { Repository, In } from 'typeorm';
import { GameEntity } from './game.entity'; import { GameEntity, Game_PersonEntity } from './game.entity';
import { CreateGameDTO } from './game.dto'; import { CreateGameDTO } from './game.dto';
import { PersonEntity } from '../user/user.entity';
import { FactionEntity } from './faction.entity';
@Injectable() @Injectable()
export class GameService { export class GameService {
constructor(@InjectRepository(GameEntity) private userRepository: Repository<GameEntity>){} constructor(
@InjectRepository(GameEntity)
async register(data: CreateGameDTO) { private gameRepository: Repository<GameEntity>,
const { gameName } = data; @InjectRepository(FactionEntity)
let game = await this.userRepository.findOne({where: {gameName}}); private factionRepository: Repository<FactionEntity>,
if (game) { @InjectRepository(PersonEntity)
throw new HttpException('Game with this name already exists', HttpStatus.BAD_REQUEST); private personRepository: Repository<PersonEntity>,
} @InjectRepository(Game_PersonEntity)
game = await this.userRepository.create(data); private game_PersonRepository: Repository<Game_PersonEntity>,
await this.userRepository.save(game); ) {}
return game.gameObject();
// create a new game
async createNewGame(personId: string, gameData: CreateGameDTO) {
// checks if a game with the same name exists already
const { gameName } = 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,
factions: gameData.factions,
});
await this.gameRepository.insert(game);
return {
message: 'New game added',
};
}
// edit already created game
async editGame(gameId: string, gameData: Partial<CreateGameDTO>) {
try {
// update game entry in db
let update = await this.gameRepository.create({
...gameData,
factions: gameData.factions
})
await this.gameRepository.update({ gameId }, update);
// add the factions to db
if (gameData.factions) {
gameData.factions.map(async faction => {
let name = await this.factionRepository.create({
...faction,
game: gameData,
});
await this.factionRepository.insert(name);
});
}
return {
message: 'Game updated',
};
} catch (error) {console.log(error)}
}
async deleteGame(gameId) {
// TODO: Delete factions from Faction table associated with the deleted game
await this.gameRepository.delete({ gameId });
return {
message: 'Game deleted',
};
}
// 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.GM_Password.indexOf(json.password);
// create game_Person entry
/* const gamePerson = await this.game_PersonRepository.create({
faction,
gameId,
person,
});
*/
return 'WIP';
}
// returns name and id of each game
async listGames() {
const games = await this.gameRepository.find({ relations: ['factions'] });
return games.map(game => {
return game.gameObject();
});
}
/*async login(data: UserDTO) { // returns information about a game identified by id
const { name, password } = data; async returnGameInfo(id: string) {
const user = await this.userRepository.findOne({ where: { name }}); const game = await this.gameRepository.findOne({
if (!user || !(await user.comparePassword(password))) { where: { id: id },
throw new HttpException( relations: ['factions'],
'Invalid username/password', });
HttpStatus.BAD_REQUEST, return game;
);
}
return user.tokenObject();
}*/
} }
}
\ No newline at end of file
...@@ -2,15 +2,15 @@ import { Controller, Body, Get, Put, UseGuards } from '@nestjs/common'; ...@@ -2,15 +2,15 @@ import { Controller, Body, Get, Put, UseGuards } from '@nestjs/common';
import { MapMarkerService } from './mapmarker.service'; import { MapMarkerService } from './mapmarker.service';
import { MapMarkerDTO } from './mapmarker.dto'; import { MapMarkerDTO } from './mapmarker.dto';
import { AuthGuard } from 'dist/shared/auth.guard'; import { AuthGuard } from '../shared/auth.guard';
import { User } from 'src/user/user.decorator'; import { User } from '../user/user.decorator';
@Controller('mapmarkers') @Controller('mapmarkers')
export class MapMarkersController { export class MapMarkersController {
constructor(private mapmarkerservice: MapMarkerService){} constructor(private mapmarkerservice: MapMarkerService){}
// Insert figure location, needs "authorization" header with valid Bearer token and content-type json // Insert figure location, needs "authorization" header with valid Bearer token and content-type json
@Put('insertLocation') @Put('insert-location')
@UseGuards(new AuthGuard()) @UseGuards(new AuthGuard())
async insertLocation(@User('id') person, @Body() data: MapMarkerDTO): Promise<string>{ async insertLocation(@User('id') person, @Body() data: MapMarkerDTO): Promise<string>{
try { try {
......
import { Controller, Post, Body, UsePipes, Get, UseGuards } from '@nestjs/common'; import { Controller, Post, Body, UsePipes, Get, UseGuards } from '@nestjs/common';
import { UserService } from './user.service'; import { UserService } from './user.service';
import { UserDTO } from './user.dto'; import { UserDTO } from './user.dto';
import { AuthGuard } from 'src/shared/auth.guard'; import { AuthGuard } from '../shared/auth.guard';
import { ValidationPipe } from 'src/shared/validation.pipe'; import { ValidationPipe } from '../shared/validation.pipe';
@Controller('user') @Controller('user')
export class UserController { export class UserController {
......
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