From 3fe464b7a6c31972f2129ef75a295f0a90fc5b49 Mon Sep 17 00:00:00 2001 From: jarrahi_mm Date: Mon, 6 May 2019 13:53:25 +0430 Subject: [PATCH 1/3] refactor server --- src/server/Server.java | 95 ++++++++++++++++++++---------------------- 1 file changed, 45 insertions(+), 50 deletions(-) diff --git a/src/server/Server.java b/src/server/Server.java index eb87a6b9..890fa9fa 100644 --- a/src/server/Server.java +++ b/src/server/Server.java @@ -452,21 +452,17 @@ private Game getGame(String clientName) throws ClientException { return game; } - private boolean isOpponentAccountValid(Message message) { + private void checkOpponentAccountValidation(Message message) throws LogicException { if (message.getNewGameFields().getOpponentUsername() == null) { - sendException("invalid opponentAccount!", message.getSender(), message.getMessageId()); - return false; + throw new ClientException("invalid opponentAccount!"); } Account opponentAccount = getAccount(message.getNewGameFields().getOpponentUsername()); if (opponentAccount == null) { - sendException("invalid opponentAccount!", message.getSender(), message.getMessageId()); - return false; + throw new ClientException("invalid opponent username!"); } /*if (accounts.get(opponentAccount) == null) { sendException("opponentAccount has not logged in!", message.getSender(), message.getMessageId()); - return false; }*/ - return true; } private void newDeckGame(Message message) throws LogicException { @@ -535,48 +531,47 @@ private void newStoryGame(Message message) throws LogicException { private void newMultiplayerGame(Message message) throws LogicException { loginCheck(message); - if (isOpponentAccountValid(message)) { - Account myAccount = clients.get(message.getSender()); - Account opponentAccount = getAccount(message.getNewGameFields().getOpponentUsername()); - if (!myAccount.hasValidMainDeck()) { - throw new ClientException("you don't have valid main deck!"); - } - if (opponentAccount == null || !opponentAccount.hasValidMainDeck()) { - throw new ClientException("opponent's main deck is not valid"); - } - if (onlineGames.get(myAccount) != null) { - throw new ClientException("you have online game!"); - } - if (onlineGames.get(opponentAccount) != null) { - throw new ClientException("opponent has online game!"); - } - accounts.replace(opponentAccount, onlineClients.get(1).getClientName()); - clients.replace(onlineClients.get(1).getClientName(), opponentAccount); - Game game = null; - GameMap gameMap = new GameMap(collectibleItems, message.getNewGameFields().getNumberOfFlags(), originalFlag); - if (message.getNewGameFields().getGameType() == null) { - throw new ClientException("invalid gameType!"); - } - switch (message.getNewGameFields().getGameType()) { - case KILL_HERO: - game = new KillHeroBattle(myAccount, opponentAccount, gameMap); - break; - case A_FLAG: - game = new SingleFlagBattle(myAccount, opponentAccount, gameMap); - break; - case SOME_FLAG: - game = new MultiFlagBattle(myAccount, opponentAccount, gameMap, message.getNewGameFields().getNumberOfFlags()); - break; - } - game.setReward(Game.getDefaultReward()); - onlineGames.put(myAccount, game); - onlineGames.put(opponentAccount, game); - addToSendingMessages(Message.makeGameCopyMessage - (serverName, message.getSender(), game, 0)); - addToSendingMessages(Message.makeGameCopyMessage - (serverName, accounts.get(opponentAccount), game, 0)); - game.startGame(); + checkOpponentAccountValidation(message); + Account myAccount = clients.get(message.getSender()); + Account opponentAccount = getAccount(message.getNewGameFields().getOpponentUsername()); + if (!myAccount.hasValidMainDeck()) { + throw new ClientException("you don't have valid main deck!"); + } + if (opponentAccount == null || !opponentAccount.hasValidMainDeck()) { + throw new ClientException("opponent's main deck is not valid"); + } + if (onlineGames.get(myAccount) != null) { + throw new ClientException("you have online game!"); + } + if (onlineGames.get(opponentAccount) != null) { + throw new ClientException("opponent has online game!"); } + accounts.replace(opponentAccount, onlineClients.get(1).getClientName()); + clients.replace(onlineClients.get(1).getClientName(), opponentAccount); + Game game = null; + GameMap gameMap = new GameMap(collectibleItems, message.getNewGameFields().getNumberOfFlags(), originalFlag); + if (message.getNewGameFields().getGameType() == null) { + throw new ClientException("invalid gameType!"); + } + switch (message.getNewGameFields().getGameType()) { + case KILL_HERO: + game = new KillHeroBattle(myAccount, opponentAccount, gameMap); + break; + case A_FLAG: + game = new SingleFlagBattle(myAccount, opponentAccount, gameMap); + break; + case SOME_FLAG: + game = new MultiFlagBattle(myAccount, opponentAccount, gameMap, message.getNewGameFields().getNumberOfFlags()); + break; + } + game.setReward(Game.getDefaultReward()); + onlineGames.put(myAccount, game); + onlineGames.put(opponentAccount, game); + addToSendingMessages(Message.makeGameCopyMessage + (serverName, message.getSender(), game, 0)); + addToSendingMessages(Message.makeGameCopyMessage + (serverName, accounts.get(opponentAccount), game, 0)); + game.startGame(); } public void finishGame(Game game) { @@ -644,7 +639,7 @@ private void checkGameFinish(Game game) { if (account == null) serverPrint("Error"); else { - account.addMatchHistory(playerOneHistory,game.getReward()); + account.addMatchHistory(playerOneHistory, game.getReward()); saveAccount(account); } } @@ -653,7 +648,7 @@ private void checkGameFinish(Game game) { if (account == null) serverPrint("Error"); else { - account.addMatchHistory(playerTwoHistory,game.getReward()); + account.addMatchHistory(playerTwoHistory, game.getReward()); saveAccount(account); } } From 273944815a7ecb4592c4e99b6bbbbc3b1e12ad47 Mon Sep 17 00:00:00 2001 From: jarrahi_mm Date: Mon, 6 May 2019 21:25:52 +0430 Subject: [PATCH 2/3] refactor server --- src/server/Server.java | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/server/Server.java b/src/server/Server.java index 29212cf2..6c8c7188 100644 --- a/src/server/Server.java +++ b/src/server/Server.java @@ -574,14 +574,21 @@ private void newMultiplayerGame(Message message) throws LogicException { game.startGame(); } - public void finishGame(Game game) { - Account myAccount = getAccount(game.getPlayerOne().getUserName()); - Account opponentAccount = getAccount(game.getPlayerTwo().getUserName()); - onlineGames.remove(myAccount); - onlineGames.remove(opponentAccount); - accounts.put(opponentAccount, null); + public void removeGame(Game game) { + if(!game.getPlayerOne().getUserName().equalsIgnoreCase("AI")){ + Account account1 = getAccount(game.getPlayerOne().getUserName()); + if(account1!=null){ + onlineGames.replace(account1,null); + } + } + if(!game.getPlayerTwo().getUserName().equalsIgnoreCase("AI")){ + Account account2 = getAccount(game.getPlayerTwo().getUserName()); + if(account2!=null){ + onlineGames.replace(account2,null); + accounts.put(account2, null); + } + } clients.put(onlineClients.get(1).getClientName(), null); - } private void insertCard(Message message) throws LogicException { @@ -653,7 +660,7 @@ private void checkGameFinish(Game game) { } } sendGameFinishMessages(game); - finishGame(game); + removeGame(game); } } From 7165ad1780035022fa424279af105b894b176f09 Mon Sep 17 00:00:00 2001 From: jarrahi_mm Date: Mon, 6 May 2019 22:22:08 +0430 Subject: [PATCH 3/3] finish server refactor --- src/server/Server.java | 79 ++++++++++++++++++++++-------------------- 1 file changed, 42 insertions(+), 37 deletions(-) diff --git a/src/server/Server.java b/src/server/Server.java index 6c8c7188..81dd5104 100644 --- a/src/server/Server.java +++ b/src/server/Server.java @@ -121,7 +121,7 @@ public void addToReceivingMessages(String messageJson) { } } - private void receiveMessage(Message message) {//TODO:add finish game message + private void receiveMessage(Message message) {//TODO:add fast finish game message try { if (message == null) { throw new ServerException("NULL Message"); @@ -575,20 +575,20 @@ private void newMultiplayerGame(Message message) throws LogicException { } public void removeGame(Game game) { - if(!game.getPlayerOne().getUserName().equalsIgnoreCase("AI")){ + if (!game.getPlayerOne().getUserName().equalsIgnoreCase("AI")) { Account account1 = getAccount(game.getPlayerOne().getUserName()); - if(account1!=null){ - onlineGames.replace(account1,null); + if (account1 != null) { + onlineGames.replace(account1, null); } } - if(!game.getPlayerTwo().getUserName().equalsIgnoreCase("AI")){ + if (!game.getPlayerTwo().getUserName().equalsIgnoreCase("AI")) { Account account2 = getAccount(game.getPlayerTwo().getUserName()); - if(account2!=null){ - onlineGames.replace(account2,null); - accounts.put(account2, null); + if (account2 != null) { + onlineGames.replace(account2, null); + accounts.replace(account2, null); } } - clients.put(onlineClients.get(1).getClientName(), null); + clients.replace(onlineClients.get(1).getClientName(), null); } private void insertCard(Message message) throws LogicException { @@ -630,8 +630,8 @@ private void endTurn(Message message) throws LogicException { private void sudo(Message message) { String command = message.getOtherFields().getSudoCommand().toLowerCase(); if (command.contains("account")) { - for (Map.Entry account : accounts.entrySet()) { - serverPrint(account.getKey().getUsername() + " " + account.getKey().getPassword()); + for (Account account : accounts.keySet()) { + serverPrint(account.getUsername() + " " + account.getPassword()); } } addToSendingMessages(Message.makeDoneMessage(serverName, message.getSender(), message.getMessageId())); @@ -664,67 +664,73 @@ private void checkGameFinish(Game game) { } } - public void sendChangeCardPositionMessage(Game game, Card card, CardPosition newCardPosition) throws ServerException { + public void sendChangeCardPositionMessage(Game game, Card card, CardPosition newCardPosition) { String clientName; if (!game.getPlayerOne().getUserName().equalsIgnoreCase("AI")) { clientName = getClientName(game.getPlayerOne().getUserName()); if (clientName == null) { - throw new ServerException("player one has logged out during game!"); + serverPrint("player one has logged out during game!");//ahmad,please don't change this to exception! + } else { + addToSendingMessages(Message.makeChangeCardPositionMessage( + serverName, clientName, card, newCardPosition, 0)); } - addToSendingMessages(Message.makeChangeCardPositionMessage( - serverName, clientName, card, newCardPosition, 0)); } if (!game.getPlayerTwo().getUserName().equalsIgnoreCase("AI")) { clientName = getClientName(game.getPlayerTwo().getUserName()); if (clientName == null) { - throw new ServerException("player two has logged out during game!"); + serverPrint("player two has logged out during game!");//ahmad,please don't change this to exception! + } else { + addToSendingMessages(Message.makeChangeCardPositionMessage( + serverName, clientName, card, newCardPosition, 0)); } - addToSendingMessages(Message.makeChangeCardPositionMessage( - serverName, clientName, card, newCardPosition, 0)); } } - public void sendTroopUpdateMessage(Game game, Troop troop) throws ServerException { + public void sendTroopUpdateMessage(Game game, Troop troop) { String clientName; if (!game.getPlayerOne().getUserName().equalsIgnoreCase("AI")) { clientName = getClientName(game.getPlayerOne().getUserName()); if (clientName == null) { - throw new ServerException("player one has logged out during game!"); + serverPrint("player one has logged out during game!"); + } else { + addToSendingMessages(Message.makeTroopUpdateMessage( + serverName, clientName, troop, 0)); } - addToSendingMessages(Message.makeTroopUpdateMessage( - serverName, clientName, troop, 0)); } if (!game.getPlayerTwo().getUserName().equalsIgnoreCase("AI")) { clientName = getClientName(game.getPlayerTwo().getUserName()); if (clientName == null) { - throw new ServerException("player two has logged out during game!"); + serverPrint("player two has logged out during game!"); + } else { + addToSendingMessages(Message.makeTroopUpdateMessage( + serverName, clientName, troop, 0)); } - addToSendingMessages(Message.makeTroopUpdateMessage( - serverName, clientName, troop, 0)); } } - public void sendGameUpdateMessage(Game game) throws ServerException { + public void sendGameUpdateMessage(Game game) { String clientName; if (!game.getPlayerOne().getUserName().equalsIgnoreCase("AI")) { clientName = getClientName(game.getPlayerOne().getUserName()); if (clientName == null) { - throw new ServerException("player one has logged out during game!"); + serverPrint("player one has logged out during game!"); + } else { + addToSendingMessages(Message.makeGameUpdateMessage( + serverName, clientName, game.getTurnNumber(), game.getPlayerOne().getCurrentMP(), + game.getPlayerOne().getNumberOfCollectedFlags(), game.getPlayerTwo().getCurrentMP(), + game.getPlayerTwo().getNumberOfCollectedFlags(), 0)); } - addToSendingMessages(Message.makeGameUpdateMessage( - serverName, clientName, game.getTurnNumber(), game.getPlayerOne().getCurrentMP(), - game.getPlayerOne().getNumberOfCollectedFlags(), game.getPlayerTwo().getCurrentMP(), - game.getPlayerTwo().getNumberOfCollectedFlags(), 0)); } if (!game.getPlayerTwo().getUserName().equalsIgnoreCase("AI")) { clientName = getClientName(game.getPlayerTwo().getUserName()); if (clientName == null) { - throw new ServerException("player two has logged out during game!"); + serverPrint("player two has logged out during game!"); + } else { + addToSendingMessages(Message.makeGameUpdateMessage( + serverName, clientName, game.getTurnNumber(), game.getPlayerOne().getCurrentMP(), + game.getPlayerOne().getNumberOfCollectedFlags(), game.getPlayerTwo().getCurrentMP(), + game.getPlayerTwo().getNumberOfCollectedFlags(), 0)); } - addToSendingMessages(Message.makeGameUpdateMessage( - serverName, clientName, game.getTurnNumber(), game.getPlayerOne().getCurrentMP(), - game.getPlayerOne().getNumberOfCollectedFlags(), game.getPlayerTwo().getCurrentMP(), - game.getPlayerTwo().getNumberOfCollectedFlags(), 0)); } } @@ -775,7 +781,6 @@ private void readAllCards() { for (File file : files) { Card card = loadFile(file, Card.class); if (card == null) continue; - if (card.getType() == CardType.COLLECTIBLE_ITEM) { collectibleItems.add(card); } else {