Commit 2e7e7f14 authored by mohammadmahdihn's avatar mohammadmahdihn
Browse files

Shop 5

parent 56ebdf0b
......@@ -4,14 +4,12 @@
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="f6e2b2ce-4ca9-4617-8f8b-40311e2222f4" name="Default Changelist" comment="Shop 3">
<change afterPath="$PROJECT_DIR$/client/src/main/java/org/model/Admin.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/server/src/main/java/org/model/Admin.java" afterDir="false" />
<list default="true" id="f6e2b2ce-4ca9-4617-8f8b-40311e2222f4" name="Default Changelist" comment="Shop 4">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/client/src/main/java/org/controller/ShopController.java" beforeDir="false" afterPath="$PROJECT_DIR$/client/src/main/java/org/controller/ShopController.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/client/src/main/java/org/view/MainMenuView.java" beforeDir="false" afterPath="$PROJECT_DIR$/client/src/main/java/org/view/MainMenuView.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/client/src/main/java/org/view/ShopView.java" beforeDir="false" afterPath="$PROJECT_DIR$/client/src/main/java/org/view/ShopView.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/client/src/main/resources/mainclass/FXML/shopMenu.fxml" beforeDir="false" afterPath="$PROJECT_DIR$/client/src/main/resources/mainclass/FXML/shopMenu.fxml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/server/src/main/java/org/MainServer.java" beforeDir="false" afterPath="$PROJECT_DIR$/server/src/main/java/org/MainServer.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/server/src/main/java/org/model/AdminCardFields.java" beforeDir="false" afterPath="$PROJECT_DIR$/server/src/main/java/org/model/AdminCardFields.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/server/src/main/java/org/serverController/ShopMenuController.java" beforeDir="false" afterPath="$PROJECT_DIR$/server/src/main/java/org/serverController/ShopMenuController.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
......@@ -95,7 +93,7 @@
<workItem from="1626795228400" duration="1311000" />
<workItem from="1626802289754" duration="2666000" />
<workItem from="1626805568591" duration="564000" />
<workItem from="1626812924370" duration="1666000" />
<workItem from="1626812924370" duration="6964000" />
</task>
<task id="LOCAL-00001" summary="gaame avval">
<created>1626533469040</created>
......@@ -153,7 +151,14 @@
<option name="project" value="LOCAL" />
<updated>1626805603392</updated>
</task>
<option name="localTasksCounter" value="9" />
<task id="LOCAL-00009" summary="Shop 4">
<created>1626814649274</created>
<option name="number" value="00009" />
<option name="presentableId" value="LOCAL-00009" />
<option name="project" value="LOCAL" />
<updated>1626814649274</updated>
</task>
<option name="localTasksCounter" value="10" />
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
......@@ -180,6 +185,7 @@
<MESSAGE value="logout" />
<MESSAGE value="Shop 1" />
<MESSAGE value="Shop 3" />
<option name="LAST_COMMIT_MESSAGE" value="Shop 3" />
<MESSAGE value="Shop 4" />
<option name="LAST_COMMIT_MESSAGE" value="Shop 4" />
</component>
</project>
\ No newline at end of file
......@@ -6,6 +6,7 @@ import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBar;
import javafx.scene.control.Label;
import javafx.scene.effect.DropShadow;
import javafx.scene.image.ImageView;
......@@ -18,14 +19,16 @@ import javafx.scene.paint.ImagePattern;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import org.MainClient;
import org.controller.CreateNewCardController;
import org.controller.LoginMenuController;
import org.controller.MainMenuController;
import org.controller.ShopController;
import org.model.*;
import javax.swing.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class ShopView extends Application {
......@@ -51,7 +54,11 @@ public class ShopView extends Application {
private Button createCard;
@FXML
private Text description;
Scanner scanner = ScannerInstance.getInstance().getScanner();
@FXML
private Text amountForAdmin;
@FXML
private ButtonBar adminButtonBar;
ShopController controller;
String selectedCardName;
......@@ -94,6 +101,7 @@ public class ShopView extends Application {
rectangle.setOnMouseClicked(new EventHandler<>() {
@Override
public void handle(MouseEvent mouseEvent) {
showCardDetailsForAdmin(cardAndImage.getCardName());
cardImage.setImage(cardAndImage.getImage());
ownedNumber.setText("You have : " + controller.getUser().numOfCardsWithThisName(cardAndImage.getCardName()) + " card of this type");
selectedCardName = cardAndImage.getCardName();
......@@ -123,6 +131,61 @@ public class ShopView extends Application {
}
}
private void showCardDetailsForAdmin(String cardName) {
if(controller.getUser() instanceof Admin){
try {
String details = (String) LoginMenuController.sendAndReceive("get card details --token " + MainClient.getToken() + " --card " + cardName);
amountForAdmin.setText(details);
adminButtonBar.getButtons().clear();
Button changeAmountButton = new Button("Change amount");
changeAmountButton.setOnMouseClicked(mouseEvent1 -> {
String result = JOptionPane.showInputDialog("Enter amount: ");
try {
int newAmount = Integer.parseInt(result);
if(newAmount < 0){
JOptionPane.showMessageDialog(null, "Invalid input");
} else {
try {
LoginMenuController.sendAndReceive("set card amount --token " + MainClient.getToken() + " --card " + cardName + " --amount " + newAmount);
} catch (IOException exception) {
exception.printStackTrace();
}
showCardDetailsForAdmin(cardName);
}
} catch(NumberFormatException e){
JOptionPane.showMessageDialog(null, "Invalid input");
}
});
adminButtonBar.getButtons().add(changeAmountButton);
if(details.endsWith("Available")) {
Button makeUnavailable = new Button("Make unavailable");
makeUnavailable.setOnMouseClicked(mouseEvent1 -> {
try {
LoginMenuController.sendAndReceive("set card unavailable --token " + MainClient.getToken() + " --card " + cardName);
} catch (IOException exception) {
exception.printStackTrace();
}
showCardDetailsForAdmin(cardName);
});
adminButtonBar.getButtons().add(makeUnavailable);
} else if (details.endsWith("Unavailable")) {
Button makeAvailable = new Button("Make available");
makeAvailable.setOnMouseClicked(mouseEvent1 -> {
try {
LoginMenuController.sendAndReceive("set card available --token " + MainClient.getToken() + " --card " + cardName);
} catch (IOException exception) {
exception.printStackTrace();
}
showCardDetailsForAdmin(cardName);
});
adminButtonBar.getButtons().add(makeAvailable);
}
} catch (IOException exception) {
exception.printStackTrace();
}
}
}
private String abbasInitiator() {
int random = (int)Math.floor(Math.random()*(4)+2);
switch (random){
......@@ -159,6 +222,7 @@ public class ShopView extends Application {
resultOfPurchase.setText("Please select a card first");
} else {
controller.buyCard(selectedCardName);
showCardDetailsForAdmin(selectedCardName);
money.setText("Your current balance is : " + controller.getUser().getBalance());
ownedNumber.setText("You have : " + controller.getUser().numOfCardsWithThisName(selectedCardName) + " card of this type");
if (Card.getPrices().get(selectedCardName) > controller.getUser().getBalance()) {
......
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.String?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<AnchorPane fx:id="parent" prefHeight="720.0" prefWidth="1280.0" styleClass="anchor" stylesheets="@../menuStyles.css" xmlns="http://javafx.com/javafx/16" xmlns:fx="http://javafx.com/fxml/1">
<AnchorPane fx:id="parent" prefHeight="720.0" prefWidth="1280.0" styleClass="anchor" stylesheets="@../menuStyles.css" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
<children>
<ScrollPane fx:id="cardsScrollBar" layoutX="145.0" layoutY="63.0" prefHeight="547.0" prefWidth="194.0">
<content>
......@@ -72,5 +67,11 @@
<Font size="15.0" />
</font>
</Text>
<Text fx:id="amountForAdmin" fill="WHITE" layoutX="386.0" layoutY="279.0" strokeType="OUTSIDE" strokeWidth="0.0" wrappingWidth="194.39999999999998">
<font>
<Font name="thewitcher" size="19.0" />
</font>
</Text>
<ButtonBar fx:id="adminButtonBar" layoutX="669.0" layoutY="263.0" prefHeight="40.0" prefWidth="200.0" />
</children>
</AnchorPane>
......@@ -21,7 +21,6 @@ public class MainServer {
private static final LoginMenuController LOGIN_MENU_CONTROLLER = new LoginMenuController();
private static final ScoreBoardController SCORE_BOARD_CONTROLLER = new ScoreBoardController();
private static final MainMenuController MAIN_MENU_CONTROLLER = new MainMenuController();
private static final ShopMenuController SHOP_MENU_CONTROLLER = new ShopMenuController();
private ServerSocket serverSocket;
public static void main(String[] args) {
......@@ -95,7 +94,7 @@ public class MainServer {
while (true) {
String input = dataInputStream.readUTF();
Object result = process(input);
if(result == null) break;
if (result == null) break;
objectOutputStream.reset();
objectOutputStream.writeObject(result);
objectOutputStream.flush();
......@@ -103,7 +102,7 @@ public class MainServer {
dataInputStream.close();
objectOutputStream.close();
socket.close();
} catch (SocketException | EOFException e){
} catch (SocketException | EOFException e) {
System.out.println("Client disconnected");
CLIENTS.remove(socket);
} catch (Exception e) {
......@@ -115,26 +114,56 @@ public class MainServer {
private static Object process(String input) {
Matcher[] matchers = getCommandMatchers(input);
if (matchers[0].find()) {
return LOGIN_MENU_CONTROLLER.controlCreateUserCommand(matchers[0].group("username"), matchers[0].group("password"), matchers[0].group("nickname"));
return LOGIN_MENU_CONTROLLER.controlCreateUserCommand(matchers[0].group("username"), matchers[0].group("password"), matchers[0].group("nickname"));
} else if (matchers[1].find()) {
return LOGIN_MENU_CONTROLLER.controlLoginUserCommand(matchers[1].group("username"), matchers[1].group("password"));
} else if (matchers[2].find()) {
User user = getUserByToken(matchers[2].group("token"));
if(user == null) {
if (user == null) {
return "";
} else {
return user;
}
}
else if(matchers[3].find()){
if(TOKENS.containsKey(matchers[3].group("token"))) {
} else if (matchers[3].find()) {
if (TOKENS.containsKey(matchers[3].group("token"))) {
return SCORE_BOARD_CONTROLLER.returnSortedUsers();
} else {
return "Authentication error";
}
} else if(matchers[4].find()) {
} else if (matchers[4].find()) {
return MAIN_MENU_CONTROLLER.logout(matchers[4].group("token"));
} else if(matchers[5].find()) {
if(TOKENS.containsKey(matchers[5].group("token"))) {
return SHOP_MENU_CONTROLLER.sellCard(matchers[5].group("cardName"), getUserByToken(matchers[5].group("token")));
} else if (matchers[5].find()) {
if (TOKENS.containsKey(matchers[5].group("token"))) {
return ShopMenuController.sellCard(matchers[5].group("cardName"), getUserByToken(matchers[5].group("token")));
} else {
return "Authentication error";
}
} else if (matchers[6].find()) {
if (TOKENS.containsKey(matchers[6].group("token")) && getUserByToken(matchers[6].group("token")) instanceof Admin) {
return ShopMenuController.cardDetailsToString(matchers[6].group("cardName"));
} else {
return "Authentication error";
}
} else if (matchers[7].find()) {
if (TOKENS.containsKey(matchers[7].group("token")) && getUserByToken(matchers[7].group("token")) instanceof Admin) {
ShopMenuController.getCardsAdminFields().get(matchers[7].group("cardName")).setAmount(Integer.parseInt(matchers[7].group("amount")));
return "Successful";
} else {
return "Authentication error";
}
} else if (matchers[8].find()) {
if (TOKENS.containsKey(matchers[8].group("token")) && getUserByToken(matchers[8].group("token")) instanceof Admin) {
ShopMenuController.getCardsAdminFields().get(matchers[8].group("cardName")).setCardAvailable(true);
return "Successful";
} else {
return "Authentication error";
}
} else if (matchers[9].find()) {
if (TOKENS.containsKey(matchers[9].group("token")) && getUserByToken(matchers[9].group("token")) instanceof Admin) {
ShopMenuController.getCardsAdminFields().get(matchers[9].group("cardName")).setCardAvailable(false);
return "Successful";
} else {
return "Authentication error";
}
}
return "invalid";
......@@ -148,6 +177,10 @@ public class MainServer {
Pattern patternForSortedUsers = Pattern.compile("^get sorted users (?<token>.+)$");
Pattern patternForLogout = Pattern.compile("^user logout (?<token>.+?)$");
Pattern patternForBuyCard = Pattern.compile("^shop buy --token (?<token>.+?) --card (?<cardName>.+?)$");
Pattern patternForGetCardDetails = Pattern.compile("^get card details --token (?<token>.+?) --card (?<cardName>.+?)$");
Pattern patternForSetCardAmount = Pattern.compile("^set card amount --token (?<token>.+?) --card (?<cardName>.+?) --amount (?<amount>\\d+)$");
Pattern patternForMakeCardAvailable = Pattern.compile("^set card available --token (?<token>.+?) --card (?<cardName>.+?)$");
Pattern patternForMakeCardUnavailable = Pattern.compile("^set card unavailable --token (?<token>.+?) --card (?<cardName>.+?)$");
Matcher[] commandMatchers = new Matcher[15];
commandMatchers[0] = patternForCreateUser1.matcher(command);
commandMatchers[1] = patternForLoginUser1.matcher(command);
......@@ -155,12 +188,16 @@ public class MainServer {
commandMatchers[3] = patternForSortedUsers.matcher(command);
commandMatchers[4] = patternForLogout.matcher(command);
commandMatchers[5] = patternForBuyCard.matcher(command);
commandMatchers[6] = patternForGetCardDetails.matcher(command);
commandMatchers[7] = patternForSetCardAmount.matcher(command);
commandMatchers[8] = patternForMakeCardAvailable.matcher(command);
commandMatchers[9] = patternForMakeCardUnavailable.matcher(command);
return commandMatchers;
}
public static User getUserByToken(String token) {
User user = User.getUserByUsername(TOKENS.get(token));
if(user == null){
if (user == null) {
System.out.println("User was null");
}
return user;
......
Supports Markdown
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