Skip to content

Commit

Permalink
Resolve circular dependencies where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
Sheikah45 committed Nov 30, 2023
1 parent 344fe0a commit 621a275
Show file tree
Hide file tree
Showing 35 changed files with 853 additions and 759 deletions.
26 changes: 4 additions & 22 deletions src/main/java/com/faforever/client/chat/ChatChannelUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import com.faforever.client.domain.PlayerBean;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.ReadOnlyStringProperty;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ObservableValue;
Expand All @@ -26,9 +24,9 @@
public class ChatChannelUser {

@EqualsAndHashCode.Include
ReadOnlyStringWrapper username = new ReadOnlyStringWrapper();
String username;
@EqualsAndHashCode.Include
ReadOnlyStringWrapper channel = new ReadOnlyStringWrapper();
String channel;
BooleanProperty moderator = new SimpleBooleanProperty();
ObjectProperty<Color> color = new SimpleObjectProperty<>();
ObjectProperty<PlayerBean> player = new SimpleObjectProperty<>();
Expand All @@ -43,8 +41,8 @@ public class ChatChannelUser {
.flatMap(category -> moderator.map(isMod -> isMod ? Set.of(ChatUserCategory.MODERATOR, category) : Set.of(category)));

public ChatChannelUser(String username, String channel) {
this.username.set(username);
this.channel.set(channel);
this.username = username;
this.channel = channel;
}

public Optional<PlayerBean> getPlayer() {
Expand Down Expand Up @@ -79,22 +77,6 @@ public void setModerator(boolean moderator) {
this.moderator.set(moderator);
}

public String getUsername() {
return username.get();
}

public ReadOnlyStringProperty usernameProperty() {
return username.getReadOnlyProperty();
}

public String getChannel() {
return channel.get();
}

public ReadOnlyStringProperty channelProperty() {
return channel.getReadOnlyProperty();
}

public BooleanProperty moderatorProperty() {
return moderator;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ private void bindProperties() {
ObservableValue<String> clanTagProperty = chatUser.flatMap(ChatChannelUser::playerProperty)
.flatMap(PlayerBean::clanProperty)
.map(clanTag -> clanTag.isBlank() ? null : String.format("[%s]", clanTag));
ObservableValue<String> usernameProperty = chatUser.flatMap(ChatChannelUser::usernameProperty);
ObservableValue<String> usernameProperty = chatUser.map(ChatChannelUser::getUsername);
usernameLabel.textProperty().bind(Bindings.createStringBinding(() -> {
String clanTag = clanTagProperty.getValue();
String username = usernameProperty.getValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ private ChatChannelUser getOrCreateChatUser(User user, Channel channel) {
private ChatChannelUser initializeUserForChannel(String username, ChatChannel chatChannel) {
ChatChannelUser chatChannelUser = new ChatChannelUser(username, chatChannel.getName());
playerService.getPlayerByNameIfOnline(username).ifPresent(chatChannelUser::setPlayer);
chatChannelUser.categoriesProperty().subscribe(() -> populateColor(chatChannelUser));
populateColor(chatChannelUser);
chatChannel.addUser(chatChannelUser);
return chatChannelUser;
Expand All @@ -211,10 +212,7 @@ void onPlayerOnline(PlayerBean player) {
.stream()
.map(channel -> channel.getUser(player.getUsername()))
.flatMap(Optional::stream)
.forEach(chatChannelUser -> fxApplicationThreadExecutor.execute(() -> {
chatChannelUser.setPlayer(player);
populateColor(chatChannelUser);
}));
.forEach(chatChannelUser -> fxApplicationThreadExecutor.execute(() -> chatChannelUser.setPlayer(player)));
}

@Handler
Expand Down Expand Up @@ -454,10 +452,10 @@ private void onSocialMessage(SocialInfo socialMessage) {
private void populateColor(ChatChannelUser chatChannelUser) {
String lowercaseUsername = chatChannelUser.getUsername().toLowerCase(US);

Color color;
ObservableMap<ChatUserCategory, Color> groupToColor = chatPrefs.getGroupToColor();
ObservableMap<String, Color> userToColor = chatPrefs.getUserToColor();

Color color;
if (chatPrefs.getChatColorMode() == RANDOM) {
color = ColorGeneratorUtil.generateRandomColor(lowercaseUsername.hashCode());
} else if (userToColor.containsKey(lowercaseUsername)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.faforever.client.config.ClientProperties;
import com.faforever.client.domain.GameBean;
import com.faforever.client.game.GameService;
import com.faforever.client.player.PlayerService;
import com.faforever.client.preferences.Preferences;
import com.faforever.commons.lobby.GameStatus;
Expand Down Expand Up @@ -38,10 +39,8 @@ public class DiscordRichPresenceService implements DisposableBean {

private final Timer timer = new Timer("Discord RPC", true);



public DiscordRichPresenceService(PlayerService playerService, DiscordEventHandler discordEventHandler,
ClientProperties clientProperties, Preferences preferences,
ClientProperties clientProperties, GameService gameService, Preferences preferences,
ObjectMapper objectMapper) {
this.playerService = playerService;
this.clientProperties = clientProperties;
Expand All @@ -62,6 +61,10 @@ public void run() {
} catch (Throwable t) {
log.error("Error in discord init", t);
}

Runnable updateGamePresence = () -> updatePlayedGameTo(gameService.getCurrentGame());
gameService.currentGameProperty().flatMap(GameBean::statusProperty).subscribe(updateGamePresence);
gameService.currentGameProperty().flatMap(GameBean::allPlayersInGameProperty).subscribe(updateGamePresence);
}

public void updatePlayedGameTo(GameBean game) {
Expand All @@ -72,8 +75,7 @@ public void updatePlayedGameTo(GameBean game) {
try {

if (game == null || game.getStatus() == GameStatus.CLOSED || game.getTeams() == null || !playerService.isCurrentPlayerInGame(game)) {
DiscordRPC.discordClearPresence();
log.debug("Cleared discord rich presence");
clearGameInfo();
return;
}

Expand Down Expand Up @@ -108,7 +110,6 @@ public void updatePlayedGameTo(GameBean game) {
}

private String getDiscordState(GameBean game) {
//I want no internationalisation in here as it should always be English
return switch (game.getStatus()) {
case OPEN -> game.getHost().equals(playerService.getCurrentPlayer().getUsername()) ? HOSTING : WAITING;
default -> PLAYING;
Expand All @@ -124,6 +125,7 @@ public void destroy() {
public void clearGameInfo() {
try {
DiscordRPC.discordClearPresence();
log.debug("Cleared discord rich presence");
} catch (Throwable t) {
log.error("Error cleaning game info for discord", t);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.faforever.client.domain.PlayerBean;
import com.faforever.client.fa.GameFullNotifier;
import com.faforever.client.game.GameService;
import com.faforever.client.mapstruct.IceServerMapper;
import com.faforever.client.os.OperatingSystem;
import com.faforever.client.os.OsUtils;
Expand Down Expand Up @@ -61,12 +60,12 @@ public class IceAdapterImpl implements IceAdapter, InitializingBean, DisposableB

private final OperatingSystem operatingSystem;
private final PlayerService playerService;
private final GameService gameService;
private final FafServerAccessor fafServerAccessor;
private final IceServerMapper iceServerMapper;
private final Preferences preferences;
private final ForgedAlliancePrefs forgedAlliancePrefs;
private final ObjectFactory<IceAdapterCallbacks> iceAdapterCallbacksFactory;
@Lazy
private final GameFullNotifier gameFullNotifier;

private final IceAdapterApi iceAdapterProxy = newIceAdapterProxy();
Expand Down Expand Up @@ -94,10 +93,6 @@ public void onIceAdapterStateChanged(String newState) {
public void onGpgGameMessage(GpgGameOutboundMessage message) {
String command = message.getCommand();

if (command.equals("Rehost")) {
gameService.onRehostRequest();
return;
}
if (command.equals("GameFull")) {
gameFullNotifier.onGameFull();
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.faforever.client.map.generator.MapGeneratorService;
import com.faforever.client.mod.ModService;
import com.faforever.client.player.PlayerService;
import com.faforever.client.social.SocialService;
import com.faforever.client.theme.UiService;
import com.faforever.commons.lobby.GameType;
import javafx.util.StringConverter;
Expand All @@ -27,17 +28,20 @@
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class LiveGamesFilterController extends AbstractFilterController<GameBean> {

private final SocialService socialService;
private final PlayerService playerService;
private final ModService modService;
private final MapGeneratorService mapGeneratorService;

public LiveGamesFilterController(UiService uiService, I18n i18n, ModService modService, PlayerService playerService,
MapGeneratorService mapGeneratorService,
FxApplicationThreadExecutor fxApplicationThreadExecutor) {
FxApplicationThreadExecutor fxApplicationThreadExecutor,
SocialService socialService) {
super(uiService, i18n, fxApplicationThreadExecutor);
this.modService = modService;
this.playerService = playerService;
this.mapGeneratorService = mapGeneratorService;
this.socialService = socialService;
}

@Override
Expand All @@ -46,7 +50,8 @@ protected void build(FilterBuilder<GameBean> filterBuilder) {

filterBuilder.checkbox(i18n.get("hideSingleGames"), (selected, game) -> !selected || game.getNumActivePlayers() != 1);

filterBuilder.checkbox(i18n.get("showGamesWithFriends"), (selected, game) -> !selected || playerService.areFriendsInGame(game));
filterBuilder.checkbox(i18n.get("showGamesWithFriends"),
(selected, game) -> !selected || socialService.areFriendsInGame(game));

filterBuilder.checkbox(i18n.get("showGeneratedMaps"), (selected, game) -> !selected || mapGeneratorService.isGeneratedMap(game.getMapFolderName()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.faforever.client.domain.PlayerBean;
import com.faforever.client.i18n.I18n;
import com.faforever.client.player.PlayerService;
import com.faforever.client.player.SocialStatus;
import com.faforever.client.social.SocialService;
import com.faforever.client.util.Assert;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
Expand All @@ -19,16 +19,16 @@
@RequiredArgsConstructor
public class AddFoeMenuItem extends AbstractMenuItem<PlayerBean> {

private final PlayerService playerService;
private final SocialService socialService;
private final I18n i18n;

@Override
protected void onClicked() {
Assert.checkNullIllegalState(object, "no player has been set");
if (object.getSocialStatus() == FRIEND) {
playerService.removeFriend(object);
socialService.removeFriend(object);
}
playerService.addFoe(object);
socialService.addFoe(object);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.faforever.client.domain.PlayerBean;
import com.faforever.client.i18n.I18n;
import com.faforever.client.player.PlayerService;
import com.faforever.client.player.SocialStatus;
import com.faforever.client.social.SocialService;
import com.faforever.client.util.Assert;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
Expand All @@ -19,16 +19,16 @@
@RequiredArgsConstructor
public class AddFriendMenuItem extends AbstractMenuItem<PlayerBean> {

private final PlayerService playerService;
private final SocialService socialService;
private final I18n i18n;

@Override
protected void onClicked() {
Assert.checkNullIllegalState(object, "No player has been set");
if (object.getSocialStatus() == FOE) {
playerService.removeFoe(object);
socialService.removeFoe(object);
}
playerService.addFriend(object);
socialService.addFriend(object);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.faforever.client.domain.PlayerBean;
import com.faforever.client.i18n.I18n;
import com.faforever.client.player.PlayerService;
import com.faforever.client.social.SocialService;
import com.faforever.client.util.Assert;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
Expand All @@ -17,12 +17,12 @@
public class RemoveFoeMenuItem extends AbstractMenuItem<PlayerBean> {

private final I18n i18n;
private final PlayerService playerService;
private final SocialService socialService;

@Override
protected void onClicked() {
Assert.checkNullIllegalState(object, "no player has been set");
playerService.removeFoe(object);
socialService.removeFoe(object);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.faforever.client.domain.PlayerBean;
import com.faforever.client.i18n.I18n;
import com.faforever.client.player.PlayerService;
import com.faforever.client.social.SocialService;
import com.faforever.client.util.Assert;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
Expand All @@ -17,12 +17,12 @@
public class RemoveFriendMenuItem extends AbstractMenuItem<PlayerBean> {

private final I18n i18n;
private final PlayerService playerService;
private final SocialService socialService;

@Override
protected void onClicked() {
Assert.checkNullIllegalState(object, "no player has been set");
playerService.removeFriend(object);
socialService.removeFriend(object);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.faforever.client.domain.PlayerBean;
import com.faforever.client.i18n.I18n;
import com.faforever.client.player.PlayerService;
import com.faforever.client.social.SocialService;
import com.faforever.client.util.Assert;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -16,12 +17,13 @@
public class RemovePlayerNoteMenuItem extends AbstractMenuItem<PlayerBean> {

private final PlayerService playerService;
private final SocialService socialService;
private final I18n i18n;

@Override
protected void onClicked() {
Assert.checkNullIllegalState(object, "No player has been set");
playerService.removeNote(object);
socialService.removeNote(object);
}

@Override
Expand Down
Loading

0 comments on commit 621a275

Please sign in to comment.