in a school project, we coded a game implementation in Java and are displaying the actual game through Angular. We have coded the game object which represent the actual game and made a "GameResource" class which allow us to manipulate the Game object argument in this class through REST call like this :
@PUT
@Path("moove/{x1}/{x2}/{y1}/{y2}")
@Produces(MediaType.APPLICATION_JSON)
public Response play(@PathParam("x1") final int oldX, @PathParam("x2") final int newX, @PathParam("y1") final int oldY, @PathParam("y2") final int newY) {
if (game.getBoard().getPiece(oldX, oldY).hasBall()) {
game.play(new MoveBall(oldX, oldY, newX, newY));
} else {
game.play(new MovePion(oldX, oldY, newX, newY));
}
game.flushUndo();
if (game.isFinished().isPresent()) {
return Response.ok().entity(game.isFinished().get()).build();
}
return Response.ok().entity(game).build();
}
As you can see, we are just Calling method on the game object and returning the game so we can display it through Angular. But the problem is, this object is shared between each user, which means that 2 people cannot play at the same time.
I already coded a multithreaded "cloud like" service, which is using socket and launching a new thread to handle a new user every time someone is connecting to this socket, but I dont know how to do it using REST API and even if I should use REST API in this case. Any advice would be very welcomed, thank you