From 5f052bd76acb6e0a7df83a56800ed9dfd38dd9ab Mon Sep 17 00:00:00 2001 From: Sewmina Dilshan Date: Wed, 31 May 2023 03:56:41 +0530 Subject: [PATCH] Seperate challenges --- lib/Backend/helpers.dart | 17 +++++++++ lib/Backend/hoarder.dart | 12 +++++- lib/home.dart | 82 ++++++++++++++++++---------------------- 3 files changed, 63 insertions(+), 48 deletions(-) diff --git a/lib/Backend/helpers.dart b/lib/Backend/helpers.dart index 085affc..8563c42 100644 --- a/lib/Backend/helpers.dart +++ b/lib/Backend/helpers.dart @@ -22,6 +22,9 @@ class Helpers{ case "LTC": return CryptoFontIcons.LTC; break; + case "BCH": + return CryptoFontIcons.AUR; + break; } return CryptoFontIcons.BTC; @@ -54,4 +57,18 @@ class Helpers{ } } } + + static double CryptoToDollars({required double amount, required String Crypto}){ + if(Hoarder.cryptoRates==null){Debug.Log("Crypto rates haven't been updated yet"); return 0;} + Debug.Log(Hoarder.cryptoRates); + if(Hoarder.cryptoRates[Crypto] == null){ + Debug.Log("$Crypto not found in ${Hoarder.cryptoRates}"); + return 0; + } + return amount * double.parse(Hoarder.cryptoRates[Crypto]); + } + + static double SatsToCoin(int sats){ + return sats.toDouble() * 0.00000001; + } } \ No newline at end of file diff --git a/lib/Backend/hoarder.dart b/lib/Backend/hoarder.dart index c000caa..702653d 100644 --- a/lib/Backend/hoarder.dart +++ b/lib/Backend/hoarder.dart @@ -56,16 +56,22 @@ class Hoarder{ 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 _ThisSeasonCryptoEarnings = {}; + 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 { @@ -91,6 +97,7 @@ class Hoarder{ 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']]); } @@ -103,11 +110,12 @@ class Hoarder{ Debug.LogError("Error while fetching challenges $e"); } } + ThisSeasonGamesEarnings = _ThisSeasonGamesEarnings; ThisSeasonCryptoEarnings = _ThisSeasonCryptoEarnings; Challenges = _Challenges; currentEarnings = _currentEarnings; - Debug.Log(ThisSeasonCryptoEarnings); + Debug.Log(ThisSeasonGamesEarnings); // Debug.Log(Challenges); // Debug.Log("Challenge, " + (Hoarder.Challenges[0][0]['game'])); diff --git a/lib/home.dart b/lib/home.dart index 1a7b5f8..8c74c1c 100644 --- a/lib/home.dart +++ b/lib/home.dart @@ -39,7 +39,7 @@ class _HomePageState extends State { await Hoarder.FetchChallenges(); DateTime now = DateTime.now(); - DateTime nextMonth = DateTime(now.year, now.month); + DateTime nextMonth = DateTime(now.year, (now.month < 12) ? now.month +1 : 1); daysLeft = "${nextMonth.difference(now).inDays} Days Remaining"; setState(() {}); @@ -52,7 +52,6 @@ class _HomePageState extends State { void dispose() { // TODO: implement dispose super.dispose(); - looping = false; } @@ -131,7 +130,7 @@ class _HomePageState extends State { // ), // title: "Earnings"), HomeCard( - title: "Seasonal Challenges $daysLeft", + title: "Ongoing Challenges", child: Container( child: Column( children: [ @@ -286,7 +285,8 @@ class _HomePageState extends State { value: current / total, color: Colors.green, )), - SizedBox( + SizedBox(width: 15,), + (total <= current) ? SizedBox(height: 30,width: 70 ,child: MaterialButton(onPressed: (){}, child: Text("Claim"),color: Colors.green,)): SizedBox( width: 60, child: Text("$current / $total $prefix", textAlign: TextAlign.end, @@ -398,25 +398,6 @@ class _HomePageState extends State { context, MaterialPageRoute(builder: (context) => GamePage(code)), ); - - // showLinkGameDialog(context, Brain.NonLinkedGamesJson[index]); - // try { - // ///checks if the app is installed on your mobile device - // String id = Brain.NonLinkedGamesJson[index]['link']; - // Debug.Log(id); - // bool isInstalled = await DeviceApps.isAppInstalled(id); - // if (isInstalled) { - // DeviceApps.openApp(id); - // } else { - // ///if the app is not installed it lunches google play store so you can install it from there - // // launchUrl(Uri(path: "market://details?id=" +Brain.NonLinkedGamesJson[index]['link']),mode: LaunchMode.externalApplication,); - // // try{launch("market://details?id=" +Brain.NonLinkedGamesJson[index]['link']);}catch(e){ - // launch("https://play.google.com/store/apps/details?id=$id"); - // // } - // } - // } catch (e) { - // print(e); - // } }, child: ListTile( leading: ClipRRect( @@ -458,7 +439,7 @@ class _HomePageState extends State { size: 60, ), title: Text(Brain.Username), - subtitle: Text("Total Earnings : \$42.69"), + subtitle: Text("Total Earnings : \$${(Hoarder.previousEarnings + Hoarder.currentEarnings).toStringAsFixed(2)}"), trailing: IconButton( icon: Icon(Icons.settings), onPressed: () { @@ -484,10 +465,6 @@ class _HomePageState extends State { SizedBox( height: 30, ), - Text( - "Sort by", - style: TextStyle(fontSize: 12), - ), DefaultTabController( length: 2, child: Column( @@ -508,24 +485,36 @@ class _HomePageState extends State { child: TabBarView(children: [ Container( child: Column( - children: [ - CurrencyListItem("BTC", "0.0004000 = \$1.20"), - CurrencyListItem("ETH", "0.0004000 = \$0.50"), - CurrencyListItem("XRP", "0.0004000 = \$0.20"), - CurrencyListItem("DOGE", "0.0004000 = \$0.020") - ], - )), + children: [ + ListView.builder( + shrinkWrap: true, + controller: scrollController, + itemCount: Hoarder.ThisSeasonCryptoEarnings.length, + itemBuilder: (BuildContext context, int index) { + return CurrencyListItem(Hoarder.ThisSeasonCryptoEarnings.keys.elementAt(index)); + }) + ], + )), Container( child: Column( - children: [ - GameListItem( - Hoarder.GamesJson[0], "0.0004000 = \$1.20"), - GameListItem( - Hoarder.GamesJson[1], "0.0004000 = \$1.20"), - GameListItem( - Hoarder.GamesJson[2], "0.0004000 = \$1.20"), - ], - )), + children: [ + ListView.builder( + shrinkWrap: true, + controller: scrollController, + itemCount: Hoarder.ThisSeasonGamesEarnings.length, + itemBuilder: (BuildContext context, int index) { + String gameName= Hoarder.ThisSeasonGamesEarnings.keys.elementAt(index); + dynamic GameJson = Helpers.GetGameFromCode(gameName); + if(GameJson == null){ + Debug.LogError("Error receiving game json from ${gameName}. returned:\n$GameJson"); + return Text("Error"); + } + double amount = Helpers.SatsToCoin(Hoarder.ThisSeasonGamesEarnings.values.elementAt(index)); + return GameListItem( + GameJson, "$amount ~ \$${Helpers.CryptoToDollars(amount: amount, Crypto: GameJson['coin']).toStringAsFixed(2)}"); + }) + ], + )), ]), ), ], @@ -620,7 +609,8 @@ class _HomePageState extends State { ])))); } - Widget CurrencyListItem(String currencyName, String value) { + Widget CurrencyListItem(String currencyName) { + double val = (Hoarder.ThisSeasonCryptoEarnings[currencyName] ?? 0).toDouble() * (0.00000001); return Container( padding: EdgeInsets.all(5), child: Row( @@ -637,7 +627,7 @@ class _HomePageState extends State { Text(currencyName) ], ), - Text(value) + Text('${val.toStringAsFixed(8)} ~ \$${Helpers.CryptoToDollars(amount: val, Crypto: currencyName).toStringAsFixed(2)}' ) ], ), );