@@ -4,6 +4,7 @@ import { e_match_types_enum } from "generated";
44import { MatchmakeService } from "./matchmake.service" ;
55import { MatchmakingLobbyService } from "./matchmaking-lobby.service" ;
66import { RedisManagerService } from "../redis/redis-manager/redis-manager.service" ;
7+ import { CacheService } from "src/cache/cache.service" ;
78import { FiveStackWebSocketClient } from "src/sockets/types/FiveStackWebSocketClient" ;
89import {
910 ConnectedSocket ,
@@ -12,6 +13,7 @@ import {
1213 WebSocketGateway ,
1314} from "@nestjs/websockets" ;
1415import { JoinQueueError } from "./utilities/joinQueueError" ;
16+ import { PlayerLobby } from "./types/PlayerLobby" ;
1517import { HasuraService } from "src/hasura/hasura.service" ;
1618import { isRoleAbove } from "src/utilities/isRoleAbove" ;
1719import { e_player_roles_enum } from "generated" ;
@@ -29,6 +31,7 @@ export class MatchmakingGateway {
2931 public readonly redisManager : RedisManagerService ,
3032 public readonly matchmakeService : MatchmakeService ,
3133 public readonly matchmakingLobbyService : MatchmakingLobbyService ,
34+ private readonly cache : CacheService ,
3235 ) {
3336 this . redis = this . redisManager . getConnection ( ) ;
3437 }
@@ -122,7 +125,7 @@ export class MatchmakingGateway {
122125 throw new JoinQueueError ( "You do not have permission to join this queue" ) ;
123126 }
124127
125- let lobby ;
128+ let lobby : PlayerLobby | undefined ;
126129 const user = client . user ;
127130
128131 if ( ! user ) {
@@ -220,16 +223,24 @@ export class MatchmakingGateway {
220223 throw new JoinQueueError ( "Unable to find Player Lobby" ) ;
221224 }
222225
223- await this . matchmakingLobbyService . verifyLobby ( lobby , user , type ) ;
224-
225226 try {
226- await this . matchmakingLobbyService . setLobbyDetails (
227- regions ,
228- type ,
229- lobby ,
227+ await this . cache . lock (
228+ `matchmaking:verify:${ lobby . id } ` ,
229+ async ( ) => {
230+ await this . matchmakingLobbyService . verifyLobby ( lobby , user , type ) ;
231+ await this . matchmakingLobbyService . setLobbyDetails (
232+ regions ,
233+ type ,
234+ lobby ,
235+ ) ;
236+ await this . matchmakeService . addLobbyToQueue ( lobby . id ) ;
237+ return true ;
238+ } ,
230239 ) ;
231- await this . matchmakeService . addLobbyToQueue ( lobby . id ) ;
232240 } catch ( error ) {
241+ if ( error instanceof JoinQueueError ) {
242+ throw error ;
243+ }
233244 this . logger . error ( `unable to add lobby to queue` , error ) ;
234245 await this . matchmakingLobbyService . removeLobbyFromQueue ( lobby . id ) ;
235246 await this . matchmakingLobbyService . removeLobbyDetails ( lobby . id ) ;
0 commit comments