apiGetTournament function
Implementation
Future<int> apiGetTournament(String id) async {
var pemBytes = await rootBundle.load("assets/cert.pem");
var context = SecurityContext()
..setTrustedCertificatesBytes(pemBytes.buffer.asUint8List(), password: '');
var client = HttpClient(context: context)
..badCertificateCallback =
(X509Certificate cert, String host, int port) => true;
try {
var request = await client
.getUrl(Uri.parse('https://api.gracehopper.xyz/v1/tournaments/$id'));
// Set headers
request.headers.add('Content-Type', 'application/json');
request.headers.add('Cookie', 'api-auth=${UserData().token}');
var response = await request.close();
var responseBody = await response.transform(utf8.decoder).join();
var responseBodyDictionary = jsonDecode(responseBody);
var data = responseBodyDictionary["data"];
// Verify subscribed tournaments
dynamic player1, player2;
int i = 0;
TournamentMatch tMatch = TournamentMatch();
TournamentData.matches = List.empty(growable: true);
for (int j = 0; j < data["rounds"]; j++) {
TournamentData.matches.add(List.empty(growable: true));
}
for (var match in data["matches"]) {
tMatch = TournamentMatch();
player1 = null;
player2 = null;
i = 0;
for (var participant in match["participants"]) {
if (i == 0) {
player1 = participant;
} else if (i == 1) {
player2 = participant;
}
i++;
}
tMatch.update(
match["id"],
match["startTime"],
player1 != null ? player1["id"] : "null",
player1 != null ? player1["username"] : "null",
player1 != null ? player1["avatar"] : "null",
player1 != null ? player1["elo"] : 0,
player2 != null ? player2["id"] : "null",
player2 != null ? player2["username"] : "null",
player2 != null ? player2["avatar"] : "null",
player2 != null ? player2["elo"] : 0,
match["hasStarted"],
match["finished"],
match["finished"] ? match["winner"] : "null");
String n = match["tournamentRoundText"].substring(6);
TournamentData.matches[int.parse(n) - 1].add(tMatch);
}
// print(data);
// print(responseBodyDictionary);
return 0;
//aqui ns que necesitas q devuelva
// return responseBodyDictionary["status"]["error_code"];
} catch (e) {
// print(e.toString());
return -1;
} finally {
client.close();
}
}