Skip to content

Commit

Permalink
Show alliances in sorted order in the player list. (#10507)
Browse files Browse the repository at this point in the history
* Show alliances in sorted order in the player list.

Also use new syntax for game locking.

* Improve comments and use local gameData variable.
  • Loading branch information
asvitkine authored May 26, 2022
1 parent 7358e0a commit 1ff8bf0
Showing 1 changed file with 13 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JLabel;
Expand Down Expand Up @@ -171,10 +172,9 @@ void setStatColumns() {
}

private synchronized void loadData() {
// copy so acquire/release read lock are on the same object!
// copy so that the object doesn't change underneath us
final GameData gameData = StatPanel.this.gameData;
gameData.acquireReadLock();
try {
try (GameData.Unlocker ignored = gameData.acquireReadLock()) {
final List<GamePlayer> players = gameData.getPlayerList().getSortedPlayers();
final Collection<String> alliances = gameData.getAllianceTracker().getAlliances();
collectedData = new String[players.size() + alliances.size()][stats.length + 1];
Expand All @@ -188,7 +188,7 @@ private synchronized void loadData() {
}
row++;
}
for (final String alliance : alliances) {
for (final String alliance : alliances.stream().sorted().collect(Collectors.toList())) {
collectedData[row][0] = alliance;
for (int i = 0; i < stats.length; i++) {
collectedData[row][i + 1] =
Expand All @@ -197,8 +197,6 @@ private synchronized void loadData() {
}
row++;
}
} finally {
gameData.releaseReadLock();
}
}

Expand Down Expand Up @@ -263,18 +261,16 @@ class TechTableModel extends AbstractTableModel {
colMap.put(colList[i], i + 1);
}
boolean useTech = false;
// copy so that the object doesn't change underneath us
final GameData gameData = StatPanel.this.gameData;
try {
gameData.acquireReadLock();
try (GameData.Unlocker ignored = gameData.acquireReadLock()) {
final int numTechs = TechAdvance.getTechAdvances(gameData.getTechnologyFrontier()).size();
if (gameData.getResourceList().getResource(Constants.TECH_TOKENS) != null) {
useTech = true;
data = new String[numTechs + 1][colList.length + 2];
} else {
data = new String[numTechs][colList.length + 1];
}
} finally {
gameData.releaseReadLock();
}
/* Load the technology -> row mapping */
int row = 0;
Expand Down Expand Up @@ -313,41 +309,36 @@ private void initColList() {

void update() {
clearAdvances();
// copy so acquire/release read lock are on the same object!
// copy so that the object doesn't change underneath us
final GameData gameData = StatPanel.this.gameData;
gameData.acquireReadLock();
try {
try (GameData.Unlocker ignored = gameData.acquireReadLock()) {
for (final GamePlayer pid : gameData.getPlayerList().getPlayers()) {
if (colMap.get(pid.getName()) == null) {
throw new IllegalStateException(
"Unexpected player in GameData.getPlayerList()" + pid.getName());
}
final int col = colMap.get(pid.getName());
int row = 0;
if (StatPanel.this.gameData.getResourceList().getResource(Constants.TECH_TOKENS)
!= null) {
if (gameData.getResourceList().getResource(Constants.TECH_TOKENS) != null) {
final int tokens = pid.getResources().getQuantity(Constants.TECH_TOKENS);
data[row][col] = Integer.toString(tokens);
}
final List<TechAdvance> advancesAll =
TechAdvance.getTechAdvances(StatPanel.this.gameData.getTechnologyFrontier());
TechAdvance.getTechAdvances(gameData.getTechnologyFrontier());
final List<TechAdvance> has =
TechAdvance.getTechAdvances(StatPanel.this.gameData.getTechnologyFrontier(), pid);
TechAdvance.getTechAdvances(gameData.getTechnologyFrontier(), pid);
for (final TechAdvance advance : advancesAll) {
if (!has.contains(advance)) {
row = rowMap.get(advance.getName());
data[row][col] = "-";
}
}
for (final TechAdvance advance :
TechTracker.getCurrentTechAdvances(
pid, StatPanel.this.gameData.getTechnologyFrontier())) {
TechTracker.getCurrentTechAdvances(pid, gameData.getTechnologyFrontier())) {
row = rowMap.get(advance.getName());
data[row][col] = "X";
}
}
} finally {
gameData.releaseReadLock();
}
}

Expand Down Expand Up @@ -396,11 +387,8 @@ class PuStat extends ResourceStat {

private static Resource getResourcePUs(final GameData data) {
final Resource pus;
try {
data.acquireReadLock();
try (GameData.Unlocker ignored = data.acquireReadLock()) {
pus = data.getResourceList().getResource(Constants.PUS);
} finally {
data.releaseReadLock();
}
return pus;
}
Expand Down

0 comments on commit 1ff8bf0

Please sign in to comment.