1<?php
2
3// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
4// See the LICENCE file in the repository root for full licence text.
5
6namespace App\Libraries;
7
8use App\Models\BanchoStats;
9use App\Models\Count;
10use Auth;
11use Cache;
12
13class CurrentStats
14{
15 public int $currentOnline;
16 public int $currentGames;
17 public array $graphData;
18 public int $onlineFriends;
19 public int $totalUsers;
20
21 public function __construct()
22 {
23 $data = Cache::remember('current_stats:v1', 300, function () {
24 $stats = BanchoStats::stats();
25 $latest = array_last($stats);
26
27 return [
28 'currentOnline' => $latest['users'] ?? 0,
29 'currentGames' => $latest['multiplayer_games'] ?? 0,
30 'graphData' => array_to_graph_json($stats, 'users'),
31 'totalUsers' => Count::totalUsers()->count,
32 ];
33 });
34
35 $this->onlineFriends = Auth::user() ? Auth::user()->friends()->online()->count() : 0;
36 $this->currentOnline = $data['currentOnline'];
37 $this->currentGames = $data['currentGames'];
38 $this->graphData = $data['graphData'];
39 $this->totalUsers = $data['totalUsers'];
40 }
41}