Skip to content

Commit

Permalink
Remove race condition that results in newbie channel being joined
Browse files Browse the repository at this point in the history
Fixes #1617
  • Loading branch information
1-alex98 committed Mar 27, 2020
1 parent 44c7059 commit a2dda19
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/main/java/com/faforever/client/chat/PircBotXChatService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.faforever.client.player.PlayerService;
import com.faforever.client.player.SocialStatus;
import com.faforever.client.player.UserOfflineEvent;
import com.faforever.client.player.event.CurrentPlayerInfo;
import com.faforever.client.preferences.ChatPrefs;
import com.faforever.client.preferences.PreferencesService;
import com.faforever.client.remote.FafService;
Expand Down Expand Up @@ -103,6 +104,7 @@ public class PircBotXChatService implements ChatService, InitializingBean, Dispo
private static final List<UserLevel> MODERATOR_USER_LEVELS = Arrays.asList(UserLevel.OP, UserLevel.HALFOP, UserLevel.SUPEROP, UserLevel.OWNER);
private static final int SOCKET_TIMEOUT = 10000;
private static final String NEWBIE_CHANNEL_NAME = "#newbie";
public static final int GAMES_FOR_NEWBIE_CHANNEL = 50;

private final PreferencesService preferencesService;
private final UserService userService;
Expand Down Expand Up @@ -141,6 +143,7 @@ public class PircBotXChatService implements ChatService, InitializingBean, Dispo
* channels after a reconnect that the user left before the reconnect.
*/
private boolean autoChannelsJoined;
private boolean newbieChannelJoined;

@Override
public void afterPropertiesSet() {
Expand Down Expand Up @@ -275,16 +278,19 @@ private void onIdentified() {
channels.keySet().forEach(this::joinChannel);
}
joinSavedAutoChannels();
playerService.getCurrentPlayer().ifPresent(player -> {
if (player.getNumberOfGames() < 50) {
joinChannel(NEWBIE_CHANNEL_NAME);
}
});
}
});
identifiedFuture.complete(null);
}

@Subscribe
public void onCurrentPlayerInfo(CurrentPlayerInfo currentPlayerInfo) {
if (!newbieChannelJoined && currentPlayerInfo.getCurrentPlayer().getNumberOfGames() < GAMES_FOR_NEWBIE_CHANNEL) {
joinChannel(NEWBIE_CHANNEL_NAME);
}
newbieChannelJoined = true;
}

private void joinAutoChannels() {
log.debug("Joining auto channel: {}", autoChannels);
if (autoChannels == null) {
Expand Down Expand Up @@ -489,6 +495,7 @@ public void disconnect() {
}
}
identifiedFuture = new CompletableFuture<>();
newbieChannelJoined = false;
}

@Override
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/faforever/client/player/PlayerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.faforever.client.game.GameAddedEvent;
import com.faforever.client.game.GameRemovedEvent;
import com.faforever.client.game.GameUpdatedEvent;
import com.faforever.client.player.event.CurrentPlayerInfo;
import com.faforever.client.player.event.FriendJoinedGameEvent;
import com.faforever.client.remote.FafService;
import com.faforever.client.remote.domain.GameStatus;
Expand Down Expand Up @@ -323,6 +324,7 @@ private void onPlayerInfo(com.faforever.client.remote.domain.Player dto) {
Player player = getCurrentPlayer().orElseThrow(() -> new IllegalStateException("Player has not been set"));
player.updateFromDto(dto);
player.setSocialStatus(SELF);
eventBus.post(new CurrentPlayerInfo(player));
} else {
Player player = createAndGetPlayerForUsername(dto.getLogin());

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.faforever.client.player.event;

import com.faforever.client.player.Player;
import lombok.Value;

@Value
public class CurrentPlayerInfo {
Player currentPlayer;
}

0 comments on commit a2dda19

Please sign in to comment.