FaucetHub/lib/Backend/hoarder.dart
2023-02-09 14:56:47 +05:30

54 lines
1.5 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 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);
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");
}
}
}