From a9020cf8445db2a1365fc6b2b29940f8c5ed3f60 Mon Sep 17 00:00:00 2001 From: Vari Date: Wed, 7 Aug 2024 00:12:45 +0200 Subject: [PATCH] Added Handling of Quitter state streak and rewards. --- .../Api/Matchmaking/MatchmakingController.php | 6 +- .../Api/Player/PlayerController.php | 13 +- dist/app/Models/Game/QuitterState.php | 169 +++++++++++++++++- dist/config/quitter-state.php | 9 + 4 files changed, 184 insertions(+), 13 deletions(-) create mode 100644 dist/config/quitter-state.php diff --git a/dist/app/Http/Controllers/Api/Matchmaking/MatchmakingController.php b/dist/app/Http/Controllers/Api/Matchmaking/MatchmakingController.php index 0f7bb3d..3218afc 100644 --- a/dist/app/Http/Controllers/Api/Matchmaking/MatchmakingController.php +++ b/dist/app/Http/Controllers/Api/Matchmaking/MatchmakingController.php @@ -31,7 +31,6 @@ use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Log; -use Laravel\Prompts\Table; class MatchmakingController extends Controller { @@ -222,6 +221,11 @@ public function playerEndOfMatch(PlayerEndOfMatchRequest $request) $playerData = $user->playerData(); $characterData = $playerData->characterDataForCharacter($request->characterGroup->getCharacter()); + if($request->hasQuit) + $playerData->quitterState->addQuitterPenalty(); + else + $playerData->quitterState->addStayedMatch($playerData); + $experienceSum = 0; foreach ($request->experienceEvents as $experienceEvent) { diff --git a/dist/app/Http/Controllers/Api/Player/PlayerController.php b/dist/app/Http/Controllers/Api/Player/PlayerController.php index f513334..d0485ab 100644 --- a/dist/app/Http/Controllers/Api/Player/PlayerController.php +++ b/dist/app/Http/Controllers/Api/Player/PlayerController.php @@ -104,12 +104,7 @@ public function getQuitterState() { $quitterState = Auth::user()->playerData()->quitterState; - if( $quitterState->strike_refresh_time !== null && - Carbon::parse($quitterState->strike_refresh_time)->isBefore(Carbon::now())) - { - ++$quitterState->strikes_left; - $quitterState->strike_refresh_time = null; - } + $quitterState->checkStrikeRefresh(); $response = new GetQuitterStateResponse( $quitterState->strike_refresh_time === null ? 0 : Carbon::parse($quitterState->strike_refresh_time)->getTimestamp(), @@ -122,11 +117,7 @@ public function getQuitterState() ); if ($quitterState->stay_match_streak > $quitterState->stay_match_streak_previous) - $response->stayMatchStreakRewards[] = new Reward( - RewardType::Currency, - 20, - 'CurrencyA', - ); + $response->stayMatchStreakRewards[] = QuitterState::getReward($quitterState->stay_match_streak); return json_encode($response); } diff --git a/dist/app/Models/Game/QuitterState.php b/dist/app/Models/Game/QuitterState.php index e42b907..5cddeeb 100644 --- a/dist/app/Models/Game/QuitterState.php +++ b/dist/app/Models/Game/QuitterState.php @@ -2,6 +2,9 @@ namespace App\Models\Game; +use App\Enums\Game\RewardType; +use App\Http\Responses\Api\General\Reward; +use App\Models\User\PlayerData; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Carbon; @@ -13,13 +16,177 @@ class QuitterState extends Model { use HasFactory; + protected $casts = [ + 'stay_match_streak' => 'int', + 'stay_match_streak_previous' => 'int', + 'quits' => 'int', + 'quit_match_streak' => 'int', + 'quit_match_streak_previous' => 'int', + 'strikes_left' => 'int', + 'has_quit_once' => 'boolean', + 'strike_refresh_time' => Carbon::class, + ]; + protected $attributes = [ 'stay_match_streak' => 0, 'stay_match_streak_previous' => 0, 'quits' => 0, 'quit_match_streak' => 0, 'quit_match_streak_previous' => 0, - 'strikes_left' => 1, 'has_quit_once' => false, ]; + + protected static function booted() + { + static::creating(function (QuitterState $quitterState) { + $quitterState->strikes_left = config('quitter-state.max-available-strikes'); + }); + } + + public function checkStrikeRefresh(): void + { + if ($this->strike_refresh_time === null || $this->strikes_left === config('quitter-state.max-available-strikes')) + return; + + while ($this->strike_refresh_time !== null) { + $currentRefreshTime = $this->strike_refresh_time; + + if($this->strike_refresh_time->isBefore(Carbon::now())) { + ++$this->strikes_left; + $this->has_quit_once = false; + + if($this->strikes_left < config('quitter-state.max-available-strikes')) { + $this->strike_refresh_time = $currentRefreshTime->add(config('quitter-state.strike-refresh-time')); + } + } + else { + $this->save(); + break; + } + } + } + + public function addStayedMatch(PlayerData &$playerData): void { + ++$this->stay_match_streak; + $rewards = static::getReward($this->stay_match_streak); + + foreach ($rewards as $reward) { + switch ($reward->id) { + case 'CurrencyA': + $playerData->currency_a += $reward->amount; + break; + case 'CurrencyB': + $playerData->currency_b += $reward->amount; + break; + case 'CurrencyC': + $playerData->currency_c += $reward->amount; + break; + } + } + } + + public function addQuitterPenalty(): void { + if ($this->strikes_left === 0) { + $this->stay_match_streak_previous = $this->stay_match_streak; + $this->stay_match_streak = 0; + $this->has_quit_once = true; + } + else { + --$this->strikes_left; + } + + $this->save(); + } + + /** + * @param int $stayMatchStreak + * @return Reward[] + */ + public static function getReward(int $stayMatchStreak): array { + if($stayMatchStreak === 5) { + return [ + new Reward( + RewardType::Currency, + 50, + 'CurrencyA', + ), + ]; + } + else if($stayMatchStreak === 10) { + return [ + new Reward( + RewardType::Currency, + 100, + 'CurrencyA', + ), + new Reward( + RewardType::Currency, + 100, + 'CurrencyB', + ), + new Reward( + RewardType::Currency, + 50, + 'CurrencyC', + ), + ]; + } + else if($stayMatchStreak === 15) { + return [ + new Reward( + RewardType::Currency, + 150, + 'CurrencyA', + ), + new Reward( + RewardType::Currency, + 150, + 'CurrencyB', + ), + new Reward( + RewardType::Currency, + 100, + 'CurrencyC', + ), + ]; + } + else if($stayMatchStreak === 20) { + return [ + new Reward( + RewardType::Currency, + 200, + 'CurrencyA', + ), + new Reward( + RewardType::Currency, + 200, + 'CurrencyB', + ), + new Reward( + RewardType::Currency, + 150, + 'CurrencyC', + ), + ]; + } + else if($stayMatchStreak >= 25 && $stayMatchStreak % 5 === 0) { + return [ + new Reward( + RewardType::Currency, + 250, + 'CurrencyA', + ), + new Reward( + RewardType::Currency, + 250, + 'CurrencyB', + ), + new Reward( + RewardType::Currency, + 200, + 'CurrencyC', + ), + ]; + } + } } diff --git a/dist/config/quitter-state.php b/dist/config/quitter-state.php new file mode 100644 index 0000000..d044cae --- /dev/null +++ b/dist/config/quitter-state.php @@ -0,0 +1,9 @@ + 5, + + 'strike-refresh-time' => CarbonInterval::hour(), +];