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

Merge branch 'scoretick' into HEAD

parents f2ea5595 3528f799
No related branches found
No related tags found
3 merge requests!59Development to master,!54Development to testing,!51Scoretick
......@@ -13,6 +13,9 @@ import { PersonEntity } from '../user/user.entity';
import { GameGroupEntity } from '../faction/faction.entity';
import { FactionEntity } from '../faction/faction.entity';
import { NotificationModule } from '../notifications/notifications.module';
import { TickService } from './tick.service';
import { ScoreService } from '../score/score.service';
import { ScoreEntity } from 'src/score/score.entity';
@Module({
imports: [
......@@ -24,10 +27,11 @@ import { NotificationModule } from '../notifications/notifications.module';
GameGroupEntity,
ObjectivePointEntity,
ObjectivePoint_HistoryEntity,
ScoreEntity,
]),
NotificationModule,
],
controllers: [GameController],
providers: [GameService],
providers: [GameService, TickService, ScoreService],
})
export class GameModule {}
import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
import { Injectable, HttpException, HttpStatus, Inject } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository, Not } from 'typeorm';
......@@ -12,6 +12,7 @@ import { GameDTO, FlagboxEventDTO, GameStateDTO } from './game.dto';
import { PersonEntity } from '../user/user.entity';
import { FactionEntity } from '../faction/faction.entity';
import { NotificationGateway } from '../notifications/notifications.gateway';
import { TickService } from './tick.service';
@Injectable()
export class GameService {
......@@ -29,8 +30,9 @@ export class GameService {
ObjectivePoint_HistoryEntity
>,
private notificationGateway: NotificationGateway,
) {}
private tickService: TickService,
) {}
// create a new game
async createNewGame(personId: PersonEntity, gameData: GameDTO) {
// checks if a game with the same name exists already
......@@ -145,7 +147,16 @@ export class GameService {
const updatedGame = await this.gameRepository.findOne({ id: game.id });
if (updatedGame) {
updatedGame.state = game.state;
console.log(game.state);
await this.gameRepository.save(updatedGame);
//add or remove from scoretick based on gamestate
if (game.state == 'STARTED') {
this.tickService.addGameToTimer(game.id);
} else {
this.tickService.removeGameFromTimer(game.id);
}
// notify players about game state change
this.notificationGateway.server.emit(game.id, {
type: 'gamestate-update',
......
import { Injectable, Logger } from '@nestjs/common';
import { ScoreService } from '../score/score.service';
@Injectable()
export class TickService {
constructor(private scoreService: ScoreService) {
// whenever Tickservice is called, it will start ticktimer
/*
WARNING: multiple calls start multiple timers,
if you need to use this service somewhere else remember to call startTimer method from somewhere else
*/
this.startTimer();
}
private logger: Logger = new Logger('TickLogger');
// tickinterval in milliseconds (10 minutes = 600 000)
private readonly tickInterval: number = 60000;
// dictionary to push gameId linked to start state
private ongoingGames = {};
// initializing timer
async startTimer() {
this.logger.log('Started timer');
setInterval(this.Tick, this.tickInterval);
}
// add the game to tick queue
async addGameToTimer(gameId: string) {
this.logger.log('Added: ' + gameId);
this.ongoingGames[gameId] = Date.now();
}
// remove game if the setting is set to pause
async removeGameFromTimer(gameId: string) {
this.logger.log('Deleted: ' + gameId);
delete this.ongoingGames[gameId];
}
// tick score for games with STARTED-status
Tick = () => {
this.logger.log('Ticking');
if (this.ongoingGames != null) {
for (var game in this.ongoingGames) {
this.scoreService.scoreTick(game);
}
}
};
}
......@@ -29,13 +29,6 @@ export class ScoreController {
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);
}
// shows scores, :id is gameId
@Get('get-score/:id')
@UseInterceptors(ClassSerializerInterceptor)
......
import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
import {
Injectable,
HttpException,
HttpStatus,
Logger,
OnModuleInit,
} from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
......@@ -111,10 +117,4 @@ export class ScoreService {
);
return scores;
}
} //
// 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