diff --git a/src/controllers/console/AccountMenu.java b/src/controllers/console/AccountMenu.java index bc99d57..9fd13b1 100644 --- a/src/controllers/console/AccountMenu.java +++ b/src/controllers/console/AccountMenu.java @@ -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()); } } diff --git a/src/controllers/graphical/BattleController.java b/src/controllers/graphical/BattleController.java index 0c24741..5ecbf8e 100644 --- a/src/controllers/graphical/BattleController.java +++ b/src/controllers/graphical/BattleController.java @@ -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; @@ -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; @@ -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.*; @@ -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; @@ -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(); } @@ -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(); @@ -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) { @@ -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(); diff --git a/src/controllers/graphical/MatchHistoryController.java b/src/controllers/graphical/MatchHistoryController.java index 6db5c5f..31879cd 100644 --- a/src/controllers/graphical/MatchHistoryController.java +++ b/src/controllers/graphical/MatchHistoryController.java @@ -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; @@ -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(); @@ -31,7 +27,7 @@ public class MatchHistoryController implements Initializable { @Override public void initialize(URL location, ResourceBundle resources) { - ArrayList matchResults = loginAccount.getMatchHistory(); + ArrayList matchResults = loginAccount.getMatchHistorys(); int i = 1; for (MatchResult matchResult : matchResults){ String userName1 = matchResult.getUser0(); @@ -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++; } } diff --git a/src/controllers/graphical/ScoreBoardController.java b/src/controllers/graphical/ScoreBoardController.java index 5a187d3..c15e832 100644 --- a/src/controllers/graphical/ScoreBoardController.java +++ b/src/controllers/graphical/ScoreBoardController.java @@ -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; @@ -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); + } } diff --git a/src/layouts/scoreBoard.fxml b/src/layouts/scoreBoard.fxml index 3475b7c..2b6ab99 100644 --- a/src/layouts/scoreBoard.fxml +++ b/src/layouts/scoreBoard.fxml @@ -24,10 +24,9 @@ - diff --git a/src/models/Account.java b/src/models/Account.java index 8da1a8b..bf804bb 100644 --- a/src/models/Account.java +++ b/src/models/Account.java @@ -10,11 +10,9 @@ 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; @@ -22,7 +20,7 @@ public class Account implements Cloneable { private String password; private transient String authToken; - private ArrayList matchHistory; + private ArrayList matchHistorys; private ArrayList cards; private ArrayList items; @@ -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(); + this.matchHistorys = new ArrayList(); cards = new ArrayList<>(); items = new ArrayList<>(); this.decks = new ArrayList(); @@ -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; } @@ -86,8 +110,11 @@ public int getStoryLvl() { return storyLvl; } - public ArrayList getMatchHistory() { - return matchHistory; + public ArrayList getMatchHistorys() { + if (matchHistorys==null){ + matchHistorys= new ArrayList<>(); + } + return matchHistorys; } public Deck getMainDeck() { @@ -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; } @@ -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; } @@ -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; } @@ -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; } diff --git a/src/network/Responses/ScoreBoardResponse.java b/src/network/Responses/ScoreBoardResponse.java index af09d22..4bef5ef 100644 --- a/src/network/Responses/ScoreBoardResponse.java +++ b/src/network/Responses/ScoreBoardResponse.java @@ -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; @@ -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); @@ -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