Commit ca87abbf authored by mohammadmahdihn's avatar mohammadmahdihn
Browse files

Regex haye mammad

parent 58d68ffd
......@@ -31,39 +31,36 @@ public class LoginMenuController {
}
public MenuEnum processCommand(String command) throws Exception {
String username = "username";
String password = "password";
String nickname = "nickname";
Matcher[] commandMatchers = getCommandMatchers(command);
if (commandMatchers[0].find()) {
return MenuEnum.BACK;
} else if (commandMatchers[1].find()) {
loginMenuView.showCurrentMenu();
return MenuEnum.CONTINUE;
} else if (commandMatchers[18].find()){
loginMenuView.pleaseLoginFirst();
return MenuEnum.CONTINUE;
}
for (int i=2 ; i<14; i++){
if(commandMatchers[i].find()){
controlCreateUserCommand(commandMatchers[i].group(username), commandMatchers[i].group(password), commandMatchers[i].group(nickname));
return MenuEnum.CONTINUE;
}
}
for (int i=14; i<18; i++){
if(commandMatchers[i].find()){
controlLoginUserCommand(commandMatchers[i].group(username), commandMatchers[i].group(password));
return MenuEnum.CONTINUE;
}
}
loginMenuView.invalidCommand();
// String username = "username";
// String password = "password";
// String nickname = "nickname";
// Matcher[] commandMatchers = getCommandMatchers(command);
// if (commandMatchers[0].find()) {
// return MenuEnum.BACK;
// } else if (commandMatchers[1].find()) {
// loginMenuView.showCurrentMenu();
// return MenuEnum.CONTINUE;
// } else if (commandMatchers[18].find()){
// loginMenuView.pleaseLoginFirst();
// return MenuEnum.CONTINUE;
// }
// for (int i=2 ; i<14; i++){
// if(commandMatchers[i].find()){
// controlCreateUserCommand(commandMatchers[i].group(username), commandMatchers[i].group(password), commandMatchers[i].group(nickname));
// return MenuEnum.CONTINUE;
// }
// }
// for (int i=14; i<18; i++){
// if(commandMatchers[i].find()){
// controlLoginUserCommand(commandMatchers[i].group(username), commandMatchers[i].group(password));
// return MenuEnum.CONTINUE;
// }
// }
// loginMenuView.invalidCommand();
return MenuEnum.CONTINUE;
}
private void controlLoginUserCommand(String username, String password) throws Exception {
if(User.isUsernameAvailable(username) || !User.getUserByUsername(username).getPassword().equals(password)){
loginMenuView.usernameAndPasswordDidNotMatch();
} else {
......@@ -74,12 +71,13 @@ public class LoginMenuController {
public void controlCreateUserCommand(String username, String password, String nickname) {
try {
String result = sendAndReceive("user create -u " + username + " -p " + password + " -n " + nickname);
System.out.println(result);
} catch (IOException e) {
e.printStackTrace();
}
if(!User.isUsernameAvailable(username)){
loginMenuView.usernameExists(username);
} else if(!User.isNicknameAvailable(nickname)){
} else if(!User.isNicknameAvailable(nickname)) {
loginMenuView.nicknameExists(nickname);
} else {
new User(username, password, nickname);
......
......@@ -130,21 +130,21 @@ public class LoginMenuView extends Application {
registerButton.setOnMouseExited(mouseEvent -> registerButton.setEffect(null));
exit.setOnMouseEntered(mouseEvent -> exit.setEffect(new Lighting()));
exit.setOnMouseExited(mouseEvent -> exit.setEffect(null));
loginButton.setOnMouseEntered(new EventHandler<MouseEvent>() {
loginButton.setOnMouseEntered(new EventHandler<>() {
@Override
public void handle(MouseEvent mouseEvent) {
MediaPlayer player = new MediaPlayer(new Media(getClass().getResource("/sound/gga.mp3").toExternalForm()));
player.play();
}
});
registerButton.setOnMouseEntered(new EventHandler<MouseEvent>() {
registerButton.setOnMouseEntered(new EventHandler<>() {
@Override
public void handle(MouseEvent mouseEvent) {
MediaPlayer player = new MediaPlayer(new Media(getClass().getResource("/sound/gga.mp3").toExternalForm()));
player.play();
}
});
exit.setOnMouseEntered(new EventHandler<MouseEvent>() {
exit.setOnMouseEntered(new EventHandler<>() {
@Override
public void handle(MouseEvent mouseEvent) {
MediaPlayer player = new MediaPlayer(new Media(getClass().getResource("/sound/gga.mp3").toExternalForm()));
......
......@@ -4,6 +4,8 @@ import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainServer {
......@@ -47,10 +49,53 @@ public class MainServer {
}
private static String process(String input) {
if(input.startsWith("user create")){
}
Matcher[] matchers = getCommandMatchers(input);
if(matchers[2].find()) {
return matchers[2].group("username") + " " + matchers[2].group("password") + " " + matchers[2].group("nickname");
} return "nashod";
}
private static Matcher[] getCommandMatchers(String command) {
Pattern patternForExit = Pattern.compile("^menu exit$");
Pattern patternForShowCurrentMenu = Pattern.compile("^menu show-current$");
Pattern patternForCreateUser1 = Pattern.compile("^user create -u (?<username>.+?) -p (?<password>.+?) -n (?<nickname>.+?)$");
Pattern patternForCreateUser2 = Pattern.compile("^user create -u (?<username>.+?) -n (?<nickname>.+?) -p (?<password>.+?)$");
Pattern patternForCreateUser3 = Pattern.compile("^user create -p (?<password>.+?) -u (?<username>.+?) -n (?<nickname>.+?)$");
Pattern patternForCreateUser4 = Pattern.compile("^user create -p (?<password>.+?) -n (?<nickname>.+?) -u (?<username>.+?)$");
Pattern patternForCreateUser5 = Pattern.compile("^user create -n (?<nickname>.+?) -p (?<password>.+?) -u (?<username>.+?)$");
Pattern patternForCreateUser6 = Pattern.compile("^user create -n (?<nickname>.+?) -u (?<username>.+?) -p (?<password>.+?)$");
Pattern patternForCreateUser7 = Pattern.compile("^user create --username (?<username>.+?) --password (?<password>.+?) --nickname (?<nickname>.+?)$");
Pattern patternForCreateUser8 = Pattern.compile("^user create --username (?<username>.+?) --nickname (?<nickname>.+?) --password (?<password>.+?)$");
Pattern patternForCreateUser9 = Pattern.compile("^user create --password (?<password>.+?) --username (?<username>.+?) --nickname (?<nickname>.+?)$");
Pattern patternForCreateUser10 = Pattern.compile("^user create --password (?<password>.+?) --nickname (?<nickname>.+?) --username (?<username>.+?)$");
Pattern patternForCreateUser11 = Pattern.compile("^user create --nickname (?<nickname>.+?) --password (?<password>.+?) --username (?<username>.+?)$");
Pattern patternForCreateUser12 = Pattern.compile("^user create --nickname (?<nickname>.+?) --username (?<username>.+?) --password (?<password>.+?)$");
Pattern patternForLoginUser1 = Pattern.compile("^user login --username (?<username>.+?) --password (?<password>.+?)$");
Pattern patternForLoginUser2 = Pattern.compile("^user login --password (?<password>.+?) --username (?<username>.+?)$");
Pattern patternForLoginUser3 = Pattern.compile("^user login -u (?<username>.+?) -p (?<password>.+?)$");
Pattern patternForLoginUser4 = Pattern.compile("^user login -p (?<password>.+?) -u (?<username>.+?)$");
Pattern patternForEnterAnotherMenu = Pattern.compile("^menu enter (Duel|Scoreboard|Deck|Import/Export|Shop|Profile)$");
Matcher[] commandMatchers = new Matcher[19];
commandMatchers[0] = patternForExit.matcher(command);
commandMatchers[1] = patternForShowCurrentMenu.matcher(command);
commandMatchers[2] = patternForCreateUser1.matcher(command);
commandMatchers[3] = patternForCreateUser2.matcher(command);
commandMatchers[4] = patternForCreateUser3.matcher(command);
commandMatchers[5] = patternForCreateUser4.matcher(command);
commandMatchers[6] = patternForCreateUser5.matcher(command);
commandMatchers[7] = patternForCreateUser6.matcher(command);
commandMatchers[8] = patternForCreateUser7.matcher(command);
commandMatchers[9] = patternForCreateUser8.matcher(command);
commandMatchers[10] = patternForCreateUser9.matcher(command);
commandMatchers[11] = patternForCreateUser10.matcher(command);
commandMatchers[12] = patternForCreateUser11.matcher(command);
commandMatchers[13] = patternForCreateUser12.matcher(command);
commandMatchers[14] = patternForLoginUser1.matcher(command);
commandMatchers[15] = patternForLoginUser2.matcher(command);
commandMatchers[16] = patternForLoginUser3.matcher(command);
commandMatchers[17] = patternForLoginUser4.matcher(command);
commandMatchers[18] = patternForEnterAnotherMenu.matcher(command);
return commandMatchers;
}
}
package server.controller;
import org.model.BattlePhase;
import org.model.Card;
import org.model.MonsterCard;
import org.model.Player;
import org.model.enums.MonsterCardPosition;
import org.model.enums.RecentActionsInGame;
import org.view.BattlePhaseView;
import org.view.GameView;
public class BattlePhaseController extends PhaseController {
GameView gameView;
private BattlePhaseView battleView = new BattlePhaseView(this);
public BattlePhaseController(BattlePhase battlePhase) {
super(battlePhase);
}
public BattlePhaseController(BattlePhase battlePhase, GameView view){
super(battlePhase);
this.gameView = view;
}
public GameView getGameView() {
return gameView;
}
protected void controlSummonCommand() {
Player player = phase.getPlayerByTurn();
if (!player.hasSelectedCard()) {
gameView.noCardSelectedYet();
} else if (!player.isSelectedCardFromHand() || !(player.getSelectedCard() instanceof MonsterCard)) {
gameView.canNotSummonCard();
} else {
gameView.actionNotAllowedInThisPhase();
}
}
protected void controlSetCommand() {
Player player = phase.getPlayerByTurn();
if (!player.hasSelectedCard()) {
gameView.noCardSelectedYet();
} else if (!player.isSelectedCardFromHand()) {
gameView.canNotSetCard();
} else {
gameView.canNotDoThisActionInThisPhase();
}
}
protected void controlSetPositionAttackCommand() {
controlSetPositionAttackOrDefenseOrFlipSummonCommand();
}
protected void controlSetPositionDefenseCommand() {
controlSetPositionAttackOrDefenseOrFlipSummonCommand();
}
protected void controlFlipSummonCommand() {
controlSetPositionAttackOrDefenseOrFlipSummonCommand();
}
private void controlSetPositionAttackOrDefenseOrFlipSummonCommand() {
Player player = phase.getPlayerByTurn();
if (!player.hasSelectedCard()) {
gameView.noCardSelectedYet();
} else if (!player.isSelectedCardFromMonsterCardZone()) {
gameView.printString("Can not change this card's position");
} else {
gameView.canNotDoThisActionInThisPhase();
}
}
protected void controlAttackDirectCommand() {
Player player = phase.getPlayerByTurn();
Player rivalPlayer = phase.getRivalPlayerByTurn();
if (!player.hasSelectedCard()) {
gameView.noCardSelectedYet();
} else if (!player.isSelectedCardFromMonsterCardZone()) {
gameView.canNotAttackWithThisCard();
} else if (player.getSelectedCard() instanceof MonsterCard && ((MonsterCard) player.getSelectedCard()).getPosition() != MonsterCardPosition.OFFENSIVE_OCCUPIED) {
gameView.canNotAttackWithThisCard();
} else if (player.getSelectedCard() instanceof MonsterCard && ((MonsterCard) player.getSelectedCard()).hasBattledInBattlePhase()) {
gameView.thisCardAlreadyAttacked();
} else if (rivalPlayer.getMonsterCardsInZone().size() > 0) {
gameView.canNotAttackDirectly();
} else if (phase.getTurnsPlayed() < 1) {
gameView.youCanNotAttackInYourFirstTurn();
} else if (phase instanceof BattlePhase) {
Card attackerCard = player.getSelectedCard();
checkForPossibleSpellOrTrapEffect(attackerCard, null, RecentActionsInGame.RIVAL_DECLARED_A_BATTLE);
if (((MonsterCard) attackerCard).isCardActionCanceledByAnEffect()) {
((MonsterCard) attackerCard).setCardActionCanceledByAnEffect(false);
return;
}
int damage = ((BattlePhase) phase).attackDirect((MonsterCard) attackerCard);
runAllMonsterPowersInZone(player);
gameView.attackDirectResult(damage);
player.setSelectedCard(null);
}
}
protected void controlActivateEffectCommand() {
Player player = phase.getPlayerByTurn();
if (!player.hasSelectedCard()) {
gameView.noCardSelectedYet();
} else if (player.getSelectedCard() instanceof MonsterCard) {
gameView.printString("Activation is only for spell/trap cards");
} else {
gameView.canNotActivateOnThisTurn();
}
}
protected void controlAttackToCardCommand(int location) {
Player player = phase.getPlayerByTurn();
Player rivalPlayer = phase.getRivalPlayerByTurn();
if (!player.hasSelectedCard()) {
gameView.noCardSelectedYet();
} else if (!player.isSelectedCardFromMonsterCardZone()) {
gameView.canNotAttackWithThisCard();
} else if (player.getSelectedCard() instanceof MonsterCard && ((MonsterCard) player.getSelectedCard()).hasBattledInBattlePhase()) {
gameView.thisCardAlreadyAttacked();
} else if (!rivalPlayer.doesHaveMonsterCardInThisLocation(location)) {
gameView.thereIsNoCardToAttackHere();
} else if (phase.getTurnsPlayed() < 1) {
gameView.youCanNotAttackInYourFirstTurn();
} else {
Card attackerCard = player.getSelectedCard();
MonsterCard defenderCard = rivalPlayer.getMonsterCardsInZone().get(location);
if(defenderCard.getPosition() == MonsterCardPosition.DEFENSIVE_HIDDEN){
defenderCard.setPosition(MonsterCardPosition.DEFENSIVE_OCCUPIED);
defenderCard.setFlipped(true);
}
monsterPowers.run(defenderCard, (MonsterCard) attackerCard, 0);
defenderCard.setFlipped(false);
checkForPossibleSpellOrTrapEffect(attackerCard, defenderCard, RecentActionsInGame.RIVAL_DECLARED_A_BATTLE);
if (((MonsterCard) attackerCard).isCardActionCanceledByAnEffect()) {
((MonsterCard) attackerCard).setCardActionCanceledByAnEffect(false);
return;
}
gameView.printString(((BattlePhase) phase).attackToCardAndReturnAttackReport(location, monsterPowers,(MonsterCard) attackerCard));
runAllMonsterPowersInZone(player);
runAllMonsterPowersInZone(rivalPlayer);
player.setSelectedCard(null);
}
}
}
package server.controller;
import org.model.*;
import org.view.CreateNewCardView;
public class CreateNewCardController {
private CreateNewCardView view;
private CreateNewCard model;
private User user;
public CreateNewCardController(User user) {
view = new CreateNewCardView(this);
model = new CreateNewCard(this);
this.user = user;
}
public void creationCommission(int price) {
user.changeBalance(-1 * (price / 10));
}
public static void addNewMonster(MonsterCard card) {
CreateNewCard.newMonsters.add(card);
addNewCard(card);
}
public static void addNewSpell(SpellCard card) {
CreateNewCard.newSpells.add(card);
addNewCard(card);
}
public static void addNewTrap(TrapCard card) {
CreateNewCard.newTraps.add(card);
addNewCard(card);
}
public static void addNewCard(Card card){
CreateNewCard.newCards.add(card);
}
public void run() {
view.run();
}
public CreateNewCardView getView() {
return view;
}
public CreateNewCard getModel() {
return model;
}
public User getUser() {
return user;
}
}
package server.controller;
import org.model.Card;
import org.model.Deck;
import org.model.User;
import org.view.DeckMenuView;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DeckMenuController {
private User user;
DeckMenuView view = new DeckMenuView(this);
public DeckMenuController(User user) {
this.user = user;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public void run() throws Exception {
view.run();
}
public MenuEnum processCommand (String command){
String cardName = "cardName";
String deckName = "deckName";
Matcher[] commandMatchers = getCommandMatchers(command);
if (commandMatchers[0].find()) {
controlCreateCommand(commandMatchers[0].group(1));
} else if (commandMatchers[1].find()) {
controlDeleteCommand(commandMatchers[1].group(1));
} else if (commandMatchers[2].find()) {
controlSetActivateCommand(commandMatchers[2].group(1));
} else if (commandMatchers[3].find() || commandMatchers[4].find()) {
view.print(user.decksArrayListToString());
} else if (commandMatchers[5].find() || commandMatchers[6].find()) {
view.print(Card.cardsArrayListToString(user.getAllCards()));
} else if (commandMatchers[7].find()) {
return MenuEnum.BACK;
} else if (commandMatchers[8].find()) {
view.showCurrentMenu();
} else {
for(int i=9; i<21; i++){
if(commandMatchers[i].find()){
controlAddCardCommand(commandMatchers[i].group(cardName), commandMatchers[i].group(deckName), true);
return MenuEnum.CONTINUE;
}
}
for (int i=21; i<25; i++){
if(commandMatchers[i].find()){
controlAddCardCommand(commandMatchers[i].group(cardName), commandMatchers[i].group(deckName), false);
return MenuEnum.CONTINUE;
}
}
for (int i=25; i<37; i++){
if(commandMatchers[i].find()){
controlRemoveCardCommand(commandMatchers[i].group(cardName), commandMatchers[i].group(deckName), true);
return MenuEnum.CONTINUE;
}
}
for (int i=37; i<41; i++){
if(commandMatchers[i].find()){
controlRemoveCardCommand(commandMatchers[i].group(cardName), commandMatchers[i].group(deckName), false);
return MenuEnum.CONTINUE;
}
}
for (int i=41; i<45; i++){
if(commandMatchers[i].find()){
controlShowOneDeckCommand(commandMatchers[i].group(deckName), true);
return MenuEnum.CONTINUE;
}
}
for (int i=45; i<47; i++){
if(commandMatchers[i].find()){
controlShowOneDeckCommand(commandMatchers[i].group(deckName), false);
return MenuEnum.CONTINUE;
}
}
if(commandMatchers[47].find()){
view.impossibleMenuNavigation();
} else {
view.invalidCommand();
}
}
return MenuEnum.CONTINUE;
}
private void controlShowOneDeckCommand(String deckName, boolean isSideDeck) {
if (!user.doesUserHaveThisDeck(deckName)) {
view.deckDoesNotExist(deckName);
} else {
view.print(user.getDeckByDeckName(deckName).showDeck(isSideDeck));
}
}
private void controlCreateCommand(String deckName) {
if (user.doesUserHaveThisDeck(deckName)) {
view.thisDeckAlreadyExists(deckName);
} else {
new Deck(deckName, user.getUsername());
view.deckCreated();
}
}
private void controlDeleteCommand(String deckName) {
if (!user.doesUserHaveThisDeck(deckName)) {
view.deckDoesNotExist(deckName);
} else {
if(user.getDeckByDeckName(deckName).equals(user.getActiveDeck())){
user.setActiveDeck(null);
}
user.getDecks().remove(user.getDeckByDeckName(deckName));
view.deckDeleted();
}
}
private void controlSetActivateCommand(String deckName) {
if (!user.doesUserHaveThisDeck(deckName)) {
view.deckDoesNotExist(deckName);
} else {
user.setActiveDeck(user.getDeckByDeckName(deckName));
view.deckActivated();
}
}
public void controlAddCardCommand(String cardName, String deckName, boolean isToSideDeck) {
if (user.numOfCardsWithThisName(cardName) == 0) {
view.cardDoesNotExist(cardName);
return;
} else if (!user.doesUserHaveThisDeck(deckName)) {
view.deckDoesNotExist(deckName);
return;
}
Deck deck = user.getDeckByDeckName(deckName);
if (!user.isAmountOfThisCardEnough(deck, cardName)) {
view.cardDoesNotExist(cardName);
} else if (!isToSideDeck && deck.isMainDeckFull()) {
view.mainOrSideDeckIsFull(false);
} else if (isToSideDeck && deck.isSideDeckFull()) {
view.mainOrSideDeckIsFull(true);
} else if (deck.doesDeckHaveThreeCardsFromThisTypeOfCard(cardName)) {
view.threeSimilarCardInADeck(cardName, deckName);
} else {
deck.addCardToDeck(cardName, isToSideDeck);
view.cardAddedToDeck();
}
}
public void controlRemoveCardCommand(String cardName, String deckName, boolean isFromSideDeck) {
if (!user.doesUserHaveThisDeck(deckName)) {
view.deckDoesNotExist(deckName);
return;
}
Deck deck = user.getDeckByDeckName(deckName);
if (isFromSideDeck && !deck.isThisCardInSideOrMainDeck(cardName, true)) {
view.cardDoesNotExistInSideOrMainDeck(cardName, true);
} else if (!isFromSideDeck && !deck.isThisCardInSideOrMainDeck(cardName, false)) {
view.cardDoesNotExistInSideOrMainDeck(cardName, false);
} else {
deck.removeCardFromDeck(cardName, isFromSideDeck);
view.cardRemovedFromDeck();
}
}
private Matcher[] getCommandMatchers(String command) {
Pattern patternForDeckCreate = Pattern.compile("^deck create (.+)$");
Pattern patternForDeckDelete = Pattern.compile("^deck delete (.+)$");
Pattern patternForSetActivateDeck = Pattern.compile("^deck set-activate (.+)$");
Pattern patternForDeckShowAll = Pattern.compile("^deck show --all$");
Pattern patternForDeckShowAll2 = Pattern.compile("^deck show -a$");
Pattern patternForDeckShowCards = Pattern.compile("^deck show --cards$");
Pattern patternForDeckShowCards2 = Pattern.compile("^deck show -c$");
Pattern patternForMenuExit = Pattern.compile("^menu exit$");
Pattern patternForShowCurrentMenu = Pattern.compile("^menu show-current$");
Pattern patternForAddCardToSideDeck1 = Pattern.compile("^deck add-card --card (?<cardName>.+?) --deck (?<deckName>.+?) --side$");
Pattern patternForAddCardToSideDeck2 = Pattern.compile("^deck add-card --deck (?<deckName>.+?) --card (?<cardName>.+?) --side$");
Pattern patternForAddCardToSideDeck3 = Pattern.compile("^deck add-card --card (?<cardName>.+?) --side --deck (?<deckName>.+?)$");
Pattern patternForAddCardToSideDeck4 = Pattern.compile("^deck add-card --deck (?<deckName>.+?) --side --card (?<cardName>.+?)$");
Pattern patternForAddCardToSideDeck5 = Pattern.compile("^deck add-card --side --card (?<cardName>.+?) --deck (?<deckName>.+?)$");