added save and load json
This commit is contained in:
11
src-tauri/Cargo.lock
generated
11
src-tauri/Cargo.lock
generated
@@ -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"
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -2,11 +2,14 @@
|
||||
all(not(debug_assertions), target_os = "windows"),
|
||||
windows_subsystem = "windows"
|
||||
)]
|
||||
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 +34,7 @@ fn autosearch_move(keyword: &str) -> serde_json::Value {
|
||||
{
|
||||
let row_result = Autosearch {
|
||||
id: row.read::<i64, _>("id"),
|
||||
name: format!(
|
||||
"{}",
|
||||
row.read::<&str, _>("name").to_string()
|
||||
),
|
||||
name: format!("{}", row.read::<&str, _>("name").to_string()),
|
||||
};
|
||||
result.push(row_result);
|
||||
}
|
||||
@@ -42,6 +42,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<Option<PokemonDataToSave>>) -> 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<serde_json::Value, String> {
|
||||
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::<Vec<PokemonDataToSave>>(&file_content_string.unwrap()) {
|
||||
Ok(c) => {
|
||||
let mut result: Vec<PokemonDataToLoad> = 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 +121,7 @@ fn autosearch(keyword: &str) -> serde_json::Value {
|
||||
{
|
||||
let row_result = Autosearch {
|
||||
id: row.read::<i64, _>("id"),
|
||||
name: format!(
|
||||
"{}",
|
||||
row.read::<&str, _>("name").to_string()
|
||||
),
|
||||
name: format!("{}", row.read::<&str, _>("name").to_string()),
|
||||
};
|
||||
result.push(row_result);
|
||||
}
|
||||
@@ -96,7 +152,7 @@ fn search_move(index: i64) -> MoveSearchResult {
|
||||
_ => None,
|
||||
},
|
||||
};
|
||||
return row_result
|
||||
return row_result;
|
||||
}
|
||||
return MoveSearchResult {
|
||||
id: 0,
|
||||
@@ -106,7 +162,7 @@ fn search_move(index: i64) -> MoveSearchResult {
|
||||
category: 0,
|
||||
priority: 0,
|
||||
condition: None,
|
||||
}
|
||||
};
|
||||
}
|
||||
#[tauri::command]
|
||||
fn get_items() -> Vec<ItemSearchResult> {
|
||||
@@ -212,7 +268,7 @@ fn search(index: i64) -> SearchResult {
|
||||
speed: row.read::<i64, _>("speed"),
|
||||
learnset: learnset,
|
||||
};
|
||||
return row_result
|
||||
return row_result;
|
||||
}
|
||||
return SearchResult {
|
||||
id: 0,
|
||||
@@ -227,11 +283,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, increase_attack_usage, search_learnset])
|
||||
.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");
|
||||
}
|
||||
|
||||
@@ -47,4 +47,124 @@ pub struct ItemSearchResult {
|
||||
pub name: String,
|
||||
pub image: String,
|
||||
pub effect: Option<serde_json::Value>,
|
||||
}
|
||||
|
||||
#[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<i64>,
|
||||
pub thumbnail: String,
|
||||
pub abilities: String,
|
||||
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<PokemonDataLearnset>,
|
||||
pub terastype: Vec<i64>,
|
||||
}
|
||||
|
||||
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: vec![],
|
||||
terastype: vec![0]
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -158,16 +158,36 @@
|
||||
{/each}
|
||||
</table>
|
||||
<div class="save-load-clear-group">
|
||||
<button>Save</button>
|
||||
<button>Load</button><br />
|
||||
<button
|
||||
on:click={() => {
|
||||
invoke("save_json", { jsonContent: pokemonDataArray})
|
||||
.then(() => {})
|
||||
.catch((err) => console.log(err));
|
||||
}}
|
||||
>
|
||||
Save
|
||||
</button>
|
||||
<button
|
||||
on:click={() => {
|
||||
invoke("load_json").then((d) => {
|
||||
pokemonDataArray = d;
|
||||
pokemonData = pokemonDataArray[index];
|
||||
})
|
||||
.catch((e) => console.log(e));
|
||||
}}
|
||||
>
|
||||
Load
|
||||
</button><br />
|
||||
<button
|
||||
on:click={() => {
|
||||
pokemonData = undefined;
|
||||
pokemonDataArray = [];
|
||||
index = 0;
|
||||
myValue = undefined;
|
||||
}}>Clear</button
|
||||
}}
|
||||
>
|
||||
Clear
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="display-data">
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
import type { ItemEffect } from "./ItemData";
|
||||
|
||||
export interface PokemonStatus {
|
||||
types: number[],
|
||||
terastype: number[],
|
||||
|
||||
Reference in New Issue
Block a user