added logic to the 'LobbyState' class
This commit is contained in:
		@@ -1,13 +1,19 @@
 | 
				
			|||||||
package pp.mdga.server.automaton;
 | 
					package pp.mdga.server.automaton;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import pp.mdga.game.Color;
 | 
				
			||||||
 | 
					import pp.mdga.game.Player;
 | 
				
			||||||
import pp.mdga.message.client.*;
 | 
					import pp.mdga.message.client.*;
 | 
				
			||||||
 | 
					import pp.mdga.message.server.LobbyPlayerJoinedMessage;
 | 
				
			||||||
import pp.mdga.message.server.ServerStartGameMessage;
 | 
					import pp.mdga.message.server.ServerStartGameMessage;
 | 
				
			||||||
import pp.mdga.message.server.UpdateReadyMessage;
 | 
					import pp.mdga.message.server.UpdateReadyMessage;
 | 
				
			||||||
import pp.mdga.message.server.UpdateTSKMessage;
 | 
					import pp.mdga.message.server.UpdateTSKMessage;
 | 
				
			||||||
import pp.mdga.server.ServerGameLogic;
 | 
					import pp.mdga.server.ServerGameLogic;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.util.Map;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 *
 | 
					 * This class represents the lobby state of the server.
 | 
				
			||||||
 | 
					 * It will handle all join and disconnect messages, as well the selection of the color of the player.
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
public class LobbyState extends ServerState {
 | 
					public class LobbyState extends ServerState {
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
@@ -36,53 +42,78 @@ public void exit() {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     *
 | 
					     * @param msg  as the message which was sent by the player as a JoinedLobbyMessage object.
 | 
				
			||||||
 | 
					     * @param from as the client id of the player as an Integer.
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    @Override
 | 
				
			||||||
 | 
					    public void received(JoinedLobbyMessage msg, int from) {
 | 
				
			||||||
 | 
					        Player player = new Player(msg.getName());
 | 
				
			||||||
 | 
					        this.logic.getGame().addPlayer(from, player);
 | 
				
			||||||
 | 
					        this.logic.getServerSender().broadcast(new LobbyPlayerJoinedMessage(from, player));
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
     * @param msg  as the message which was sent by the player as a SelectTSK object.
 | 
					     * @param msg  as the message which was sent by the player as a SelectTSK object.
 | 
				
			||||||
     * @param from as the client id of the player as an Integer.
 | 
					     * @param from as the client id of the player as an Integer.
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
    public void received(SelectTSKMessage msg, int from) {
 | 
					    public void received(SelectTSKMessage msg, int from) {
 | 
				
			||||||
 | 
					        for (Map.Entry<Integer, Player> entry : this.logic.getGame().getPlayers().entrySet()) {
 | 
				
			||||||
 | 
					            if (entry.getValue().getColor() == msg.getColor()) {
 | 
				
			||||||
 | 
					                return;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        this.logic.getGame().getPlayerById(from).setColor(msg.getColor());
 | 
				
			||||||
        this.logic.getServerSender().broadcast(new UpdateTSKMessage(from, msg.getColor()));
 | 
					        this.logic.getServerSender().broadcast(new UpdateTSKMessage(from, msg.getColor()));
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     *
 | 
					 | 
				
			||||||
     * @param msg  as the message which was sent by the player as a DeselectTSK object.
 | 
					     * @param msg  as the message which was sent by the player as a DeselectTSK object.
 | 
				
			||||||
     * @param from as the client id of the player as an Integer.
 | 
					     * @param from as the client id of the player as an Integer.
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
    public void received(DeselectTSKMessage msg, int from) {
 | 
					    public void received(DeselectTSKMessage msg, int from) {
 | 
				
			||||||
        this.logic.getServerSender().broadcast(new UpdateTSKMessage(from, msg.getColor()));
 | 
					        this.logic.getGame().getPlayerById(from).setColor(Color.NONE);
 | 
				
			||||||
 | 
					        this.logic.getServerSender().broadcast(new UpdateTSKMessage(from, Color.NONE));
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     *
 | 
					 | 
				
			||||||
     * @param msg  as the message which was sent by the player as a LobbyReady object.
 | 
					     * @param msg  as the message which was sent by the player as a LobbyReady object.
 | 
				
			||||||
     * @param from as the client id of the player as an Integer.
 | 
					     * @param from as the client id of the player as an Integer.
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
    public void received(LobbyReadyMessage msg, int from) {
 | 
					    public void received(LobbyReadyMessage msg, int from) {
 | 
				
			||||||
 | 
					        this.logic.getGame().getPlayerById(from).setReady(true);
 | 
				
			||||||
        this.logic.getServerSender().broadcast(new UpdateReadyMessage(from, true));
 | 
					        this.logic.getServerSender().broadcast(new UpdateReadyMessage(from, true));
 | 
				
			||||||
 | 
					        for (Map.Entry<Integer, Player> entry : this.logic.getGame().getPlayers().entrySet()) {
 | 
				
			||||||
 | 
					            if (!entry.getValue().isActive()) {
 | 
				
			||||||
 | 
					                return;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        this.logic.getGame().setAllReady(true);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     *
 | 
					 | 
				
			||||||
     * @param msg  as the message which was sent by the player as a LobbyNotReady object.
 | 
					     * @param msg  as the message which was sent by the player as a LobbyNotReady object.
 | 
				
			||||||
     * @param from as the client id of the player as an Integer.
 | 
					     * @param from as the client id of the player as an Integer.
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
    public void received(LobbyNotReadyMessage msg, int from) {
 | 
					    public void received(LobbyNotReadyMessage msg, int from) {
 | 
				
			||||||
 | 
					        this.logic.getGame().getPlayerById(from).setReady(false);
 | 
				
			||||||
 | 
					        this.logic.getGame().setAllReady(false);
 | 
				
			||||||
        this.logic.getServerSender().broadcast(new UpdateReadyMessage(from, false));
 | 
					        this.logic.getServerSender().broadcast(new UpdateReadyMessage(from, false));
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     *
 | 
					 | 
				
			||||||
     * @param msg  as the message which was sent by the player as a ForceStartGame object.
 | 
					     * @param msg  as the message which was sent by the player as a ForceStartGame object.
 | 
				
			||||||
     * @param from as the client id of the player as an Integer.
 | 
					     * @param from as the client id of the player as an Integer.
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
    public void received(StartGameMessage msg, int from) {
 | 
					    public void received(StartGameMessage msg, int from) {
 | 
				
			||||||
        this.logic.getServerSender().broadcast(new ServerStartGameMessage());
 | 
					        if (msg.isForceStartGame() || this.logic.getGame().allReady()) {
 | 
				
			||||||
        this.logic.setCurrentState(this.logic.getGameState());
 | 
					            this.logic.getServerSender().broadcast(new ServerStartGameMessage());
 | 
				
			||||||
 | 
					            this.logic.setCurrentState(this.logic.getGameState());
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user