Commit fe97423e authored by Natanael BOUTEILLER's avatar Natanael BOUTEILLER

Ajout de getJoueur dans thrift + ajout structure de stockage des joueurs dans hashmap

parent e72e43a1
......@@ -11,4 +11,6 @@ service GameServerService {
i32 createGame(i32 numberOfPlayers)
i32 join(i32 gameId, JoinRequest request) throws (1:common.GameNotFound e)
list<string> getJoueurs(i32 gameId) throws (1:common.GameNotFound e)
}
......@@ -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
....
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<GameServiceHandler>(handler), protocolFactory);
return new ServletRegistrationBean(tServlet, "/api");
}
}
package fr.univnantes.alma.common;
public class GameJoinRequest {
private String name;
public GameJoinRequest(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
}
......@@ -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<String> getJoueurs(int gameId);
}
......@@ -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<GameJoinRequest> requests;
private Map<String, Player> 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<GameJoinRequest>(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<String> getJoueurs() {
LinkedList<String> liste = new LinkedList<>();
liste.addAll(players.keySet());
return liste;
}
}
......@@ -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<String> getJoueurs(int gameId) {
Game game = games.get(gameId);
return game.getJoueurs();
}
}
package fr.univnantes.alma.game;
public class Player {
String name;
Player(String name) {
this.name = name;
}
}
......@@ -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<String> getJoueurs(int gameId) throws TException{
return service.getJoueurs(gameId);
}
}
......@@ -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);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment