Skip to content

Commit

Permalink
Fixed lock
Browse files Browse the repository at this point in the history
  • Loading branch information
syd711 committed Dec 12, 2024
1 parent e29a46d commit c9563f2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
2 changes: 1 addition & 1 deletion resources/vpsdb.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import de.mephisto.vpin.server.highscores.HighscoreChangeEvent;
import de.mephisto.vpin.server.highscores.HighscoreChangeListener;
import de.mephisto.vpin.server.highscores.HighscoreService;
import de.mephisto.vpin.server.preferences.PreferenceChangedListener;
import de.mephisto.vpin.server.preferences.PreferencesService;
import de.mephisto.vpin.server.system.DefaultPictureService;
import de.mephisto.vpin.server.system.SystemService;
Expand All @@ -30,11 +31,10 @@
import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Semaphore;
import java.util.stream.Collectors;

@Service
public class CardService implements InitializingBean, HighscoreChangeListener {
public class CardService implements InitializingBean, HighscoreChangeListener, PreferenceChangedListener {
private final static Logger LOG = LoggerFactory.getLogger(CardService.class);

@Autowired
Expand All @@ -52,6 +52,8 @@ public class CardService implements InitializingBean, HighscoreChangeListener {
@Autowired
private CardTemplatesService cardTemplatesService;

private CardSettings cardSettings;

public File generateTableCardFile(Game game) {
generateCard(game);
return getCardSampleFile();
Expand Down Expand Up @@ -99,15 +101,16 @@ public boolean generateCard(Game game, boolean generateSampleCard, int templateI

public boolean generateCard(Game game, boolean generateSampleCard, CardTemplate template) {
try {
Semaphore semaphore = new Semaphore(0);
Platform.runLater(() -> {
doGenerateCard(game, generateSampleCard, template);
semaphore.release();
});
semaphore.acquire();

synchronized (this) {
this.wait();
}
return true;
}
catch (InterruptedException e) {
catch (Exception e) {
LOG.error("Failed to generate image: " + e.getMessage(), e);
return false;
}
Expand All @@ -118,12 +121,6 @@ private boolean doGenerateCard(Game game, boolean generateSampleCard, CardTempla
long serverId = preferencesService.getPreferenceValueLong(PreferenceNames.DISCORD_GUILD_ID, -1);
ScoreSummary summary = highscoreService.getScoreSummary(serverId, game);
if (!summary.getScores().isEmpty() && !StringUtils.isEmpty(summary.getRaw())) {
//otherwise check if the card rendering is enabled
CardSettings cardSettings = preferencesService.getJsonPreference(PreferenceNames.HIGHSCORE_CARD_SETTINGS, CardSettings.class);
if (cardSettings == null) {
cardSettings = new CardSettings();
}

//sample card are always generated
if (generateSampleCard) {
BufferedImage bufferedImage = new CardGraphics(directB2SService, cardSettings.getCardResolution(), template, game, summary).draw();
Expand Down Expand Up @@ -168,6 +165,11 @@ private boolean doGenerateCard(Game game, boolean generateSampleCard, CardTempla
LOG.error("Failed to generate highscore card: " + e.getMessage(), e);
SLOG.error("Failed to generate highscore card: " + e.getMessage());
}
finally {
synchronized (this) {
notifyAll();
}
}
return false;
}

Expand All @@ -192,8 +194,20 @@ public void highscoreUpdated(@NonNull Game game, @NonNull Highscore highscore) {
generateCard(game);
}

@Override
public void preferenceChanged(String propertyName, Object oldValue, Object newValue) throws Exception {
if (PreferenceNames.HIGHSCORE_CARD_SETTINGS.equalsIgnoreCase(propertyName)) {
cardSettings = preferencesService.getJsonPreference(PreferenceNames.HIGHSCORE_CARD_SETTINGS, CardSettings.class);
if (cardSettings == null) {
cardSettings = new CardSettings();
}
}
}

@Override
public void afterPropertiesSet() throws Exception {
this.highscoreService.addHighscoreChangeListener(this);
this.preferencesService.addChangeListener(this);
this.preferenceChanged(PreferenceNames.HIGHSCORE_CARD_SETTINGS, null, null);
}
}

0 comments on commit c9563f2

Please sign in to comment.