Skip to content

Commit 740fdd8

Browse files
alexp8GrantBartlettCopilotSneer-ra2rohsyl
authored
Develop to main (#423)
* add debug logs in PodiumController * filter out observers from players_in queue count * Add back missing cache to player stat views, tweak cache to 10 mins (#418) * remvoe yuri check (#419) * remvoe yuri check * just remove the commented code for now, if need it later, fetch from git history * optimize getRecentLadderGames * Update cncnet-api/app/Http/Services/LadderService.php Co-authored-by: Copilot <[email protected]> * Hotfix for the faction policy service. * Fixed getRecentLadderGames. * monitor duration * removing caching from getRecentLadderGames * add more details in the duration log in matchMeUp * revert max connections back to 500 * Add a missing index on games table * fix missing faction icons * feature/audit log (#411) * first commit * Update cncnet-api/app/Models/UserSettings.php Co-authored-by: Copilot <[email protected]> * remove old api, and add twitch profile to the get active matches * implement getActivitylogOptions * create migration, implement activity logging * add LogsActivity to qmMap * null check just in case --------- Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: Grant Bartlett <[email protected]> Co-authored-by: Copilot <[email protected]> Co-authored-by: Sneer <[email protected]> Co-authored-by: rohs <[email protected]>
1 parent 7d20c19 commit 740fdd8

22 files changed

+945
-517
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,4 +269,5 @@ cncnet-api/database/migrations/2022_05_14_214858_createDummyTestAccounts.php
269269
/.backup.env
270270
docker-compose.test.yml
271271
cncnet-api/.bash_history
272-
**/debugging/
272+
**/debugging/
273+
**/vendor/

cncnet-api/app/Http/Controllers/AdminController.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,32 @@
3030
use Illuminate\Validation\ValidationException;
3131
use App\Models\IpAddressHistory;
3232
use App\Models\QmUserId;
33+
use Spatie\Activitylog\Models\Activity;
3334

3435
class AdminController extends Controller
3536
{
37+
/**
38+
* Show audit log events with filtering.
39+
*/
40+
public function getAuditLog(Request $request)
41+
{
42+
if (!$request->user() || !$request->user()->isAdmin()) {
43+
abort(403);
44+
}
45+
46+
$query = Activity::query();
47+
48+
if ($request->filled('model_type')) {
49+
$query->where('subject_type', $request->input('model_type'));
50+
}
51+
if ($request->filled('event')) {
52+
$query->where('event', $request->input('event'));
53+
}
54+
55+
$activities = $query->with('causer')->orderByDesc('created_at')->paginate(30);
56+
57+
return view('admin.audit-log', compact('activities'));
58+
}
3659
/**
3760
* Show all observer users and allow add/remove.
3861
*/

cncnet-api/app/Http/Controllers/ApiQuickMatchController.php

Lines changed: 22 additions & 171 deletions
Original file line numberDiff line numberDiff line change
@@ -111,61 +111,14 @@ private function getStats(Ladder|string $ladder, int $tierId = 1)
111111
];
112112
}
113113

114-
/**
115-
* Fetch details about games thare are currently in match
116-
*/
117-
public function getActiveMatches(Request $request, $ladderAbbrev = null)
118-
{
119-
$games = [];
120-
if ($ladderAbbrev == "all")
121-
{
122-
$ladders = \App\Models\Ladder::query()
123-
->where('private', '=', false)
124-
->with([
125-
'current_history' => function ($q)
126-
{
127-
},
128-
])
129-
->with([
130-
'sides',
131-
'recent_spawned_matches',
132-
'recent_spawned_matches.players',
133-
'recent_spawned_matches.players.clan:id,short',
134-
'recent_spawned_matches.players.player:id,username',
135-
'recent_spawned_matches.map',
136-
])
137-
->get();
138-
139-
foreach ($ladders as $ladder)
140-
{
141-
$results = $this->getActiveMatchesByLadder($ladder);
142-
143-
foreach ($results as $key => $val)
144-
{
145-
$games[$ladder->abbreviation][$key] = $val;
146-
}
147-
}
148-
}
149-
else
150-
{
151-
$results = $this->getActiveMatchesByLadder($ladderAbbrev);
152-
153-
foreach ($results as $key => $val)
154-
{
155-
$games[$ladderAbbrev][$key] = $val;
156-
}
157-
}
158-
159-
return $games;
160-
}
161-
162114
/**
163115
* This V2 function will return the data as json array, where each object in the array is an object.
164116
* V1 returns array of strings.
165117
*/
166-
public function getActiveMatchesV2(Request $request, string $ladderAbbrev)
118+
public function getActiveMatches(Request $request, string $ladderAbbrev)
167119
{
168120
$games = [];
121+
169122
if ($ladderAbbrev == "all")
170123
{
171124
$ladders = Ladder::query()
@@ -178,14 +131,15 @@ public function getActiveMatchesV2(Request $request, string $ladderAbbrev)
178131
'recent_spawned_matches',
179132
'recent_spawned_matches.players',
180133
'recent_spawned_matches.players.clan:id,short',
181-
'recent_spawned_matches.players.player:id,username',
134+
'recent_spawned_matches.players.player:id,user_id,username',
135+
'recent_spawned_matches.players.player.user:id,twitch_profile',
182136
'recent_spawned_matches.map',
183137
])
184138
->get();
185139

186140
$ladders->each(function (Ladder $ladder) use (&$games)
187141
{
188-
$games[$ladder->abbreviation] = $this->getActiveMatchesByLadderV2($ladder);
142+
$games[$ladder->abbreviation] = $this->getActiveMatchesByLadder($ladder);
189143
});
190144
}
191145
else
@@ -200,61 +154,19 @@ public function getActiveMatchesV2(Request $request, string $ladderAbbrev)
200154
'recent_spawned_matches',
201155
'recent_spawned_matches.players',
202156
'recent_spawned_matches.players.clan:id,short',
203-
'recent_spawned_matches.players.player:id,username',
157+
'recent_spawned_matches.players.player:id,user_id,username',
158+
'recent_spawned_matches.players.player.user:id,twitch_profile',
204159
'recent_spawned_matches.map',
205160
])
206161
->first();
207162

