|
1 | 1 | // Learn more about Tauri commands at https://tauri.app/v1/guides/features/command |
2 | 2 | // Prevents additional console window on Windows in release, DO NOT REMOVE!! |
3 | 3 | #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] |
4 | | -use reqwest::get; |
5 | | -use serde_json::{Value, json}; |
6 | | -use std::collections::HashMap; |
7 | 4 |
|
8 | 5 | #[macro_use] |
9 | 6 | extern crate maplit; |
10 | 7 |
|
11 | | -// Function to fetch type effectiveness |
12 | | -async fn fetch_type_data(url: &str) -> Result<Value, String> { |
13 | | - match get(url).await { |
14 | | - Ok(response) => { |
15 | | - if response.status().is_success() { |
16 | | - let json: Value = response.json().await.unwrap(); |
17 | | - Ok(json) |
18 | | - } else { |
19 | | - Err("Failed to fetch type data".to_string()) |
20 | | - } |
21 | | - } |
22 | | - Err(_) => Err("Failed to connect to PokéAPI".to_string()), |
23 | | - } |
24 | | -} |
| 8 | +mod weakness_helpers; |
25 | 9 |
|
26 | | -// Helper function to generate icon URL based on type ID |
27 | | -fn generate_icon_url(type_id: &str) -> String { |
28 | | - format!("https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/types/generation-viii/sword-shield/{}.png", type_id) |
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_id = type_url.split('/').filter(|&s| !s.is_empty()).last().unwrap(); // Extract the type ID |
38 | | - let type_url = pokemon_type["type"]["url"].as_str().unwrap(); |
39 | | - |
40 | | - if let Ok(type_data) = fetch_type_data(type_url).await { |
41 | | - let damage_relations = &type_data["damage_relations"]; |
42 | | - |
43 | | - for (key, effectiveness) in &[ |
44 | | - ("2x", "double_damage_from"), |
45 | | - ("0.5x", "half_damage_from"), |
46 | | - ("0x", "no_damage_from"), |
47 | | - ] { |
48 | | - if let Some(damage_types) = damage_relations[effectiveness].as_array() { |
49 | | - for damage_type in damage_types { |
50 | | - let type_name = damage_type["name"].as_str().unwrap().to_string(); |
51 | | - let type_id = damage_type["url"] |
52 | | - .as_str() |
53 | | - .unwrap() |
54 | | - .split('/') |
55 | | - .filter(|&s| !s.is_empty()) |
56 | | - .last() |
57 | | - .unwrap(); |
58 | | - let icon_url = generate_icon_url(type_id); |
59 | | - weaknesses.entry(key.to_string()).or_default().push( |
60 | | - hashmap!{ |
61 | | - "type".to_string() => type_name, |
62 | | - "icon".to_string() => icon_url |
63 | | - } |
64 | | - ); |
65 | | - } |
66 | | - } |
67 | | - } |
68 | | - } else { |
69 | | - return Err("Failed to fetch type data".to_string()); |
70 | | - } |
71 | | - } |
72 | | - |
73 | | - Ok(weaknesses) |
74 | | -} |
| 10 | +use reqwest::get; |
| 11 | +use serde_json::{Value, json}; |
| 12 | +use weakness_helpers::calculate_weaknesses; |
75 | 13 |
|
76 | 14 | // Fetch input from PokéAPI and include weaknesses |
77 | 15 | #[tauri::command] |
|
0 commit comments