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

restructure score ticking

parent b651be99
No related branches found
No related tags found
3 merge requests!59Development to master,!58Development to testing,!57restructure score ticking
...@@ -2,15 +2,12 @@ import { Module } from '@nestjs/common'; ...@@ -2,15 +2,12 @@ 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 { Connection } from 'typeorm';
import { AppController } from './app.controller'; import { AppController } from './app.controller';
import { AppService } from './app.service'; import { AppService } from './app.service';
import { RolesGuard } from './shared/roles.guard'; import { RolesGuard } from './shared/roles.guard';
//import { LoggingInterceptor } from './shared/logging.interceptor'; //import { LoggingInterceptor } from './shared/logging.interceptor';
import { StatesGuard } from './shared/states.guard'; import { StatesGuard } from './shared/states.guard';
import { HttpErrorFilter } from './shared/http-error.filter'; import { HttpErrorFilter } from './shared/http-error.filter';
import { NotificationModule } from './notifications/notifications.module'; import { NotificationModule } from './notifications/notifications.module';
import { TaskModule } from './task/task.module'; import { TaskModule } from './task/task.module';
import { TrackingModule } from './tracking/tracking.module'; import { TrackingModule } from './tracking/tracking.module';
...@@ -20,6 +17,10 @@ import { FactionModule } from './faction/faction.module'; ...@@ -20,6 +17,10 @@ import { FactionModule } from './faction/faction.module';
import { GameModule } from './game/game.module'; import { GameModule } from './game/game.module';
import { ScoreModule } from './score/score.module'; import { ScoreModule } from './score/score.module';
import { ReplayModule } from './replay/replay.module'; import { ReplayModule } from './replay/replay.module';
import { TickModule } from './tick/tick.module';
/* /*
Core of the server, Core of the server,
...@@ -50,6 +51,7 @@ import { ReplayModule } from './replay/replay.module'; ...@@ -50,6 +51,7 @@ import { ReplayModule } from './replay/replay.module';
TrackingModule, TrackingModule,
ScoreModule, ScoreModule,
ReplayModule, ReplayModule,
TickModule,
], ],
controllers: [AppController], controllers: [AppController],
providers: [ providers: [
......
...@@ -24,7 +24,6 @@ import { GameDTO, FlagboxEventDTO, GameStateDTO, newGameDTO } from './game.dto'; ...@@ -24,7 +24,6 @@ import { GameDTO, FlagboxEventDTO, GameStateDTO, newGameDTO } from './game.dto';
import { ValidationPipe } from '../shared/validation.pipe'; import { ValidationPipe } from '../shared/validation.pipe';
import { Roles, GameStates } from '../shared/guard.decorator'; import { Roles, GameStates } from '../shared/guard.decorator';
import { GameEntity } from './game.entity'; import { GameEntity } from './game.entity';
import { TickService } from './tick.service';
///////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////
/// GameController /// /// GameController ///
...@@ -37,13 +36,7 @@ import { TickService } from './tick.service'; ...@@ -37,13 +36,7 @@ import { TickService } from './tick.service';
@Controller('game') @Controller('game')
export class GameController { export class GameController {
constructor( constructor(private gameservice: GameService) {}
private gameservice: GameService,
private tickservice: TickService,
) {
// starts timer
this.tickservice.startTimer();
}
//new game //new game
@Post('new') @Post('new')
......
...@@ -13,7 +13,6 @@ import { PersonEntity } from '../user/user.entity'; ...@@ -13,7 +13,6 @@ import { PersonEntity } from '../user/user.entity';
import { GameGroupEntity } from '../faction/faction.entity'; import { GameGroupEntity } from '../faction/faction.entity';
import { FactionEntity } from '../faction/faction.entity'; import { FactionEntity } from '../faction/faction.entity';
import { NotificationModule } from '../notifications/notifications.module'; import { NotificationModule } from '../notifications/notifications.module';
import { TickService } from './tick.service';
import { ScoreService } from '../score/score.service'; import { ScoreService } from '../score/score.service';
import { ScoreEntity } from '../score/score.entity'; import { ScoreEntity } from '../score/score.entity';
import { MulterModule } from '@nestjs/platform-express'; import { MulterModule } from '@nestjs/platform-express';
...@@ -40,6 +39,6 @@ import { MulterModule } from '@nestjs/platform-express'; ...@@ -40,6 +39,6 @@ import { MulterModule } from '@nestjs/platform-express';
}), }),
], ],
controllers: [GameController], controllers: [GameController],
providers: [GameService, TickService, ScoreService], providers: [GameService, ScoreService],
}) })
export class GameModule {} export class GameModule {}
...@@ -12,7 +12,6 @@ import { GameDTO, FlagboxEventDTO, GameStateDTO, newGameDTO } from './game.dto'; ...@@ -12,7 +12,6 @@ import { GameDTO, FlagboxEventDTO, GameStateDTO, newGameDTO } from './game.dto';
import { PersonEntity } from '../user/user.entity'; import { PersonEntity } from '../user/user.entity';
import { FactionEntity } from '../faction/faction.entity'; import { FactionEntity } from '../faction/faction.entity';
import { NotificationGateway } from '../notifications/notifications.gateway'; import { NotificationGateway } from '../notifications/notifications.gateway';
import { TickService } from './tick.service';
@Injectable() @Injectable()
export class GameService { export class GameService {
...@@ -30,8 +29,6 @@ export class GameService { ...@@ -30,8 +29,6 @@ export class GameService {
ObjectivePoint_HistoryEntity ObjectivePoint_HistoryEntity
>, >,
private notificationGateway: NotificationGateway, private notificationGateway: NotificationGateway,
private tickService: TickService,
) {} ) {}
// create a new game // create a new game
async createNewGame(personId: PersonEntity, gameData: newGameDTO) { async createNewGame(personId: PersonEntity, gameData: newGameDTO) {
...@@ -164,13 +161,6 @@ export class GameService { ...@@ -164,13 +161,6 @@ export class GameService {
updatedGame.state = game.state; updatedGame.state = game.state;
await this.gameRepository.save(updatedGame); 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 // notify players about game state change
this.notificationGateway.server.emit(game.id, { this.notificationGateway.server.emit(game.id, {
type: 'gamestate-update', type: 'gamestate-update',
......
...@@ -21,9 +21,8 @@ import { ...@@ -21,9 +21,8 @@ import {
} from '../draw/coordinate.entity'; } from '../draw/coordinate.entity';
import { ScoreService } from '../score/score.service'; import { ScoreService } from '../score/score.service';
import { ScoreEntity } from '../score/score.entity'; import { ScoreEntity } from '../score/score.entity';
import { NotificationModule } from 'src/notifications/notifications.module'; import { NotificationModule } from '../notifications/notifications.module';
import { GameService } from 'src/game/game.service'; import { GameService } from '../game/game.service';
import { TickService } from 'src/game/tick.service';
///////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////
/// Replay /// /// Replay ///
...@@ -54,7 +53,6 @@ import { TickService } from 'src/game/tick.service'; ...@@ -54,7 +53,6 @@ import { TickService } from 'src/game/tick.service';
TrackingService, TrackingService,
ScoreService, ScoreService,
GameService, GameService,
TickService,
], ],
}) })
export class ReplayModule {} export class ReplayModule {}
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import {
GameEntity,
ObjectivePointEntity,
ObjectivePoint_HistoryEntity,
} from '../game/game.entity';
import { TickService } from './tick.service';
import { ScoreService } from '../score/score.service';
import { FactionEntity } from '../faction/faction.entity';
import { ScoreEntity } from '../score/score.entity';
import { NotificationModule } from '../notifications/notifications.module';
@Module({
imports: [
TypeOrmModule.forFeature([
GameEntity,
ScoreEntity,
ObjectivePointEntity,
ObjectivePoint_HistoryEntity,
FactionEntity,
]),
NotificationModule,
],
providers: [TickService, ScoreService],
})
export class TickModule {
constructor(private tickService: TickService) {
this.tickService.startTimer();
}
}
import { Injectable, Logger } from '@nestjs/common'; import { Injectable, Logger } from '@nestjs/common';
import { ScoreService } from '../score/score.service'; import { ScoreService } from '../score/score.service';
import { GameService } from './game.service'; import { InjectRepository } from '@nestjs/typeorm';
import { GameEntity } from 'src/game/game.entity';
import { Repository } from 'typeorm';
@Injectable() @Injectable()
export class TickService { export class TickService {
constructor( constructor(
private scoreService: ScoreService, //private gameService: GameService, private scoreService: ScoreService,
@InjectRepository(GameEntity)
private gameRepository: Repository<GameEntity>,
) { ) {
// whenever Tickservice is called, it will start ticktimer // whenever Tickservice is called, it will start ticktimer
/* /*
...@@ -17,38 +21,30 @@ export class TickService { ...@@ -17,38 +21,30 @@ export class TickService {
private logger: Logger = new Logger('TickLogger'); private logger: Logger = new Logger('TickLogger');
// tickinterval in milliseconds (10 minutes = 600 000) // tickinterval in milliseconds (10 minutes = 600 000)
private readonly tickInterval: number = 60000; private readonly tickInterval: number = 60000;
// dictionary to push gameId linked to start state
private ongoingGames = {};
// initializing timer // initializing timer
async startTimer() { async startTimer() {
this.logger.log('Started timer'); this.logger.log('Started timer');
setInterval(this.Tick, this.tickInterval); setInterval(this.Tick, this.tickInterval);
/* // add STARTED games to dictionary
games.map(game => {
this.ongoingGames[game.id] = Date.now();
}); */
}
// 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 // returns name and id of each game
async removeGameFromTimer(gameId: string) { async listGames() {
this.logger.log('Deleted: ' + gameId); const games = await this.gameRepository.find({
delete this.ongoingGames[gameId]; where: { state: 'STARTED' },
});
return games.map(game => {
return game.gameObject();
});
} }
// tick score for games with STARTED-status // tick score for games with STARTED-status
Tick = () => { Tick = async _ => {
this.logger.log('Ticking'); this.logger.log('Ticking');
if (this.ongoingGames != null) { let games = await this.listGames();
for (var game in this.ongoingGames) { games.map(game => {
this.scoreService.scoreTick(game); this.logger.log(game);
} this.scoreService.scoreTick(game.id);
} });
}; };
} }
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