208-
$games[$ladder->abbreviation] = $this->getActiveMatchesByLadderV2($ladder);
163+
$games[$ladder->abbreviation] = $this->getActiveMatchesByLadder($ladder);
209164
}
210165

211166
return $games;
212167
}
213168

214-
private function getActiveMatchesByLadder(Ladder|string $ladder)
215-
{
216-
$ladder = is_string($ladder) ? $this->ladderService->getLadderByGame($ladder) : $ladder;
217-
$sides = $ladder->sides->pluck('name', 'local_id')->toArray();
218-
219-
if ($ladder == null)
220-
abort(400, "Invalid ladder provided");
221-
222-
//get all recent QMs that whose games have spawned. (state_type_id == 5)
223-
$qms = $ladder->recent_spawned_matches;
224-
225-
$games = [];
226-
227-
foreach ($qms as $qm) //iterate over every active quick match
228-
{
229-
$map = trim($qm->map->description);
230-
$dt = $qm->created_at;
231-
232-
//get the player data pertaining to this quick match
233-
$players = $qm->players;
234-
235-
$playersString = "";
236-
if ($ladder->clans_allowed)
237-
{
238-
$playersString = $this->getActiveClanMatchesData($sides, $players);
239-
}
240-
else if ($ladder->ladder_type == \App\Models\Ladder::TWO_VS_TWO) // 2v2
241-
{
242-
$playersString = $this->getTeamActivePlayerMatchesData($sides, $players, $qm->created_at);
243-
}
244-
else
245-
{
246-
$playersString = $this->getActivePlayerMatchesData($sides, $players, $qm->created_at);
247-
}
248-
249-
$duration = Carbon::now()->diff($dt);
250-
$duration_formatted = $duration->format('%i mins %s sec');
251-
$games[] = $playersString . " on " . $map . " (" . $duration_formatted . ")";
252-
}
253-
254-
return $games;
255-
}
256-
257-
private function getActiveMatchesByLadderV2(Ladder $ladder)
169+
private function getActiveMatchesByLadder(Ladder $ladder)
258170
{
259171
$sides = $ladder->sides->pluck('name', 'local_id')->toArray();
260172

@@ -282,11 +194,11 @@ private function getActiveMatchesByLadderV2(Ladder $ladder)
282194
}
283195
else if ($ladder->ladder_type == Ladder::TWO_VS_TWO) // 2v2
284196
{
285-
$playersData = $this->getTeamActivePlayerMatchesDataV2($sides, $qmPlayers, $qm->created_at);
197+
$playersData = $this->getTeamActivePlayerMatchesData($sides, $qmPlayers, $qm->created_at);
286198
}
287199
else
288200
{
289-
$playersData = $this->getActivePlayerMatchesDataV2($sides, $qmPlayers, $qm->created_at);
201+
$playersData = $this->getActivePlayerMatchesData($sides, $qmPlayers, $qm->created_at);
290202
}
291203

