11package com .rakeshv .tictactoe .controllers ;
22
3- import com .fasterxml .jackson .core .JsonProcessingException ;
4- import com .fasterxml .jackson .databind .ObjectMapper ;
53import com .rakeshv .tictactoe .exceptions .GameNotFoundException ;
64import com .rakeshv .tictactoe .exceptions .InvalidGameException ;
75import com .rakeshv .tictactoe .models .Game ;
86import com .rakeshv .tictactoe .models .GamePlay ;
97import com .rakeshv .tictactoe .models .Player ;
8+ import com .rakeshv .tictactoe .models .Response ;
109import com .rakeshv .tictactoe .services .GameService ;
10+ import com .rakeshv .tictactoe .services .GameStorage ;
1111import lombok .extern .slf4j .Slf4j ;
1212import org .springframework .beans .factory .annotation .Autowired ;
1313import org .springframework .messaging .Message ;
1414import org .springframework .messaging .MessageHeaders ;
1515import org .springframework .messaging .handler .annotation .MessageMapping ;
1616import org .springframework .messaging .handler .annotation .Payload ;
17- import org .springframework .messaging .handler .annotation .SendTo ;
1817import org .springframework .messaging .simp .SimpMessagingTemplate ;
1918import org .springframework .messaging .simp .stomp .StompHeaderAccessor ;
2019import org .springframework .stereotype .Controller ;
2120
22- import java .util .Map ;
23- import java .util .Set ;
21+ import java .util .Optional ;
2422
2523@ Controller
2624@ Slf4j
@@ -35,33 +33,93 @@ public void startGame(@Payload Player player, Message<?> message) {
3533 MessageHeaders headers = message .getHeaders ();
3634 StompHeaderAccessor accessor = StompHeaderAccessor .wrap (message );
3735 Object sessionId = headers .get ("simpSessionId" );
38- log .info ("Player {} started game with session id {}" , player .getLogin (), sessionId );
39- log .error ("session id is {}" , sessionId );
40- Game game = gameService .createGame (player , sessionId );
41- this .messagingTemplate .convertAndSend ("/topic/start-game/" + player .getLogin (), game );
36+ if (checkIfUserExists (player .getLogin ())) {
37+ sendNotification ("/topic/invalid-username/" + player .getLogin (),
38+ Response .builder ().message ("Username already exists. Select different username" )
39+ .sessionid (sessionId ).build ());
40+ } else {
41+ log .info ("Player {} started game with session id {}" , player .getLogin (), sessionId );
42+ GameStorage .getInstance ().addPlayer (sessionId , player .getLogin ());
43+ Game game = gameService .createGame (player , sessionId );
44+ this .messagingTemplate .convertAndSend ("/topic/start-game/" + player .getLogin (), game );
45+ }
4246 }
4347
4448 @ MessageMapping ("/play" )
4549 public void playGame (@ Payload GamePlay request ) throws InvalidGameException , GameNotFoundException {
4650 log .info ("playing the game {}" , request );
4751 Game game = gameService .playGame (request );
48- this .messagingTemplate .convertAndSend ("/topic/game-progress/" + game .getGameId (), game );
52+ if (game .getWinner () != null ) {
53+ String player1 = game .getPlayer1 ().getLogin ();
54+ String player2 = game .getPlayer2 ().getLogin ();
55+ this .messagingTemplate .convertAndSend ("/topic/game-over/" + player1 ,
56+ Response .builder ().message ("Winner is " + game .getWinner ()).build ());
57+ this .messagingTemplate .convertAndSend ("/topic/game-over/" + player2 ,
58+ Response .builder ().message ("Winner is " + game .getWinner ()).build ());
59+ } else {
60+ this .messagingTemplate .convertAndSend ("/topic/game-progress/" + game .getPlayer1 ().getLogin (), game );
61+ this .messagingTemplate .convertAndSend ("/topic/game-progress/" + game .getPlayer2 ().getLogin (), game );
62+ }
4963 }
5064
5165 @ MessageMapping ("/connect/random" )
5266 public void connectRandomGame (@ Payload Player player , Message <?> message ) throws GameNotFoundException {
53- log .info ("Connecting player {} to random game" , player .getLogin ());
5467 MessageHeaders headers = message .getHeaders ();
5568 StompHeaderAccessor accessor = StompHeaderAccessor .wrap (message );
5669 Object sessionId = headers .get ("simpSessionId" );
57- log .info ("Player {} started game with session id {}" , player .getLogin (), sessionId );
58- log .error ("session id is {}" , sessionId );
59- this .messagingTemplate .convertAndSend ("/topic/connect-random/" + player .getLogin (),
60- gameService .connectToRandomGame (player , sessionId ));
70+ if (checkIfUserExists (player .getLogin ())) {
71+ sendNotification ("/topic/invalid-username/" + player .getLogin (),
72+ Response .builder ().message ("Username already exists. Select different username" ).build ());
73+ } else {
74+ log .info ("connecting Player {} to random game" , player .getLogin ());
75+ Optional <Game > optionalGame = gameService .connectToRandomGame (player , sessionId );
76+ if (optionalGame .isPresent ()) {
77+ sendGameNotification (player , sessionId .toString (), optionalGame .get ());
78+ } else {
79+ sendNotification ("/topic/game-not-found/" + player .getLogin (),
80+ Response .builder ().message ("No game found. Please create new game" ).build ());
81+ }
82+ }
83+ }
84+
85+ @ MessageMapping ("/connect/gameid" )
86+ public void connectToGameId (@ Payload Player player , Message <?> message ) {
87+ MessageHeaders headers = message .getHeaders ();
88+ Object sessionId = headers .get ("simpSessionId" );
89+ if (checkIfUserExists (player .getLogin ())) {
90+ sendNotification ("/topic/invalid-username/" + player .getLogin (),
91+ Response .builder ().message ("Username already exists. Select different username" ).build ());
92+ } else {
93+ log .info ("User {} is trying to connect to game id {}" , player .getLogin (), player .getGameId ());
94+ Optional <Game > optionalGame = gameService .connectToGame (player , sessionId .toString ());
95+ if (optionalGame .isPresent ()) {
96+ log .info ("Found the game with id {}" , player .getGameId ());
97+ sendGameNotification (player , sessionId .toString (), optionalGame .get ());
98+ } else {
99+ sendNotification ("/topic/game-not-found/" + player .getLogin (),
100+ Response .builder ().message ("No game found. Please create new game" ).build ());
101+ }
102+ }
61103 }
62104
63105 @ MessageMapping ("/disconnect" )
64106 public void disconnectPlayer (@ Payload Player player ) {
65107 log .error ("Player {} disconnected" , player );
108+ GameStorage .getInstance ().removeGame (player .getLogin ());
109+ GameStorage .getInstance ().removePlayer (player .getLogin ());
110+ }
111+
112+ private boolean checkIfUserExists (String username ) {
113+ return GameStorage .getInstance ().checkIfUserExists (username );
114+ }
115+
116+ private void sendNotification (String prefix , Object response ) {
117+ this .messagingTemplate .convertAndSend (prefix , response );
118+ }
119+
120+ private void sendGameNotification (Player player , String sessionId , Game game ) {
121+ GameStorage .getInstance ().addPlayer (sessionId , player .getLogin ());
122+ sendNotification ("/topic/connect-random/player2/" + player .getLogin (), game );
123+ sendNotification ("/topic/connect-random/player1/" + game .getPlayer1 ().getLogin (), game );
66124 }
67125}
0 commit comments