Revisão | ec8e2d8433f8701e7d8e1aa5ece72cfc317b6e64 (tree) |
---|---|
Hora | 2019-01-29 06:03:44 |
Autor | sebastian_bugiu <sebastian_ <bugiu@head...> |
Commiter | sebastian_bugiu <sebastian_ |
Adding lobby support.
@@ -417,14 +417,19 @@ | ||
417 | 417 | }); |
418 | 418 | LobbyInvitationList lobbyInvitationListRet = new LobbyInvitationList(lobbyInvitationList); |
419 | 419 | if (lobby.getExpectedPlayerNum() == usersJoined[0]) { |
420 | - lobby.setStatus(1); | |
421 | - final int updatedLobbyStatusNum = em.createNamedQuery(Lobby.UPDATE_LOBBY_STATUS) | |
422 | - .setParameter("lobbyId", lobby.getId()) | |
423 | - .setParameter("lobbyStatus", lobby.getStatus()) | |
424 | - .executeUpdate(); | |
425 | - if (updatedLobbyStatusNum != 1) { | |
420 | + if (lobby.getStatus() == 0) { | |
421 | + // We are ready to start the game if we are coming from the lobby. | |
422 | + lobby.setStatus(1); | |
426 | 423 | |
424 | + final int updatedLobbyStatusNum = em.createNamedQuery(Lobby.UPDATE_LOBBY_STATUS) | |
425 | + .setParameter("lobbyId", lobby.getId()) | |
426 | + .setParameter("lobbyStatus", lobby.getStatus()) | |
427 | + .executeUpdate(); | |
428 | + if (updatedLobbyStatusNum != 1) { | |
429 | + | |
430 | + } | |
427 | 431 | } |
432 | + | |
428 | 433 | } |
429 | 434 | lobbyInvitationListRet.setLobbyStatus(lobby.getStatus()); |
430 | 435 | return createResponse(lobbyInvitationListRet); |
@@ -949,7 +954,8 @@ | ||
949 | 954 | } |
950 | 955 | |
951 | 956 | // ONLY TEMPORARY FOR TESTING. IN THE END WE CAN'T DELETE THE SESSION BECAUSE SOMEBODY MAY BE ALREADY IN A SESSION ON A DIFFERENT SERVER |
952 | - List<Session> existingSessionList = em.createNamedQuery(Session.GET_SESSION_BY_NAME).setParameter("sessionName", session.getSessionName()).getResultList(); | |
957 | + List<Session> existingSessionList = em.createNamedQuery(Session.GET_SESSION_BY_NAME) | |
958 | + .setParameter("sessionName", session.getSessionName()).getResultList(); | |
953 | 959 | if (!existingSessionList.isEmpty()) { |
954 | 960 | for (Session s : existingSessionList) { |
955 | 961 | em.createNamedQuery(Session.DELETE_BY_NAME).setParameter("sessionName", s.getSessionName()); |
@@ -993,6 +999,38 @@ | ||
993 | 999 | return createResponse(server1); |
994 | 1000 | } |
995 | 1001 | |
1002 | + List<Lobby> lobbyList = em.createNamedQuery(Lobby.GET_LOBBY_BY_ID).setParameter("lobbyId", session.getLobbyId()).getResultList(); | |
1003 | + if (lobbyList.isEmpty()) { | |
1004 | + Server server1 = new Server(); | |
1005 | + server1.setErrorCode(ErrorCodes.GET_LOBBY_STATUS_LOBBY_NOT_FOUND); | |
1006 | + server1.setError(ErrorCodes.GET_LOBBY_STATUS_LOBBY_NOT_FOUND_STR); | |
1007 | + return createResponse(server1); | |
1008 | + } | |
1009 | + if (lobbyList.size() > 1) { | |
1010 | + // Shouldn't be possible. | |
1011 | + Server server1 = new Server(); | |
1012 | + server1.setErrorCode(ErrorCodes.GET_LOBBY_STATUS_MULTIPLE_LOBBIES); | |
1013 | + server1.setError(ErrorCodes.GET_LOBBY_STATUS_MULTIPLE_LOBBIES_STR); | |
1014 | + return createResponse(server1); | |
1015 | + } | |
1016 | + | |
1017 | + final Lobby lobby = lobbyList.get(0); | |
1018 | + | |
1019 | + if (lobby.getStatus() == 1) { | |
1020 | + // If the session has been started by the lobby leader it means we are ready for everybody to join. | |
1021 | + lobby.setStatus(2); | |
1022 | + | |
1023 | + final int updatedLobbyStatusNum = em.createNamedQuery(Lobby.UPDATE_LOBBY_STATUS) | |
1024 | + .setParameter("lobbyId", lobby.getId()) | |
1025 | + .setParameter("lobbyStatus", lobby.getStatus()) | |
1026 | + .executeUpdate(); | |
1027 | + if (updatedLobbyStatusNum != 1) { | |
1028 | + | |
1029 | + } | |
1030 | + } else { | |
1031 | + // How is this possible? | |
1032 | + } | |
1033 | + | |
996 | 1034 | Session createdSession = new Session(); |
997 | 1035 | createdSession.setSessionName(session.getSessionName()); |
998 | 1036 | createdSession.setMapId(selectedMapId); |
@@ -1115,16 +1153,16 @@ | ||
1115 | 1153 | } |
1116 | 1154 | User user = getUserByToken.getUser(); |
1117 | 1155 | |
1118 | - List<LobbyInvitation> lobbyInvitationList = em.createNamedQuery(LobbyInvitation.GET_LOBBY_INVITATIONS_BY_LOBBY_ID_AND_INVITED_USER_ID) | |
1156 | + List<LobbyInvitation> currentPlayerLobbyInvitationList = em.createNamedQuery(LobbyInvitation.GET_LOBBY_INVITATIONS_BY_LOBBY_ID_AND_INVITED_USER_ID) | |
1119 | 1157 | .setParameter("lobbyId", session.getLobbyId()) |
1120 | 1158 | .setParameter("userId", user.getId()).getResultList(); |
1121 | - if (lobbyInvitationList.isEmpty()) { | |
1159 | + if (currentPlayerLobbyInvitationList.isEmpty()) { | |
1122 | 1160 | Lobby server = new Lobby(); |
1123 | 1161 | server.setErrorCode(ErrorCodes.JOIN_SESSION_CANNOT_FIND_INVITATION); |
1124 | 1162 | server.setError(ErrorCodes.JOIN_SESSION_CANNOT_FIND_INVITATION_STR); |
1125 | 1163 | return createResponse(server); |
1126 | 1164 | } |
1127 | - final LobbyInvitation lobbyInvitation = lobbyInvitationList.get(0); | |
1165 | + final LobbyInvitation lobbyInvitation = currentPlayerLobbyInvitationList.get(0); | |
1128 | 1166 | |
1129 | 1167 | List<Session> sessionList = em.createNamedQuery(Session.GET_SESSION_BY_LOBBY_ID) |
1130 | 1168 | .setParameter("lobbyId", lobbyInvitation.getLobbyId()).getResultList();//em.createNamedQuery(Session.GET_SESSION_BY_NAME_ACTIVE).setParameter("sessionName", session.getSessionName()).getResultList(); |
@@ -1198,6 +1236,56 @@ | ||
1198 | 1236 | |
1199 | 1237 | server.setMapId(usedSession.getMapId()); |
1200 | 1238 | server.setSpawnPoint(sessionUserSpawnPoint.getSpawnPoint()); |
1239 | + | |
1240 | + // If everybody joined we must make the lobby show that it's in game. | |
1241 | + List<Lobby> lobbyList = em.createNamedQuery(Lobby.GET_LOBBY_BY_ID).setParameter("lobbyId", session.getLobbyId()).getResultList(); | |
1242 | + if (lobbyList.isEmpty()) { | |
1243 | + Server server1 = new Server(); | |
1244 | + server1.setErrorCode(ErrorCodes.GET_LOBBY_STATUS_LOBBY_NOT_FOUND); | |
1245 | + server1.setError(ErrorCodes.GET_LOBBY_STATUS_LOBBY_NOT_FOUND_STR); | |
1246 | + return createResponse(server1); | |
1247 | + } | |
1248 | + if (lobbyList.size() > 1) { | |
1249 | + // Shouldn't be possible. | |
1250 | + Server server1 = new Server(); | |
1251 | + server1.setErrorCode(ErrorCodes.GET_LOBBY_STATUS_MULTIPLE_LOBBIES); | |
1252 | + server1.setError(ErrorCodes.GET_LOBBY_STATUS_MULTIPLE_LOBBIES_STR); | |
1253 | + return createResponse(server1); | |
1254 | + } | |
1255 | + | |
1256 | + final Lobby lobby = lobbyList.get(0); | |
1257 | + | |
1258 | + List<LobbyInvitation> lobbyInvitationList = em.createNamedQuery(LobbyInvitation.GET_LOBBY_INVITATIONS_BY_LOBBY_ID) | |
1259 | + .setParameter("lobbyId", lobby.getId()).getResultList(); | |
1260 | + if (lobbyInvitationList.isEmpty()) { | |
1261 | + // Is this possible? | |
1262 | + Server server1 = new Server(); | |
1263 | + server1.setErrorCode(ErrorCodes.GET_LOBBY_STATUS_NOBODY_INVITED); | |
1264 | + server1.setError(ErrorCodes.GET_LOBBY_STATUS_NOBODY_INVITED_STR); | |
1265 | + return createResponse(server1); | |
1266 | + } | |
1267 | + final int[] usersJoined = {0}; | |
1268 | + lobbyInvitationList.forEach(lobbyInvitation1 -> { | |
1269 | + if (lobbyInvitation1.getUserJoined() == 1) { | |
1270 | + ++usersJoined[0]; | |
1271 | + } | |
1272 | + }); | |
1273 | + if (lobby.getStatus() == 2 && usersJoined[0] == lobby.getExpectedPlayerNum() && | |
1274 | + usedSession.getUserNum() == lobby.getExpectedPlayerNum() + 1) { // +1 for the player who started the lobby. | |
1275 | + // If the session has been started by the lobby leader it means we are ready for everybody to join. | |
1276 | + lobby.setStatus(3); | |
1277 | + | |
1278 | + final int updatedLobbyStatusNum = em.createNamedQuery(Lobby.UPDATE_LOBBY_STATUS) | |
1279 | + .setParameter("lobbyId", lobby.getId()) | |
1280 | + .setParameter("lobbyStatus", lobby.getStatus()) | |
1281 | + .executeUpdate(); | |
1282 | + if (updatedLobbyStatusNum != 1) { | |
1283 | + | |
1284 | + } | |
1285 | + } else { | |
1286 | + // How is this possible? | |
1287 | + } | |
1288 | + | |
1201 | 1289 | return createResponse(server); |
1202 | 1290 | } |
1203 | 1291 |
@@ -26,7 +26,7 @@ | ||
26 | 26 | @Column(name = "lobby_invitations") |
27 | 27 | private long lobbyInvitations; |
28 | 28 | @Column(name = "status") |
29 | - private int status; // 0 - lobby created 1 - ready to start 2 - in game 3 - lobby closed. | |
29 | + private int status; // 0 - lobby created 1 - ready to start server 2 - ready to join 3 - in game 4 - lobby closed. | |
30 | 30 | @Column(name = "expected_player_num") |
31 | 31 | private int expectedPlayerNum; |
32 | 32 | @Column(name = "joined_player_num") |