Skip to content

Commit

Permalink
Integrate finish() and promptForSide().
Browse files Browse the repository at this point in the history
  • Loading branch information
riverwanderer committed Sep 9, 2023
1 parent 5896768 commit 90f4c27
Showing 1 changed file with 25 additions and 28 deletions.
53 changes: 25 additions & 28 deletions vassal-app/src/main/java/VASSAL/build/module/PlayerRoster.java
Original file line number Diff line number Diff line change
Expand Up @@ -469,23 +469,9 @@ public void finish() {
GameModule.getGameModule().warn(Resources.getString("PlayerRoster.failed_pref_write", e.getLocalizedMessage()));
}

String newSide = untranslateSide(sideConfig.getValueString());
final ArrayList<String> alreadyTaken = new ArrayList<>();

// Check if attempting switch to side taken whilst dialog was active. If found, return without setting side i.e. pickedSide = false.
for (final PlayerInfo p : players) {
alreadyTaken.add(p.side);
}

while (alreadyTaken.contains(newSide)) {
wc.setProblem(newSide + " already taken"); // Resources.getString("PlayerRoster.side_unavailable")
newSide = String.valueOf(getControls());
alreadyTaken.clear();
for (final PlayerInfo p : players) {
alreadyTaken.add(p.side);
}
newSide = untranslateSide(sideConfig.getValueString());
}
// Drop into standard routine, starting with checking side is still available (race condition mitigation)
// returns untranslated side
String newSide = promptForSide(sideConfig.getValueString());

if (newSide != null) {
if (GameModule.getGameModule().isMultiplayerConnected()) {
Expand Down Expand Up @@ -774,10 +760,20 @@ public List<String> getAvailableSides() {
return availableSides;
}

protected String promptForSide() {
protected String promptForSide(String newSide) {

final ArrayList<String> availableSides = new ArrayList<>(sides);
final ArrayList<String> alreadyTaken = new ArrayList<>();
String newSide = "";
boolean fromWizard;
boolean found = false; // Set when we find a usable side

if (newSide == null) {
newSide = "";
fromWizard = false;
}
else {
fromWizard = true;
}

while (newSide != null) { // Loops until a valid side is found or op is canceled (repeats side check to minimuse race condition window)

Expand Down Expand Up @@ -807,15 +803,16 @@ protected String promptForSide() {
// Thus hotseat players can easily cycle through the player positions as they will appear successively as the default.
// Common names for Solitaire players (Solitaire, Solo, Referee) do not count as "real" player sides, and will be skipped.
// If we have no "next" side available to offer, we stay with the observer side as our default offering.
final String mySide = getMySide(); // Get our own side, so we can find the "next" one
final int myidx = (mySide != null) ? sides.indexOf(mySide) : -1; // See if we have a current non-observe side.
boolean found = false; // Set when we find a usable side
int i = (myidx >= 0) ? ((myidx + 1) % sides.size()) : 0; // If we do, start looking in the "next" slot, otherwise start at beginning.
for (int tries = 0; i != myidx && tries < sides.size(); i = (i + 1) % sides.size(), tries++) { // Wrap-around search of sides
final String s = sides.get(i);
if (!alreadyTaken.contains(s) && !isSoloSide(s)) {
found = true; // Found an available slot that's not our current one and not a "solo" slot.
break;
if (!fromWizard) {
final String mySide = getMySide(); // Get our own side, so we can find the "next" one
final int myidx = (mySide != null) ? sides.indexOf(mySide) : -1; // See if we have a current non-observe side.
int i = (myidx >= 0) ? ((myidx + 1) % sides.size()) : 0; // If we do, start looking in the "next" slot, otherwise start at beginning.
for (int tries = 0; i != myidx && tries < sides.size(); i = (i + 1) % sides.size(), tries++) { // Wrap-around search of sides
final String s = sides.get(i);
if (!alreadyTaken.contains(s) && !isSoloSide(s)) {
found = true; // Found an available slot that's not our current one and not a "solo" slot.
break;
}
}
}

Expand Down

0 comments on commit 90f4c27

Please sign in to comment.