Skip to content

Commit

Permalink
feat: connect getRanking function to Ranking view and render real dat…
Browse files Browse the repository at this point in the history
…a from blockchain
  • Loading branch information
dnielopez committed Oct 14, 2023
1 parent 37c5145 commit ef7e514
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 78 deletions.
44 changes: 2 additions & 42 deletions lib/pages/my-dinogrow/my_dinogrow.dart
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
import 'dart:convert';
import 'dart:typed_data';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:go_router/go_router.dart';
import 'package:http/http.dart' as http;
import 'package:flutter/material.dart';
import 'package:solana/dto.dart';
import 'package:solana/solana.dart';

import '../../anchor_types/score_parameters.dart' as anchor_types_parameters;
import '../../anchor_types/dino_score_info.dart' as anchor_types_dino;
import '../../anchor_types/dino_game_info.dart' as anchor_types_dino_game;

import '../../ui/widgets/widgets.dart';

import 'dart:math';
import 'package:solana/solana.dart' as solana;
import 'package:solana/anchor.dart' as solana_anchor;
import 'package:solana/encoder.dart' as solana_encoder;
import 'package:solana_common/borsh/borsh.dart' as solana_borsh;
import 'package:solana_common/utils/buffer.dart' as solana_buffer;
import '../../anchor_types/nft_parameters.dart' as anchor_types;

Expand Down Expand Up @@ -137,7 +133,7 @@ class _MydinogrowScreenState extends State<MydinogrowScreen> {
child: const Padding(
padding: EdgeInsets.all(3),
child: Text(
"Hi ^.^ Please select one Dino to use as character when you play minigames",
"Hi ^.^ Please choose one Dino to use it as your avatar",
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
Expand Down Expand Up @@ -528,40 +524,4 @@ class _MydinogrowScreenState extends State<MydinogrowScreen> {
);
print('Tx successful with hash: $signature');
}

getRanking() async {
//Get rank from blockchain
await dotenv.load(fileName: ".env");

SolanaClient? client;
client = SolanaClient(
rpcUrl: Uri.parse(dotenv.env['QUICKNODE_RPC_URL'].toString()),
websocketUrl: Uri.parse(dotenv.env['QUICKNODE_RPC_WSS'].toString()),
);

const programId = '9V9ttZw7WTYW78Dx3hi2hV7V76PxAs5ZwbCkGi7qq8FW';

// Obtener todas las cuentas del programa
final accounts = await client.rpcClient.getProgramAccounts(
programId,
encoding: Encoding.jsonParsed,
);

// Recorre las cuentas y muestra los datos
for (var account in accounts) {
final bytes = account.account.data as BinaryAccountData;

//Get Score
final decoderDataScore = anchor_types_dino.DinoScoreArguments.fromBorsh(
bytes.data as Uint8List);
print("score: ${decoderDataScore.gamescore}");

//Get Game Data
final decoderDataGame =
anchor_types_dino_game.DinoGameArguments.fromBorsh(
bytes.data as Uint8List);
print("score: ${decoderDataGame.playerPubkey}");
print("score: ${decoderDataGame.dinoPubkey}");
}
}
}
170 changes: 134 additions & 36 deletions lib/pages/ranking/ranking.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:solana/solana.dart';
import 'package:solana/dto.dart';
import 'dart:typed_data';

// import 'package:solana_common/borsh/borsh.dart' as solana_borsh;
import '../../anchor_types/dino_score_info.dart' as anchor_types_dino;
import '../../anchor_types/dino_game_info.dart' as anchor_types_dino_game;
import '../../ui/widgets/widgets.dart';

class RankingScreen extends StatelessWidget {
RankingScreen({super.key});
class RankingScreen extends StatefulWidget {
const RankingScreen({super.key});

final List<ItemsProps> items = List<ItemsProps>.generate(
20,
(i) => ItemsProps(wallet: 'User $i', score: (i * 100).toString()),
);
@override
State<RankingScreen> createState() => _RankingScreenState();
}

class _RankingScreenState extends State<RankingScreen> {
List<ItemsProps> items = [];
bool loading = true;

@override
void initState() {
super.initState();
getRanking();
}

@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -45,33 +61,43 @@ class RankingScreen extends StatelessWidget {
color: const Color.fromRGBO(241, 189, 57, 1),
width: 6),
),
child: ListView.builder(
shrinkWrap: true,
itemCount: items.length,
itemBuilder: (context, index) {
items.sort((a, b) => double.parse(b.score)
.compareTo(double.parse(a.score)));

final item = items[index];

return ListTile(
leading: index < 3
? Icon(
Icons.emoji_events,
color: prizeColors[index],
)
: null,
title: Text(item.wallet,
style: const TextStyle(
fontWeight: FontWeight.bold,
color: Colors.black)),
trailing: Text(item.score,
style: const TextStyle(
fontWeight: FontWeight.bold,
color: Colors.black)),
);
},
),
child: loading
? const Center(
child: CircularProgressIndicator(
color: Colors.black,
),
)
: RefreshIndicator(
onRefresh: getRanking,
child: ListView.builder(
shrinkWrap: true,
itemCount: items.length,
itemBuilder: (context, index) {
items.sort((a, b) => double.parse(b.gamescore)
.compareTo(double.parse(a.gamescore)));

final item = items[index];

return ListTile(
leading: index < 3
? Icon(
Icons.emoji_events,
color: prizeColors[index],
)
: null,
title: Text(
'${item.playerPubkey.substring(0, 5)}...${item.playerPubkey.substring(item.playerPubkey.length - 5, item.playerPubkey.length)}',
style: const TextStyle(
fontWeight: FontWeight.bold,
color: Colors.black)),
trailing: Text(item.gamescore,
style: const TextStyle(
fontWeight: FontWeight.bold,
color: Colors.black)),
);
},
),
),
),
),
]),
Expand All @@ -80,6 +106,74 @@ class RankingScreen extends StatelessWidget {
),
);
}

