Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
kasraabdollahi committed Jul 9, 2019
2 parents 90ba52e + d698dfd commit e2c7d20
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 45 deletions.
2 changes: 1 addition & 1 deletion src/controllers/console/AccountMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ private static void showLeaderBoard() {
sortAccounts();
for (int i = 0; i < accounts.size(); i++) {
MyPrinter.purple(i + 1 + "-UserName:" + accounts.get(i).getUserName() +
"-Wins:" + accounts.get(i).numOfWin());
"-Wins:" + accounts.get(i).getWins());
}
}

Expand Down
15 changes: 7 additions & 8 deletions src/controllers/graphical/BattleController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.gilecode.yagson.YaGson;
import controllers.console.AccountMenu;
import controllers.console.BattleMenu;
import controllers.console.MainMenu;
import javafx.animation.Animation;
import javafx.animation.AnimationTimer;
import javafx.animation.TranslateTransition;
Expand All @@ -24,7 +25,6 @@
import javafx.scene.paint.Paint;
import javafx.scene.shape.Circle;
import javafx.util.Duration;
import models.Account;
import models.battle.*;
import models.battle.board.Board;
import models.battle.board.Location;
Expand All @@ -42,7 +42,6 @@
import network.Requests.battle.MatchResultRequest;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;

import static models.battle.BattleActionType.*;
Expand Down Expand Up @@ -79,7 +78,6 @@ public class BattleController {
private GraphicButton specialPower;
private ManaViewer[] manaViewers;
private GraveYard graveYard;
private Account loginAccount = AccountMenu.getLoginAccount();
private YaGson yaGson = new YaGson();
private boolean storyMod;

Expand Down Expand Up @@ -660,7 +658,7 @@ public void endTurnRequest(){

private void doAndSendBattleAction(BattleAction battleAction) {
doOneAction(battleAction);
BattleActionRequest battleActionRequest = new BattleActionRequest(loginAccount.getAuthToken(), getUserNameOfOpponent(), battleAction);
BattleActionRequest battleActionRequest = new BattleActionRequest(AccountMenu.getLoginAccount().getAuthToken(), getUserNameOfOpponent(), battleAction);
Client.getWriter().println(yaGson.toJson(battleActionRequest));
Client.getWriter().flush();
}
Expand Down Expand Up @@ -851,7 +849,7 @@ public void end() {
MyAlert myAlert = new MyAlert(string);
myAlert.setSpeeds(6000.0, 5.0);
myAlert.setOnfinishEvent(event -> {
Client.getStage().getScene().setRoot(BattleMenu.getRoot());
Client.getStage().getScene().setRoot(MainMenu.getRoot());
});
myAlert.setMiddleEventHandler(event -> {
//rainShit();
Expand All @@ -868,10 +866,11 @@ private void sendMatchResult() {
MatchResult result = battle.getMatchResult();
if (result==null)return;
result.setBattleHistory(lastBattleHistory);
loginAccount.getMatchHistory().add(result);
AccountMenu.getLoginAccount().getMatchHistorys().add(result);
YaGson yaGson = new YaGson();
try {
MatchResultRequest matchResultRequest = new MatchResultRequest(loginAccount.getAuthToken(), result);
MatchResultRequest matchResultRequest =
new MatchResultRequest(AccountMenu.getLoginAccount().getAuthToken(), result);
Client.getWriter().println(yaGson.toJson(matchResultRequest));
Client.getWriter().flush();
} catch (Exception e) {
Expand Down Expand Up @@ -923,7 +922,7 @@ private void analyseMatchResult() {
}
if (win) {
AccountMenu.getLoginAccount().addStoryLvl();
UpdateAccountRequest updateAccountRequest = new UpdateAccountRequest(loginAccount);
UpdateAccountRequest updateAccountRequest = new UpdateAccountRequest(AccountMenu.getLoginAccount());
String yaJson1 = yaGson.toJson(updateAccountRequest);
Client.getWriter().println(yaJson1);
Client.getWriter().flush();
Expand Down
7 changes: 2 additions & 5 deletions src/controllers/graphical/MatchHistoryController.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package controllers.graphical;

import com.gilecode.yagson.YaGson;
import com.jfoenix.controls.JFXButton;
import controllers.console.AccountMenu;
import controllers.console.MainMenu;
Expand All @@ -17,12 +16,9 @@
import models.battle.board.Board;
import network.Client;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
import java.util.Scanner;

public class MatchHistoryController implements Initializable {
Account loginAccount = AccountMenu.getLoginAccount();
Expand All @@ -31,7 +27,7 @@ public class MatchHistoryController implements Initializable {

@Override
public void initialize(URL location, ResourceBundle resources) {
ArrayList<MatchResult> matchResults = loginAccount.getMatchHistory();
ArrayList<MatchResult> matchResults = loginAccount.getMatchHistorys();
int i = 1;
for (MatchResult matchResult : matchResults){
String userName1 = matchResult.getUser0();
Expand All @@ -49,6 +45,7 @@ public void initialize(URL location, ResourceBundle resources) {
gridPane.add(new Label(userName2), 1, i);
gridPane.add(new Label(winner), 2, i);
gridPane.add(reviewButton, 3, i);
i++;
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/controllers/graphical/ScoreBoardController.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import controllers.console.MainMenu;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
Expand Down Expand Up @@ -36,4 +37,9 @@ public void initialize(URL url, ResourceBundle resourceBundle) {
public void back(MouseEvent mouseEvent) {
Client.getStage().getScene().setRoot(MainMenu.getRoot());
}
public void addDetails(Account account, int row){
gridPane.add(new Label(account.getUserName()), 0, row);
gridPane.add(new Label(Integer.toString(account.getWins())), 1, row);
gridPane.add(new Label(Integer.toString(account.getLoses())), 2, row);
}
}
7 changes: 3 additions & 4 deletions src/layouts/scoreBoard.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@
<RowConstraints minHeight="10.0" prefHeight="50.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label styleClass="title-label" text="Thing Name" GridPane.halignment="CENTER" />
<Label styleClass="title-label" text="Price" GridPane.columnIndex="1" GridPane.halignment="CENTER" />
<Label styleClass="title-label" text="Type" GridPane.columnIndex="2" GridPane.halignment="CENTER" />
<Label styleClass="title-label" text="Inventory" GridPane.columnIndex="3" GridPane.halignment="CENTER" />
<Label styleClass="title-label" text="Player name" GridPane.halignment="CENTER" />
<Label styleClass="title-label" text="wins" GridPane.columnIndex="1" GridPane.halignment="CENTER" />
<Label styleClass="title-label" text="loses" GridPane.columnIndex="2" GridPane.halignment="CENTER" />
</children>
<opaqueInsets>
<Insets />
Expand Down
64 changes: 39 additions & 25 deletions src/models/Account.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,17 @@
import models.item.Item;
import views.MyPrinter;

import java.io.IOException;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;

public class Account implements Cloneable {
private String userName;
private int money;
private String password;
private transient String authToken;

private ArrayList<MatchResult> matchHistory;
private ArrayList<MatchResult> matchHistorys;

private ArrayList<Card> cards;
private ArrayList<Item> items;
Expand All @@ -34,7 +32,7 @@ public Account(String userName, String password) {
this.userName = new String(userName);
this.money = 300_000;
storyLvl = 1;
this.matchHistory = new ArrayList<MatchResult>();
this.matchHistorys = new ArrayList<MatchResult>();
cards = new ArrayList<>();
items = new ArrayList<>();
this.decks = new ArrayList<Deck>();
Expand Down Expand Up @@ -74,6 +72,32 @@ private static String hexadecimalCharacter(int code) {
return Character.toString(ch);
}

public int getWins() {
int count = 0;
for (MatchResult matchResult : this.matchHistorys) {
if (matchResult.getUser0().equals(userName)) {
if (matchResult.getWinner() == 0) count++;
}
if (matchResult.getUser1().equals(userName)) {
if (matchResult.getWinner() == 1) count++;
}
}
return count;
}

public int getLoses(){
int count = 0;
for (MatchResult matchResult : this.matchHistorys) {
if (matchResult.getUser1().equals(userName)) {
if (matchResult.getWinner() == 0) count++;
}
if (matchResult.getUser0().equals(userName)) {
if (matchResult.getWinner() == 1) count++;
}
}
return count;
}

public void setAuthToken(String authToken) {
this.authToken = authToken;
}
Expand All @@ -86,8 +110,11 @@ public int getStoryLvl() {
return storyLvl;
}

public ArrayList<MatchResult> getMatchHistory() {
return matchHistory;
public ArrayList<MatchResult> getMatchHistorys() {
if (matchHistorys==null){
matchHistorys= new ArrayList<>();
}
return matchHistorys;
}

public Deck getMainDeck() {
Expand Down Expand Up @@ -119,25 +146,12 @@ public void buy(int value) {
}

public void addMatchResult(MatchResult matchResult) {
this.matchHistory.add(matchResult);
}

public int numOfWin() {
int count = 0;
for (MatchResult matchResult : this.matchHistory) {
if (matchResult.getUser0().equals(userName)) {
if (matchResult.getWinner() == 0) count++;
}
if (matchResult.getUser1().equals(userName)) {
if (matchResult.getWinner() == 1) count++;
}
}
return count;
this.matchHistorys.add(matchResult);
}

public boolean compare(Account account) {
if (this.numOfWin() > account.numOfWin()) return true;
if (this.numOfWin() < account.numOfWin()) return false;
if (this.getWins() > account.getWins()) return true;
if (this.getLoses() < account.getLoses()) return false;
if (this.userName.compareTo(account.userName) < 0) return true;
return false;
}
Expand All @@ -156,7 +170,7 @@ public void setPassword(String password) {
}

public Player makePlayer(int playerNum) {
Deck deck =this.mainDeck.clone();
Deck deck = this.mainDeck.clone();
Player player = new Player(playerNum, this.userName, deck, true);
return player;
}
Expand Down Expand Up @@ -195,7 +209,7 @@ public boolean sellCardOrItem(int code) {
cards.remove(card);
moneyRise(card.getPrice());
for (Card card1 : Shop.getInstance().getCards().keySet()) {
if (card1.getName().equals(card.getName())){
if (card1.getName().equals(card.getName())) {
Shop.getInstance().getCards().replace(card1, Shop.getInstance().getCards().get(card1) + 1);
break;
}
Expand All @@ -212,7 +226,7 @@ public boolean sellCardOrItem(int code) {
items.remove(item);
moneyRise(item.getCode());
for (Item item1 : Shop.getInstance().getItems().keySet()) {
if (item1.getName().equals(item.getName())){
if (item1.getName().equals(item.getName())) {
Shop.getInstance().getItems().replace(item, Shop.getInstance().getItems().get(item1) + 1);
break;
}
Expand Down
7 changes: 5 additions & 2 deletions src/network/Responses/ScoreBoardResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.gilecode.yagson.YaGson;
import controllers.Constants;
import controllers.console.AccountMenu;
import controllers.graphical.ScoreBoardController;
import models.Account;
import network.Requests.account.ScoreBoardRequest;

Expand All @@ -19,7 +20,7 @@ public ScoreBoardResponse(ScoreBoardRequest request){
@Override
public void handleRequest() {
YaGson yaGson = new YaGson();
File file = new File("/JSONs/Accounts/");
File file = new File("src/JSONs/Accounts/");
for (File file1 : file.listFiles()){
try {
Scanner scanner = new Scanner(file1);
Expand All @@ -35,7 +36,9 @@ public void handleRequest() {

@Override
public void handleResponse() {

for (int i = 0; i < accounts.size(); i++) {
ScoreBoardController.instance.addDetails(accounts.get(i), i+1);
}
}

@Override
Expand Down

0 comments on commit e2c7d20

Please sign in to comment.