Search in sources :

Example 66 with Game

use of com.faforever.server.entity.Game in project faf-java-server by FAForever.

the class GameServiceTest method updateGameValidityUnrankedMod.

@Test
public void updateGameValidityUnrankedMod() throws Exception {
    Game game = hostGame(player1);
    game.getSimMods().add(new ModVersion().setRanked(false).setMod(new Mod().setDisplayName("Mod")));
    launchGame(game);
    instance.updateGameValidity(game);
    assertThat(game.getValidity(), is(Validity.BAD_MOD));
}
Also used : ModVersion(com.faforever.server.entity.ModVersion) Game(com.faforever.server.entity.Game) FeaturedMod(com.faforever.server.entity.FeaturedMod) Mod(com.faforever.server.entity.Mod) Test(org.junit.Test)

Example 67 with Game

use of com.faforever.server.entity.Game in project faf-java-server by FAForever.

the class GameServiceTest method scoresCalculatedCorrectly.

/**
 * Tests that scores are calculated based on the reports of the majority of players that are still connected when the
 * game ends, even if there are kiddies trying to trick the system. The only case they are able to do so is if they
 * stay until the end of the game and share the majority of the result reports. The only way to prevent this would be
 * a neutral referee who can always be trusted.
 */
@Test
public void scoresCalculatedCorrectly() throws Exception {
    Player player3 = (Player) new Player().setId(3);
    Player player4 = (Player) new Player().setId(4);
    Player player5 = (Player) new Player().setId(5);
    Player player6 = (Player) new Player().setId(6);
    Player player7 = (Player) new Player().setId(7);
    Player player8 = (Player) new Player().setId(8);
    Game game = hostGame(player1);
    Stream.of(player2, player3, player4, player5, player6, player7, player8).forEach(player -> addPlayer(game, player));
    Stream.of(player1, player3, player5, player7).forEach(player -> instance.updatePlayerOption(player1, player.getId(), OPTION_TEAM, 2));
    Stream.of(player2, player4, player6, player8).forEach(player -> instance.updatePlayerOption(player1, player.getId(), OPTION_TEAM, 3));
    Stream.of(player1, player2, player3, player4, player5, player6, player7, player8).forEach(player -> instance.updatePlayerOption(player1, player.getId(), OPTION_ARMY, player.getId()));
    launchGame(game);
    // 6 of 8 players got killed, player 1 - 3 are the only ones who report the correct result
    Stream.of(player1, player2, player3).forEach(reporter -> {
        // Army 3-8 have been defeated
        IntStream.range(3, 9).forEach(armyId -> {
            instance.reportArmyOutcome(reporter, armyId, Outcome.DEFEAT);
            instance.reportArmyScore(reporter, armyId, -1);
        });
        // Army 1 and 2 scored and are still in game
        IntStream.range(1, 3).forEach(armyId -> instance.reportArmyScore(reporter, armyId, 3));
    });
    // Players 4 - 8 are kiddies who try to trick the score system by reporting everybody but players 1 and 2 lost
    Stream.of(player4, player5, player6, player7, player8).forEach(reporter -> {
        // Army 1 and 2 have allegedly been defeated
        IntStream.range(1, 3).forEach(armyId -> {
            instance.reportArmyOutcome(reporter, armyId, Outcome.DEFEAT);
            instance.reportArmyScore(reporter, armyId, -1);
        });
        // Armies 3 - 8 allegedly won
        IntStream.range(3, 9).forEach(armyId -> {
            instance.reportArmyOutcome(reporter, armyId, Outcome.VICTORY);
            instance.reportArmyScore(reporter, armyId, 10);
        });
    });
    // All but players 1, 2, and 3 disconnect
    Stream.of(player4, player5, player6, player7, player8).forEach(player -> instance.removePlayer(player));
    // Player 1 claims to have defeated player 2, but player 2 and 3 claim that player 1 has been defeated.
    instance.reportArmyScore(player1, 1, 4);
    instance.reportArmyOutcome(player1, 1, Outcome.VICTORY);
    instance.reportArmyScore(player1, 2, 2);
    instance.reportArmyOutcome(player1, 2, Outcome.DEFEAT);
    Stream.of(player2, player3).forEach(reporter -> {
        instance.reportArmyScore(reporter, 1, 4);
        instance.reportArmyOutcome(reporter, 1, Outcome.DEFEAT);
        instance.reportArmyScore(reporter, 2, 2);
        instance.reportArmyOutcome(reporter, 2, Outcome.VICTORY);
    });
    /* Even though the intermediate results are the ones reported most often, the results to be considered are the ones
    with an outcome, since only those players saw the end of the game. */
    assertThat(game.getValidity(), is(Validity.VALID));
    // Expect that reports of all disconnected players have been discarded
    assertThat(game.getReportedArmyResults().values(), hasSize(3));
    // Expect all remaining players to have reported army results for all players
    game.getReportedArmyResults().forEach((playerId, resultMap) -> assertThat("Player " + playerId + " did not report all results", resultMap.size(), is(8)));
    assertThat(game.getPlayerStats().get(1).getScore(), is(4));
    assertThat(game.getPlayerStats().get(2).getScore(), is(2));
    assertThat(game.getPlayerStats().get(3).getScore(), is(-1));
    assertThat(game.getPlayerStats().get(4).getScore(), is(-1));
    assertThat(game.getPlayerStats().get(5).getScore(), is(-1));
    assertThat(game.getPlayerStats().get(6).getScore(), is(-1));
    assertThat(game.getPlayerStats().get(7).getScore(), is(-1));
    assertThat(game.getPlayerStats().get(8).getScore(), is(-1));
}
Also used : Player(com.faforever.server.entity.Player) Game(com.faforever.server.entity.Game) Test(org.junit.Test)

