2

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

NohTow
  • 21
  • 3

2 Answers2

2

You need many game objects for each user. That means you need a stateful Rest service although it goes against the main idea of Rest (Statelessness).

Anyway you can implement stateful by adding one of the authentication method. See here for details: RESTful Authentication.

Then you could store all games in some data structure like Map<User, Game> and get necessary game for each user.

See more about state in Rest: If REST applications are supposed to be stateless, how do you manage sessions?

Ruslan
  • 6,090
  • 1
  • 21
  • 36
  • It was the solution I came with in the end, but found this ugly, and that's because REST should be stateless, thank you The best way to create Statefull services is to use (web)socket, right ? – NohTow Mar 03 '19 at 16:41
  • @NohTow Yes, I think socket it is a good way for that. Also want to note, if your application is really big and you have a lot of data to store, you should consider the REST service with data base as a storage for user's and game's data (and something else). Since all states are stored not at the service but in database, It will be still stateless. It's up to you. Hope it helps :) – Ruslan Mar 03 '19 at 19:06
  • Alright thank you ! :) Do you know if there's a way to write a service working through defined request sended by a socket ? Or do I need to send a command and analyze it every time ? – NohTow Mar 03 '19 at 20:58
  • Not sure I understood right.. There are many ways to write a service and the client may be a browser, socket or even another service. So.. if you have something more to ask, it would be better to post it as a new question. I'm sure there are many people who can suggest a good solution ;) – Ruslan Mar 03 '19 at 21:36
  • Alright, thanks, now I understand that I should use REST only if I need a stateless service, otherwise I'll certainly work on a socket related connexion. :) – NohTow Mar 08 '19 at 02:25
1

Your question is not related to Angular or Rest.

The problem is that your "game" instance is shared between all sessions. You need an instance of the game for each user on the server, and there are several ways to achieve this.

My suggestion is to create some kind of "GameStore" that will use a session id to store the game instances. Depending on the framework you are using, you probably already have a cookie named JSESSIONID that identifies the different users accessing your Rest service.

clayton.carmo
  • 121
  • 1
  • 4