import 'dart:convert'; import 'package:fhub/backend/helpers.dart'; import 'package:http/http.dart' as http; import 'DebugHelper.dart'; class DataManager{ static const String API_ENDPOINT = "http://vps.playpoolstudios.com/faucet/api/"; static Map Settings = {}; static List AllGames = []; static List FeaturedGames = []; static List LinkedGamesJson = []; static List NonLinkedGamesJson = []; static List> Challenges= []; static dynamic UserJson; static Future Init() async{ bool isSettingsDone = await GetSettings(); bool isGamesDone = await GetGames(); return isSettingsDone && isGamesDone; } static Future GetGamesProgress() async{ FilterLinkedGames(); await GetChallenges(); } static void FilterLinkedGames(){ LinkedGamesJson = []; NonLinkedGamesJson = []; List> linkedGames = jsonDecode( UserJson['linkedGames']).cast>(); AllGames.forEach((element) { bool foundLink = false; for (var linkedGame in linkedGames) { int gid = linkedGame["game_id"]; int id = int.parse(element['id']); if (gid == id) { LinkedGamesJson.add(element); foundLink = true; break; } } if (!foundLink) { NonLinkedGamesJson.add(element); } }); } static Future GetSettings() async{ try { var response = (await http.post(Uri.parse('${API_ENDPOINT}get_settings.php'))); Debug.LogResponse("settings: ${response.body}"); List jsonList = jsonDecode(response.body.toString()); jsonList.forEach((jsonItem) { Settings[jsonItem['id']] = jsonItem['value']; }); return true; } catch (e) { Debug.LogError("Error while gettings settings : $e"); return false; } } static FutureGetGames() async{ try { var response = (await http.post(Uri.parse('${API_ENDPOINT}get_games.php'))); Debug.LogResponse("Games: ${response.body}"); AllGames=[]; 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']; AllGames.add(Json); print(Json); if(featuredGames.contains(id)){ FeaturedGames.add(Json); } }); return true; } catch (e) { Debug.LogError("Error while loading games : $e"); return false; } } static Future GetChallenges() async{ var response = null; List> _Challenges = []; double _currentEarnings = 0; for (var value in LinkedGamesJson) { try { Debug.Log("game_code : ${value['code']}, id: ${UserJson['id']}"); response = (await http.post( Uri.parse('${API_ENDPOINT}get_challenges.php'), body: { "game_code": value['code'], "id": 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"); return false; } } Challenges = _Challenges; return true; } }