<commit_before>/*
 * New BSD License (BSD-new)
 *
 * Copyright (c) 2015 <NAME>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     - Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     - Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     - Neither the name of the copyright holder nor the names of its contributors
 *       may be used to endorse or promote products derived from this software
 *       without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package net.caseif.flint.steel.round;

import net.caseif.flint.challenger.Challenger;
import net.caseif.flint.common.CommonCore;
import net.caseif.flint.common.arena.CommonArena;
import net.caseif.flint.common.challenger.CommonChallenger;
import net.caseif.flint.common.event.round.CommonRoundTimerStartEvent;
import net.caseif.flint.common.event.round.CommonRoundTimerStopEvent;
import net.caseif.flint.common.event.round.challenger.CommonChallengerJoinRoundEvent;
import net.caseif.flint.common.event.round.challenger.CommonChallengerLeaveRoundEvent;
import net.caseif.flint.common.exception.round.CommonRoundJoinException;
import net.caseif.flint.common.round.CommonJoinResult;
import net.caseif.flint.common.round.CommonRound;
import net.caseif.flint.component.exception.OrphanedComponentException;
import net.caseif.flint.config.ConfigNode;
import net.caseif.flint.exception.round.RoundJoinException;
import net.caseif.flint.lobby.LobbySign;
import net.caseif.flint.minigame.Minigame;
import net.caseif.flint.round.JoinResult;
import net.caseif.flint.round.LifecycleStage;
import net.caseif.flint.round.Round;
import net.caseif.flint.steel.SteelCore;
import net.caseif.flint.steel.arena.SteelArena;
import net.caseif.flint.steel.challenger.SteelChallenger;
import net.caseif.flint.steel.minigame.SteelMinigame;
import net.caseif.flint.steel.util.helper.LocationHelper;
import net.caseif.flint.steel.util.helper.PlayerHelper;
import net.caseif.flint.util.physical.Location3D;

import com.google.common.collect.ImmutableSet;
import org.bukkit.Bukkit;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.entity.Player;

import java.io.IOException;
import java.sql.SQLException;
import java.util.UUID;

/**
 * Implements {@link Round}.
 *
 * @author <NAME>
 */
public class SteelRound extends CommonRound {

    private final int schedulerHandle;
    private boolean timerTicking = true;

    public SteelRound(CommonArena arena, ImmutableSet<LifecycleStage> stages) {
        super(arena, stages);
        schedulerHandle = Bukkit.getScheduler().scheduleSyncRepeatingTask(
                ((SteelMinigame) getArena().getMinigame()).getBukkitPlugin(),
                new RoundWorker(this),
                0L,
                20L
        );
        try {
            ((SteelArena) getArena()).getRollbackHelper().createRollbackDatabase();
        } catch (IOException | SQLException ex) {
            SteelCore.logSevere("Failed to create rollback store");
            ex.printStackTrace();
        }
    }

    @Override
    @SuppressWarnings("DuplicateThrows")
    public JoinResult addChallenger(UUID uuid) throws IllegalStateException, OrphanedComponentException {
        checkState();

        Player bukkitPlayer = Bukkit.getPlayer(uuid);
        if (bukkitPlayer == null) {
            return new CommonJoinResult(JoinResult.Status.PLAYER_OFFLINE);
        }

        if (getChallengers().size() >= getConfigValue(ConfigNode.MAX_PLAYERS)) {
            return new CommonJoinResult(JoinResult.Status.ROUND_FULL);
        }

        for (Minigame mg : CommonCore.getMinigames().values()) {
            for (Challenger c : mg.getChallengers()) {
                if (c.getUniqueId().equals(uuid)) {
                    return new CommonJoinResult(JoinResult.Status.ALREADY_IN_ROUND);
                }
            }
        }

        SteelChallenger challenger = new SteelChallenger(uuid, this);

        try {
            PlayerHelper.storeLocation(bukkitPlayer);
        } catch (IllegalArgumentException | InvalidConfigurationException | IOException ex) {
            return new CommonJoinResult(ex);
        }


        bukkitPlayer.teleport(LocationHelper.convertLocation(nextSpawnPoint()));

        getChallengerMap().put(uuid, challenger);

        for (LobbySign sign : getArena().getLobbySigns()) {
            sign.update();
        }

        try {
            PlayerHelper.pushInventory(bukkitPlayer);
        } catch (IOException ex) {
            return new CommonJoinResult(ex);
        }

        getArena().getMinigame().getEventBus().post(new CommonChallengerJoinRoundEvent(challenger));
        return new CommonJoinResult(challenger);
    }

