FaucetHub/lib/Backend/hoarder.dart
2023-05-19 23:23:42 +05:30

121 lines
3.8 KiB
Dart

import 'dart:convert';
import 'package:faucethub/Backend/DebugHelper.dart';
import 'package:faucethub/Backend/brains.dart';
import 'package:http/http.dart' as http;
class Hoarder{
static List<dynamic> GamesJson = [];
static List<List<dynamic>> Challenges= [];
static Map<String,String> Settings = {};
static List<dynamic> FeaturedGames = [];
static Future<void> GrabInitData() async{
try {
var response = (await http.post(Uri.parse('${Brain.API_ENDPOINT}get_settings.php')));
Debug.LogResponse(response.body.toString());
// Settings= jsonDecode(response.body.toString());
List jsonList = jsonDecode(response.body.toString());
jsonList.forEach((jsonItem) {
Settings[jsonItem['id']] = jsonItem['value'];
});
} catch (e) {
Debug.LogError("Error while loading settings : $e");
}
try {
var response = (await http.post(Uri.parse('${Brain.API_ENDPOINT}get_games.php')));
Debug.LogResponse(response.body.toString());
GamesJson=[];
FeaturedGames = [];
List<String> games = response.body.toString().split("<td>");
List<String> featuredGames = Settings["featured_games"].toString().split(',');
games.forEach((game) {
dynamic Json = jsonDecode(game);
String id = Json['id'];
GamesJson.add(Json);
print(Json);
if(featuredGames.contains(id)){
FeaturedGames.add(Json);
}
});
Debug.Log("Total games ${GamesJson.length} and ${FeaturedGames.length} featured");
} catch (e) {
Debug.LogError("Error while loading games : $e");
}
FetchChallenges();
}
static Future<void> FetchChallenges() async {
var response = null;
Challenges = [];
for (var value in Brain.LinkedGamesJson) {
try {
Debug.Log("game_code : ${value['code']}, id: ${Brain.UserJson['id']}");
response = (await http.post(
Uri.parse('${Brain.API_ENDPOINT}get_challenges.php'),
body: <String, String>{
"game_code": value['code'],
"id": Brain.UserJson['id']
}));
Debug.Log("Challenges response: " +response.body.toString());
List<dynamic> newChallenges = jsonDecode(response.body.toString());
Challenges.add(newChallenges);
} catch (e) {
Debug.LogError("Error while fetching challenges $e");
}
}
// Debug.Log(Challenges);
Debug.Log("Challenge, " + (Hoarder.Challenges[0][0]['game']));
}
static Future<void> GetGameProgress() async{
// try {
// var response = (await http.post(Uri.parse('${Brain.API_ENDPOINT}get_settings.php')));
//
// Debug.LogResponse(response.body.toString());
// // Settings= jsonDecode(response.body.toString());
// List jsonList = jsonDecode(response.body.toString());
// jsonList.forEach((jsonItem) {
// Settings[jsonItem['id']] = jsonItem['value'];
// });
//
// } catch (e) {
// Debug.LogError("Error while loading settings : $e");
// }
//
// try {
// var response = (await http.post(Uri.parse('${Brain.API_ENDPOINT}get_games.php')));
//
// Debug.LogResponse(response.body.toString());
// GamesJson=[];
// FeaturedGames = [];
// List<String> games = response.body.toString().split("<td>");
// List<String> featuredGames = Settings["featured_games"].toString().split(',');
// games.forEach((game) {
// dynamic Json = jsonDecode(game);
// String id = Json['id'];
// GamesJson.add(Json);
//
// if(featuredGames.contains(id)){
// FeaturedGames.add(Json);
// }
// });
//
// Debug.Log("Total games ${GamesJson.length} and ${FeaturedGames.length} featured");
// } catch (e) {
// Debug.LogError("Error while loading games : $e");
// }
}
}