import 'dart:async'; import 'dart:convert'; import 'package:http/http.dart' as http; /// The club's shooting league (liga.bl3kunja.top): the current league, its /// tournaments and the live standings, from the site's public JSON API. /// /// `GET /api/lestvica` is the current league (what the site's Lestvica page /// shows). The app reads it only when the Liga tab is opened (or pulled to /// refresh), never in the background. class LeagueService { static const String site = 'https://liga.bl3kunja.top'; static const Duration _timeout = Duration(seconds: 10); final http.Client _client; LeagueService([http.Client? client]) : _client = client ?? http.Client(); Future standings() async { final res = await _client.get(Uri.parse('$site/api/lestvica')).timeout(_timeout); if (res.statusCode != 200) throw LeagueException('HTTP ${res.statusCode}'); return LeagueStandings.fromJson(jsonDecode(utf8.decode(res.bodyBytes)) as Map); } void dispose() => _client.close(); } class LeagueException implements Exception { final String message; const LeagueException(this.message); @override String toString() => message; } class LeagueStandings { final League league; final List rounds; final List players; // by rank const LeagueStandings({required this.league, required this.rounds, required this.players}); factory LeagueStandings.fromJson(Map j) => LeagueStandings( league: League.fromJson(j['league'] as Map), rounds: [for (final r in (j['rounds'] as List? ?? const [])) LeagueRound.fromJson(r as Map)] ..sort((a, b) => a.number.compareTo(b.number)), players: [for (final p in (j['players'] as List? ?? const [])) LeaguePlayer.fromJson(p as Map)] ..sort((a, b) => a.rank.compareTo(b.rank)), ); /// The tournament that is open now, if any. LeagueRound? get openRound => rounds.where((r) => r.isOpen).firstOrNull; /// The next one to open, if none is open. LeagueRound? get nextRound => rounds.where((r) => r.isUpcoming).firstOrNull; } class League { final int id; final String name; // "Liga 2026-2027" final String status; // "active", … final int counted; // best N tournaments count final int shots; // shots per target final int targets; // targets per tournament const League({required this.id, required this.name, required this.status, required this.counted, required this.shots, required this.targets}); factory League.fromJson(Map j) => League( id: (j['id'] as num?)?.toInt() ?? 0, name: j['name'] as String? ?? '', status: j['status'] as String? ?? '', counted: (j['counted'] as num?)?.toInt() ?? 0, shots: (j['shots'] as num?)?.toInt() ?? 0, targets: (j['targets'] as num?)?.toInt() ?? 0, ); bool get isActive => status == 'active'; } class LeagueRound { final int number; final DateTime? startsAt; final DateTime? endsAt; final String state; // "open", "upcoming", or done final int submissions; // clubs that have entered their results const LeagueRound({required this.number, this.startsAt, this.endsAt, required this.state, required this.submissions}); factory LeagueRound.fromJson(Map j) => LeagueRound( number: (j['number'] as num?)?.toInt() ?? 0, startsAt: DateTime.tryParse(j['starts_at'] as String? ?? ''), endsAt: DateTime.tryParse(j['ends_at'] as String? ?? ''), state: j['state'] as String? ?? '', submissions: (j['submissions'] as num?)?.toInt() ?? 0, ); bool get isOpen => state == 'open'; bool get isUpcoming => state == 'upcoming'; bool get isDone => !isOpen && !isUpcoming; } class LeaguePlayer { final int rank; final String name; final String club; final List cells; // one per tournament; null = not shot final int total; // all tournaments final int finalScore; // the counted ones: what the ranking uses final int tens; // tie-break final int played; const LeaguePlayer({ required this.rank, required this.name, required this.club, required this.cells, required this.total, required this.finalScore, required this.tens, required this.played, }); factory LeaguePlayer.fromJson(Map j) => LeaguePlayer( rank: (j['rank'] as num?)?.toInt() ?? 0, name: j['name'] as String? ?? '', club: j['club'] as String? ?? '', cells: [for (final c in (j['cells'] as List? ?? const [])) c == null ? null : LeagueCell.fromJson(c as Map)], total: (j['total'] as num?)?.toInt() ?? 0, finalScore: (j['final'] as num?)?.toInt() ?? 0, tens: (j['tens'] as num?)?.toInt() ?? 0, played: (j['played'] as num?)?.toInt() ?? 0, ); } class LeagueCell { final int score; final int tens; final bool counted; // among the best N final bool joker; const LeagueCell({required this.score, required this.tens, required this.counted, required this.joker}); factory LeagueCell.fromJson(Map j) => LeagueCell( score: (j['score'] as num?)?.toInt() ?? 0, tens: (j['tens'] as num?)?.toInt() ?? 0, counted: j['counted'] as bool? ?? true, joker: j['joker'] as bool? ?? false, ); }