    @SuppressWarnings({"DuplicateThrows", "deprecation"})
    @Override
    public Challenger _INVALID_addChallenger(UUID uuid) throws IllegalStateException, RoundJoinException,
            OrphanedComponentException {
        JoinResult result = addChallenger(uuid);
        RoundJoinException.Reason reason;
        switch (result.getStatus()) {
            case SUCCESS: {
                return result.getChallenger();
            }
            case INTERNAL_ERROR: {
                throw new CommonRoundJoinException(uuid, this, result.getThrowable());
            }
            case ALREADY_IN_ROUND: {
                reason = RoundJoinException.Reason.ALREADY_ENTERED;
                break;
            }
            case PLAYER_OFFLINE: {
                reason = RoundJoinException.Reason.OFFLINE;
                break;
            }
            case ROUND_FULL: {
                reason = RoundJoinException.Reason.FULL;
                break;
            }
            default: {
                throw new AssertionError();
            }
        }
        throw new CommonRoundJoinException(uuid, this, reason);
    }

    @Override // overridden from CommonRound
    public void removeChallenger(Challenger challenger, boolean isDisconnecting, boolean updateSigns,
            boolean roundEnding) throws OrphanedComponentException {
        super.removeChallenger(challenger, isDisconnecting, updateSigns, roundEnding);

        Player bukkitPlayer = Bukkit.getPlayer(challenger.getUniqueId());
        Location3D returnPoint;
        try {
            returnPoint = PlayerHelper.getReturnLocation(bukkitPlayer);
        } catch (InvalidConfigurationException | IOException ex) {
            ex.printStackTrace();
            returnPoint = LocationHelper.convertLocation(Bukkit.getWorlds().get(0).getSpawnLocation());
        }

        if (!isDisconnecting) {
            try {
                PlayerHelper.popInventory(bukkitPlayer);
            } catch (InvalidConfigurationException | IOException ex) {<commit_msg>Move invalid addChallenger method to Common<commit_after>/*
 * New BSD License (BSD-new)
 *
 * Copyright (c) 2015 <NAME>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     - Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     - Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     - Neither the name of the copyright holder nor the names of its contributors
 *       may be used to endorse or promote products derived from this software
 *       without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package net.caseif.flint.steel.round;

import net.caseif.flint.challenger.Challenger;
import net.caseif.flint.common.CommonCore;
import net.caseif.flint.common.arena.CommonArena;
import net.caseif.flint.common.challenger.CommonChallenger;
import net.caseif.flint.common.event.round.CommonRoundTimerStartEvent;
import net.caseif.flint.common.event.round.CommonRoundTimerStopEvent;
import net.caseif.flint.common.event.round.challenger.CommonChallengerJoinRoundEvent;
import net.caseif.flint.common.event.round.challenger.CommonChallengerLeaveRoundEvent;
import net.caseif.flint.common.exception.round.CommonRoundJoinException;
import net.caseif.flint.common.round.CommonJoinResult;
import net.caseif.flint.common.round.CommonRound;
import net.caseif.flint.component.exception.OrphanedComponentException;
import net.caseif.flint.config.ConfigNode;
import net.caseif.flint.exception.round.RoundJoinException;
import net.caseif.flint.lobby.LobbySign;
import net.caseif.flint.minigame.Minigame;
import net.caseif.flint.round.JoinResult;
import net.caseif.flint.round.LifecycleStage;
import net.caseif.flint.round.Round;
import net.caseif.flint.steel.SteelCore;
import net.caseif.flint.steel.arena.SteelArena;
import net.caseif.flint.steel.challenger.SteelChallenger;
import net.caseif.flint.steel.minigame.SteelMinigame;
import net.caseif.flint.steel.util.helper.LocationHelper;
import net.caseif.flint.steel.util.helper.PlayerHelper;
import net.caseif.flint.util.physical.Location3D;

import com.google.common.collect.ImmutableSet;
import org.bukkit.Bukkit;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.entity.Player;

import java.io.IOException;
import java.sql.SQLException;
import java.util.UUID;

/**
 * Implements {@link Round}.
 *
 * @author <NAME>
 */
public class SteelRound extends CommonRound {

    private final int schedulerHandle;
    private boolean timerTicking = true;

    public SteelRound(CommonArena arena, ImmutableSet<LifecycleStage> stages) {
        super(arena, stages);
        schedulerHandle = Bukkit.getScheduler().scheduleSyncRepeatingTask(
                ((SteelMinigame) getArena().getMinigame()).getBukkitPlugin(),
                new RoundWorker(this),
                0L,
                20L
        );
        try {
            ((SteelArena) getArena()).getRollbackHelper().createRollbackDatabase();
        } catch (IOException | SQLException ex) {
            SteelCore.logSevere("Failed to create rollback store");
            ex.printStackTrace();
        }
    }

    @Override
    @SuppressWarnings("DuplicateThrows")
    public JoinResult addChallenger(UUID uuid) throws IllegalStateException, OrphanedComponentException {
        checkState();

        Player bukkitPlayer = Bukkit.getPlayer(uuid);
        if (bukkitPlayer == null) {
            return new CommonJoinResult(JoinResult.Status.PLAYER_OFFLINE);
        }

        if (getChallengers().size() >= getConfigValue(ConfigNode.MAX_PLAYERS)) {
            return new CommonJoinResult(JoinResult.Status.ROUND_FULL);
        }

        for (Minigame mg : CommonCore.getMinigames().values()) {
            for (Challenger c : mg.getChallengers()) {
                if (c.getUniqueId().equals(uuid)) {
                    return new CommonJoinResult(JoinResult.Status.ALREADY_IN_ROUND);
                }
            }
        }

        SteelChallenger challenger = new SteelChallenger(uuid, this);

        try {
            PlayerHelper.storeLocation(bukkitPlayer);
        } catch (IllegalArgumentException | InvalidConfigurationException | IOException ex) {
            return new CommonJoinResult(ex);
        }


        bukkitPlayer.teleport(LocationHelper.convertLocation(nextSpawnPoint()));

        getChallengerMap().put(uuid, challenger);

        for (LobbySign sign : getArena().getLobbySigns()) {
            sign.update();
        }

        try {
            PlayerHelper.pushInventory(bukkitPlayer);
        } catch (IOException ex) {
            return new CommonJoinResult(ex);
        }

        getArena().getMinigame().getEventBus().post(new CommonChallengerJoinRoundEvent(challenger));
        return new CommonJoinResult(challenger);
    }

    @Override // overridden from CommonRound
    public void removeChallenger(Challenger challenger, boolean isDisconnecting, boolean updateSigns,
            boolean roundEnding) throws OrphanedComponentException {
        super.removeChallenger(challenger, isDisconnecting, updateSigns, roundEnding);

        Player bukkitPlayer = Bukkit.getPlayer(challenger.getUniqueId());
        Location3D returnPoint;
        try {
            returnPoint = PlayerHelper.getReturnLocation(bukkitPlayer);
        } catch (InvalidConfigurationException | IOException ex) {
            ex.printStackTrace();
            returnPoint = LocationHelper.convertLocation(Bukkit.getWorlds().get(0).getSpawnLocation());
        }

        if (!isDisconnecting) {
            try {
                PlayerHelper.popInventory(bukkitPlayer);
            } catch (InvalidConfigurationException | IOException ex) {
{'fraction_non_alphanumeric': 0.0852008745558896, 'fraction_numerical': 0.001366493577480186, 'mean_word_length': 3.215725806451613, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, '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': '675429', 'n_tokens_mistral': 4109, 'n_tokens_neox': 3938, 'n_words': 970}