Commit 3b33b9d2 authored by Mamadou Saliou DIALLO's avatar Mamadou Saliou DIALLO
parents 85473dd0 477de5b4
......@@ -18,7 +18,7 @@ service GameService {
common.Response resits(common.Player hunted)
common.Response toGo(common.Player hunted)
common.Response letGo(common.Player hunted)
common.Response playPlaceCard(common.Player hunted, common.Card card)
......
......@@ -2,12 +2,21 @@ package fr.univnantes.alma.game;
import fr.univnantes.alma.common.GameJoinRequest;
import fr.univnantes.alma.model.BoardGame;
import fr.univnantes.alma.model.card.BasicCard;
import fr.univnantes.alma.model.card.place.*;
import fr.univnantes.alma.model.card.player.survival.*;
import fr.univnantes.alma.model.card.player.track.*;
import fr.univnantes.alma.references.associations.BidirectionalOneToOne;
import fr.univnantes.alma.references.interfaces.OneToOneAssociation;
import fr.univnantes.alma.thrift.Board;
import fr.univnantes.alma.thrift.InvalidOperationException;
import fr.univnantes.alma.thrift.Player;
import org.atlanmod.commons.log.Log;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;
......@@ -26,6 +35,11 @@ public class Game {
* The id of the game
*/
private int id;
/**
* The creator of the game
*/
private Player creator;
/**
* Counter used to increment player identifications.
*/
......@@ -41,6 +55,11 @@ public class Game {
*/
private final Map<Integer, Player> players = new HashMap<>();
/**
* The player who is the creature
*/
private Player creature;
/**
* Thread play.
*/
......@@ -51,20 +70,117 @@ public class Game {
*/
private OneToOneAssociation<Game, BoardGame> boardGame;
/**
* Deck of survival cards
*/
private List<BasicSurvivalCard> survivalCardsDeck;
/**
* Deck of tracking cards
*/
private List<BasicTrackingCard> trackingCardsDeck;
/**
* Deck of reserve
*/
private List<BasicPlaceCard> reserve;
/**
* Create new instance of game.
* @param expectedPlayersForGame Expected players
*/
public Game(int expectedPlayersForGame, int id) {
public Game(Player creator,int expectedPlayersForGame, int id) {
this.creator = creator;
this.expectedPlayers = new AtomicInteger(expectedPlayersForGame);
this.id = id;
this.requests =
new ArrayBlockingQueue<GameJoinRequest>(expectedPlayersForGame);
boardGame = new BidirectionalOneToOne<>(this, BoardGame::getGame);
initilizationSurvivalCards();
initializationTrackCards();
initializationReserve();
play = new Thread(() -> this.start());
play.start();
}
private void initilizationSurvivalCards(){
this.survivalCardsDeck.add(new DodgeCard());
this.survivalCardsDeck.add(new FlipFlopCard());
this.survivalCardsDeck.add(new RisposteCard());
this.survivalCardsDeck.add(new VortexCard());
}
private void initializationTrackCards(){
this.trackingCardsDeck.add(new AnguishCard());
this.trackingCardsDeck.add(new AnticipationCard());
this.trackingCardsDeck.add(new CataclysmCard());
this.trackingCardsDeck.add(new CloneCard());
this.trackingCardsDeck.add(new DeploymentCard());
this.trackingCardsDeck.add(new DespairCard());
this.trackingCardsDeck.add(new DetourCard());
this.trackingCardsDeck.add(new DominationCard());
this.trackingCardsDeck.add(new DreadCard());
this.trackingCardsDeck.add(new EpidemicCard());
this.trackingCardsDeck.add(new FlashBackCard());
this.trackingCardsDeck.add(new ForceFieldCard());
this.trackingCardsDeck.add(new GargantuaCard());
this.trackingCardsDeck.add(new GripCard());
this.trackingCardsDeck.add(new HarassementCard());
this.trackingCardsDeck.add(new InertiaCard());
this.trackingCardsDeck.add(new InterferenceCard());
this.trackingCardsDeck.add(new IntuitionCard());
this.trackingCardsDeck.add(new MagnetismCard());
this.trackingCardsDeck.add(new MirageCard());
this.trackingCardsDeck.add(new MutationCard());
this.trackingCardsDeck.add(new PsychosisCard());
this.trackingCardsDeck.add(new QuicksandsCard());
this.trackingCardsDeck.add(new RelentlessnessCard());
this.trackingCardsDeck.add(new ReminiscenceCard());
this.trackingCardsDeck.add(new RestrictedZoneCard());
this.trackingCardsDeck.add(new ScreamsCard());
this.trackingCardsDeck.add(new SpottingCard());
this.trackingCardsDeck.add(new StasisCard());
this.trackingCardsDeck.add(new TelepathyCard());
this.trackingCardsDeck.add(new ThirstyForBloodCard());
this.trackingCardsDeck.add(new TimeRiftCard());
this.trackingCardsDeck.add(new TornadoCard());
this.trackingCardsDeck.add(new ToxinCard());
this.trackingCardsDeck.add(new UbiquityCard());
this.trackingCardsDeck.add(new VirusCard());
}
private void initializationReserve(){
int nb_players = players.size();
List<BasicPlaceCard> liste = new ArrayList<BasicPlaceCard>();
if (nb_players == 2){
liste.addAll(generateCard(SwampCard.class.getCanonicalName(),1));
liste.addAll(generateCard(ShelterCard.class.getCanonicalName(),1));
liste.addAll(generateCard(WreckCard.class.getCanonicalName(),1));
liste.addAll(generateCard(SourceCard.class.getCanonicalName(),1));
liste.addAll(generateCard(ArtefactCard.class.getCanonicalName(),1));
}
else if (nb_players == 2 || nb_players == 3){
liste.addAll(generateCard(SwampCard.class.getCanonicalName(),2));
liste.addAll(generateCard(ShelterCard.class.getCanonicalName(),2));
liste.addAll(generateCard(WreckCard.class.getCanonicalName(),2));
liste.addAll(generateCard(SourceCard.class.getCanonicalName(),2));
liste.addAll(generateCard(ArtefactCard.class.getCanonicalName(),2));
}
else if (nb_players >= 4 || nb_players <= 6){
liste.addAll(generateCard(SwampCard.class.getCanonicalName(),3));
liste.addAll(generateCard(ShelterCard.class.getCanonicalName(),3));
liste.addAll(generateCard(WreckCard.class.getCanonicalName(),3));
liste.addAll(generateCard(SourceCard.class.getCanonicalName(),3));
liste.addAll(generateCard(ArtefactCard.class.getCanonicalName(),3));
}
reserve.addAll(liste);
}
/**
* Set the creature
*/
public void setCreature(Player creature){
this.creature = creature;
}
/**
* Join a game.
* @param request game join request
......@@ -81,7 +197,7 @@ public class Game {
}
/**
* Lunch the game.
* Launch the game.
*/
private void start() {
this.waitForPlayers();
......@@ -90,7 +206,7 @@ public class Game {
}
/**
* Wait for players until expected players not reached.
* Wait for players until the expected number of players are reached.
*/
private void waitForPlayers() {
Log.info("Waiting for request. Expecting {0} players.",
......@@ -122,4 +238,27 @@ public class Game {
public OneToOneAssociation<Game, BoardGame> getBoardGame() {
return boardGame;
}
private List<BasicPlaceCard> generateCard(String className, int number) {
List<BasicPlaceCard> result = new ArrayList<>();
try {
Class clazz = Class.forName(className);
Constructor<?> constructor = clazz.getConstructor(String.class);
for(int i = 0; i< number; i++) {
BasicPlaceCard card = (BasicPlaceCard) constructor.newInstance(new Object[0]);
result.add(card);
}
} catch (ClassNotFoundException | NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return result;
}
}
\ No newline at end of file
......@@ -28,7 +28,7 @@ public class GameController implements GameInterface {
throw new InvalidOperationException(400,"Player is already registered in a game");
}
int newId = idCounter.incrementAndGet();
Game newGame = new Game(expectedPlayers, newId);
Game newGame = new Game(creator,expectedPlayers, newId);
games.put(newId, newGame);
connectedPlayers.put(newId,creator);
return newId;
......
......@@ -9,6 +9,11 @@ import fr.univnantes.alma.references.associations.BidirectionalOneToMany;
import fr.univnantes.alma.references.interfaces.ManyToOneAssociation;
import fr.univnantes.alma.references.interfaces.OneToManyAssociation;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
/**
* Basic place card.
*/
......@@ -82,4 +87,5 @@ public abstract class BasicPlaceCard extends BasicCard implements PlaceCard {
public OneToManyAssociation<PlaceCard, Token> getToken() {
return tokens;
}
}
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