Skip to content

Commit

Permalink
Check for active nicks each month
Browse files Browse the repository at this point in the history
* Resolves #25
* Automatically assign active handles if they're not active when requesting
matchups
*
  • Loading branch information
GrantBartlett committed Sep 1, 2019
1 parent 7231bef commit df8ddbc
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 10 deletions.
7 changes: 4 additions & 3 deletions cncnet-api/app/Http/Controllers/AccountController.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,11 @@ public function toggleUsernameStatus(Request $request)
}

$date = Carbon::now();
$startOfMonth = $date->startOfMonth()->toDateTimeString();
$endOfMonth = $date->endOfMonth()->toDateTimeString();

// Check if there are active handles within this month
$hasActiveHandles = PlayerActiveHandle::getUserActiveHandleCount($user->id, $ladder->id, $endOfMonth);
$hasActiveHandles = PlayerActiveHandle::getUserActiveHandleCount($user->id, $ladder->id, $startOfMonth, $endOfMonth);

// Allow TS players to have 3 nicks as opposed to just 1
// Other games are still restricted to 1
Expand All @@ -124,12 +125,12 @@ public function toggleUsernameStatus(Request $request)
{
$request->session()->flash('error', 'You have a username active for this month and ladder already.
If you are trying to make a username inactive, the month we are in has to complete first.');

return redirect("/account");
}

// Get the player thats being requested to change
$activeHandle = PlayerActiveHandle::getPlayerActiveHandle($player->id, $ladder->id);
$activeHandle = PlayerActiveHandle::getPlayerActiveHandle($player->id, $ladder->id, $startOfMonth, $endOfMonth);

// If it's not an active handle make it one
if ($activeHandle == null)
Expand Down
13 changes: 12 additions & 1 deletion cncnet-api/app/Http/Controllers/ApiQuickMatchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use \App\Http\Services\PlayerService;
use \App\Http\Services\PointService;
use \App\Http\Services\AuthService;
use \App\PlayerActiveHandle;
use \Carbon\Carbon;
use DB;
use Log;
Expand Down Expand Up @@ -91,15 +92,25 @@ public function matchRequest(Request $request, $ladderAbbrev = null, $playerName
{
return $check;
}

$player = $this->playerService->findPlayerByUsername($playerName, $ladder);

if ($player == null)
{
return array("type"=>"fail", "description" => "$playerName is not registered in $ladderAbbrev");
}

$date = Carbon::now();
$startOfMonth = $date->startOfMonth()->toDateTimeString();
$endOfMonth = $date->endOfMonth()->toDateTimeString();

// Player checks - ensure nick is registered as an active handle

$hasActiveHandle = PlayerActiveHandle::getPlayerActiveHandle($player->id, $ladder->id, $startOfMonth, $endOfMonth);
if ($hasActiveHandle == null)
{
PlayerActiveHandle::setPlayerActiveHandle($ladder->id, $player->id, $player->user->id);
}


$ban = $player->user->getBan(true);
if ($ban !== null)
Expand Down
6 changes: 5 additions & 1 deletion cncnet-api/app/Http/Controllers/ApiUserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ public function getAccount(Request $request)

private function getActivePlayerList($userId)
{
$activeHandles = PlayerActiveHandle::where("user_id", $userId)->get();
$date = Carbon::now();
$startOfMonth = $date->startOfMonth()->toDateTimeString();
$endOfMonth = $date->endOfMonth()->toDateTimeString();

$activeHandles = PlayerActiveHandle::getUserActiveHandles($userId, $startOfMonth, $endOfMonth);

$players = [];
foreach($activeHandles as $activeHandle)
Expand Down
17 changes: 15 additions & 2 deletions cncnet-api/app/PlayerActiveHandle.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,33 @@ public static function setPlayerActiveHandle($ladderId, $playerId, $userId)
return $activeHandle;
}

public static function getPlayerActiveHandle($playerId, $ladderId)
public static function getPlayerActiveHandle($playerId, $ladderId, $dateStart, $dateEnd)
{
$activeHandle = PlayerActiveHandle::where("player_id", $playerId)
->where("ladder_id", $ladderId)
->where("created_at", ">=", $dateStart)
->where("created_at", "<=", $dateEnd)
->first();

return $activeHandle;
}

public static function getUserActiveHandles($userId, $dateStart, $dateEnd)
{
$hasActiveHandles = PlayerActiveHandle::where("user_id", $userId)
->where("created_at", ">=", $dateStart)
->where("created_at", "<=", $dateEnd)
->get();

return $hasActiveHandles;
}

public static function getUserActiveHandleCount($userId, $ladderId,
$dateEnd)
$dateStart, $dateEnd)
{
$hasActiveHandles = PlayerActiveHandle::where("ladder_id", $ladderId)
->where("user_id", $userId)
->where("created_at", ">=", $dateStart)
->where("created_at", "<=", $dateEnd)
->count();

Expand Down
9 changes: 6 additions & 3 deletions cncnet-api/resources/views/auth/account.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,12 @@
->where("ladder_id", $u->ladder_id)
->first();
$activeHandle = \App\PlayerActiveHandle::where("ladder_id", $u->ladder_id)
->where("player_id", $player->id)
->first();
$date = \Carbon\Carbon::now();
$startOfMonth = $date->startOfMonth()->toDateTimeString();
$endOfMonth = $date->endOfMonth()->toDateTimeString();
$activeHandle = \App\PlayerActiveHandle::getPlayerActiveHandle($player->id, $u->ladder_id,
$startOfMonth, $endOfMonth);
?>

<div class="username-status">
Expand Down

0 comments on commit df8ddbc

Please sign in to comment.