Skip to content

Commit

Permalink
finish server refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarrahi-MM committed May 6, 2019
1 parent 6c2bbb0 commit 7165ad1
Showing 1 changed file with 42 additions and 37 deletions.
79 changes: 42 additions & 37 deletions src/server/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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, String> 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()));
Expand Down Expand Up @@ -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));
}
}

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 7165ad1

Please sign in to comment.