FaucetHub/lib/Backend/hoarder.dart

163 lines
5.7 KiB
Dart

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<dynamic> GamesJson = [];
static List<List<dynamic>> Challenges= [];
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);
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<String, int> ThisSeasonGamesEarnings = {};
static Map<String, int> ThisSeasonCryptoEarnings = {};
static dynamic cryptoRates;
static Future<void> FetchChallenges() async {
var response = null;
List<List<dynamic>> _Challenges = [];
double _currentEarnings = 0;
Map<String,int> _ThisSeasonGamesEarnings = {};
Map<String, int> _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: <String, String>{
"game_code": value['code'],
"id": Brain.UserJson['id']
}));
Debug.Log("Challenges response: " +response.body.toString());
List<dynamic> 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<void> 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<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");
// }
}
}