11// Prevents additional console window on Windows in release, DO NOT REMOVE!!
22#![ cfg_attr( not( debug_assertions) , windows_subsystem = "windows" ) ]
33use reqwest:: get;
4- use serde_json:: Value ;
4+ use serde_json:: { Value , json} ;
5+ use std:: collections:: HashMap ;
6+
7+ #[ macro_use]
8+ extern crate maplit;
59
610// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
711#[ tauri:: command]
812fn greet ( name : & str ) -> String {
913 format ! ( "{} does not exist yet!" , name)
1014}
1115
12- // fetch input from pokeapi
16+ // Function to fetch type effectiveness
17+ async fn fetch_type_data ( url : & str ) -> Result < Value , String > {
18+ match get ( url) . await {
19+ Ok ( response) => {
20+ if response. status ( ) . is_success ( ) {
21+ let json: Value = response. json ( ) . await . unwrap ( ) ;
22+ Ok ( json)
23+ } else {
24+ Err ( "Failed to fetch type data" . to_string ( ) )
25+ }
26+ }
27+ Err ( _) => Err ( "Failed to connect to PokéAPI" . to_string ( ) ) ,
28+ }
29+ }
30+
31+ // Function to calculate weaknesses based on Pokémon types
32+ async fn calculate_weaknesses ( types : & Vec < Value > ) -> Result < HashMap < String , Vec < HashMap < String , String > > > , String > {
33+ let mut weaknesses: HashMap < String , Vec < HashMap < String , String > > > = HashMap :: new ( ) ;
34+
35+ for pokemon_type in types {
36+ let type_name = pokemon_type[ "type" ] [ "name" ] . as_str ( ) . unwrap ( ) ;
37+ let type_url = pokemon_type[ "type" ] [ "url" ] . as_str ( ) . unwrap ( ) ;
38+ let type_id = type_url. split ( '/' ) . filter ( |& s| !s. is_empty ( ) ) . last ( ) . unwrap ( ) ; // Extract the type ID
39+
40+ let icon_url = format ! ( "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/types/generation-viii/sword-shield/{}.png" , type_id) ;
41+
42+ if let Ok ( type_data) = fetch_type_data ( type_url) . await {
43+ let damage_relations = & type_data[ "damage_relations" ] ;
44+
45+ // Group by 2x effectiveness
46+ if let Some ( double_damage_from) = damage_relations[ "double_damage_from" ] . as_array ( ) {
47+ for damage_type in double_damage_from {
48+ let type_name = damage_type[ "name" ] . as_str ( ) . unwrap ( ) . to_string ( ) ;
49+ let type_id = damage_type[ "url" ] . as_str ( ) . unwrap ( ) . split ( '/' ) . filter ( |& s| !s. is_empty ( ) ) . last ( ) . unwrap ( ) ;
50+ let icon_url = format ! ( "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/types/generation-viii/sword-shield/{}.png" , type_id) ;
51+ weaknesses. entry ( "2x" . to_string ( ) ) . or_default ( ) . push (
52+ hashmap ! {
53+ "type" . to_string( ) => type_name,
54+ "icon" . to_string( ) => icon_url
55+ }
56+ ) ;
57+ }
58+ }
59+
60+ // Group by 0.5x effectiveness
61+ if let Some ( half_damage_from) = damage_relations[ "half_damage_from" ] . as_array ( ) {
62+ for damage_type in half_damage_from {
63+ let type_name = damage_type[ "name" ] . as_str ( ) . unwrap ( ) . to_string ( ) ;
64+ let type_id = damage_type[ "url" ] . as_str ( ) . unwrap ( ) . split ( '/' ) . filter ( |& s| !s. is_empty ( ) ) . last ( ) . unwrap ( ) ;
65+ let icon_url = format ! ( "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/types/generation-viii/sword-shield/{}.png" , type_id) ;
66+ weaknesses. entry ( "0.5x" . to_string ( ) ) . or_default ( ) . push (
67+ hashmap ! {
68+ "type" . to_string( ) => type_name,
69+ "icon" . to_string( ) => icon_url
70+ }
71+ ) ;
72+ }
73+ }
74+
75+ // Group by 0x effectiveness
76+ if let Some ( no_damage_from) = damage_relations[ "no_damage_from" ] . as_array ( ) {
77+ for damage_type in no_damage_from {
78+ let type_name = damage_type[ "name" ] . as_str ( ) . unwrap ( ) . to_string ( ) ;
79+ let type_id = damage_type[ "url" ] . as_str ( ) . unwrap ( ) . split ( '/' ) . filter ( |& s| !s. is_empty ( ) ) . last ( ) . unwrap ( ) ;
80+ let icon_url = format ! ( "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/types/generation-viii/sword-shield/{}.png" , type_id) ;
81+ weaknesses. entry ( "0x" . to_string ( ) ) . or_default ( ) . push (
82+ hashmap ! {
83+ "type" . to_string( ) => type_name,
84+ "icon" . to_string( ) => icon_url
85+ }
86+ ) ;
87+ }
88+ }
89+ } else {
90+ return Err ( "Failed to fetch type data" . to_string ( ) ) ;
91+ }
92+ }
93+
94+ Ok ( weaknesses)
95+ }
96+
97+ // Fetch input from PokéAPI and include weaknesses
1398#[ tauri:: command]
1499async fn search_pokemon ( name : & str ) -> Result < String , String > {
15100 let url = format ! ( "https://pokeapi.co/api/v2/pokemon/{}" , name. to_lowercase( ) ) ;
@@ -18,7 +103,16 @@ async fn search_pokemon(name: &str) -> Result<String, String> {
18103 Ok ( response) => {
19104 if response. status ( ) . is_success ( ) {
20105 let json: Value = response. json ( ) . await . unwrap ( ) ;
21- Ok ( json. to_string ( ) )
106+ let types = json[ "types" ] . as_array ( ) . unwrap ( ) ;
107+
108+ // Calculate weaknesses
109+ let weaknesses = calculate_weaknesses ( types) . await ?;
110+
111+ // Add weaknesses to the Pokémon data
112+ let mut result = json. clone ( ) ;
113+ result[ "weaknesses" ] = json ! ( weaknesses) ;
114+
115+ Ok ( result. to_string ( ) )
22116 } else {
23117 Err ( format ! ( "Error: Pokémon {} not found!" , name) )
24118 }
0 commit comments