292204
$duration = Carbon::now()->diff($dt);
@@ -356,29 +268,10 @@ private function getActiveClanMatchesData($sides, $players)
356268
return $playersString;
357269
}
358270

359-
private function getActivePlayerMatchesData($sides, $players, $created_at)
360-
{
361-
$playersString = "";
362-
$dt = new DateTime($created_at);
363-
for ($i = 0; $i < count($players); $i++)
364-
{
365-
$player = $players[$i];
366-
$playerName = "Player" . ($i + 1);
367-
if (abs(Carbon::now()->diffInSeconds($dt)) > 120) //only show real player name if 2mins has passed
368-
$playerName = $player->player->username;
369-
370-
$playersString .= $playerName . " (" . ($sides[$player->actual_side] ?? '') . ")";
371-
372-
if ($i < count($players) - 1)
373-
$playersString .= " vs ";
374-
}
375-
return $playersString;
376-
}
377-
378271
/**
379272
* @return an array containing every player's name and their faction
380273
*/
381-
private function getActivePlayerMatchesDataV2(array $sides, $qmPlayers, $created_at)
274+
private function getActivePlayerMatchesData(array $sides, $qmPlayers, $created_at)
382275
{
383276
$dt = new DateTime($created_at);
384277
$showRealNames = abs(Carbon::now()->diffInSeconds($dt)) > 120;
@@ -390,74 +283,32 @@ private function getActivePlayerMatchesDataV2(array $sides, $qmPlayers, $created
390283
return [
391284
"playerName" => $showRealNames ? $qmPlayer->player->username : "Player" . ($index + 1),
392285
"playerFaction" => $sides[$qmPlayer->actual_side] ?? '',
393-
"playerColor" => $qmPlayer->color
286+
"playerColor" => $qmPlayer->color,
287+
"twitchProfile" => $qmPlayer->player?->user?->twitch_profile
394288
];
395289
})
396290
->all();
397291
}
398292