Example 68 with Game

use of com.faforever.server.entity.Game in project faf-java-server by FAForever.

the class GameServiceTest method onClientDisconnectRemovesPlayerAndUnsetsGameAndRemovesGameIfLastPlayer.

@Test
public void onClientDisconnectRemovesPlayerAndUnsetsGameAndRemovesGameIfLastPlayer() throws Exception {
    Game game = hostGame(player1);
    assertThat(player1.getCurrentGame(), is(game));
    assertThat(player1.getGameState(), is(PlayerGameState.LOBBY));
    assertThat(instance.getActiveGame(game.getId()).isPresent(), is(true));
    instance.removePlayer(player1);
    assertThat(player1.getCurrentGame(), is(nullValue()));
    assertThat(player1.getGameState(), is(PlayerGameState.NONE));
    assertThat(instance.getActiveGame(game.getId()).isPresent(), is(false));
}
Also used : Game(com.faforever.server.entity.Game) Test(org.junit.Test)

Example 69 with Game

use of com.faforever.server.entity.Game in project faf-java-server by FAForever.

the class GameServiceTest method updateGameValidityWrongVictoryConditionSandboxNotCoop.

@Test
public void updateGameValidityWrongVictoryConditionSandboxNotCoop() throws Exception {
    Game game = hostGame(player1);
    game.setVictoryCondition(VictoryCondition.SANDBOX);
    when(modService.isCoop(any())).thenReturn(false);
    launchGame(game);
    instance.updateGameValidity(game);
    assertThat(game.getValidity(), is(Validity.WRONG_VICTORY_CONDITION));
}
Also used : Game(com.faforever.server.entity.Game) Test(org.junit.Test)

Example 70 with Game

use of com.faforever.server.entity.Game in project faf-java-server by FAForever.

the class GameServiceTest method updateGameModsCountClearsIfZero.

@Test
public void updateGameModsCountClearsIfZero() throws Exception {
    List<String> modUids = Arrays.asList("1-1-1-1", "2-2-2-2");
    when(modService.findModVersionsByUids(modUids)).thenReturn(Arrays.asList(new ModVersion().setUid("1-1-1-1").setMod(new Mod().setDisplayName("Mod #1")), new ModVersion().setUid("2-2-2-2").setMod(new Mod().setDisplayName("Mod #2"))));
    Game game = hostGame(player1);
    instance.updateGameMods(game, Arrays.asList("1-1-1-1", "2-2-2-2"));
    instance.updateGameModsCount(game, 0);
    assertThat(game.getSimMods(), is(empty()));
}
Also used : ModVersion(com.faforever.server.entity.ModVersion) FeaturedMod(com.faforever.server.entity.FeaturedMod) Mod(com.faforever.server.entity.Mod) Game(com.faforever.server.entity.Game) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Aggregations

Game (com.faforever.server.entity.Game)96 Test (org.junit.Test)71 Player (com.faforever.server.entity.Player)26 FeaturedMod (com.faforever.server.entity.FeaturedMod)15 ModVersion (com.faforever.server.entity.ModVersion)12 ConnectionAware (com.faforever.server.client.ConnectionAware)11 Ladder1v1Rating (com.faforever.server.entity.Ladder1v1Rating)11 List (java.util.List)11 ClientService (com.faforever.server.client.ClientService)10 ServerProperties (com.faforever.server.config.ServerProperties)10 RequestException (com.faforever.server.error.RequestException)10 ModService (com.faforever.server.mod.ModService)10 VisibleForTesting (com.google.common.annotations.VisibleForTesting)10 Duration (java.time.Duration)10 Map (java.util.Map)10 Optional (java.util.Optional)10 Slf4j (lombok.extern.slf4j.Slf4j)10 Service (org.springframework.stereotype.Service)10 ArmyResult (com.faforever.server.entity.ArmyResult)9 GamePlayerStats (com.faforever.server.entity.GamePlayerStats)9