mirror of
https://github.com/OpenLiberty/liberty-bikes.git
synced 2025-03-07 11:26:52 +08:00
move validation check to frontend
This commit is contained in:
parent
914cc65457
commit
47ed3fb16a
@ -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() {
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user