Skip to content

Commit

Permalink
Lazy loading for a leaderboards in redesign was implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
dan63047 committed Sep 14, 2024
1 parent a2a85ce commit c3214f5
Show file tree
Hide file tree
Showing 4 changed files with 255 additions and 160 deletions.
6 changes: 5 additions & 1 deletion lib/data_objects/tetra_league.dart
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,11 @@ class TetraLeague {
apm ?? 0,
pps ?? 0,
vs ?? 0,
decaying);
decaying,
-1,
-1,
Duration(seconds: -1),
-1);

num? getStatByEnum(Stats stat){
switch (stat) {
Expand Down
20 changes: 18 additions & 2 deletions lib/data_objects/tetrio_player_from_leaderboard.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// ignore_for_file: hash_and_equals

import 'dart:math';

import 'package:tetra_stats/data_objects/est_tr.dart';
import 'package:tetra_stats/data_objects/nerd_stats.dart';
import 'package:tetra_stats/data_objects/playstyle.dart';
Expand Down Expand Up @@ -27,6 +29,10 @@ class TetrioPlayerFromLeaderboard {
late NerdStats nerdStats;
late EstTr estTr;
late Playstyle playstyle;
late int gamesPlayedTotal;
late int gamesWonTotal;
late Duration playtime;
late int ar;

TetrioPlayerFromLeaderboard(
this.userId,
Expand All @@ -46,13 +52,19 @@ class TetrioPlayerFromLeaderboard {
this.apm,
this.pps,
this.vs,
this.decaying){
this.decaying,
this.gamesPlayedTotal,
this.gamesWonTotal,
this.playtime,
this.ar){
nerdStats = NerdStats(apm, pps, vs);
estTr = EstTr(apm, pps, vs, nerdStats.app, nerdStats.dss, nerdStats.dsp, nerdStats.gbe);
playstyle = Playstyle(apm, pps, nerdStats.app, nerdStats.vsapm, nerdStats.dsp, nerdStats.gbe, estTr.srarea, estTr.statrank);
}

double get winrate => gamesWon / gamesPlayed;
double get winrateTotal => gamesWonTotal / gamesWonTotal;
double get level => pow((xp / 500), 0.6) + (xp / (5000 + (max(0, xp - 4 * pow(10, 6)) / 5000))) + 1;
double get esttracc => estTr.esttr - tr;
double get s1tr => gxe * 250;

Expand All @@ -66,7 +78,7 @@ class TetrioPlayerFromLeaderboard {
gamesPlayed = json['league']['gamesplayed'] as int;
gamesWon = json['league']['gameswon'] as int;
tr = json['league']['tr'] != null ? json['league']['tr'].toDouble() : 0;
gxe = json['league']['gxe']??-1;
gxe = json['league']['gxe']?.toDouble();
glicko = json['league']['glicko']?.toDouble();
rd = json['league']['rd']?.toDouble();
rank = json['league']['rank'];
Expand All @@ -75,6 +87,10 @@ class TetrioPlayerFromLeaderboard {
pps = json['league']['pps'] != null ? json['league']['pps'].toDouble() : 0.00;
vs = json['league']['vs'] != null ? json['league']['vs'].toDouble(): 0.00;
decaying = json['league']['decaying'];
gamesPlayedTotal = json['gamesplayed'] as int;
gamesWonTotal = json['gameswon'] as int;
playtime = Duration(microseconds: (json['gametime'].toDouble() * 1000000).floor());
ar = json['ar'];
nerdStats = NerdStats(apm, pps, vs);
estTr = EstTr(apm, pps, vs, nerdStats.app, nerdStats.dss, nerdStats.dsp, nerdStats.gbe);
playstyle = Playstyle(apm, pps, nerdStats.app, nerdStats.vsapm, nerdStats.dsp, nerdStats.gbe, estTr.srarea, estTr.statrank);
Expand Down
78 changes: 52 additions & 26 deletions lib/services/tetrio_crud.dart
Original file line number Diff line number Diff line change
Expand Up @@ -798,34 +798,60 @@ class TetrioService extends DB {
}
}

// Stream<TetrioPlayersLeaderboard> fetchFullLeaderboard() async* {
// late double after;
// int lbLength = 100;
// TetrioPlayersLeaderboard leaderboard = await fetchTLLeaderboard();
// after = leaderboard.leaderboard.last.tr;
// while (lbLength == 100){
// TetrioPlayersLeaderboard pseudoLb = await fetchTLLeaderboard(after: after);
// leaderboard.addPlayers(pseudoLb.leaderboard);
// lbLength = pseudoLb.leaderboard.length;
// after = pseudoLb.leaderboard.last.tr;
// yield leaderboard;
// }
// }

// i want to know progress, so i trying to figure out this thing:
// Stream<TetrioPlayersLeaderboard> fetchTLLeaderboardAsStream() async {
// TetrioPlayersLeaderboard? cached = _cache.get("league", TetrioPlayersLeaderboard);
// if (cached != null) return cached;
Future<List<TetrioPlayerFromLeaderboard>> fetchTetrioLeaderboard({double? after, String? lb}) async {
const int lbLength = 100;
// TetrioPlayersLeaderboard? cached = _cache.get("league", TetrioPlayersLeaderboard);
// if (cached != null) return cached;

// Uri url;
// if (kIsWeb) {
// url = Uri.https('ts.dan63.by', 'oskware_bridge.php', {"endpoint": "TLLeaderboard"});
// } else {
// url = Uri.https('ch.tetr.io', 'api/users/lists/league/all');
// }
Uri url;
if (kIsWeb) {
url = Uri.https('ts.dan63.by', 'oskware_bridge.php', {"endpoint": "TLLeaderboard"});
} else {
url = Uri.https('ch.tetr.io', 'api/users/by/${lb??"league"}', {
"limit": "100",
if (after != null && after != -1) "after": "$after:0:0"
});
}
try{
final response = await client.get(url);

// Stream<TetrioPlayersLeaderboard> stream = http.StreamedRequest("GET", url);
// }
switch (response.statusCode) {
case 200:
_lbPositions.clear();
var rawJson = jsonDecode(response.body);
if (rawJson['success']) { // if api confirmed that everything ok
List<TetrioPlayerFromLeaderboard> leaderboard = [];
for (Map<String, dynamic> entry in rawJson['data']['entries']) {
leaderboard.add(TetrioPlayerFromLeaderboard.fromJson(entry, DateTime.fromMillisecondsSinceEpoch(rawJson['cache']['cached_at'])));
}
developer.log("fetchTLLeaderboard: Leaderboard retrieved and cached", name: "services/tetrio_crud");
//_leaderboardsCache[rawJson['cache']['cached_until'].toString()] = leaderboard;
//_cache.store(leaderboard, rawJson['cache']['cached_until']);
return leaderboard;
} else { // idk how to hit that one
developer.log("fetchTLLeaderboard: Bruh", name: "services/tetrio_crud", error: rawJson);
throw Exception("Failed to get leaderboard (problems on the tetr.io side)"); // will it be on tetr.io side?
}
case 403:
throw TetrioForbidden();
case 429:
throw TetrioTooManyRequests();
case 418:
throw TetrioOskwareBridgeProblem();
case 500:
case 502:
case 503:
case 504:
throw TetrioInternalProblem();
default:
developer.log("fetchTLLeaderboard: Failed to fetch leaderboard", name: "services/tetrio_crud", error: response.statusCode);
throw ConnectionIssue(response.statusCode, response.reasonPhrase??"No reason");
}
} on http.ClientException catch (e, s) {
developer.log("$e, $s");
throw http.ClientException(e.message, e.uri);
}
}

TetrioPlayersLeaderboard? getCachedLeaderboard(){
return _cache.get("league", TetrioPlayersLeaderboard);
Expand Down
Loading

0 comments on commit c3214f5

Please sign in to comment.