66import io .github .tfkfan .orbital .core .configuration .props .RoomConfig ;
77import io .github .tfkfan .orbital .core .event .*;
88import io .github .tfkfan .orbital .core .network .message .Message ;
9+ import io .github .tfkfan .orbital .core .room .GameRoom ;
910import io .github .tfkfan .orbital .core .room .RoomType ;
1011import io .github .tfkfan .orbital .core .route .MessageRoute ;
1112import io .github .tfkfan .orbital .core .session .GatewaySession ;
1213import io .github .tfkfan .orbital .core .shared .ActionType ;
1314import io .github .tfkfan .orbital .core .shared .UniqueQueue ;
15+ import io .vertx .core .Future ;
1416import io .vertx .core .Vertx ;
15- import io .vertx .core .eventbus .DeliveryOptions ;
1617import io .vertx .core .eventbus .EventBus ;
1718import io .vertx .core .json .JsonArray ;
1819import io .vertx .core .json .JsonObject ;
1920import lombok .extern .slf4j .Slf4j ;
2021
2122import java .util .Collections ;
2223import java .util .List ;
24+ import java .util .Objects ;
2325import java .util .UUID ;
2426import java .util .stream .Collectors ;
2527
28+ import static io .github .tfkfan .orbital .core .verticle .BaseVerticle .defaults ;
29+
2630@ Slf4j
2731public abstract class BaseMatchmakerManager extends BaseManager implements MatchmakerManager {
28- protected final Vertx vertx ;
29- protected final EventBus eventBus ;
3032 protected final RoomConfig roomConfig ;
3133 private final UniqueQueue <GameRoomJoinEvent > playersQueue = new UniqueQueue <>();
3234
33- public BaseMatchmakerManager (RoomConfig roomConfig ) {
35+ public BaseMatchmakerManager (Vertx vertx , RoomConfig roomConfig ) {
36+ super (vertx );
3437 this .roomConfig = roomConfig ;
35- vertx = Vertx .currentContext ().owner ();
36- eventBus = vertx .eventBus ();
3738 initConsumers ();
3839 }
3940
@@ -47,6 +48,9 @@ protected void initConsumers() {
4748 @ Override
4849 public void onDisconnect (GatewaySession data ) {
4950 playersQueue .removeIf (e -> e .getSession ().getId ().equals (data .getId ()));
51+ publishRoomManagementEvent (data .getRoomKey (), ActionType .PLAYER_DISCONNECT , null , new JsonArray (
52+ Collections .singletonList (new JsonObject ().put (Fields .sessionId , data .getId ()))
53+ ));
5054 }
5155
5256 @ Override
@@ -66,7 +70,7 @@ protected void onJoin(GameRoomJoinEvent joinEvent) {
6670 }
6771
6872 protected void handleJoinTraining (final GameRoomJoinEvent joinEvent ) {
69- newRoomEvent (newRoomId (), RoomType .TRAINING , Collections .singletonList (joinEvent ));
73+ requestRoomManagementCreateEvent (newRoomId (), RoomType .TRAINING , Collections .singletonList (joinEvent ));
7074 }
7175
7276 protected void handleJoin (final RoomType roomType , final GameRoomJoinEvent joinEvent ) {
@@ -77,27 +81,20 @@ protected void handleJoin(final RoomType roomType, final GameRoomJoinEvent joinE
7781 if (playersQueue .size () < roomConfig .getMaxPlayers ())
7882 return ;
7983
80- newRoomEvent (newRoomId (), roomType , playersQueue .chunk (roomConfig .getMaxPlayers ()));
84+ requestRoomManagementCreateEvent (newRoomId (), roomType , playersQueue .chunk (roomConfig .getMaxPlayers ()));
8185 }
8286
8387 private UUID newRoomId () {
8488 return UUID .randomUUID ();
8589 }
8690
87- protected void newRoomEvent (final UUID roomId , final RoomType roomType , final List <GameRoomJoinEvent > userSessions ) {
88- eventBus .sender (Constants .ROOM_VERTICAL_CHANNEL , new DeliveryOptions ()
89- .setLocalOnly (true )
90- .setSendTimeout (1000 ))
91- .write (new JsonObject ()
92- .put (Fields .action , ActionType .NEW_ROOM )
93- .put (Fields .roomId , roomId .toString ())
94- .put (Fields .roomType , roomType .toString ())
95- .put (Fields .sessions , new JsonArray (userSessions .stream ().map (e ->
96- new JsonObject ()
97- .put (Fields .sessionId , e .getSession ().getId ())
98- .put (Fields .admin , e .getSession ().isAdmin ())
99- .put (Fields .initialData , e .getData ()))
100- .collect (Collectors .toList ()))))
91+ protected void requestRoomManagementCreateEvent (final UUID roomId , final RoomType roomType , List <GameRoomJoinEvent > userSessions ) {
92+ requestRoomManagementEvent (roomId , ActionType .NEW_ROOM , roomType , new JsonArray (userSessions .stream ().map (e ->
93+ new JsonObject ()
94+ .put (Fields .sessionId , e .getSession ().getId ())
95+ .put (Fields .admin , e .getSession ().isAdmin ())
96+ .put (Fields .initialData , e .getData ()))
97+ .collect (Collectors .toList ())))
10198 .onSuccess (t -> userSessions .forEach (it -> it .getSession ().setRoomKey (roomId )))
10299 .onFailure (t -> {
103100 log .error ("Room verticle {} failed to join" , roomId , t );
0 commit comments