import 'dart:convert'; import 'package:faucethub/Backend/DebugHelper.dart'; import 'package:faucethub/Backend/brains.dart'; import 'package:http/http.dart' as http; import 'helpers.dart'; 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 double currentEarnings = 0; static double previousEarnings = 0; static Map ThisSeasonGamesEarnings = {}; static Map ThisSeasonCryptoEarnings = {}; static dynamic cryptoRates; static Future FetchChallenges() async { var response = null; List> _Challenges = []; double _currentEarnings = 0; Map _ThisSeasonGamesEarnings = {}; Map _ThisSeasonCryptoEarnings = {}; var cryptoValuesResponse = await http.get(Uri.parse('http://vps.playpoolstudios.com:2009/')); dynamic cryptoValues = jsonDecode(cryptoValuesResponse.body.toString()); cryptoRates = cryptoValues; Debug.Log(cryptoValues); 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()); dynamic GameJson = Helpers.GetGameFromCode(value['code']); newChallenges.forEach((challengeJson) { int total = int.parse(challengeJson['total']); int current = int.parse(challengeJson['current']); int reward = int.parse(challengeJson['reward']); double rewardInDollars = double.parse(cryptoValues[GameJson['coin']]) * (0.00000001) * reward; Debug.Log("Reward is $reward ~ \$$rewardInDollars"); if(total <= current){ _currentEarnings+= rewardInDollars; // Debug.Log(_ThisSeasonCryptoEarnings[GameJson['coin']]); _ThisSeasonGamesEarnings.update(GameJson['code'], (value) => value + reward, ifAbsent: ()=> reward); _ThisSeasonCryptoEarnings.update(GameJson['coin'], (value) => value + reward, ifAbsent: ()=> reward); // Debug.Log(_ThisSeasonCryptoEarnings[GameJson['coin']]); } }); _Challenges.add(newChallenges); } catch (e) { Debug.LogError("Error while fetching challenges $e"); } } ThisSeasonGamesEarnings = _ThisSeasonGamesEarnings; ThisSeasonCryptoEarnings = _ThisSeasonCryptoEarnings; Challenges = _Challenges; currentEarnings = _currentEarnings; Debug.Log(ThisSeasonGamesEarnings); // 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"); // } } }