Skip to content

Commit

Permalink
feat(Genshin): Add csv format
Browse files Browse the repository at this point in the history
Close #149
  • Loading branch information
wormtql committed May 5, 2024
1 parent 743ab9a commit 8523d80
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 18 deletions.
107 changes: 95 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions yas-genshin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ image = "0.24"
serde_json = "1.0"
edit-distance = "2.1"
regex = "1.5"
strum = "0.25"
strum_macros = "0.25"
strum = "0.26"
strum_macros = "0.26"
lazy_static = "1.4"
serde = { version = "1.0", features = ["derive"] }
env_logger = "0.10.0"
env_logger = "0.11"
serde_yaml = "0.9"
csv = "1.3.0"

[target.'cfg(target_os = "windows")'.dependencies]
windows-capture = "1.0.65"
Expand Down
4 changes: 2 additions & 2 deletions yas-genshin/src/artifact/artifact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use strum_macros::Display;
use crate::character::CHARACTER_NAMES;
use crate::scanner::GenshinArtifactScanResult;

#[derive(Debug, Hash, Clone, PartialEq, Eq)]
#[derive(Debug, Hash, Clone, PartialEq, Eq, Display)]
pub enum ArtifactStatName {
HealingBonus,
CriticalDamage,
Expand All @@ -30,7 +30,7 @@ pub enum ArtifactStatName {
DendroBonus,
}

#[derive(Debug, Hash, Clone, PartialEq, Eq)]
#[derive(Debug, Hash, Clone, PartialEq, Eq, Display)]
pub enum ArtifactSlot {
Flower,
Feather,
Expand Down
76 changes: 76 additions & 0 deletions yas-genshin/src/export/artifact/csv.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use serde::{Serialize, Serializer};
use crate::artifact::GenshinArtifact;

pub struct GenshinArtifactCSVFormat<'a> {
artifacts: &'a [GenshinArtifact],
}

/// CSV format:
/// set name, slot, star, level, main stat name, main stat value, [sub state name, sub state value]*4, equip
fn single_artifact_to_string(artifact: &GenshinArtifact) -> String {
let mut s = String::new();
s = s + &artifact.set_name.to_string();
s = s + "," + &artifact.slot.to_string();
s = s + "," + &format!("{}", artifact.star);
s = s + "," + &format!("{}", artifact.level);
s = s + "," + &artifact.main_stat.name.to_string();
s = s + "," + &format!("{}", artifact.main_stat.value);
if let Some(sub) = &artifact.sub_stat_1 {
s = s + "," + &sub.name.to_string();
s = s + "," + &format!("{}", sub.value);
} else {
s = s + ",,";
}
if let Some(sub) = &artifact.sub_stat_2 {
s = s + "," + &sub.name.to_string();
s = s + "," + &format!("{}", sub.value);
} else {
s = s + ",,";
}
if let Some(sub) = &artifact.sub_stat_3 {
s = s + "," + &sub.name.to_string();
s = s + "," + &format!("{}", sub.value);
} else {
s = s + ",,";
}
if let Some(sub) = &artifact.sub_stat_4 {
s = s + "," + &sub.name.to_string();
s = s + "," + &format!("{}", sub.value);
} else {
s = s + ",,";
}
if let Some(e) = &artifact.equip {
s = s + "," + e;
} else {
s = s + ","
}

s
}

impl<'a> GenshinArtifactCSVFormat<'a> {
pub fn new(artifacts: &'a [GenshinArtifact]) -> Self {
Self {
artifacts
}
}

pub fn to_csv_string(&self) -> String {
let header = "套装,部位,星级,等级,主词条名,主词条值,副词条名1,副词条值1,副词条名2,副词条值2,副词条名3,副词条值3,副词条名4,副词条值4,装备";
let mut result = String::from(header) + "\n";

for artifact in self.artifacts.iter() {
let line = single_artifact_to_string(artifact);
result = result + &line + "\n";
}

result
}
}

impl<'a> Serialize for GenshinArtifactCSVFormat<'a> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer {
let s = self.to_csv_string();
serializer.serialize_str(&s)
}
}
1 change: 1 addition & 0 deletions yas-genshin/src/export/artifact/export_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub enum GenshinArtifactExportFormat {
Mona,
MingyuLab,
Good,
CSV,
}

impl Default for GenshinArtifactExportFormat {
Expand Down
12 changes: 12 additions & 0 deletions yas-genshin/src/export/artifact/exporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use yas::export::{AssetEmitter, ExportAssets};

use crate::artifact::GenshinArtifact;
use crate::export::artifact::{ExportArtifactConfig, GenshinArtifactExportFormat};
use crate::export::artifact::csv::GenshinArtifactCSVFormat;

use super::good::GOODFormat;
use super::mingyu_lab::MingyuLabFormat;
Expand Down Expand Up @@ -71,6 +72,17 @@ impl<'a> AssetEmitter for GenshinArtifactExporter<'a> {
contents.into_bytes(),
Some(String::from("GOOD圣遗物格式")));
},
GenshinArtifactExportFormat::CSV => {
let path = self.output_dir.join("artifacts.csv");
let value = GenshinArtifactCSVFormat::new(results);
let contents = value.to_csv_string();
export_assets.add_asset(
Some(String::from("artifacts csv format")),
path,
contents.into_bytes(),
Some(String::from("CSV格式圣遗物"))
);
}
};
}
}
2 changes: 1 addition & 1 deletion yas-genshin/src/export/artifact/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ mod mona_uranai;
mod exporter;
mod export_format;
mod config;

mod csv;

0 comments on commit 8523d80

Please sign in to comment.