Show live online-players count on the home screen

- OnlineService.getOnlineCount(); mock random-walks a believable number,
  SignalrService reads GET /api/stats/online (server tracks hub connections)
- Home screen badge with pulsing dot, polls every 8s, localized digits

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
soroush.asadi
2026-06-04 13:20:51 +03:30
parent ceccf70de7
commit 292dedd843
7 changed files with 75 additions and 1 deletions
+2
View File
@@ -27,6 +27,7 @@ const fa: Dict = {
"home.start": "بزن بریم",
"home.howTo": "آموزش بازی",
"home.lang": "English",
"home.onlineCount": "{n} نفر آنلاین",
"seat.you": "شما",
"team.us": "ما",
@@ -249,6 +250,7 @@ const en: Dict = {
"home.start": "Let's go",
"home.howTo": "How to play",
"home.lang": "فارسی",
"home.onlineCount": "{n} players online",
"seat.you": "You",
"team.us": "Us",
+8
View File
@@ -737,6 +737,14 @@ export class MockOnlineService implements OnlineService {
/* --------------------- leaderboard / shop / daily ------------------ */
private onlineCount = 600 + Math.floor(Math.random() * 900);
async getOnlineCount(): Promise<number> {
// gentle random walk so the badge feels alive
this.onlineCount += Math.round((Math.random() - 0.5) * 40);
this.onlineCount = Math.max(120, Math.min(6000, this.onlineCount));
return this.onlineCount;
}
async getLeaderboard(): Promise<LeaderboardEntry[]> {
const p = await this.getProfile();
const others = Array.from({ length: 24 }, () => ({
+3
View File
@@ -98,6 +98,9 @@ export interface OnlineService {
getMatchPlayers(): { id: string; displayName: string; avatar: string; level: number }[] | null;
submitMatchResult(summary: MatchSummary): Promise<RewardResult>;
/* ----- stats ----- */
getOnlineCount(): Promise<number>;
/* ----- leaderboard / shop / daily ----- */
getLeaderboard(): Promise<LeaderboardEntry[]>;
getShopItems(): Promise<ShopItem[]>;
+13
View File
@@ -262,6 +262,19 @@ export class SignalrService implements OnlineService {
markRead(id: string) { return this.mock.markRead(id); }
onChat(cb: (id: string, m: ChatMessage[]) => void) { return this.mock.onChat(cb); }
async getOnlineCount(): Promise<number> {
try {
const res = await fetch(`${SERVER}/api/stats/online`);
if (res.ok) {
const j = (await res.json()) as { online: number };
return j.online ?? 0;
}
} catch {
/* fall through */
}
return this.mock.getOnlineCount();
}
getLeaderboard(): Promise<LeaderboardEntry[]> { return this.mock.getLeaderboard(); }
getShopItems(): Promise<ShopItem[]> { return this.mock.getShopItems(); }
buyItem(id: string) { return this.mock.buyItem(id); }