diff --git a/frontend/prebuild/src/app/login/login.component.ts b/frontend/prebuild/src/app/login/login.component.ts index 2553712..e82ff9c 100644 --- a/frontend/prebuild/src/app/login/login.component.ts +++ b/frontend/prebuild/src/app/login/login.component.ts @@ -26,18 +26,32 @@ export class LoginComponent implements OnInit { let roundID: string = $('#roundid').val(); roundID = roundID.toUpperCase().replace(/[^A-Z]/g, ''); if (roundID.length !== 6) { - alert('Not a Valid Round ID'); + alert(roundID + ' is not a valid round ID, because it must be 6 letters long'); return; } - $.post(`http://${document.location.hostname}:8080/round/joinRound/${roundID}`, function(data) { - if (data === 'VALID') { - sessionStorage.setItem('username', $('#username').val()); - sessionStorage.setItem('roundId', roundID); - lc.router.navigate(['/game']); - } else { - alert('Response is: ' + data); + + $.get(`http://${document.location.hostname}:8080/round/roundStatus/${roundID}`, function(data) { + if (data === undefined) { + alert('Game round does not exist!'); + return; } - }); + if (data.gameState === 'FULL') { + alert('Game round is FULL!'); + return; + } + if (data.gameState === 'RUNNING') { + alert('Game round has already started!'); + return; + } + if (data.gameState === 'FINISHED') { + alert('Game round has already finished!'); + return; + } + + sessionStorage.setItem('username', $('#username').val()); + sessionStorage.setItem('roundId', roundID); + lc.router.navigate(['/game']); + }); } hostRound() { diff --git a/game-service/src/main/java/org/libertybikes/game/core/GameRound.java b/game-service/src/main/java/org/libertybikes/game/core/GameRound.java index f4585f6..5a200e6 100644 --- a/game-service/src/main/java/org/libertybikes/game/core/GameRound.java +++ b/game-service/src/main/java/org/libertybikes/game/core/GameRound.java @@ -16,6 +16,7 @@ import javax.enterprise.concurrent.ManagedScheduledExecutorService; import javax.enterprise.inject.spi.CDI; import javax.json.Json; import javax.json.JsonArrayBuilder; +import javax.json.JsonObject; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.websocket.Session; @@ -271,4 +272,11 @@ public class GameRound implements Runnable { } state = State.RUNNING; } + + /** + * @return + */ + public JsonObject toJson() { + return Json.createObjectBuilder().add("gameId", id).add("gameState", state.toString()).build(); + } } diff --git a/game-service/src/main/java/org/libertybikes/game/round/service/GameRoundService.java b/game-service/src/main/java/org/libertybikes/game/round/service/GameRoundService.java index 1afbf35..ad4ad86 100644 --- a/game-service/src/main/java/org/libertybikes/game/round/service/GameRoundService.java +++ b/game-service/src/main/java/org/libertybikes/game/round/service/GameRoundService.java @@ -5,6 +5,7 @@ import java.util.HashMap; import java.util.Map; import javax.enterprise.context.ApplicationScoped; +import javax.json.JsonObject; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; @@ -15,7 +16,6 @@ import javax.ws.rs.core.MediaType; import javax.ws.rs.core.UriInfo; import org.libertybikes.game.core.GameRound; -import org.libertybikes.game.core.GameRound.State; @Path("/") @ApplicationScoped @@ -32,22 +32,6 @@ public class GameRoundService { return allRounds.values(); } - @POST - @Path("/joinRound/{roundId}") - public String joinRound(@PathParam("roundId") String roundId) { - GameRound r = allRounds.get(roundId.toUpperCase()); - if (r == null) { - return "Game Does Not Exist"; - } - if (r.state.equals(State.FULL)) { - return "Game Is Full"; - } - if (!r.state.equals(State.OPEN)) { - return "Game Already Started"; - } - return "VALID"; - } - @POST @Path("/create") public String createRound() { @@ -67,6 +51,14 @@ public class GameRoundService { return allRounds.get(roundId); } + @GET + @Path("/roundStatus/{roundId}") + @Produces(MediaType.APPLICATION_JSON) + public JsonObject roundStatus(@PathParam("roundId") String roundId) { + GameRound gr = allRounds.get(roundId); + return gr != null ? gr.toJson() : null; + } + public GameRound requeue(GameRound oldRound) { GameRound nextRound = new GameRound(oldRound.nextRoundId); GameRound existingRound = allRounds.putIfAbsent(oldRound.nextRoundId, nextRound);