<commit_before>package se.cygni.snake.tournament;

import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import se.cygni.game.Player;
import se.cygni.snake.api.event.GameEndedEvent;
import se.cygni.snake.api.event.TournamentEndedEvent;
import se.cygni.snake.api.exception.InvalidPlayerName;
import se.cygni.snake.api.exception.NoActiveTournament;
import se.cygni.snake.api.model.GameMode;
import se.cygni.snake.api.model.GameSettings;
import se.cygni.snake.api.model.PlayerPoints;
import se.cygni.snake.api.request.RegisterMove;
import se.cygni.snake.api.request.RegisterPlayer;
import se.cygni.snake.api.response.PlayerRegistered;
import se.cygni.snake.api.util.MessageUtils;
import se.cygni.snake.apiconversion.GameSettingsConverter;
import se.cygni.snake.apiconversion.TournamentPlanConverter;
import se.cygni.snake.event.InternalGameEvent;
import se.cygni.snake.eventapi.model.TournamentGamePlan;
import se.cygni.snake.eventapi.model.TournamentInfo;
import se.cygni.snake.game.*;
import se.cygni.snake.player.HistoricalPlayer;
import se.cygni.snake.player.IPlayer;
import se.cygni.snake.player.RemotePlayer;
import se.cygni.snake.tournament.util.TournamentUtil;

import java.util.*;

@Component
public class TournamentManager {
    private static final Logger log = LoggerFactory.getLogger(TournamentManager.class);

    private GameManager gameManager;
    private final EventBus outgoingEventBus;
    private final EventBus incomingEventBus;
    private final EventBus globalEventBus;

    private boolean tournamentActive = false;
    private boolean tournamentStarted = false;
    private String tournamentId;
    private String tournamentName;
    private GameFeatures gameFeatures = new GameFeatures();
    private TournamentPlan tournamentPlan;
    private PlayerManager playerManager = new PlayerManager();
    private PlayerManager playersStillInTournament = new PlayerManager();
    private int currentLevel = 0;
    private HashMap<String, Game> games = new HashMap<>();
    private HashMap<String, TournamentPlannedGame> tournamentGameMap = new HashMap<>();

    @Autowired
    public TournamentManager(GameManager gameManager, EventBus globalEventBus) {
        this.gameManager = gameManager;
        this.globalEventBus = globalEventBus;

        this.outgoingEventBus = new EventBus("tournament-outgoing");
        this.incomingEventBus = new EventBus("tournament-incoming");

        incomingEventBus.register(this);
        globalEventBus.register(this);
    }

    public TournamentInfo getTournamentInfo() {
        TournamentInfo ti = new TournamentInfo(
                getTournamentId(),
                getTournamentName(),
                getGameSettings(),
                getTournamentPlan());

        if (isTournamentComplete()) {
            TournamentPlannedGame lastGame = tournamentPlan.getLevelAt(currentLevel-1).getPlannedGames().get(0);
            HistoricalPlayer winner = lastGame.getHistoricalPlayer(lastGame.getGameResult().getWinner().getPlayerId());

            ti.setWinner(TournamentPlanConverter.getPlayer(winner));
        }

        return ti;
    }

    public void killTournament() {
        tournamentActive = false;
        tournamentStarted = false;

        tournamentId = null;
        tournamentName = null;
        gameFeatures = null;
        tournamentPlan = null;
        currentLevel = 0;

        playerManager.clear();
        playersStillInTournament.clear();<commit_msg>Send tournamentEndedMessage when tournament is killed<commit_after>package se.cygni.snake.tournament;

import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import se.cygni.game.Player;
import se.cygni.snake.api.event.GameEndedEvent;
import se.cygni.snake.api.event.TournamentEndedEvent;
import se.cygni.snake.api.exception.InvalidPlayerName;
import se.cygni.snake.api.exception.NoActiveTournament;
import se.cygni.snake.api.model.GameMode;
import se.cygni.snake.api.model.GameSettings;
import se.cygni.snake.api.model.PlayerPoints;
import se.cygni.snake.api.request.RegisterMove;
import se.cygni.snake.api.request.RegisterPlayer;
import se.cygni.snake.api.response.PlayerRegistered;
import se.cygni.snake.api.util.MessageUtils;
import se.cygni.snake.apiconversion.GameSettingsConverter;
import se.cygni.snake.apiconversion.TournamentPlanConverter;
import se.cygni.snake.event.InternalGameEvent;
import se.cygni.snake.eventapi.model.TournamentGamePlan;
import se.cygni.snake.eventapi.model.TournamentInfo;
import se.cygni.snake.game.*;
import se.cygni.snake.player.HistoricalPlayer;
import se.cygni.snake.player.IPlayer;
import se.cygni.snake.player.RemotePlayer;
import se.cygni.snake.tournament.util.TournamentUtil;

import java.util.*;

@Component
public class TournamentManager {
    private static final Logger log = LoggerFactory.getLogger(TournamentManager.class);

    private GameManager gameManager;
    private final EventBus outgoingEventBus;
    private final EventBus incomingEventBus;
    private final EventBus globalEventBus;

    private boolean tournamentActive = false;
    private boolean tournamentStarted = false;
    private String tournamentId;
    private String tournamentName;
    private GameFeatures gameFeatures = new GameFeatures();
    private TournamentPlan tournamentPlan;
    private PlayerManager playerManager = new PlayerManager();
    private PlayerManager playersStillInTournament = new PlayerManager();
    private int currentLevel = 0;
    private HashMap<String, Game> games = new HashMap<>();
    private HashMap<String, TournamentPlannedGame> tournamentGameMap = new HashMap<>();

    @Autowired
    public TournamentManager(GameManager gameManager, EventBus globalEventBus) {
        this.gameManager = gameManager;
        this.globalEventBus = globalEventBus;

        this.outgoingEventBus = new EventBus("tournament-outgoing");
        this.incomingEventBus = new EventBus("tournament-incoming");

        incomingEventBus.register(this);
        globalEventBus.register(this);
    }

    public TournamentInfo getTournamentInfo() {
        TournamentInfo ti = new TournamentInfo(
                getTournamentId(),
                getTournamentName(),
                getGameSettings(),
                getTournamentPlan());

        if (isTournamentComplete()) {
            TournamentPlannedGame lastGame = tournamentPlan.getLevelAt(currentLevel-1).getPlannedGames().get(0);
            HistoricalPlayer winner = lastGame.getHistoricalPlayer(lastGame.getGameResult().getWinner().getPlayerId());

            ti.setWinner(TournamentPlanConverter.getPlayer(winner));
        }

        return ti;
    }

    public void killTournament() {
        // Disconnect all players
        TournamentEndedEvent tee = new TournamentEndedEvent(
                "aborted",
                "aborted",
                new ArrayList<PlayerPoints>(),
                tournamentName,
                tournamentId);

        playerManager.toSet().forEach( player -> {
            player.onTournamentEnded(tee);
        });

        tournamentActive = false;
        tournamentStarted = false;

        tournamentId = null;
        tournamentName = null;
        gameFeatures = null;
        tournamentPlan = null;
        currentLevel = 0;

        playerManager.clear();
        playersStillInTournament.clear();
{'fraction_non_alphanumeric': 0.09057829759584146, 'fraction_numerical': 0.0015594541910331384, 'mean_word_length': 3.7830950901180858, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2404382', 'n_tokens_mistral': 2248, 'n_tokens_neox': 2117, 'n_words': 404}