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 GamesJson = []; static List> Challenges= []; static Map Settings = {}; static List FeaturedGames = []; static Future 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 games = response.body.toString().split(""); List 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 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: { "game_code": value['code'], "id": Brain.UserJson['id'] })); Debug.Log("Challenges response: " +response.body.toString()); List 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 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 games = response.body.toString().split(""); // List 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"); // } } }