package server.lobby.state;

import proto.Command.LobbyCmd;
import server.game.Player;
import server.lobby.Lobby;
import server.game.Team;

public class TeamState extends AState {

    public TeamState(Lobby lobby) {
        super("Team", lobby);
    }

    @Override
    public AState initialize() {
        this.getLobby().broadcast("The lobby is complete, you can choose your teams.", null);
        for (Player player : this.getLobby().getPlayers()) {
            player.setTeam(Team.Undefined);
        }
        return this;
    }

    @Override
    public boolean isFinished() {
        for (Player player : this.getLobby().getPlayers()) {
            if (player.getTeam() == Team.Undefined)
                return false;
        }
        return true;
    }

    @Override
    public AState getNextState() {
        return null;
    }

    @Override
    public void handleAction(Player player, LobbyCmd cmd) {

    }

}

@@ -24,16 +24,50 @@
 ate;%0D%0A%0D%0A
+import io.netty.channel.Channel;%0D%0A
 import p
@@ -892,19 +892,45 @@
 return n
-ull
+ew DrawState(this.getLobby())
 ;%0D%0A    %7D
@@ -981,40 +981,1580 @@
 ion(
-Player player, LobbyCmd cm
+Channel channel, LobbyCmd cmd) %7B%0D%0A        Player player = this.getLobby().getPlayer(channel);%0D%0A        if (cmd.getCmdType().equals(LobbyCmd.CmdType.TEAM)) %7B%0D%0A            this.handleTeam(channel, player, cmd.getValue());%0D%0A        %7D%0D%0A    %7D%0D%0A%0D%0A    private boolean isTeamFull(Player playerToNotCheck, Team teamToCheck) %7B%0D%0A        int i = 0;%0D%0A%0D%0A        for (Player player : this.getLobby().getPlayers()) %7B%0D%0A            if (!player.equals(playerToNotCheck)) %7B%0D%0A                if (player.getTeam().equals(teamToCheck)) %7B%0D%0A                    ++i;%0D%0A                %7D%0D%0A            %7D%0D%0A        %7D%0D%0A        return i %3E= 2;%0D%0A    %7D%0D%0A%0D%0A    private void handleTeam(Channel channel, Player player, String team) %7B%0D%0A        if (team.toUpperCase().equals(%22RED%22)) %7B%0D%0A            if (!this.isTeamFull(player, Team.Re
 d)
+)
  %7B%0D%0A
+                this.getLobby().sendMsg(%22%5BSERVER%5D You changed your team to Red.%22, channel);%0D%0A                player.setTeam(Team.Red);%0D%0A            %7D else %7B%0D%0A                this.getLobby().sendMsg(%22%5BSERVER%5D This team is full, choose the other.%22, channel);%0D%0A            %7D%0D%0A        %7D else if (team.toUpperCase().equals(%22BLUE%22)) %7B%0D%0A            if (!this.isTeamFull(player, Team.Blue)) %7B%0D%0A                this.getLobby().sendMsg(%22%5BSERVER%5D You changed your team to Blue.%22, channel);%0D%0A                player.setTeam(Team.Blue);%0D%0A            %7D else %7B%0D%0A                this.getLobby().sendMsg(%22%5BSERVER%5D This team is full, choose the other.%22, channel);%0D%0A            %7D%0D%0A        %7D else %7B%0D%0A            this.getLobby().sendMsg(%22%5BSERVER%5D Team: Invalid parameter.%22, channel);%0D%0A        %7D
 %0D%0A  

Handle Teams server-side with full team checks.