From fe97423eb01097680ffd5e3db3b2d7073289a332 Mon Sep 17 00:00:00 2001 From: CozyDino Date: Fri, 27 Nov 2020 12:19:24 +0100 Subject: [PATCH] Ajout de getJoueur dans thrift + ajout structure de stockage des joueurs dans hashmap --- .../src/main/thrift/game-server.thrift | 2 ++ .../src/doc/asciidoc/_sections/composants.adoc | 15 +++------------ .../univnantes/alma/NotAloneApplication.java | 3 +++ .../alma/common/GameJoinRequest.java | 8 ++++++++ .../fr/univnantes/alma/common/GameService.java | 4 ++++ .../java/fr/univnantes/alma/game/Game.java | 18 ++++++++++++++++-- .../alma/game/GameServiceController.java | 7 +++++++ .../java/fr/univnantes/alma/game/Player.java | 9 +++++++++ .../alma/handler/GameServiceHandler.java | 10 +++++++++- .../alma/NotAloneApplicationTest.java | 8 +++++++- 10 files changed, 68 insertions(+), 16 deletions(-) create mode 100644 not-alone-server/src/main/java/fr/univnantes/alma/game/Player.java diff --git a/not-alone-core/src/main/thrift/game-server.thrift b/not-alone-core/src/main/thrift/game-server.thrift index 6f33177..bf4074d 100644 --- a/not-alone-core/src/main/thrift/game-server.thrift +++ b/not-alone-core/src/main/thrift/game-server.thrift @@ -11,4 +11,6 @@ service GameServerService { i32 createGame(i32 numberOfPlayers) i32 join(i32 gameId, JoinRequest request) throws (1:common.GameNotFound e) + + list getJoueurs(i32 gameId) throws (1:common.GameNotFound e) } diff --git a/not-alone-doc/src/doc/asciidoc/_sections/composants.adoc b/not-alone-doc/src/doc/asciidoc/_sections/composants.adoc index 5a20716..a2b2e18 100644 --- a/not-alone-doc/src/doc/asciidoc/_sections/composants.adoc +++ b/not-alone-doc/src/doc/asciidoc/_sections/composants.adoc @@ -45,18 +45,9 @@ component GameServer as gs component Player as player component Traque as traque component Creature as creature -traque -(0-"livrerCarteSurvie" gs -gs -0)--"jouerCarte" player -gs -0)--"jouerPion" player -gs -0)--"join" player +traque -(0- gs +gs -0)-- player -creature -left0)--"livrerCarteTraque" gs -.... -.Diagramme de composants -[plantuml] -.... -component GameServer as gs -gs --> createGame -gs --> join +creature -left0)-- gs .... diff --git a/not-alone-server/src/main/java/fr/univnantes/alma/NotAloneApplication.java b/not-alone-server/src/main/java/fr/univnantes/alma/NotAloneApplication.java index 37d7196..fd08aaf 100644 --- a/not-alone-server/src/main/java/fr/univnantes/alma/NotAloneApplication.java +++ b/not-alone-server/src/main/java/fr/univnantes/alma/NotAloneApplication.java @@ -1,5 +1,6 @@ package fr.univnantes.alma; +import fr.univnantes.alma.common.GameService; import fr.univnantes.alma.thrift.GameServerService; import fr.univnantes.alma.handler.GameServiceHandler; import org.apache.thrift.protocol.TBinaryProtocol; @@ -32,6 +33,8 @@ public class NotAloneApplication { public ServletRegistrationBean gameServer(TProtocolFactory protocolFactory, GameServiceHandler handler) { TServlet tServlet = new TServlet(new GameServerService.Processor(handler), protocolFactory); + + return new ServletRegistrationBean(tServlet, "/api"); } } diff --git a/not-alone-server/src/main/java/fr/univnantes/alma/common/GameJoinRequest.java b/not-alone-server/src/main/java/fr/univnantes/alma/common/GameJoinRequest.java index 4f06a2d..ca70ddb 100644 --- a/not-alone-server/src/main/java/fr/univnantes/alma/common/GameJoinRequest.java +++ b/not-alone-server/src/main/java/fr/univnantes/alma/common/GameJoinRequest.java @@ -1,4 +1,12 @@ package fr.univnantes.alma.common; public class GameJoinRequest { + private String name; + public GameJoinRequest(String name) { + this.name = name; + } + + public String getName() { + return this.name; + } } diff --git a/not-alone-server/src/main/java/fr/univnantes/alma/common/GameService.java b/not-alone-server/src/main/java/fr/univnantes/alma/common/GameService.java index 9d442fc..5051a6b 100644 --- a/not-alone-server/src/main/java/fr/univnantes/alma/common/GameService.java +++ b/not-alone-server/src/main/java/fr/univnantes/alma/common/GameService.java @@ -2,6 +2,8 @@ package fr.univnantes.alma.common; import fr.univnantes.alma.common.GameJoinRequest; +import java.util.List; + public interface GameService { /** @@ -20,4 +22,6 @@ public interface GameService { */ int join(int gameId, GameJoinRequest request); + + List getJoueurs(int gameId); } diff --git a/not-alone-server/src/main/java/fr/univnantes/alma/game/Game.java b/not-alone-server/src/main/java/fr/univnantes/alma/game/Game.java index b77e941..f33af32 100644 --- a/not-alone-server/src/main/java/fr/univnantes/alma/game/Game.java +++ b/not-alone-server/src/main/java/fr/univnantes/alma/game/Game.java @@ -3,6 +3,10 @@ package fr.univnantes.alma.game; import fr.univnantes.alma.common.GameJoinRequest; import org.atlanmod.commons.log.Log; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; import java.util.concurrent.atomic.AtomicInteger; @@ -12,7 +16,7 @@ public class Game { * Stores arriving registrations. */ private final BlockingQueue requests; - + private Map players; /** * Counter used to increment player identifications. */ @@ -25,6 +29,7 @@ public class Game { public Game(int expectedPlayers) { this.expectedPlayers = new AtomicInteger(expectedPlayers); this.requests = new ArrayBlockingQueue(expectedPlayers); + this.players = new HashMap<>(expectedPlayers); play = new Thread(() -> this.start()); play.start(); @@ -47,9 +52,10 @@ public class Game { GameJoinRequest request; - while (requests.size() < expectedPlayers.intValue()) { + while (players.size() < expectedPlayers.intValue()) { try { request = requests.take(); + System.out.println(request.getName()); this.handleRequest(request); } catch (InterruptedException e) { Log.error(e); @@ -58,5 +64,13 @@ public class Game { } private void handleRequest(GameJoinRequest request) { + players.put(request.getName(), new Player(request.getName())); + } + + public List getJoueurs() { + LinkedList liste = new LinkedList<>(); + liste.addAll(players.keySet()); + + return liste; } } diff --git a/not-alone-server/src/main/java/fr/univnantes/alma/game/GameServiceController.java b/not-alone-server/src/main/java/fr/univnantes/alma/game/GameServiceController.java index 73a3e1d..809e13d 100644 --- a/not-alone-server/src/main/java/fr/univnantes/alma/game/GameServiceController.java +++ b/not-alone-server/src/main/java/fr/univnantes/alma/game/GameServiceController.java @@ -6,6 +6,7 @@ import fr.univnantes.alma.common.GameService; import org.springframework.stereotype.Component; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.concurrent.atomic.AtomicInteger; @@ -34,4 +35,10 @@ public class GameServiceController implements GameService { return game.join(request); } + @Override + public List getJoueurs(int gameId) { + Game game = games.get(gameId); + return game.getJoueurs(); + } + } diff --git a/not-alone-server/src/main/java/fr/univnantes/alma/game/Player.java b/not-alone-server/src/main/java/fr/univnantes/alma/game/Player.java new file mode 100644 index 0000000..e2ee519 --- /dev/null +++ b/not-alone-server/src/main/java/fr/univnantes/alma/game/Player.java @@ -0,0 +1,9 @@ +package fr.univnantes.alma.game; + +public class Player { + String name; + + Player(String name) { + this.name = name; + } +} diff --git a/not-alone-server/src/main/java/fr/univnantes/alma/handler/GameServiceHandler.java b/not-alone-server/src/main/java/fr/univnantes/alma/handler/GameServiceHandler.java index 02e388e..4ebe70a 100644 --- a/not-alone-server/src/main/java/fr/univnantes/alma/handler/GameServiceHandler.java +++ b/not-alone-server/src/main/java/fr/univnantes/alma/handler/GameServiceHandler.java @@ -9,6 +9,9 @@ import org.apache.thrift.TException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +import java.util.LinkedList; +import java.util.List; + @Component public class GameServiceHandler implements GameServerService.Iface { @@ -23,7 +26,12 @@ public class GameServiceHandler implements GameServerService.Iface { @Override public int join(int gameId, JoinRequest request) throws TException { //TODO: translate JoinRequest into GameJoinRequest - return service.join(gameId, new GameJoinRequest()); + String name = request.getName(); + return service.join(gameId, new GameJoinRequest(name)); } + @Override + public List getJoueurs(int gameId) throws TException{ + return service.getJoueurs(gameId); + } } diff --git a/not-alone-server/src/test/java/fr/univnantes/alma/NotAloneApplicationTest.java b/not-alone-server/src/test/java/fr/univnantes/alma/NotAloneApplicationTest.java index 70323b1..b6c2ce2 100644 --- a/not-alone-server/src/test/java/fr/univnantes/alma/NotAloneApplicationTest.java +++ b/not-alone-server/src/test/java/fr/univnantes/alma/NotAloneApplicationTest.java @@ -52,6 +52,12 @@ class NotAloneApplicationTest { client.join(id, new JoinRequest("four")); - Thread.sleep(1000); + + Thread.sleep(10000); + + System.out.println(client.getJoueurs(id)); + + Thread.sleep(10000); + } } -- GitLab