added save and load json

This commit is contained in:
2023-02-06 19:11:19 +09:00
parent 1ef2570f45
commit 20a3a0bbe4
6 changed files with 235 additions and 19 deletions

View File

@@ -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");
}

View File

@@ -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]
}
}
}