Fix #178, allow players to sign up for games

Needs testing, @dylanhansch @kugick ? :D
This commit is contained in:
Connor Monahan 2017-07-24 03:39:16 -05:00
parent a4038c6df1
commit d3b5795dda
2 changed files with 71 additions and 0 deletions

View File

@ -117,6 +117,7 @@ public class Warzone {
private final List<Player> respawn = new ArrayList<Player>();
private final List<String> reallyDeadFighters = new ArrayList<String>();
private HashMap<Player, PermissionAttachment> attachments = new HashMap<Player, PermissionAttachment>();
private HashMap<Player, Team> delayedJoinPlayers = new HashMap<Player, Team>();
private List<LogKillsDeathsJob.KillsDeathsRecord> killsDeathsTracker = new ArrayList<KillsDeathsRecord>();
@ -1055,6 +1056,7 @@ public class Warzone {
War.war.msg(player, "join.inventorystored");
this.respawnPlayer(team, player);
this.broadcast("join.broadcast", player.getName(), team.getKind().getFormattedName());
this.tryCallDelayedPlayers();
return true;
}
@ -1544,6 +1546,65 @@ public class Warzone {
return false;
}
/**
* Test whether there would be enough players with the addition of one more player
*
* @param plusOne Team to test
* @return true if there would be enough players
*/
public boolean testEnoughPlayers(TeamKind plusOne, boolean testDelayedJoin) {
int teamsWithEnough = 0;
for (Team team : teams) {
int addl = 0;
if (team.getKind() == plusOne) {
addl = 1;
}
if (testDelayedJoin) {
for (Iterator<Map.Entry<Player, Team>> iterator = this.delayedJoinPlayers.entrySet().iterator(); iterator.hasNext(); ) {
Map.Entry<Player, Team> e = iterator.next();
if (!isDelayedPlayerStillValid(e.getKey())) {
iterator.remove();
continue;
}
if (e.getValue() == team) {
addl += 1;
}
}
}
if (team.getPlayers().size() + addl >= this.getWarzoneConfig().getInt(WarzoneConfig.MINPLAYERS)) {
teamsWithEnough++;
}
}
return teamsWithEnough >= this.getWarzoneConfig().getInt(WarzoneConfig.MINTEAMS);
}
public void signup(Player player, Team team) {
War.war.msg(player, "You will be automatically sent to warzone when minplayers is reached.");
this.delayedJoinPlayers.put(player, team);
tryCallDelayedPlayers();
}
// prevent tryCallDelayedPlayers from being recursively called by Warzone#assign
private boolean activeDelayedCall = false;
private void tryCallDelayedPlayers() {
if (activeDelayedCall || (!isEnoughPlayers() && !testEnoughPlayers(null, true))) {
return;
}
activeDelayedCall = true;
for (Map.Entry<Player, Team> e : delayedJoinPlayers.entrySet()) {
this.assign(e.getKey(), e.getValue());
}
delayedJoinPlayers.clear();
activeDelayedCall = false;
}
private boolean isDelayedPlayerStillValid(Player player) {
// Make sure they're online, they can play in this team, and they're not in another game
return player.isOnline() && War.war.canPlayWar(player, delayedJoinPlayers.get(player))
&& Warzone.getZoneByPlayerName(player.getName()) == null;
}
public HashMap<String, LoadoutSelection> getLoadoutSelections() {
return loadoutSelections;
}

View File

@ -31,10 +31,15 @@ public class JoinCommand extends AbstractWarCommand {
Warzone zone;
TeamKind kind;
boolean signup = false;
if (this.args.length == 2) {
// zone by name
zone = Warzone.getZoneByName(this.args[0]);
kind = TeamKind.teamKindFromString(this.args[1]);
} else if (this.args.length == 3 && args[0].equals("delayed")) {
signup = true;
zone = Warzone.getZoneByName(this.args[1]);
kind = TeamKind.teamKindFromString(this.args[2]);
} else if (this.args.length == 1) {
zone = Warzone.getZoneByLocation((Player) this.getSender());
if (zone == null) {
@ -77,6 +82,11 @@ public class JoinCommand extends AbstractWarCommand {
previousTeam.removePlayer(player);
previousTeam.resetSign();
}
if (signup && !zone.testEnoughPlayers(kind, false)) {
// player wants to automatically join the zone when everyone else is ready
zone.signup(player, team);
return true;
}
zone.assign(player, team);
}
}