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(); CalculateEarnings(); StartGrabbing(); return isSettingsDone && isGamesDone; } static void StartGrabbing() async{ while(true){ await Future.delayed(const Duration(minutes: 1)); await GetSettings(); await GetGames(); await GetChallenges(); CalculateEarnings(); } } static Future GetGamesProgress() async{ FilterLinkedGames(); await GetChallenges(); } static void FilterLinkedGames(){ LinkedGamesJson = []; NonLinkedGamesJson = []; if(UserJson['linkedGames'].toString().length < 3){ NonLinkedGamesJson = AllGames; return; } 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; } static dynamic cryptoRates; static double currentEarnings =0; static Map GamesEarnings = {}; static Map CryptoEarnings = {}; static void CalculateEarnings() async{ Debug.Log("Going to calculate earnings from games"); while(UserJson == null){ await Future.delayed(const Duration(seconds: 1)); } try{ if(UserJson == null){Debug.LogError("User json is null, Abort calculating earnings");return;} Map earningsObj = jsonDecode(UserJson['earnings']); var cryptoValuesResponse = await http.get(Uri.parse('http://vps.playpoolstudios.com:2009/')); dynamic cryptoValues = jsonDecode(cryptoValuesResponse.body.toString()); cryptoRates = cryptoValues; Debug.Log(cryptoValues); currentEarnings=0; Map _ThisSeasonCryptoEarnings = {}; Map _ThisSeasonGamesEarnings = {}; earningsObj.forEach((key, earnings) { Debug.Log("$key : $earnings"); dynamic gameData = Helpers.GetGameFromCode(key); _ThisSeasonCryptoEarnings.update(gameData['coin'], (value) => earnings, ifAbsent: ()=> earnings); _ThisSeasonGamesEarnings.update(key, (value) => earnings, ifAbsent: ()=> earnings); double rewardInDollars = double.parse(cryptoValues[gameData['coin']]) * (0.00000001) * earnings; Debug.Log(rewardInDollars); currentEarnings += rewardInDollars; }); currentEarnings = currentEarnings; CryptoEarnings = _ThisSeasonCryptoEarnings; GamesEarnings = _ThisSeasonGamesEarnings; Debug.Log(_ThisSeasonCryptoEarnings); // return true; }catch(e){ Debug.LogError("Error at calculating earnings from games $e"); // return false; } } static Future ClaimChallenge(dynamic challengeData) async{ Map body = { "game_code": challengeData['game'], "id": UserJson['id'], "challenge_id" : challengeData['id'] }; var response = (await http.post( Uri.parse('${API_ENDPOINT}claim_challenge.php'), body: body)); Debug.Log("Challenges response: " +response.body.toString()); try{ dynamic newEarnings = jsonDecode(response.body.toString()); UserJson['earnings'] = response.body.toString(); await GetChallenges(); CalculateEarnings(); }catch(e){ Debug.LogError("Error claiming challenge : ${challengeData}"); Debug.LogError(body); Debug.LogError(e); } } }