Skip to content
Snippets Groups Projects
Commit 464925e2 authored by Ronnie Friman's avatar Ronnie Friman
Browse files

fixed score

parent 5222e2a9
No related branches found
No related tags found
3 merge requests!59Development to master,!36Development,!33Small fixes
import { Controller, Post, UsePipes, Body, Param, Get } from '@nestjs/common';
import { ValidationPipe } from '../shared/validation.pipe';
import { ScoreService } from './score.service';
import { ScoreDTO } from './score.dto';
import { GameEntity } from '../game/game.entity';
import { Roles, GameStates } from '../shared/guard.decorator';
@Controller('score')
export class ScoreController {
constructor(private scoreService: ScoreService) {}
// adds score manually to Faction
// :id is gameId
@Post('add-score/:id')
@Roles('admin')
@GameStates('STARTED')
@UsePipes(new ValidationPipe())
async addingScore(@Body() data: ScoreDTO, @Param('id') gameId: GameEntity) {
return this.scoreService.addScore(data, gameId);
}
// temporary scoreTick path, :id is gameId
@Get('tick-score/:id')
@GameStates('STARTED')
async scoreTick(@Param('id') gameId: GameEntity) {
return this.scoreService.scoreTick(gameId);
}
}
import { IsNumber, Min, Max, IsUUID } from 'class-validator';
export class ScoreDTO {
@IsNumber()
@Min(1)
@Max(99)
score: number;
@IsUUID('4')
faction: string;
}
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ScoreController } from './score.controller';
import { ScoreService } from './score.service';
import { FactionEntity } from '../faction/faction.entity';
import {
ObjectivePointEntity,
ObjectivePoint_HistoryEntity,
} from '../game/game.entity';
import { ScoreEntity } from './score.entity';
@Module({
imports: [
TypeOrmModule.forFeature([
ScoreEntity,
ObjectivePointEntity,
ObjectivePoint_HistoryEntity,
FactionEntity,
]),
],
controllers: [ScoreController],
providers: [ScoreService],
})
export class ScoreModule {}
import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { FactionEntity } from '../faction/faction.entity';
import { ScoreDTO } from './score.dto';
import {
ObjectivePoint_HistoryEntity,
ObjectivePointEntity,
GameEntity,
} from '../game/game.entity';
import { ScoreEntity } from './score.entity';
import { map } from 'rxjs/operators';
@Injectable()
export class ScoreService {
constructor(
@InjectRepository(ScoreEntity)
private scoreRepository: Repository<ScoreEntity>,
@InjectRepository(ObjectivePointEntity)
private flagRepository: Repository<ObjectivePointEntity>,
@InjectRepository(ObjectivePoint_HistoryEntity)
private flagHistoryRepository: Repository<ObjectivePoint_HistoryEntity>,
@InjectRepository(FactionEntity)
private factionRepository: Repository<FactionEntity>,
) {}
async addScore(scoreData: ScoreDTO, gameId: GameEntity) {
// check if faction exists
const faction = await this.factionRepository.findOne({
factionId: scoreData.faction,
});
if (!faction) {
throw new HttpException('Faction was not found', HttpStatus.BAD_REQUEST);
}
// get the previous score and add it, if it exists
let lastScore = await this.scoreRepository.findOne({
where: { faction: scoreData.faction },
order: { scoreTimeStamp: 'DESC' },
});
if (lastScore) {
scoreData.score += lastScore.score;
}
// add the score for Faction
const newScore = await this.scoreRepository.create(scoreData);
await this.scoreRepository.insert(newScore);
return {
code: 201,
message: 'Score updated!',
};
}
async scoreTick(gameId) {
// get game's flagboxes
const flagboxes = await this.flagRepository.find({ game: gameId });
// create an array of DTOs for adding score
let scoreData = [];
await Promise.all(
flagboxes.map(async box => {
// get the newest entry in history
let current = await this.flagHistoryRepository.findOne({
where: { objective_point: box.objectivePointId },
relations: ['owner'],
order: { oP_HistoryTimestamp: 'DESC' },
});
// if result was found, add score to the owner
if (current.owner) {
let index = await scoreData.findIndex(
i => i.faction === current.owner.factionId,
);
index !== -1
? await (scoreData[index]['score'] += box.objectivePointMultiplier)
: await scoreData.push({
score: box.objectivePointMultiplier,
faction: current.owner.factionId,
});
}
}),
);
scoreData.map(async data => {
await this.addScore(data, gameId);
});
return {
code: 200,
message: 'Scores added',
};
}
} //
// Hae kaikki Objective pointit
// aja map funktio pelin objective pointteihin
// jokaisella objective point ID:llä hae historystä
// relaatio, missä uusin timestamp
// katso uusimmista history entrystä omistaja
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