@@ -3,12 +3,14 @@ import { Injectable, NotFoundException } from '@nestjs/common';
33import { InjectRepository } from '@nestjs/typeorm' ;
44import { Repository , LessThan } from 'typeorm' ;
55import { GameSession } from '../entities/game-session.entity' ;
6+ import { PlayerEventsService } from '../../player-events/player-events.service' ;
67
78@Injectable ( )
89export class GameSessionService {
910 constructor (
1011 @InjectRepository ( GameSession )
1112 private readonly sessionRepo : Repository < GameSession > ,
13+ private readonly playerEventsService : PlayerEventsService ,
1214 ) { }
1315
1416 async create ( userId : string ) {
@@ -18,7 +20,19 @@ export class GameSessionService {
1820 state : { } ,
1921 lastActiveAt : new Date ( ) ,
2022 } ) ;
21- return this . sessionRepo . save ( session ) ;
23+ const savedSession = await this . sessionRepo . save ( session ) ;
24+
25+ await this . playerEventsService . emitPlayerEvent ( {
26+ userId,
27+ sessionId : savedSession . id ,
28+ eventType : 'puzzle.started' ,
29+ payload : {
30+ sessionId : savedSession . id ,
31+ startedAt : savedSession . createdAt || new Date ( ) ,
32+ } ,
33+ } ) ;
34+
35+ return savedSession ;
2236 }
2337
2438 async updateState ( sessionId : string , partialState : Record < string , any > ) {
@@ -44,10 +58,29 @@ export class GameSessionService {
4458 session . status = status ;
4559 session . lastActiveAt = new Date ( ) ;
4660
47- return this . sessionRepo . save ( session ) ;
61+ const savedSession = await this . sessionRepo . save ( session ) ;
62+
63+ if ( status === 'ABANDONED' ) {
64+ await this . playerEventsService . emitPlayerEvent ( {
65+ userId : savedSession . userId ,
66+ sessionId : savedSession . id ,
67+ eventType : 'puzzle.abandoned' ,
68+ payload : {
69+ sessionId : savedSession . id ,
70+ reason : 'session ended as abandoned' ,
71+ endedAt : savedSession . lastActiveAt ,
72+ } ,
73+ } ) ;
74+ }
75+
76+ return savedSession ;
4877 }
4978
5079 async getActiveSessions ( ) {
5180 return this . sessionRepo . find ( { where : { status : 'IN_PROGRESS' } } ) ;
5281 }
82+
83+ async getById ( sessionId : string ) {
84+ return this . sessionRepo . findOne ( { where : { id : sessionId } } ) ;
85+ }
5386}
0 commit comments