<commit_before><?php

namespace App\Services\PlayerBans;

use App\Entities\Bans\Repositories\GameBanRepository;
use App\Entities\Bans\Models\GameBan;
use App\Entities\GamePlayerType;
use App\Services\PlayerLookup\PlayerLookupService;
use App\Services\PlayerBans\Exceptions\UserAlreadyBannedException;
use App\Entities\ServerKeys\Models\ServerKey;
use App\Services\PlayerBans\Exceptions\UnauthorisedKeyActionException;
use App\Entities\Bans\Repositories\GameUnbanRepository;
use App\Entities\Bans\Models\GameUnban;
use Illuminate\Support\Facades\DB;
use App\Services\PlayerBans\Exceptions\UserNotBannedException;
use Carbon\Carbon;

final class PlayerBanService
{
    /**
     * @var GameBanRepository
     */
    private $gameBanRepository;

    /**
     * @var GameUnbanRepository
     */
    private $gameUnbanRepository;
    
    /**
     * @var PlayerLookupService
     */
    private $playerLookupService;


    public function __construct(
        GameBanRepository $gameBanRepository,
        GameUnbanRepository $gameUnbanRepository,
        PlayerLookupService $playerLookupService
    ) {
        $this->gameBanRepository = $gameBanRepository;
        $this->gameUnbanRepository = $gameUnbanRepository;
        $this->playerLookupService = $playerLookupService;
    }


    public function ban(
        ServerKey $serverKey,
        int $serverId,
        string $bannedPlayerId,
        GamePlayerType $bannedPlayerType,
        string $bannedPlayerAlias,
        string $staffPlayerId,
        GamePlayerType $staffPlayerType,
        ?string $banReason,
        ?int $banExpiresAt = null,
        bool $isGlobalBan = false
    ) : GameBan {
        // if performing a global ban, assert that the key is allowed to do so
        if ($isGlobalBan && !$serverKey->can_global_ban) {
           throw new UnauthorisedKeyActionException('limited_key', 'This server key does not have permission to create global bans');
       }

       if ($bannedPlayerType->valueOf() === GamePlayerType::Minecraft) {
           // Strip hyphens from Minecraft UUIDs
           $bannedPlayerId = str_replace('-', '', $bannedPlayerId);
       }

        $bannedPlayer = $this->playerLookupService->getOrCreatePlayer($bannedPlayerType, $bannedPlayerId);
        $staffPlayer  = $this->playerLookupService->getOrCreatePlayer($staffPlayerType, $staffPlayerId);
        
        $activeBan = $this->gameBanRepository->getActiveBanByGameUserId($bannedPlayer->getKey(), $bannedPlayerType);

        if ($activeBan !== null) {
            throw new UserAlreadyBannedException('player_already_banned', 'Player is already banned');
        }

        return $this->gameBanRepository->store(
            $serverId,
            $bannedPlayer->getKey(),
            $bannedPlayerType,
            $bannedPlayerAlias,
            $staffPlayer->getKey(),
            $staffPlayerType,
            $banReason,
            true,
            $isGlobalBan,
            $banExpiresAt ? Carbon::createFromTimestamp($banExpiresAt) : null
        );
    }

    public function unban(
        string $bannedPlayerId,
        GamePlayerType $bannedPlayerType,
        string $staffPlayerId,
        GamePlayerType $staffPlayerType
    ) : GameUnban {
        if ($bannedPlayerType->valueOf() === GamePlayerType::Minecraft) {
            // Strip hyphens from Minecraft UUIDs
            $bannedPlayerId = str_replace('-', '', $bannedPlayerId);
        }

        $bannedPlayer = $this->playerLookupService->getOrCreatePlayer($bannedPlayerType, $bannedPlayerId);
        $staffPlayer  = $this->playerLookupService->getOrCreatePlayer($staffPlayerType, $staffPlayerId);
        
        $activeBan = $this->gameBanRepository->getActiveBanByGameUserId($bannedPlayer->getKey(), $bannedPlayerType);
        if ($activeBan === null) {
            throw new UserNotBannedException('player_not_banned', 'This player is not currently banned');
        }

        DB::beginTransaction();
        try {
            $activeBan->is_active = false;
            $activeBan->save();
    
            $unban = $this->gameUnbanRepository->store(
                $activeBan->getKey(),
                $staffPlayer->getKey(),
                $staffPlayerType
            );
            DB::commit();

        } catch (\Exception $e) {
            DB::rollBack();
            throw $e;
        }
        return $unban;
    }
}<commit_msg>Strip hyphens from staff player UUIDs<commit_after><?php

namespace App\Services\PlayerBans;