399-
/**
400-
* returns a 'pretty' message describing the players on each team
401-
*
402-
* should probably return a json array with the data but we are where we are
403-
*/
404-
private function getTeamActivePlayerMatchesData($sides, $players, $created_at)
405-
{
406-
$playersString = "";
407-
$dt = new DateTime($created_at);
408-
$teams = [];
409-
410-
foreach ($players as $player)
411-
{
412-
$teams[$player->team][] = $player;
413-
}
414-
415-
$teamCount = 0;
416-
$playerNum = 0;
417-
foreach ($teams as $teamId => $teammates)
418-
{
419-
$teammateNum = 0;
420-
foreach ($teammates as $player)
421-
{
422-
$playerName = "Player" . ($playerNum + 1);
423-
if (abs(Carbon::now()->diffInSeconds($dt)) > 60) //only show real player name if 1 min has passed
424-
{
425-
$playerName = $player->player->username;
426-
}
427-
$playersString .= $playerName . " (" . ($sides[$player->actual_side] ?? '') . ")";
428-
429-
if ($teammateNum < count($teammates) - 1) // if not last player on the team append ' and '
430-
$playersString .= " and ";
431-
432-
$teammateNum++;
433-
$playerNum++;
434-
}
435-
436-
if ($teamCount < count($teams) - 1) // if not last team append ' vs '
437-
$playersString .= " vs ";
438-
439-
$teamCount++;
440-
}
441-
442-
return $playersString;
443-
}
444-
445-
private function getTeamActivePlayerMatchesDataV2(array $sides, $qmPlayers, $created_at)
293+
private function getTeamActivePlayerMatchesData(array $sides, $qmPlayers, $created_at)
446294
{
447295
$dt = new DateTime($created_at);
448-
$showRealNames = abs(Carbon::now()->diffInSeconds($dt)) > 120;
296+
$showRealNames = abs(Carbon::now()->diffInSeconds($dt)) > 90;
449297

450298
return collect($qmPlayers)
451299
->groupBy('team')
452300
->flatten()
453301
->values()
454302
->map(function ($qmPlayer, $index) use ($sides, $showRealNames)
455303
{
304+
$useRealName = $showRealNames || $qmPlayer->team === "observer";
305+
$faction = $qmPlayer->team === "observer" ? "Observer" : $sides[$qmPlayer->actual_side] ?? '';
456306
return [
457307
"teamId" => $qmPlayer->team,
458-
"playerName" => $showRealNames ? $qmPlayer->player->username : "Player" . ($index + 1),
459-
"playerFaction" => $sides[$qmPlayer->actual_side] ?? '',
460-
"playerColor" => $qmPlayer->color
308+
"playerName" => $useRealName ? $qmPlayer->player->username : "Player" . ($index + 1),
309+
"playerFaction" => $faction,
310+
"playerColor" => $qmPlayer->color,
311+
"twitchProfile" => $qmPlayer->player->user->twitch_profile
461312
];
462313
})
463314
->all();

cncnet-api/app/Http/Services/QuickMatchService.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,6 +1218,12 @@ public function createTeamQmMatch(LadderHistory $history, Collection $maps, Coll
12181218
$this->setTeamSpawns('A', $spawns[0], $teamAPlayers, $qmMatch, $colors);
12191219
$this->setTeamSpawns('B', $spawns[1], $teamBPlayers, $qmMatch, $colors);
12201220

1221+
// Set observers' team to 'observer'
1222+
foreach ($observers->values() as $observer) {
1223+
$qmObserver = $observer->qmPlayer;
1224+
$qmObserver->team = 'observer';
1225+
$qmObserver->save();
1226+
}
12211227
$this->setObserversSpawns($observers, $qmMatch, $colors);
12221228

12231229
return $qmMatch;

cncnet-api/app/Models/Ban.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,29 @@
55
use Carbon\Carbon;
66
use Illuminate\Database\Eloquent\Model;
77
use Illuminate\Support\Facades\Log;
8+
use Spatie\Activitylog\LogOptions;
9+
use Spatie\Activitylog\Traits\LogsActivity;
810

911
class Ban extends Model
1012
{
13+
use LogsActivity;
1114

15+
public function getActivitylogOptions(): LogOptions
16+
{
17+
return LogOptions::defaults()
18+
->logOnly(['*'])
19+
->logOnlyDirty()
20+
->dontSubmitEmptyLogs();
21+
}
1222

1323
public $fillable = [
14-
'admin_id', 'user_id', 'ban_type', 'internal_note', 'plubic_reason', 'expires', 'ip_address_id'
24+
'admin_id',
25+
'user_id',
26+
'ban_type',
27+
'internal_note',
28+
'plubic_reason',
29+
'expires',
30+
'ip_address_id'
1531
];
1632

1733
protected $casts = [

cncnet-api/app/Models/Map.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,25 @@
44

55
use Illuminate\Database\Eloquent\Factories\HasFactory;
66
use Illuminate\Database\Eloquent\Model;
7+
use Spatie\Activitylog\Traits\LogsActivity;
8+
use \Spatie\Activitylog\LogOptions;
79

810
class Map extends Model
911
{
10-
use HasFactory;
12+
use LogsActivity, HasFactory;
13+
1114
protected $table = 'maps';
1215
protected $fillable = ['name', 'hash', 'ladder_id', 'spawn_count'];
1316
public $timestamps = false;
1417

18+
public function getActivitylogOptions(): LogOptions
19+
{
20+
return LogOptions::defaults()
21+
->logOnly(['name', 'hash', 'image_path', 'description'])
22+
->logOnlyDirty()
23+
->dontSubmitEmptyLogs();
24+
}
25+
1526
public function qmMaps()
1627
{
1728
return $this->hasMany(QmMap::class);

0 commit comments

Comments
 (0)