Future<void> getRanking() async {
try {
List<ItemsProps> items2save = [];

setState(() {
loading = true;
});

//Get rank from blockchain
await dotenv.load(fileName: ".env");

SolanaClient? client;
client = SolanaClient(
rpcUrl: Uri.parse(dotenv.env['QUICKNODE_RPC_URL'].toString()),
websocketUrl: Uri.parse(dotenv.env['QUICKNODE_RPC_WSS'].toString()),
);

const programId = '9V9ttZw7WTYW78Dx3hi2hV7V76PxAs5ZwbCkGi7qq8FW';

// Obtener todas las cuentas del programa
final accounts = await client.rpcClient.getProgramAccounts(
programId,
encoding: Encoding.jsonParsed,
);

// Recorre las cuentas y muestra los datos
for (var account in accounts) {
final bytes = account.account.data as BinaryAccountData;

//Get Score
final decoderDataScore = anchor_types_dino.DinoScoreArguments.fromBorsh(
bytes.data as Uint8List);

//Get Game Data
final decoderDataGame =
anchor_types_dino_game.DinoGameArguments.fromBorsh(
bytes.data as Uint8List);

items2save.add(ItemsProps(
dinoPubkey: '${decoderDataGame.dinoPubkey}',
gamescore: '${decoderDataScore.gamescore}',
playerPubkey: '${decoderDataGame.playerPubkey}'));

// print("score: ${decoderDataScore.gamescore}");
// print("score: ${decoderDataGame.playerPubkey}");
// print("score: ${decoderDataGame.dinoPubkey}");
}

setState(() {
items = items2save;
});
} catch (e) {
final snackBar = SnackBar(
content: Text(
'Error: $e',
style: const TextStyle(color: Colors.white),
),
backgroundColor: Colors.red,
);

ScaffoldMessenger.of(context).showSnackBar(snackBar);
} finally {
setState(() {
loading = false;
});
}
}
}

List prizeColors = List.generate(
Expand All @@ -88,10 +182,14 @@ List prizeColors = List.generate(
index == 0 ? Colors.orange : (index == 1 ? Colors.grey : Colors.brown));

class ItemsProps {
String wallet;
String score;
String playerPubkey;
String gamescore;
String dinoPubkey;

ItemsProps({required this.wallet, required this.score});
ItemsProps(
{required this.playerPubkey,
required this.gamescore,
required this.dinoPubkey});
}

Future<void> _launchUrl() async {
Expand Down

0 comments on commit ef7e514

Please sign in to comment.