use App\Entities\Bans\Repositories\GameBanRepository;
use App\Entities\Bans\Models\GameBan;
use App\Entities\GamePlayerType;
use App\Services\PlayerLookup\PlayerLookupService;
use App\Services\PlayerBans\Exceptions\UserAlreadyBannedException;
use App\Entities\ServerKeys\Models\ServerKey;
use App\Services\PlayerBans\Exceptions\UnauthorisedKeyActionException;
use App\Entities\Bans\Repositories\GameUnbanRepository;
use App\Entities\Bans\Models\GameUnban;
use Illuminate\Support\Facades\DB;
use App\Services\PlayerBans\Exceptions\UserNotBannedException;
use Carbon\Carbon;

final class PlayerBanService
{
    /**
     * @var GameBanRepository
     */
    private $gameBanRepository;

    /**
     * @var GameUnbanRepository
     */
    private $gameUnbanRepository;
    
    /**
     * @var PlayerLookupService
     */
    private $playerLookupService;


    public function __construct(
        GameBanRepository $gameBanRepository,
        GameUnbanRepository $gameUnbanRepository,
        PlayerLookupService $playerLookupService
    ) {
        $this->gameBanRepository = $gameBanRepository;
        $this->gameUnbanRepository = $gameUnbanRepository;
        $this->playerLookupService = $playerLookupService;
    }


    public function ban(
        ServerKey $serverKey,
        int $serverId,
        string $bannedPlayerId,
        GamePlayerType $bannedPlayerType,
        string $bannedPlayerAlias,
        string $staffPlayerId,
        GamePlayerType $staffPlayerType,
        ?string $banReason,
        ?int $banExpiresAt = null,
        bool $isGlobalBan = false
    ) : GameBan {
        // if performing a global ban, assert that the key is allowed to do so
        if ($isGlobalBan && !$serverKey->can_global_ban) {
           throw new UnauthorisedKeyActionException('limited_key', 'This server key does not have permission to create global bans');
        }

        if ($bannedPlayerType->valueOf() === GamePlayerType::Minecraft) {
            // Strip hyphens from Minecraft UUIDs
            $bannedPlayerId = str_replace('-', '', $bannedPlayerId);
        }
        if ($staffPlayerType->valueOf() === GamePlayerType::Minecraft) {
            // Strip hyphens from Minecraft UUIDs
            $staffPlayerId = str_replace('-', '', $staffPlayerId);
        }

        $bannedPlayer = $this->playerLookupService->getOrCreatePlayer($bannedPlayerType, $bannedPlayerId);
        $staffPlayer  = $this->playerLookupService->getOrCreatePlayer($staffPlayerType, $staffPlayerId);
        
        $activeBan = $this->gameBanRepository->getActiveBanByGameUserId($bannedPlayer->getKey(), $bannedPlayerType);

        if ($activeBan !== null) {
            throw new UserAlreadyBannedException('player_already_banned', 'Player is already banned');
        }

        return $this->gameBanRepository->store(
            $serverId,
            $bannedPlayer->getKey(),
            $bannedPlayerType,
            $bannedPlayerAlias,
            $staffPlayer->getKey(),
            $staffPlayerType,
            $banReason,
            true,
            $isGlobalBan,
            $banExpiresAt ? Carbon::createFromTimestamp($banExpiresAt) : null
        );
    }

    public function unban(
        string $bannedPlayerId,
        GamePlayerType $bannedPlayerType,
        string $staffPlayerId,
        GamePlayerType $staffPlayerType
    ) : GameUnban {
        if ($bannedPlayerType->valueOf() === GamePlayerType::Minecraft) {
            // Strip hyphens from Minecraft UUIDs
            $bannedPlayerId = str_replace('-', '', $bannedPlayerId);
        }
        if ($staffPlayerType->valueOf() === GamePlayerType::Minecraft) {
            // Strip hyphens from Minecraft UUIDs
            $staffPlayerId = str_replace('-', '', $staffPlayerId);
        }

        $bannedPlayer = $this->playerLookupService->getOrCreatePlayer($bannedPlayerType, $bannedPlayerId);
        $staffPlayer  = $this->playerLookupService->getOrCreatePlayer($staffPlayerType, $staffPlayerId);
        
        $activeBan = $this->gameBanRepository->getActiveBanByGameUserId($bannedPlayer->getKey(), $bannedPlayerType);
        if ($activeBan === null) {
            throw new UserNotBannedException('player_not_banned', 'This player is not currently banned');
        }

        DB::beginTransaction();
        try {
            $activeBan->is_active = false;
            $activeBan->save();
    
            $unban = $this->gameUnbanRepository->store(
                $activeBan->getKey(),
                $staffPlayer->getKey(),
                $staffPlayerType
            );
            DB::commit();

        } catch (\Exception $e) {
            DB::rollBack();
            throw $e;
        }
        return $unban;
    }
}
{'fraction_non_alphanumeric': 0.1057326226476667, 'fraction_numerical': 0.0, 'mean_word_length': 2.655666003976143, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 65, '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': '5993676', 'n_tokens_mistral': 2583, 'n_tokens_neox': 2516, 'n_words': 469}