Skip to content

Commit

Permalink
Count online players by country
Browse files Browse the repository at this point in the history
  • Loading branch information
micheljung committed Oct 18, 2018
1 parent 79e1399 commit 414b82b
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.WebSocketSession;
Expand Down Expand Up @@ -195,6 +196,11 @@ public void afterSessionStarted(WebSocketSession session, MessageChannel outputC
extractClientDetailsOrNull(sessionPrincipal)
.ifPresent(clientDetails -> clientDetails.setClientConnection(clientConnection));

if (!(sessionPrincipal instanceof Authentication)) {
throw new IllegalStateException("Session principal needs to be a subclass of Authentication");
}
clientConnection.setAuthentication((Authentication) sessionPrincipal);

extractUserDetailsOrNull(sessionPrincipal)
.ifPresent(userDetails -> {
userDetails.setClientConnection(clientConnection);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public Optional<TimeZone> lookupTimezone(InetAddress inetAddress) {
return lookupCity(inetAddress).map(city -> city.getLocation().getTimeZone()).map(TimeZone::getTimeZone);
}

private Optional<CityResponse> lookupCity(InetAddress inetAddress) {
public Optional<CityResponse> lookupCity(InetAddress inetAddress) {
Assert.state(databaseReader != null, "Database has not been initialized");
return noCatch(() -> {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import org.springframework.data.repository.CrudRepository;

import java.util.List;
import java.util.Optional;

public interface OnlinePlayerRepository extends CrudRepository<Player, Integer> {
Optional<Player> findByLogin(String login);

List<Player> findAllByCountry(String country);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.faforever.server.client.ClientService;
import com.faforever.server.geoip.GeoIpService;
import com.faforever.server.stats.Metrics;
import com.google.common.base.Strings;
import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.MeterRegistry;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -26,15 +27,18 @@
@MXBean
public class PlayerService {

private final MeterRegistry meterRegistry;
private final OnlinePlayerRepository onlinePlayerRepository;
private final ClientService clientService;
private final ApplicationEventPublisher eventPublisher;
private final GeoIpService geoIpService;

public static final String TAG_PLAYER_GAME_STATE = "gameState";
private static final String TAG_PLAYER_GAME_COUNTRY = "country";

public PlayerService(ClientService clientService, MeterRegistry meterRegistry, OnlinePlayerRepository onlinePlayerRepository, ApplicationEventPublisher eventPublisher, GeoIpService geoIpService) {
this.clientService = clientService;
this.meterRegistry = meterRegistry;
this.onlinePlayerRepository = onlinePlayerRepository;
this.eventPublisher = eventPublisher;
this.geoIpService = geoIpService;
Expand All @@ -61,6 +65,12 @@ public void setPlayerOnline(Player player) {

eventPublisher.publishEvent(new PlayerOnlineEvent(this, player));
announceOnline(player);

String country = player.getCountry();
Gauge.builder(Metrics.PLAYERS_BY_LOCATION, onlinePlayerRepository, repo -> repo.findAllByCountry(country).size())
.description("The number of online players in country " + country)
.tag(TAG_PLAYER_GAME_COUNTRY, Strings.nullToEmpty(country))
.register(meterRegistry);
}

public void removePlayer(Player player) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ public class Metrics {
public static final String CLIENTS = "counter.clients";
public static final String GAMES = "counter.games";
public static final String PLAYERS = "counter.players";
public static final String PLAYERS_BY_LOCATION = "counter.playersByLocation";
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.net.InetAddress;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
Expand Down Expand Up @@ -45,11 +46,9 @@ public class PlayerServiceTest {
@Mock
private GeoIpService geoIpService;

private OnlinePlayerRepository onlinePlayerRepository;

@Before
public void setUp() throws Exception {
onlinePlayerRepository = new FakeOnlinePlayerRepository();
OnlinePlayerRepository onlinePlayerRepository = new FakeOnlinePlayerRepository();
player = (Player) new Player().setId(1);
player.setLogin("JUnit");
instance = new PlayerService(clientService, meterRegistry, onlinePlayerRepository, eventPublisher, geoIpService);
Expand Down Expand Up @@ -175,6 +174,11 @@ public Optional<Player> findByLogin(String login) {
.filter(player1 -> player1.getLogin().equals(login))
.findFirst();
}

@Override
public List<Player> findAllByCountry(String country) {
return players.values().stream().filter(player -> player.getCountry() != null).collect(Collectors.toList());
}
}

}

0 comments on commit 414b82b

Please sign in to comment.