From 5feadcfe0914a327c491c158831f654ce5d5f96b Mon Sep 17 00:00:00 2001 From: dan63047 Date: Thu, 11 May 2023 23:08:01 +0300 Subject: [PATCH] some shit happening idk --- lib/data_objects/tetrio.dart | 98 ++++++++++++++++++++++++++---------- lib/views/main_view.dart | 43 +++++++++++++--- 2 files changed, 106 insertions(+), 35 deletions(-) diff --git a/lib/data_objects/tetrio.dart b/lib/data_objects/tetrio.dart index d7b2b7e..0b72fdb 100644 --- a/lib/data_objects/tetrio.dart +++ b/lib/data_objects/tetrio.dart @@ -1,4 +1,27 @@ import 'dart:math'; +import 'package:http/http.dart' as http; +import 'dart:convert'; + +Future fetchTetrioRecords(String user) async { + var url = Uri.https('ch.tetr.io', 'api/users/$user/records'); + final response = await http.get(url); + // final response = await http.get(Uri.parse('https://ch.tetr.io/')); + + if (response.statusCode == 200) { + // If the server did return a 200 OK response, + // then parse the JSON. + return TetrioPlayer.fromJson(jsonDecode(response.body)); + } else { + // If the server did not return a 200 OK response, + // then throw an exception. + throw Exception('Failed to fetch player'); + } +} + +Duration doubleSecondsToDuration(double value) { + value = value * 1000000; + return Duration(microseconds: value.floor()); +} class TetrioPlayer { String? userId; @@ -13,7 +36,7 @@ class TetrioPlayer { int? friendCount; int? gamesPlayed; int? gamesWon; - double? gameTime; + Duration? gameTime; double? xp; int? supporterTier; bool? verified; @@ -52,35 +75,56 @@ class TetrioPlayer { } TetrioPlayer.fromJson(Map json) { - userId = json['data']['user']['_id']; - username = json['data']['user']['username']; - role = json['data']['user']['role']; - registrationTime = json['data']['user']['ts'] != null - ? DateTime.parse(json['data']['user']['ts']) - : null; - if (json['data']['user']['badges'] != null) { + userId = json['_id']; + username = json['username']; + role = json['role']; + registrationTime = json['ts'] != null ? DateTime.parse(json['ts']) : null; + if (json['badges'] != null) { badges = []; - json['data']['user']['badges'].forEach((v) { + json['badges'].forEach((v) { badges!.add(Badge.fromJson(v)); }); } - xp = json['data']['user']['xp'].toDouble(); - gamesPlayed = json['data']['user']['gamesplayed']; - gamesWon = json['data']['user']['gameswon']; - gameTime = json['data']['user']['gametime'].toDouble(); - country = json['data']['user']['country']; - supporterTier = json['data']['user']['supporter_tier']; - verified = json['data']['user']['verified']; - tlSeason1 = json['data']['user']['league'] != null - ? TetraLeagueAlpha.fromJson(json['data']['user']['league']) + xp = json['xp'].toDouble(); + gamesPlayed = json['gamesplayed']; + gamesWon = json['gameswon']; + gameTime = doubleSecondsToDuration(json['gametime'].toDouble()); + country = json['country']; + supporterTier = json['supporter_tier']; + verified = json['verified']; + tlSeason1 = json['league'] != null + ? TetraLeagueAlpha.fromJson(json['league']) : null; - avatarRevision = json['data']['user']['avatar_revision']; - bannerRevision = json['data']['user']['banner_revision']; - bio = json['data']['user']['bio']; - connections = json['data']['user']['connections'] != null - ? Connections.fromJson(json['data']['user']['connections']) + avatarRevision = json['avatar_revision']; + bannerRevision = json['banner_revision']; + bio = json['bio']; + connections = json['connections'] != null + ? Connections.fromJson(json['connections']) : null; - friendCount = json['data']['user']['friend_count']; + var url = Uri.https('ch.tetr.io', 'api/users/$userId/records'); + Future response = http.get(url); + response.then((value) { + if (value.statusCode == 200) { + sprint = jsonDecode(value.body)['data']['records']['40l']['record'] != + null + ? [ + RecordSingle.fromJson( + jsonDecode(value.body)['data']['records']['40l']['record']) + ] + : null; + blitz = + jsonDecode(value.body)['data']['records']['blitz']['record'] != null + ? [ + RecordSingle.fromJson(jsonDecode(value.body)['data'] + ['records']['blitz']['record']) + ] + : null; + zen = TetrioZen.fromJson(jsonDecode(value.body)['data']['zen']); + } else { + throw Exception('Failed to fetch player'); + } + }); + friendCount = json['friend_count']; } Map toJson() { @@ -271,7 +315,7 @@ class EndContextSingle { int? lines; int? score; int? seed; - double? finalTime; + Duration? finalTime; int? tSpins; Clears? clears; Finesse? finesse; @@ -297,7 +341,7 @@ class EndContextSingle { lines = json['lines']; inputs = json['inputs']; holds = json['holds']; - finalTime = json['finalTime']; + finalTime = doubleSecondsToDuration(json['finalTime'].toDouble()); score = json['score']; level = json['level']; topCombo = json['topcombo']; @@ -554,7 +598,7 @@ class RecordSingle { ? EndContextSingle.fromJson(json['endcontext']) : null; replayId = json['replayid']; - timestamp = json['ts']; + timestamp = DateTime.parse(json['ts']); userId = json['user']['_id']; } diff --git a/lib/views/main_view.dart b/lib/views/main_view.dart index 422ba9c..e3060e2 100644 --- a/lib/views/main_view.dart +++ b/lib/views/main_view.dart @@ -22,7 +22,7 @@ class _MainViewState extends State { if (response.statusCode == 200) { // If the server did return a 200 OK response, // then parse the JSON. - return TetrioPlayer.fromJson(jsonDecode(response.body)); + return TetrioPlayer.fromJson(jsonDecode(response.body)['data']['user']); } else { // If the server did not return a 200 OK response, // then throw an exception. @@ -52,6 +52,11 @@ class _MainViewState extends State { onChanged: (String value) { _searchFor = value; }, + onSubmitted: (String value) { + setState(() { + me = fetchTetrioPlayer(value); + }); + }, maxLength: 25, )), TextButton( @@ -65,15 +70,37 @@ class _MainViewState extends State { future: me, builder: (context, snapshot) { if (snapshot.hasData) { - return Column(children: [ + return Flexible( + child: Column(children: [ Text(snapshot.data!.username.toString()), - Text("Level ${snapshot.data!.getLevel()}"), - Text("Registered ${snapshot.data!.registrationTime}"), - Text("${snapshot.data!.tlSeason1!.rating} TR"), + Text(snapshot.data!.userId.toString()), + Text(snapshot.data!.role.toString()), Text( - "${snapshot.data!.tlSeason1!.glicko}±${snapshot.data!.tlSeason1!.rd} GLICKO"), - Text("${snapshot.data!.zen}") - ]); + "Level ${snapshot.data!.getLevel().toStringAsFixed(2)} (${snapshot.data!.xp} XP)"), + Text("Registered ${snapshot.data!.registrationTime}"), + Text("Bio: ${snapshot.data!.bio}", softWrap: true), + Text("Country: ${snapshot.data!.country}"), + Text("${snapshot.data!.friendCount} friends"), + Text( + "Won/PLayed: ${snapshot.data!.gamesWon}/${snapshot.data!.gamesPlayed}"), + Text("Gametime: ${snapshot.data!.gameTime}"), + Text("Supporter tier ${snapshot.data!.supporterTier}"), + const Text("\nTetra League", softWrap: true), + Text( + "${snapshot.data!.tlSeason1!.rating?.toStringAsFixed(2)} TR"), + Text( + "${snapshot.data!.tlSeason1!.glicko?.toStringAsFixed(2)}±${snapshot.data!.tlSeason1!.rd?.toStringAsFixed(2)} GLICKO"), + Text( + "Rank: ${snapshot.data!.tlSeason1!.rank?.toUpperCase()} (top ${(snapshot.data!.tlSeason1!.percentile! * 100).toStringAsFixed(2)}%)"), + Text( + "Won/Games: ${snapshot.data!.tlSeason1!.gamesPlayed}/${snapshot.data!.tlSeason1!.gamesWon}"), + Text( + "№${snapshot.data!.tlSeason1!.standing} (№${snapshot.data!.tlSeason1!.standingLocal} in country)"), + Text( + "${snapshot.data!.tlSeason1!.apm} APM, ${snapshot.data!.tlSeason1!.pps} PPS, ${snapshot.data!.tlSeason1!.vs} VS"), + const Text("\n40 Lines", softWrap: true), + Text(snapshot.data!.sprint.toString()), + ])); } else if (snapshot.hasError) { return Text('${snapshot.error}'); }