diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 28c42dc..d17333c 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -1709,6 +1709,7 @@ dependencies = [ "tauri", "tauri-build", "wana_kana", + "wfd", ] [[package]] @@ -2949,6 +2950,16 @@ dependencies = [ "windows-metadata", ] +[[package]] +name = "wfd" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e713040b67aae5bf1a0ae3e1ebba8cc29ab2b90da9aa1bff6e09031a8a41d7a8" +dependencies = [ + "libc", + "winapi", +] + [[package]] name = "winapi" version = "0.3.9" diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 3f61907..6c8b931 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -19,6 +19,7 @@ serde = { version = "1.0.152", features = ["derive"] } tauri = { version = "1.2", features = ["shell-open"] } sqlite = "0.30.3" wana_kana = "2.1.0" +wfd = "0.1.7" [features] # by default Tauri runs in production mode diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 7ea737c..f4713f6 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -2,11 +2,15 @@ all(not(debug_assertions), target_os = "windows"), windows_subsystem = "windows" )] +use std::collections::HashMap; +use std::fs; + use serde_json::json; -use wana_kana::to_katakana::*; use wana_kana::to_hiragana::*; +use wana_kana::to_katakana::*; mod model; use model::*; +use wfd::DialogParams; #[tauri::command] fn autosearch_move(keyword: &str) -> serde_json::Value { @@ -31,10 +35,7 @@ fn autosearch_move(keyword: &str) -> serde_json::Value { { let row_result = Autosearch { id: row.read::("id"), - name: format!( - "{}", - row.read::<&str, _>("name").to_string() - ), + name: format!("{}", row.read::<&str, _>("name").to_string()), }; result.push(row_result); } @@ -42,6 +43,65 @@ fn autosearch_move(keyword: &str) -> serde_json::Value { serde_json::to_value(result).unwrap_or(serde_json::json!("[]")) } +#[tauri::command] +fn save_json(json_content: Vec>) -> Result<(), String> { + let params = DialogParams { + title: "Select a file to save", + file_types: vec![("JSON File", "*.json")], + file_name: "team.json", + default_extension: "json", + ..Default::default() + }; + match wfd::save_dialog(params) { + Ok(r) => { + let result = fs::write(r.selected_file_path, serde_json::to_string_pretty(&json_content).unwrap()); + if let Err(e) = result { + Err(format!("Error: {e:?}")) + } else { + Ok(()) + } + } + Err(e) => { + Err(format!("Error: {e:?}")) + } + } +} +#[tauri::command] +fn load_json() -> Result { + let params = DialogParams { + title: "Select a file to load", + file_types: vec![("JSON File", "*.json")], + default_extension: "json", + ..Default::default() + }; + match wfd::open_dialog(params) { + Ok(r) => { + let file_content_string = fs::read_to_string(r.selected_file_path); + if let Err(e) = file_content_string { + return Err(format!("Error: {e:?}")) + } + match serde_json::from_str::>(&file_content_string.unwrap()) { + Ok(c) => { + let mut result: Vec = vec![]; + for d in c { + let pokemon_data = search(d.id); + let pokemon_data_to_load = PokemonDataToLoad::from((d, pokemon_data)); + result.push(pokemon_data_to_load); + } + if result.len() > 0 { + Ok(serde_json::to_value(result).unwrap()) + } else { + Err("Unable to load pokemon data.".to_string()) + } + }, + Err(e) => Err(format!("Error: {e:?}")), + } + } + Err(e) => { + Err(format!("Error: {e:?}")) + } + } +} #[tauri::command] fn autosearch(keyword: &str) -> serde_json::Value { let connection = sqlite::open("./pokemon.db").unwrap(); @@ -62,10 +122,7 @@ fn autosearch(keyword: &str) -> serde_json::Value { { let row_result = Autosearch { id: row.read::("id"), - name: format!( - "{}", - row.read::<&str, _>("name").to_string() - ), + name: format!("{}", row.read::<&str, _>("name").to_string()), }; result.push(row_result); } @@ -87,7 +144,7 @@ fn search_move(index: i64) -> MoveSearchResult { let row_result = MoveSearchResult { id: row.read::("id"), name: row.read::<&str, _>("name").to_string(), - types: row.read::<&str, _>("types").to_string(), + types: serde_json::from_str(row.read::<&str, _>("types")).unwrap_or(vec![0]), power: row.read::("power"), category: row.read::("category"), priority: row.read::("priority"), @@ -96,17 +153,17 @@ fn search_move(index: i64) -> MoveSearchResult { _ => None, }, }; - return row_result + return row_result; } return MoveSearchResult { id: 0, name: "Wrong".to_string(), - types: "[0]".to_string(), + types: vec![0], power: 0, category: 0, priority: 0, condition: None, - } + }; } #[tauri::command] fn get_items() -> Vec { @@ -134,10 +191,18 @@ fn get_items() -> Vec { } #[tauri::command] -fn search(index: i64) -> SearchResult { +fn increase_attack_usage(index: i64) { + println!("update index: {index}"); + let connection = sqlite::open("./pokemon.db").unwrap(); + let query = format!("UPDATE pokemon__learnset SET usage = usage + 1 WHERE id = {index}"); + connection.execute(query).unwrap(); +} + +#[tauri::command] +fn search_learnset(index: i64) -> Vec { let connection = sqlite::open("./pokemon.db").unwrap(); let mut learnset: Vec = vec![]; - let move_query = "SELECT pl.id, pl.learnset_id, l.name, l.types, l.power, l.category FROM pokemon__learnset pl JOIN learnset l on pl.learnset_id = l.id WHERE pl.pokemon_id = ? ORDER BY usage"; + let move_query = "SELECT pl.id, pl.learnset_id, l.name, l.types, l.power, l.category, l.priority, pl.usage FROM pokemon__learnset pl JOIN learnset l on pl.learnset_id = l.id WHERE pl.pokemon_id = ? ORDER BY usage DESC"; for row in connection .prepare(move_query) .unwrap() @@ -153,10 +218,18 @@ fn search(index: i64) -> SearchResult { types: serde_json::from_str(row.read::<&str, _>("types")).unwrap_or(vec![0]), power: row.read::("power"), category: row.read::("category"), + priority: row.read::("priority"), + usage: row.read::("usage"), }; learnset.push(row_result) } - println!("Get Thing2"); + return learnset; +} +#[tauri::command] +fn search(index: i64) -> SearchResult { + let connection = sqlite::open("./pokemon.db").unwrap(); + let learnset = search_learnset(index.clone()); + let query = "SELECT * FROM pokemon WHERE id = ?"; for row in connection .prepare(query) @@ -169,9 +242,9 @@ fn search(index: i64) -> SearchResult { let row_result = SearchResult { id: row.read::("id"), name: row.read::<&str, _>("name").to_string(), - types: row.read::<&str, _>("types").to_string(), + types: serde_json::from_str(row.read::<&str, _>("types")).unwrap_or(vec![0]), thumbnail: row.read::<&str, _>("thumbnail").to_string(), - abilities: row.read::<&str, _>("abilities").to_string(), + abilities: serde_json::from_str(row.read::<&str, _>("abilities")).unwrap_or(HashMap::new()), hp: row.read::("hp"), attack: row.read::("attack"), defense: row.read::("defense"), @@ -180,14 +253,14 @@ fn search(index: i64) -> SearchResult { speed: row.read::("speed"), learnset: learnset, }; - return row_result + return row_result; } return SearchResult { id: 0, name: "Missigno".to_string(), - types: "[]".to_string(), + types: vec![], thumbnail: "".to_string(), - abilities: "{}".to_string(), + abilities: HashMap::new(), hp: 0, attack: 0, defense: 0, @@ -195,11 +268,21 @@ fn search(index: i64) -> SearchResult { special_defense: 0, speed: 0, learnset: vec![], - } + }; } fn main() { tauri::Builder::default() - .invoke_handler(tauri::generate_handler![autosearch_move, search_move, autosearch, search, get_items]) + .invoke_handler(tauri::generate_handler![ + autosearch_move, + search_move, + autosearch, + search, + get_items, + increase_attack_usage, + search_learnset, + save_json, + load_json, + ]) .run(tauri::generate_context!()) .expect("error while running tauri application"); } diff --git a/src-tauri/src/model.rs b/src-tauri/src/model.rs index 14b4673..89e3fb8 100644 --- a/src-tauri/src/model.rs +++ b/src-tauri/src/model.rs @@ -1,17 +1,19 @@ +use std::collections::HashMap; + use serde::{Deserialize, Serialize}; #[derive(Serialize, Deserialize, Debug, Clone)] pub struct SearchResult { pub id: i64, pub name: String, - pub types: String, + pub types: Vec, pub hp: i64, pub attack: i64, pub defense: i64, pub special_attack: i64, pub special_defense: i64, pub speed: i64, - pub abilities: String, + pub abilities: HashMap, pub thumbnail: String, pub learnset: Vec, } @@ -24,12 +26,14 @@ pub struct PokemonDataLearnset { pub types: Vec, pub power: i64, pub category: i64, + pub priority: i64, + pub usage: i64, } #[derive(Serialize, Deserialize, Debug, Clone)] pub struct MoveSearchResult { pub id: i64, pub name: String, - pub types: String, + pub types: Vec, pub power: i64, pub category: i64, pub priority: i64, @@ -47,4 +51,124 @@ pub struct ItemSearchResult { pub name: String, pub image: String, pub effect: Option, +} + +#[derive(Serialize, Deserialize, Debug, Clone)] +pub struct PokemonDataToSave { + pub id: i64, + pub hp_v: i64, + pub attack_v: i64, + pub defense_v: i64, + pub special_attack_v: i64, + pub special_defense_v: i64, + pub speed_v: i64, + pub hp_d: i64, + pub attack_d: i64, + pub defense_d: i64, + pub special_attack_d: i64, + pub special_defense_d: i64, + pub speed_d: i64, + pub attack_plus: bool, + pub attack_minus: bool, + pub defense_plus: bool, + pub defense_minus: bool, + pub special_attack_plus: bool, + pub special_attack_minus: bool, + pub special_defense_plus: bool, + pub special_defense_minus: bool, + pub speed_plus: bool, + pub speed_minus: bool, + pub item: i64, +} +#[derive(Serialize, Deserialize, Debug, Clone)] +pub struct PokemonDataToLoad { + pub id: i64, + pub name: String, + pub types: Vec, + pub thumbnail: String, + pub abilities: HashMap, + pub hp: i64, + pub attack: i64, + pub defense: i64, + pub special_attack: i64, + pub special_defense: i64, + pub speed: i64, + pub hp_v: i64, + pub attack_v: i64, + pub defense_v: i64, + pub special_attack_v: i64, + pub special_defense_v: i64, + pub speed_v: i64, + pub hp_d: i64, + pub attack_d: i64, + pub defense_d: i64, + pub special_attack_d: i64, + pub special_defense_d: i64, + pub speed_d: i64, + pub attack_plus: bool, + pub attack_minus: bool, + pub defense_plus: bool, + pub defense_minus: bool, + pub special_attack_plus: bool, + pub special_attack_minus: bool, + pub special_defense_plus: bool, + pub special_defense_minus: bool, + pub speed_plus: bool, + pub speed_minus: bool, + pub attack_buff: i64, + pub defense_buff: i64, + pub special_attack_buff: i64, + pub special_defense_buff: i64, + pub speed_buff: i64, + pub item: i64, + pub learnset: Vec, + pub terastype: Vec, +} + +impl From<(PokemonDataToSave, SearchResult)> for PokemonDataToLoad { + fn from(value: (PokemonDataToSave, SearchResult)) -> Self { + PokemonDataToLoad { + id: value.0.id, + name: value.1.name, + types: value.1.types, + thumbnail: value.1.thumbnail, + abilities: value.1.abilities, + hp: value.1.hp, + attack: value.1.attack, + defense: value.1.defense, + special_attack: value.1.special_attack, + special_defense: value.1.special_defense, + speed: value.1.speed, + hp_v: value.0.hp_v, + attack_v: value.0.attack_v, + defense_v: value.0.defense_v, + special_attack_v: value.0.special_attack_v, + special_defense_v: value.0.special_defense_v, + speed_v: value.0.speed_v, + hp_d: value.0.hp_d, + attack_d: value.0.attack_d, + defense_d: value.0.defense_d, + special_attack_d: value.0.special_attack_d, + special_defense_d: value.0.special_defense_d, + speed_d: value.0.speed_d, + attack_plus: value.0.attack_plus, + attack_minus: value.0.attack_minus, + defense_plus: value.0.defense_plus, + defense_minus: value.0.defense_minus, + special_attack_plus: value.0.special_attack_plus, + special_attack_minus: value.0.special_attack_minus, + special_defense_plus: value.0.special_defense_plus, + special_defense_minus: value.0.special_defense_minus, + speed_plus: value.0.speed_plus, + speed_minus: value.0.speed_minus, + attack_buff: 0, + defense_buff: 0, + special_attack_buff: 0, + special_defense_buff: 0, + speed_buff: 0, + item: value.0.item, + learnset: value.1.learnset, + terastype: vec![0] + } + } } \ No newline at end of file diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index 2cb4592..bcda16a 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -8,7 +8,7 @@ }, "package": { "productName": "pokemon-data-displayer", - "version": "0.0.4" + "version": "0.0.7" }, "tauri": { "allowlist": { diff --git a/src/App.svelte b/src/App.svelte index 12a32d3..722e343 100644 --- a/src/App.svelte +++ b/src/App.svelte @@ -1,23 +1,48 @@
- +
- +
- +
diff --git a/src/lib/DamageCalculator.svelte b/src/lib/DamageCalculator.svelte index f7c2727..5d262d6 100644 --- a/src/lib/DamageCalculator.svelte +++ b/src/lib/DamageCalculator.svelte @@ -2,26 +2,37 @@ import { invoke } from "@tauri-apps/api/tauri"; import AutoComplete from "simple-svelte-autocomplete"; import attack_types from "../const/attack_types.json"; - import type { PokemonData } from "../model/PokemonStatus"; + import type { PokemonStatus } from "../model/PokemonStatus"; - export let player1Data: PokemonData | undefined; - export let player2Data: PokemonData | undefined; + export let player1Data: PokemonStatus | undefined; + export let player2Data: PokemonStatus | undefined; export let attackId: number | undefined; + export let attackSide: number | undefined; let attackData; $: { - if (myValue > 0 && myValue !== currentValue) { - invoke("search_move", { index: myValue }).then((r) => { - currentValue = myValue; + if (moveValue > 0) { + invoke("search_move", { index: moveValue }).then((r) => { attackData = r; - console.log(attackData); + moveValue = undefined; }); } if (!!attackId) { + console.log(attackId); invoke("search_move", { index: attackId }).then((r) => { - currentValue = myValue = attackId; attackData = r; - console.log(attackData); attackId = undefined; + console.log(attackSide); + switch (attackSide) { + case 1: { + attack_direction = "p1p2"; + break; + } + case 2: { + attack_direction = "p2p1"; + break; + } + } + attackSide = undefined; }); } } @@ -32,8 +43,8 @@ let total_min_damage_percentage = 0; let item_magnification = 1.0; function calculate_damage() { - let attacker; - let defender; + let attacker: PokemonStatus; + let defender: PokemonStatus; let atk_value; let def_value; if ( @@ -43,9 +54,14 @@ (!!attackData && ![1, 2].includes(attackData.category)) ) { // display some message somewhere + total_max_damage = 0; + total_min_damage = 0; + total_max_damage_percentage = 0; + total_min_damage_percentage = 0; console.log("skip"); return; } + console.log(attackData); if (attack_direction === "p1p2") { attacker = player1Data; defender = player2Data; @@ -55,11 +71,11 @@ } // 物理or特殊の判定 if (attackData.category === 1) { - atk_value = attacker.atk; - def_value = defender.def; + atk_value = attacker.attack; + def_value = defender.defense; } else { - atk_value = attacker.spatk; - def_value = defender.spdef; + atk_value = attacker.special_attack; + def_value = defender.special_defense; } // debug @@ -103,7 +119,7 @@ attack_data_category: attackData.category, // 技タイプ - move_type: JSON.parse(attackData.types), + move_type: attackData.types, // 範囲の計算で使用する }; // 計算 @@ -352,8 +368,7 @@ return []; } } - let myValue; - let currentValue = 0; + let moveValue;
@@ -391,9 +406,10 @@ localFiltering={false} labelFieldName="name" valueFieldName="id" - bind:value={myValue} + bind:value={moveValue} /> + { attackData ? attackData.name : "" } diff --git a/src/lib/DisplayData.svelte b/src/lib/DisplayData.svelte index e5ea9cd..e3049b3 100644 --- a/src/lib/DisplayData.svelte +++ b/src/lib/DisplayData.svelte @@ -1,12 +1,17 @@